Coder Social home page Coder Social logo

avro-examples's Introduction

Avro Examples

An Avro example which makes use of builtin logicalType as well as adds a custom logicalType.

Run the examples

# Create logicalType package first

cd logical-types
./gradlew build

# Next you can run the example project
cd ../example
./gradlew build

Reason behind is, that the Gradle plugin generating Java code from Avro schema needs the LogicalType, its factory and Conversion to generate the right types.

Optional Field

{
  "name": "email",
  "type": [
    "null",
    "string"
  ],
  "default": null,
  "doc": "EMail adress of user"
}

Built-In LogicalType decimal with default value 0

{
  "name": "salary",
  "type": {
    "type": "bytes",
    "logicalType": "decimal",
    "precision": 10,
    "scale": 2
  },
  "default": "\u0000",
  "doc": "Salary of User"
}

Needs specific configuration of the Avro compiler

avro {
    enableDecimalLogicalType = true
}

Custom LogicalType

    {
      "name": "street",
      "type": {
        "type": "string",
        "logicalType": "street"
      },
      "doc": "Street format ending with house number"
    }

Street Object

public class Street {

    private String streetPart;
    private String housenumberPart;

    public Street(String streetPart, String housenumberPart) {
        this.streetPart = streetPart;
        this.housenumberPart = housenumberPart;
    }

    public Street(String[] split) {
        streetPart = split[0];
        housenumberPart = split[1];
    }

    public String getStreetPart() {
        return streetPart;
    }

    public String getHousenumberPart() {
        return housenumberPart;
    }

    public String serialize() {
        return String.format("%s|%s",streetPart,housenumberPart);
    }

    public static Street deserialize(String street) {
        return new Street(street.split("|"));
    }
}

Custom StreetLogicalType

public class StreetLogicalType extends LogicalType {

    StreetLogicalType() {
        super("street");
    }

    @Override
    public void validate(Schema schema) {
        super.validate(schema);
        if (schema.getType() != Schema.Type.STRING) {
            throw new IllegalArgumentException("Street can only be used with an underlying string type");
        }
    }
}

StreetLogicalTypeFactory

public class StreetLogicalTypeFactory implements LogicalTypes.LogicalTypeFactory {
    @Override
    public LogicalType fromSchema(Schema schema) {
        return new de.mh.examples.avro.StreetLogicalType();
    }
}

Custom StreetConversion

public class StreetConversion extends Conversion<Street> {

    @Override
    public Class<Street> getConvertedType() {
        return Street.class;
    }

    @Override
    public String getLogicalTypeName() {
        return "street";
    }

    @Override
    public Street fromCharSequence(CharSequence value, Schema schema, LogicalType type) {
        return Street.deserialize(value.toString());
    }

    @Override
    public CharSequence toCharSequence(Street value, Schema schema, LogicalType type) {
        return value.serialize();
    }
}

Gradle Plugin configuration

plugins {
    id 'com.commercehub.gradle.plugin.avro' version '1.8.0'
}

dependencies {
    implementation 'org.apache.avro:avro:1.11.2'
}

avro {
    enableDecimalLogicalType = true
    stringType = "String"
    outputCharacterEncoding = "UTF-8"
    logicalTypeFactory("street", "de.mh.examples.avro.StreetLogicalTypeFactory")
    customConversion("de.mh.examples.avro.StreetConversion")
}

Register LogicalType before schema parser is instantiated

LogicalTypes.register("street", new StreetLogicalTypeFactory());

avro-examples's People

Contributors

markush81 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

gamussa

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.