简体   繁体   中英

Java: How can I read Struct type data in Java

I'm reading avro data from kafka and the value is of type org.apache.kafka.connect.data.Struct and the value itself looks like this Struct{f1=value3} .

How can I read just the key(f1) and value(value3)?

If you know the name of the field, you can do this:

Object value = struct.get("f1");

If you don't know the name of the field, you have to get all fields:

for (Field field : struct.schema().fields())
{
    String name = field.name();
    Object value = struct.get(field);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM