avro - 逻辑类型的架构

hin*_*ner 4 avro

我正在尝试学习 avro 并且有一个模式问题。

有些文件说

{
      "name": "userid",
       "type" : "string",
       "logicalType" : "uuid"
},
Run Code Online (Sandbox Code Playgroud)

还有人说

{
  "name": "userid",
  "type" : {
    "type" : "string",
    "logicalType" : "uuid"
  }
},
Run Code Online (Sandbox Code Playgroud)

哪一个是正确的?或者它们是相同的吗?

谢谢你!

JE4*_*E42 5

我使用avro 工具“随机”命令(别名avro如下)运行了您的模式的变体。它尝试为模式生成随机值。

\n

仅具有此类型且使用嵌套类型语法指定的模式logicalType将被拒绝:

\n
\n

avro random --schema \'{ "name": "userid", "type" : { "type": "string", "logicalType" : "uuid" } }\' -

\n

[...]无类型:{“名称”:“用户ID”,“类型”:{“类型”:“字符串”,“逻辑类型”:“uuid”}}

\n
\n

logicalType但是,当将next 放在以下位置时它会起作用type

\n
\n

avro random --schema \' { "type" : "string", "logicalType" : "uuid" }\' -

\n

[...] Objavro.schemaL{"类型":"字符串","逻辑类型":"uuid"}avro.codecdeflate}\xef\xbf\xbdj\xef\xbf\xbdU\xef\xbf\xbd.\xef \xbf\xbd\\\xef\xbf\xbdo\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd

\n
\n

现在,当我们在记录中使用它时,在将其放在logicalTypetype 旁边时会收到警告:

\n
\n

avro random --schema \'{ "type": "record", "fields": [ { "type" : "string", "logicalType" : "uuid", "name": "f"} ] , "name": "rec"}\' -

\n

[...] 警告avro.Schema:忽略了rec.f.逻辑类型属性(“uuid”)。它可能应该嵌套在字段的“类型”内。\nObjavro.schema\xef\xbf\xbd{"type":"record","name":"rec","fields":[{"name" :"f","类型":"字符串","逻辑类型":"uuid"}]}avro.codecdeflate\xef\xbf\xbd\xef\xbf\xbdw\xef\xbf\xbd9\xef\xbf\xbd9 \xef\xbf\xbdn\xef\xbf\xbds\xef\xbf\xbd

\n
\n

接受嵌套语法而不发出警告:

\n
\n

avro random --schema \'{ "type": "record", "fields": [ { "type" : { "type": "string", "logicalType" : "uuid" } , "name": "f"} ] , "name": "rec"}\' -

\n

\xef\xbf\xbdw<\xef\xbf\xbd\xef\xbf\xbdqcord","名称":"rec","字段":[{"名称":"f","类型":{"类型":"字符串","逻辑类型":"uuid"}}]}avro.codecdeflate8\xef\xbf\xbd\xef\xbf\xbdt

\n
\n

此外,如果我们查看数组内的逻辑类型:

\n
\n

avro random --count 1 --schema \' { "type": "array", "items": { "type" : "string", "logicalType" : "uuid" , "name": "f"} , "name": "farr" } \' -

\n

[...随机位]

\n
\n

当嵌套版本失败时:

\n
\n

avro random --count 1 --schema \' { "type": "array", "items": {"type": { "type" : "string", "logicalType" : "uuid" , "name": "f"} } , "name": "farr" } \' -

\n

[...] 无类型:{“type”:{“type”:“string”,“逻辑类型”:“uuid”,“name”:“f”}}

\n
\n

看起来,如果逻辑类型是记录中字段的类型,则需要使用嵌套语法。\n否则,需要使用非嵌套语法。

\n