Reputation: 20370
With the Avro Java API, I can make a simple record schema like:
Schema schemaWithTimestamp = SchemaBuilder
.record("MyRecord").namespace("org.demo")
.fields()
.name("timestamp").type().longType().noDefault()
.endRecord();
How do I tag a schema field with a logical type, specifically: https://avro.apache.org/docs/1.8.1/api/java/org/apache/avro/LogicalTypes.TimestampMillis.html
Upvotes: 13
Views: 24033
Reputation: 161
Thanks for the first solution, now for nullable logicalType like:
{
"name":"maturityDate",
"type":["null", {
"type":"long","logicalType":"timestamp-millis"
}]
},
I figure the following:
Schema timestampMilliType = LogicalTypes.timestampMillis().addToSchema(Schema.create(Schema.Type.LONG));
Schema clientIdentifier = SchemaBuilder.record("ClientIdentifier")
.namespace("com.baeldung.avro")
.fields()
.requiredString("hostName")
.requiredString("ipAddress")
.name("maturityDate")
.type()
.unionOf()
.nullType()
.and()
.type(timestampMilliType)
.endUnion()
.noDefault()
.endRecord();
Upvotes: 7
Reputation: 20370
Thanks to DontPanic:
Schema timestampMilliType = LogicalTypes.timestampMillis().addToSchema(Schema.create(Schema.Type.LONG));
Schema schemaWithTimestamp = SchemaBuilder
.record("MyRecord").namespace("org.demo")
.fields()
.name("timestamp_with_logical_type").type(timestampMilliType).noDefault()
.name("timestamp_no_logical_type").type().longType().noDefault()
.endRecord();
System.out.println(schemaWithTimestamp.toString(true));
This results in:
{
"type" : "record",
"name" : "MyRecord",
"namespace" : "org.demo",
"fields" : [ {
"name" : "timestamp_with_logical_type",
"type" : {
"type" : "long",
"logicalType" : "timestamp-millis"
}
}, {
"name" : "timestamp_no_logical_type",
"type" : "long"
} ]
}
Upvotes: 23
Reputation: 1367
I think you may create schema manually:
List<Schema.Field> fields = new ArrayList<>();
Schema timeStampField = Schema.create(Schema.Type.LONG);
fields.add(new Schema.Field("timestamp", LogicalTypes.timestampMillis().addToSchema(timeStampField), null, null));
Schema resultSchema = Schema.createRecord("MyRecord", null, "org.demo", false, fields);
System.out.println(resultSchema);
your schema:
{"type":"record","name":"MyRecord","namespace":"org.demo","fields":[{"name":"timestamp","type":"long"}]}
resultSchema with timestampMillis:
{"type":"record","name":"MyRecodr","namespace":"org.demo","fields":[{"name":"timestamp","type":{"type":"long","logicalType":"timestamp-millis"}}]}
Upvotes: 2