我试图使用这个avro shcema
{
"namespace": "nothing",
"name": "myAvroSchema",
"type": "record",
"fields": [
{
"name": "checkInCustomerReference",
"type": "string"
},
{
"name": "customerContacts",
"type": "record",
"fields": [
{
"name": "customerEmail",
"type": "array",
"items": {
"type": "record",
"name": "customerEmail_element",
"fields": [
{
"name": "emailAddress",
"type": "string"
},
{
"name": "typeOfEmail",
"type": "string"
}
]
}
},
{
"name": "customerPhone",
"type": "array",
"items": {
"type": "record",
"name": "customerPhone_element",
"fields": [
{
"name": "fullContactNumber",
"type": "string"
},
{
"name": "ISDCode",
"type": "string"
}
]
}
},
{
"name": "DonotAskIndicator",
"type": "record",
"fields": [
{
"name": "donotAskDetails",
"type": "string"
}
]
}
]
},
{
"name": "somethingElseToCheck",
"type": "string"
}
]
}
使用avro-tools生成和avro文件:
avro-tools fromjson --schema-file myAvroSchema.avsc myJson.json > myAvroData.avro
但是我收到以下错误消息:
线程中的异常" main" org.apache.avro.SchemaParseException: "记录"不是定义的名称。 " customerContacts"的类型 字段必须是已定义的名称或{" type":...}表达式。
有谁能告诉我为什么记录没有被识别为定义名称?
答案 0 :(得分:7)
“customerContacts”字段的类型必须是已定义的名称或{“type”:...}表达式
看起来不像您正确定义嵌套记录。我重现了你的架构,并提出了这个,试一试:
{
"type":"record",
"name":"myAvroSchema",
"namespace":"nothing",
"fields":[
{
"name":"checkInCustomerReference",
"type":"string"
},
{
"name":"customerContacts",
"type":{
"type":"record",
"name":"customerContacts",
"namespace":"nothing",
"fields":[
{
"name":"customerEmail",
"type":{
"type":"array",
"items":{
"type":"record",
"name":"customerEmail",
"namespace":"nothing",
"fields":[
{
"name":"emailAddress",
"type":"string"
},
{
"name":"typeOfEmail",
"type":"string"
}
]
}
}
},
{
"name":"customerPhone",
"type":{
"type":"array",
"items":{
"type":"record",
"name":"customerPhone",
"namespace":"nothing",
"fields":[
{
"name":"fullContactNumber",
"type":"string"
},
{
"name":"ISDCode",
"type":"string"
}
]
}
}
},
{
"name":"DonotAskIndicator",
"type":{
"type":"record",
"name":"donotAskIndicator",
"namespace":"nothing",
"fields":[
{
"name":"donotAskDetails",
"type":"string"
}
]
}
}
]
}
},
{
"name":"somethingElseToCheck",
"type":"string"
}
]
}
答案 1 :(得分:-1)
您必须在使用前定义记录。你可以在这里找到更好的(例子): Problems in creating scheme .avsc Avro