【问题标题】:Creating a Proper avro schema for timestamp record为时间戳记录创建正确的 avro 模式
【发布时间】:2018-07-06 02:07:14
【问题描述】:

我想知道对于这种格式的 json 到 avro 的转换,正确的 avro 架构是什么:

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

我的架构:

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

我一直在努力

`'Cannot convert field entryDate: Cannot resolve union: 
"2018-01-26T12:00:40.930" 
not in 
["null",{"type":"long","logicalType":"timestamp-millis"}]'`

【问题讨论】:

  • 你发送的有效载荷是什么?

标签: json avro apache-nifi data-conversion


【解决方案1】:

这是一个愚蠢的错误...显然我将时间戳值存储为字符串,因此 avro 架构需要字符串而不是 long 作为类型。

即。

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

应该是

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

哇!

【讨论】:

  • 简单但完全有意义。我最常见的用例:当我从 postgres db 序列化时,我必须将类型更改为“long”。当我从 json 文件序列化时,我必须将类型更改为“字符串”
  • 逻辑类型似乎受到限制,因为它们只能注释某些基本类型。时间和日期的逻辑类型应该只注释数字类型,long 在时间戳微的情况下(参见docs)。您在使用string 时遇到问题了吗?
  • 同样,有谁知道 String -> time/timestamp millis/micros?
猜你喜欢
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
相关资源
最近更新 更多