本文介绍了Avro反序列化错误-ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作流程是-我创建avsc文件,使用avrogencpp工具生成C ++类,并在C ++应用程序中创建avro二进制编码的数据.

My workflow is - I create avsc files, generate C++ classes using avrogencpp tool and create avro binary encoded data in my C++ application.

我正在尝试找出方案2无法正常工作的原因.

I am trying to find out why Scenario 2 does not work.

场景1

Scenario 1

{
"namespace": "com.company.project",
"name": "Component_DeviceInfo",
"type": "record",
"doc": "Identifies a client device",
"fields": [
    {
        "name": "deviceId",
        "type": [
            "null",
            "string"
        ],
        "default": null,
        "doc": "Multicast Data Client Device Id. Usually unique MAC address"
    },
    {
        "name": "zoneId",
        "type": [
            "null",
            "string"
        ],
        "default": null,
        "doc": "Zone id where device belongs to"
    }
]
}
Component_DeviceInfo deviceInfo;
    deviceInfo.deviceId.set_string("device1");
    deviceInfo.zoneId.set_string("zone1");
    std::vector <char>tele_bytes_;
    std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream(1);
    avro::EncoderPtr enc = avro::binaryEncoder();
    enc->init(*out);
    avro::encode(*enc, deviceInfo);
    out->flush();

    size_t byte_count = out->byteCount();
    DBG("BYTE COUNT " << byte_count);

    std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
    avro::StreamReader reader(*in);
    std::vector<uint8_t> row_data(byte_count);
    reader.readBytes(&row_data[0], byte_count);
@Override
    public Object deserializeByteArr(Schema schema, final byte[] data){
        DatumReader<GenericRecord> genericDatumReader = new SpecificDatumReader<>(schema);
        Decoder decoder = DecoderFactory.get().binaryDecoder(data, null);
        try {
            GenericRecord userData = genericDatumReader.read(null, decoder);
            System.out.println(userData);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

方案2-不起作用

Scenario 2 - Does not work

请注意,我已经更新了架构并重新生成了与该架构相对应的c ++文件

Note that I have updated the schema and regenerated c++ files corresponding to the schema

[
    {
        "namespace": "com.company.project",
        "name": "Component_DeviceInfo",
        "type": "record",
        "doc": "Identifies a client device",
        "fields": [
            {
                "name": "deviceId",
                "type": [
                    "null",
                    "string"
                ],
                "default": null,
                "doc": "Unique MAC address"
            },
            {
                "name": "zoneId",
                "type": [
                    "null",
                    "string"
                ],
                "default": null,
                "doc": "Zone id where Client device belongs to"
            }
        ]
    },
    {
        "namespace": "com.company.project",
        "name": "Component_EventList",
        "type": "record",
        "doc": "Component Event list",
        "fields": [
            {
                "name": "deviceInfo",
                "type": [
                    "null",
                    "com.company.project.Component_DeviceInfo"
                ],
                "default": null,
                "doc": "Device information such as device id and zone id"
            }
        ]
    }
]
Component_DeviceInfo deviceInfo;
    deviceInfo.deviceId.set_string("device1");
    deviceInfo.zoneId.set_string("zone1");

    std::vector <char>tele_bytes_;

    Component_EventList ComponentEventList;
    ComponentEventList.deviceInfo.set_Component_DeviceInfo(deviceInfo);

    std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream(1);
    avro::EncoderPtr enc = avro::binaryEncoder();
    enc->init(*out);
    avro::encode(*enc, ComponentEventList);
    out->flush();

    size_t byte_count = out->byteCount();
    DBG("BYTE COUNT " << byte_count);

    std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
    avro::StreamReader reader(*in);
    std::vector<uint8_t> row_data(byte_count);
    reader.readBytes(&row_data[0], byte_count);
org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void com.company.telemetry.services.consumer.TelemetryConsumerService.consume(org.apache.kafka.clients.consumer.ConsumerRecord<java.lang.String, byte[]>)' threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 7
    at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:188) ~[spring-kafka-1.1.6.RELEASE.jar:na]
    at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:72) ~[spring-kafka-1.1.6.RELEASE.jar:na]
    at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:47) ~[spring-kafka-1.1.6.RELEASE.jar:na]
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:794) [spring-kafka-1.1.6.RELEASE.jar:na]
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:738) [spring-kafka-1.1.6.RELEASE.jar:na]
    at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:570) [spring-kafka-1.1.6.RELEASE.jar:na]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_91]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_91]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_91]
Caused by: java.lang.ArrayIndexOutOfBoundsException: 7
    at org.apache.avro.io.parsing.Symbol$Alternative.getSymbol(Symbol.java:402) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.io.parsing.Parser.advance(Parser.java:88) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:155) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:155) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:155) ~[avro-1.7.7.jar:1.7.7]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142) ~[avro-1.7.7.jar:1.7.7]
    at com.company.telemetry.services.serde.AvroByteArrDeserializer.deserializeByteArr(AvroByteArrDeserializer.java:32) ~[classes/:na]
    at com.company.telemetry.services.TelemetryService.handleByteArr(TelemetryService.java:59) ~[classes/:na]
    at com.company.telemetry.services.consumer.TelemetryConsumerService.consume(TelemetryConsumerService.java:39) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:180) ~[spring-messaging-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:112) ~[spring-messaging-4.3.11.RELEASE.jar:4.3.11.RELEASE]
    at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48) ~[spring-kafka-1.1.6.RELEASE.jar:na]
    at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:174) ~[spring-kafka-1.1.6.RELEASE.jar:na]
    ... 8 common frames omitted

如果有人能告诉我发生了什么事,我将不胜感激!谢谢 !

I would appreciate if someone could tell me whats going on ! Thanks !

推荐答案

问题是我使用错误的类创建了Avro编码的数据.但是更大的问题竟然是顶级工会- https://issues.apache.org/jira/browse/AVRO-2095

The issue was I was creating Avro encoded data using wrong classes. But the bigger issue turned out to be top level unions - https://issues.apache.org/jira/browse/AVRO-2095

这篇关于Avro反序列化错误-ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 17:14