当前位置: 首页 > 知识库问答 >
问题:

从 avro 1.8.2 中的通用记录中获取值

姬歌者
2023-03-14

我试图从< code > generic record (< code > 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"]},,"

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

共有1个答案

邢修明
2023-03-14

如果您查看您的code"字段现在是:"nextField"vs输出字段现在是: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));
}

我不确定< code>binaryObjBuilder想要什么,但是如果您想通过名称获取/设置,那么我建议您尝试一下

String nextField = itr.next().toString();
String fieldname = nextField.split("\\s+")[0];
 类似资料:
  • 问题内容: 我正在尝试从通话记录中获取联系人。我可以使用以下代码从主要联​​系人获取联系电话: 它的作用是,当我们单击“联系人”按钮时,它将打开一个包含所有联系人的列表,用户可以选择任何联系人,并且所选联系人将添加到“收件人”字段中。我想做完全相同的事情,但是我不想显示所有联系人,而是只显示最近使用过的那些(呼叫记录)进行选择的联系人。 如果您还可以告诉如何与组一起执行此操作,那也很好。 问题答案

  • 我得到了这段代码,但我认为这是不好的方法,如果有像100K的结果,它会返回100K的新用户怎么办?对于,我是否可以使用其他方法,但我不太确定如何实现。另外,是否应该使用块?

  • 问题内容: 假设有两个表: 表A 表B 表之间的联系是 messageID 字段。 我想要一个查询来生成这样的结果,在该查询中,我将所有字段从表A中拉出,并对表B中每条消息的注释数进行计数,如下所示: 我已经尝试过这样的事情: 但这是行不通的。有任何想法吗?似乎应该可以在一个查询中执行此操作。我正在使用MSSQL。谢谢你的帮助。 问题答案: 标量子查询将起作用: 与往常一样,有很多方法可以改变这只

  • 问题内容: 所以我有两个表,一个是RAWtable,另一个是MAINtable,如果存在多个记录(比较相同的名称,代码),我必须获取最新的groupID。例如,我在RAWtable上有这个: 这两个记录应视为一个,并且应仅返回此值: 该行是应插入主表中的唯一行。提供返回的最新GroupID(groupid是日期和时间的组合) 我已经尝试过了,但是没有用: 我怎样才能做到这一点?多谢。 问题答案:

  • 问题内容: 我有一个表 的学生 这样 我想获取所有记录,但不应重复邮政编码。因此,在上述表记录的情况下,应获取记录1和2。将不会提取第3条记录,因为它的邮政编码已在第1条记录中。 问题答案: 以下查询将仅选择不同的“ zip”字段。 以下查询将选择所有字段以及不同的zip字段。

  • 首先,在我的mysql数据库中,我有两个名为“users”和“users\u details”的表,它们是相互关联的 用户表 id(PK,AI,INT),用户名(VARCHAR,UNQ),密码(VARCHAR),全名(VARCHAR) 和用户表 id(PK,AI,INT),email(VARCHAR),phone(VARCHAR),address(VARCHAR),has_record(VARCH