我正在尝试从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));
}
以上代码段的输出为
从通用记录+ {“ 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
答案 0 :(得分:0)
如果您查看代码"field right now is :"+ nextField+"
与输出field right now is :datetime type:LONG pos:0
,那么您会看到它正在调用get("datetime type:LONG pos:0")
,应该返回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
会是什么,但是如果您想按名称获取/设置,那么我建议您尝试
String nextField = itr.next().toString();
String fieldname = nextField.split("\\s+")[0];