Skip to content

Commit

Permalink
Merge pull request #7 from Nuvindu/graalvm
Browse files Browse the repository at this point in the history
Add config files for GraalVM
  • Loading branch information
Nuvindu authored Apr 16, 2024
2 parents 2a509a1 + 74b44cd commit 980daed
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 164 deletions.
36 changes: 15 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,14 @@ Import the `ballerinax/confluent.cavroserdes` module into your Ballerina project
import ballerinax/confluent.cavroserdes;
```

### Step 2: Instantiate a new connector
### Step 2: Invoke the connector operation

```ballerina
configurable string baseUrl = ?;
configurable int identityMapCapacity = ?;
configurable map<anydata> originals = ?;
configurable map<string> headers = ?;
cavroserdes:Client avroSerDes = check new ({
baseUrl,
identityMapCapacity,
originals,
headers
});
```

### Step 3: Invoke the connector operation

You can now utilize the operations available within the connector.
You can now utilize the operations available within the connector. To instantiate a `cregistry:Client` instance refer to the guidelines [here](https://central.ballerina.io/ballerinax/confluent.cregistry/latest).

```ballerina
public function main() returns error? {
cregistry:Client registry = ; // instantiate a schema registry client
string schema = string `
{
"type": "int",
Expand All @@ -53,19 +39,27 @@ public function main() returns error? {
}`;
int value = 1;
byte[] bytes = check avroSerDes->serialize(schema, value, "subject");
int number = check avroSerDes->deserialize(bytes);
byte[] bytes = check cavroserdes:serialize(registry, schema, value, "subject");
int number = check cavroserdes:deserialize(registry, bytes);
}
```

### Step 4: Run the Ballerina application
### Step 3: Run the Ballerina application

Use the following command to compile and run the Ballerina program.

```bash
bal run
```

## Examples

The Ballerina Avro Serializer/Deserializer connector for Confluent Schema Registry provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples).

1. [Kafka Avro producer](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples/kafka-avro-producer) - This example demonstrates how to publish Avro serialized data to a Kafka topic.

2. [Kafka Avro consumer](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples/kafka-avro-consumer) - This guide demonstrates how to consume data in the correct format according to the Avro schema from a Kafka topic.

## Issues and projects

The **Issues** and **Projects** tabs are disabled for this repository as this is part of the Ballerina library. To report bugs, request new features, start new discussions, view project boards, etc., visit the Ballerina library [parent repository](https://github.com/ballerina-platform/ballerina-library).
Expand Down
36 changes: 15 additions & 21 deletions ballerina/Module.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,14 @@ Import the `ballerinax/confluent.cavroserdes` module into your Ballerina project
import ballerinax/confluent.cavroserdes;
```

### Step 2: Instantiate a new connector
### Step 2: Invoke the connector operation

```ballerina
configurable string baseUrl = ?;
configurable int identityMapCapacity = ?;
configurable map<anydata> originals = ?;
configurable map<string> headers = ?;
cavroserdes:Client avroSerDes = check new ({
baseUrl,
identityMapCapacity,
originals,
headers
});
```

### Step 3: Invoke the connector operation

