java - 从 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));
}
上述代码段的输出是
从通用记录中获取模式 + {"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"]}]}["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"]}]}["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"]}]}ca_gmt_offset","type":["null","double"]},{"name":ca_location_type","type":["null","string"]}]}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")
,它应该返回 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];
推荐阅读
- bash - 如何从 vcf 表中提取数据
- spring - Spring Boot 2.1/2.2 测试 - 如何在不为其他所有内容创建 bean 的情况下测试单个控制器?
- python - 硒下拉菜单
- android - 在子类中定义后如何通过super发送参数?
- uwp - UWP 应用服务可以有用户界面吗?
- jquery - 使用箭头键浏览结果时在 jQuery UI 自动完成上触发“选择”事件
- c# - 从 Visual Studio 自定义扩展中检测设置
- javascript - 当您通过 jQuery 附加新选项时,为什么 jQuery sort() 会选择错误的选项
- python - 将 2 个 Combobox 与一个字典链接
- mysql - 当我插入一个新值时,MySQL 如何自动生成一个唯一的 X 长度 ID?