Creating a Proper avro schema for timestamp record
Asked Answered
I

1

13

I would like to know what the proper avro schema would be for some json to avro conversion that is in this format:

{"entryDate": "2018-01-26T12:00:40.930"}

My schema:

{
    "type" : "record",
    "name" : "schema",
    "fields" : [{
        "name" : "entryDate",
        "type" : ["null", {
            "type" : "long",
            "logicalType" : "timestamp-micros"
        }],
        "default" : null
    }]
}

I keep getting

`'Cannot convert field entryDate: Cannot resolve union: 
"2018-01-26T12:00:40.930" 
not in 
["null",{"type":"long","logicalType":"timestamp-millis"}]'`
Incongruity answered 26/1, 2018 at 17:18 Comment(1)
what is the payload you are sending ?Somewise
I
13

It was a silly mistake...obviously I was storing the timestamp value as a string so the avro schema needed a string instead of long for type.

ie.

{
    "type" : "record",
    "name" : "schema",
    "fields" : [{
        "name" : "entryDate",
        "type" : ["null", {
            "type" : `**"long"**`,
            "logicalType" : "timestamp-micros"
        }],
        "default" : null
    }]
}

should be

{
    "type" : "record",
    "name" : "schema",
    "fields" : [{
        "name" : "entryDate",
        "type" : ["null", {
            "type" : `**"string"**`,
            "logicalType" : "timestamp-micros"
        }],
        "default" : null
    }]
}

doh!

Incongruity answered 26/1, 2018 at 17:48 Comment(3)
Simple but makes totally sense. My most common use cases: When I am serializing from postgres db I have to change the type to "long". When I am serializing from json file I have to change the type to "string"Unipod
It seems that logical types are restricted in the sense that they can only annotate certain basic types. The logical types for time and date should only annotate numeric types, long in the case of timestamp-micros (see docs). Did you run into problems using string?Mirandamire
Same, anyone figure out String -> time/timestamp millis/micros?Desecrate

© 2022 - 2024 — McMap. All rights reserved.