Avro Java API 时间戳逻辑类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43080894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Avro Java API Timestamp Logical Type?
提问by clay
With the Avro Java API, I can make a simple record schema like:
使用 Avro Java API,我可以创建一个简单的记录模式,如:
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
如何使用逻辑类型标记架构字段,特别是:https: //avro.apache.org/docs/1.8.1/api/java/org/apache/avro/LogicalTypes.TimestampMillis.html
回答by clay
Thanks to DontPanic:
感谢 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"
} ]
}
回答by DontPanic
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:
带有timestampMillis的resultSchema:
{"type":"record","name":"MyRecodr","namespace":"org.demo","fields":[{"name":"timestamp","type":{"type":"long","logicalType":"timestamp-millis"}}]}
回答by Edmond Wong
Thanks for the first solution, now for nullable logicalType like:
感谢第一个解决方案,现在为可为空的 logicalType ,如:
{
"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();