You can now utilize the operations available within the connector.
You can now utilize the operations available within the connector. To instantiate a `cregistry:Client` instance refer to the guidelines [here](https://central.ballerina.io/ballerinax/confluent.cregistry/latest).

```ballerina
public function main() returns error? {
cregistry:Client registry = ; // instantiate a schema registry client
string schema = string `
{
"type": "int",
Expand All @@ -46,15 +32,23 @@ public function main() returns error? {
}`;
int value = 1;
byte[] bytes = check avroSerDes->serialize(schema, value, "subject");
int number = check avroSerDes->deserialize(bytes);
byte[] bytes = check cavroserdes:serialize(registry, schema, value, "subject");
int number = check cavroserdes:deserialize(registry, bytes);
}
```

### Step 4: Run the Ballerina application
### Step 3: Run the Ballerina application

Use the following command to compile and run the Ballerina program.

```bash
bal run
```

## Examples

The Ballerina Avro Serializer/Deserializer connector for Confluent Schema Registry provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples).

1. [Kafka Avro producer](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples/kafka-avro-producer) - This example demonstrates how to publish Avro serialized data to a Kafka topic.

2. [Kafka Avro consumer](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples/kafka-avro-consumer) - This guide demonstrates how to consume data in the correct format according to the Avro schema from a Kafka topic.
36 changes: 15 additions & 21 deletions ballerina/Package.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,14 @@ Import the `ballerinax/confluent.cavroserdes` module into your Ballerina project
import ballerinax/confluent.cavroserdes;
```

### Step 2: Instantiate a new connector
### Step 2: Invoke the connector operation

```ballerina
configurable string baseUrl = ?;
configurable int identityMapCapacity = ?;
configurable map<anydata> originals = ?;
configurable map<string> headers = ?;
cavroserdes:Client avroSerDes = check new ({
baseUrl,
identityMapCapacity,
originals,
headers
});
```

### Step 3: Invoke the connector operation

You can now utilize the operations available within the connector.
You can now utilize the operations available within the connector. To instantiate a `cregistry:Client` instance refer to the guidelines [here](https://central.ballerina.io/ballerinax/confluent.cregistry/latest).

```ballerina
public function main() returns error? {
cregistry:Client registry = ; // instantiate a schema registry client
string schema = string `
{
"type": "int",
Expand All @@ -46,15 +32,23 @@ public function main() returns error? {
}`;
int value = 1;
byte[] bytes = check avroSerDes->serialize(schema, value, "subject");
int number = check avroSerDes->deserialize(bytes);
byte[] bytes = check cavroserdes:serialize(registry, schema, value, "subject");
int number = check cavroserdes:deserialize(registry, bytes);
}
```

### Step 4: Run the Ballerina application
### Step 3: Run the Ballerina application

Use the following command to compile and run the Ballerina program.

```bash
bal run
```

## Examples

The Ballerina Avro Serializer/Deserializer connector for Confluent Schema Registry provides practical examples illustrating usage in various scenarios. Explore these [examples](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples).

1. [Kafka Avro producer](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples/kafka-avro-producer) - This example demonstrates how to publish Avro serialized data to a Kafka topic.

2. [Kafka Avro consumer](https://github.com/ballerina-platform/module-ballerinax-confluent.cavroserdes/tree/main/examples/kafka-avro-consumer) - This guide demonstrates how to consume data in the correct format according to the Avro schema from a Kafka topic.
86 changes: 39 additions & 47 deletions ballerina/client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,51 @@ import ballerina/avro;
import ballerina/jballerina.java;
import ballerinax/confluent.cregistry;

# Consists of APIs to integrate with Avro Serializer/Deserializer for Confluent Schema Registry.
public isolated client class Client {
private final cregistry:Client schemaClient;

public isolated function init(*cregistry:ConnectionConfig config) returns Error? {
cregistry:Client|error schemaClient = new (config);
if schemaClient is error {
return error Error("Client invocation error", schemaClient);
}
self.schemaClient = schemaClient;
# Serializes the given data according to the Avro format and registers the schema into the schema registry.
#
# + registry - The schema registry client
# + schema - The Avro schema
# + data - The data to be serialized according to the schema
# + subject - The subject under which the schema should be registered
# + return - A `byte` array of the serialized data or else an `cavroserdes:Error`
public isolated function serialize(cregistry:Client registry, string schema, anydata data,
string subject) returns byte[]|Error {
do {
int id = check registry->register(subject, schema);
byte[] encodedId = check toBytes(id);
avro:Schema avroClient = check new (schema);
byte[] serializedData = check avroClient.toAvro(data);
return [...encodedId, ...serializedData];
} on fail error e {
return error Error(SERIALIZATION_ERROR, e);
}
}

# Serializes the given data according to the Avro format and registers the schema into the schema registry.
#
# + schema - The Avro schema
# + data - The data to be serialized according to the schema
# + subject - The subject under which the schema should be registered
# + return - A `byte` array of the serialized data or else an `cavroserdes:Error`
remote isolated function serialize(string schema, anydata data, string subject) returns byte[]|Error {
do {
int id = check self.schemaClient->register(subject, schema);
byte[] encodedId = check toBytes(id);
avro:Schema avroClient = check new (schema);
byte[] serializedData = check avroClient.toAvro(data);
return [...encodedId, ...serializedData];
} on fail error e {
return error Error(SERIALIZATION_ERROR, e);
}
}
# Deserializes the given Avro serialized message to the given data type by retrieving the schema
# from the schema registry.
#
# + registry - The schema registry client
# + data - Avro serialized data which includes the schema id
# + targetType - Default parameter use to infer the user specified type
# + return - A deserialized data with the given type or else an `cavroserdes:Error`
public isolated function deserialize(cregistry:Client registry, byte[] data, typedesc<anydata> targetType = <>)
returns targetType|Error = @java:Method {
'class: "io.ballerina.lib.confluent.avro.serdes.AvroDeserializer"
} external;

# Deserializes the given Avro serialized message to the given data type by retrieving the schema
# from the schema registry.
#
# + data - Avro serialized data which includes the schema id
# + targetType - Default parameter use to infer the user specified type
# + return - A deserialized data with the given type or else an `cavroserdes:Error`
remote isolated function deserialize(byte[] data, typedesc<anydata> targetType = <>)
returns targetType|Error = @java:Method {
'class: "io.ballerina.lib.confluent.avro.serdes.AvroSerializer"
} external;
isolated function toBytes(int id) returns byte[]|error = @java:Method {
'class: "io.ballerina.lib.confluent.avro.serdes.AvroDeserializer"
} external;

isolated function deserializeData(byte[] data) returns anydata|Error {
isolated function getId(byte[] bytes) returns int = @java:Method {
'class: "io.ballerina.lib.confluent.avro.serdes.AvroDeserializer"
} external;

class Deserializer {
isolated function deserializeData(cregistry:Client registry, byte[] data) returns anydata|Error {
do {
int schemaId = getId(data.slice(1, 5));
string retrievedSchema = check self.schemaClient->getSchemaById(schemaId);
string retrievedSchema = check registry->getSchemaById(schemaId);
avro:Schema avroClient = check new (retrievedSchema);
anydata deserializedData = check avroClient.fromAvro(data.slice(5, data.length()));
return deserializedData;
Expand All @@ -71,11 +71,3 @@ public isolated client class Client {
}
}
}

isolated function toBytes(int id) returns byte[]|error = @java:Method {
'class: "io.ballerina.lib.confluent.avro.serdes.AvroSerializer"
} external;

isolated function getId(byte[] bytes) returns int = @java:Method {
'class: "io.ballerina.lib.confluent.avro.serdes.AvroSerializer"
} external;
20 changes: 10 additions & 10 deletions ballerina/tests/tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
// under the License.

import ballerina/test;
import ballerinax/confluent.cregistry;

configurable string baseUrl = ?;
configurable int identityMapCapacity = ?;
configurable map<anydata> originals = ?;
configurable map<string> headers = ?;

Client avroSerDes = check new ({
cregistry:Client regsitry = check new ({
baseUrl,
identityMapCapacity,
originals,
Expand All @@ -45,9 +46,8 @@ public function testSerDes() returns error? {
name: "Red",
colors: ["maroon", "dark red", "light red"]
};

byte[] bytes = check avroSerDes->serialize(schema, colors, "subject-0");
Color getColors = check avroSerDes->deserialize(bytes);
byte[] bytes = check serialize(regsitry, schema, colors, "subject-0");
Color getColors = check deserialize(regsitry, bytes);
test:assertEquals(getColors, colors);
}

Expand All @@ -69,8 +69,8 @@ public function testWithRecords() returns error? {
subject: "Math"
};

byte[] bytes = check avroSerDes->serialize(schema, student, "subject-1");
Student getStudent = check avroSerDes->deserialize(bytes);
byte[] bytes = check serialize(regsitry, schema, student, "subject-1");
Student getStudent = check deserialize(regsitry, bytes);
test:assertEquals(getStudent, student);
}

Expand All @@ -85,8 +85,8 @@ public function testSerDesWithInteger() returns error? {

int value = 1;

byte[] bytes = check avroSerDes->serialize(schema, value, "subject-5");
int getValue = check avroSerDes->deserialize(bytes);
byte[] bytes = check serialize(regsitry, schema, value, "subject-5");
int getValue = check deserialize(regsitry, bytes);
test:assertEquals(getValue, value);
}

Expand All @@ -108,8 +108,8 @@ public function testSerDesWithCourse() returns error? {
credits: 3
};

byte[] bytes = check avroSerDes->serialize(schema, course, "subject-3");
Course getCourse = check avroSerDes->deserialize(bytes);
byte[] bytes = check serialize(regsitry, schema, course, "subject-3");
Course getCourse = check deserialize(regsitry, bytes);
test:assertEquals(getCourse, course);
}

Expand Down
Loading

0 comments on commit 980daed

Please sign in to comment.