Trade Message definition in Avro

·

2 min read

Apache Avro is a powerful data serialization system that allows you to define data structures in a concise, language-independent format. This allows you to easily share data structures between different systems and programming languages.

Define Avro schema

Prepare a new git repository with Gradle configuration. Avro schemas are defined in a .avsc file using a JSON-like syntax. For example, let's define a simple Avro schema for a TradeMsg record:

{
  "type": "record",
  "name": "TradeMsg",
  "namespace": "com.lda.avro.trade",
  "fields": [
    {
      "name": "id",
      "type": [
        "null",
        "int"
      ],
      "default": null
    },
    {
      "name": "orderId",
      "type": [
        "null",
        "int"
      ],
      "default": null
    },
    {
      "name": "price",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "qty",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "quoteQty",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "commission",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "commissionAsset",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "time",
      "type": [
        "null",
        "string"
      ],
      "default": null
    },
    {
      "name": "isBuyer",
      "type": [
        "null",
        "boolean"
      ],
      "default": null
    }
  ]
}

Configure gradle

Add dependencies and tasks to build.gradle file.

dependencies {
    implementation 'io.confluent:kafka-avro-serializer:5.3.0'
    implementation 'org.apache.avro:avro:1.11.1'
}

task sourcesJar(type: Jar, dependsOn: generateAvroJava) {
    group = 'build'
    archiveClassifier = 'source'
    from sourceSets.main.allSource
    from 'src/main/avro'
}

And add configuration for publishing. All needed steps can be found at: Publish to local nexus repository

In the project where you would like to use classes from the published jar add your jar to dependencies.