从 avro 1.8.2 中的通用记录中获取值
Get value from an generic record in avro 1.8.2
我正在尝试从 GenericRecord
(Avro 1.8.2
)
中检索字段值
genericRecord.get(index) works but genericRecord.get(field_name) only gets me a null value.
genericRecord =reader.read(null, decoder);
ListIterator<Field> itr= genericRecord.getSchema().getFields().listIterator();
System.out.println("getting schema from generic record +" + genericRecord.getSchema());
while(itr.hasNext())
{
String nextField = itr.next().toString();
System.out.println("field right now is :"+ nextField+" the value in the generic record is "+ genericRecord.get(nextField));
binaryObjBuilder.setField(nextField, genericRecord.get(nextField));
}
for (int i=0;i<14;i++)
{
System.out.println("the values at index: "+i+" is "+ genericRecord.get(i));
}
以上片段的输出是
getting schema from generic record + {"type":"record","name":"customer_address","namespace":"customer_address.avro","fields":[{"name":"datetime","type":"long"},{"name":"ca_address_sk","tye":["null","long"]},{"name":"ca_address_id","type":["null","string"]},{"name":"ca_street_number","type":["null","string"]},{"name":"ca_street_name","type":["null","string"]},{"name":"ca_treet_type","type":["null","string"]},{"name":"ca_suite_number","type":["null","string"]},{"name":"ca_city","type":["null","string"]},{"name":"ca_county","type":["null","string"]},{"name:"ca_state","type":["null","string"]},{"name":"ca_zip","type":["null","string"]},{"name":"ca_country","type":["null","string"]},{"name":"ca_gmt_offset","type":["null","double"]},{"name":ca_location_type","type":["null","string"]}]}
field right now is :datetime type:LONG pos:0 the value in the generic record is null
field right now is :ca_address_sk type:UNION pos:1 the value in the generic record is null
field right now is :ca_address_id type:UNION pos:2 the value in the generic record is null
field right now is :ca_street_number type:UNION pos:3 the value in the generic record is null
field right now is :ca_street_name type:UNION pos:4 the value in the generic record is null
field right now is :ca_street_type type:UNION pos:5 the value in the generic record is null
field right now is :ca_suite_number type:UNION pos:6 the value in the generic record is null
field right now is :ca_city type:UNION pos:7 the value in the generic record is null
field right now is :ca_county type:UNION pos:8 the value in the generic record is null
field right now is :ca_state type:UNION pos:9 the value in the generic record is null
field right now is :ca_zip type:UNION pos:10 the value in the generic record is null
field right now is :ca_country type:UNION pos:11 the value in the generic record is null
field right now is :ca_gmt_offset type:UNION pos:12 the value in the generic record is null
field right now is :ca_location_type type:UNION pos:13 the value in the generic record is null
the values at index: 0 is `201812190510`
the values at index: 1 is `596508`
the values at index: 2 is `AAAAAAAAMBKBJAAA`
the values at index: 3 is 613
the values at index: 4 is 3rd
the values at index: 5 is Ln
the values at index: 6 is Suite 300
the values at index: 7 is Pleasant Hill
the values at index: 8 is Marion County
the values at index: 9 is OH
the values at index: 10 is 43604
the values at index: 11 is United States
the values at index: 12 is -5.0
the values at index: 13 is single family
如果您查看代码 "field right now is :"+ nextField+"
与输出 field right now is :datetime type:LONG pos:0
,您会发现它正在调用 get("datetime type:LONG pos:0")
, 应该 return null,因为这与仅针对字段名称调用 get("datetime")
不同。
这是一个解决方法,它结合了两个循环并通过位置访问值
for (int pos = 0; itr.hasNext(); pos++) {
String nextField = itr.next().toString();
System.out.printf("field right now at pos:%d is :%s ; the value in the generic record is %s%n",
pos, nextField, genericRecord.get(pos));
binaryObjBuilder.setField(nextField, genericRecord.get(nextField));
}
我不确定 binaryObjBuilder
的期望是什么,但如果您想按名称 get/set,那么我可能建议尝试
String nextField = itr.next().toString();
String fieldname = nextField.split("\s+")[0];
我正在尝试从 GenericRecord
(Avro 1.8.2
)
genericRecord.get(index) works but genericRecord.get(field_name) only gets me a null value.
genericRecord =reader.read(null, decoder);
ListIterator<Field> itr= genericRecord.getSchema().getFields().listIterator();
System.out.println("getting schema from generic record +" + genericRecord.getSchema());
while(itr.hasNext())
{
String nextField = itr.next().toString();
System.out.println("field right now is :"+ nextField+" the value in the generic record is "+ genericRecord.get(nextField));
binaryObjBuilder.setField(nextField, genericRecord.get(nextField));
}
for (int i=0;i<14;i++)
{
System.out.println("the values at index: "+i+" is "+ genericRecord.get(i));
}
以上片段的输出是
getting schema from generic record + {"type":"record","name":"customer_address","namespace":"customer_address.avro","fields":[{"name":"datetime","type":"long"},{"name":"ca_address_sk","tye":["null","long"]},{"name":"ca_address_id","type":["null","string"]},{"name":"ca_street_number","type":["null","string"]},{"name":"ca_street_name","type":["null","string"]},{"name":"ca_treet_type","type":["null","string"]},{"name":"ca_suite_number","type":["null","string"]},{"name":"ca_city","type":["null","string"]},{"name":"ca_county","type":["null","string"]},{"name:"ca_state","type":["null","string"]},{"name":"ca_zip","type":["null","string"]},{"name":"ca_country","type":["null","string"]},{"name":"ca_gmt_offset","type":["null","double"]},{"name":ca_location_type","type":["null","string"]}]}
field right now is :datetime type:LONG pos:0 the value in the generic record is null
field right now is :ca_address_sk type:UNION pos:1 the value in the generic record is null
field right now is :ca_address_id type:UNION pos:2 the value in the generic record is null
field right now is :ca_street_number type:UNION pos:3 the value in the generic record is null
field right now is :ca_street_name type:UNION pos:4 the value in the generic record is null
field right now is :ca_street_type type:UNION pos:5 the value in the generic record is null
field right now is :ca_suite_number type:UNION pos:6 the value in the generic record is null
field right now is :ca_city type:UNION pos:7 the value in the generic record is null
field right now is :ca_county type:UNION pos:8 the value in the generic record is null
field right now is :ca_state type:UNION pos:9 the value in the generic record is null
field right now is :ca_zip type:UNION pos:10 the value in the generic record is null
field right now is :ca_country type:UNION pos:11 the value in the generic record is null
field right now is :ca_gmt_offset type:UNION pos:12 the value in the generic record is null
field right now is :ca_location_type type:UNION pos:13 the value in the generic record is null
the values at index: 0 is `201812190510`
the values at index: 1 is `596508`
the values at index: 2 is `AAAAAAAAMBKBJAAA`
the values at index: 3 is 613
the values at index: 4 is 3rd
the values at index: 5 is Ln
the values at index: 6 is Suite 300
the values at index: 7 is Pleasant Hill
the values at index: 8 is Marion County
the values at index: 9 is OH
the values at index: 10 is 43604
the values at index: 11 is United States
the values at index: 12 is -5.0
the values at index: 13 is single family
如果您查看代码 "field right now is :"+ nextField+"
与输出 field right now is :datetime type:LONG pos:0
,您会发现它正在调用 get("datetime type:LONG pos:0")
, 应该 return null,因为这与仅针对字段名称调用 get("datetime")
不同。
这是一个解决方法,它结合了两个循环并通过位置访问值
for (int pos = 0; itr.hasNext(); pos++) {
String nextField = itr.next().toString();
System.out.printf("field right now at pos:%d is :%s ; the value in the generic record is %s%n",
pos, nextField, genericRecord.get(pos));
binaryObjBuilder.setField(nextField, genericRecord.get(nextField));
}
我不确定 binaryObjBuilder
的期望是什么,但如果您想按名称 get/set,那么我可能建议尝试
String nextField = itr.next().toString();
String fieldname = nextField.split("\s+")[0];