使用架构序列化AVRO数据时出现异常

时间:2018-09-15 16:49:03

标签: java apache-kafka avro confluent-schema-registry

我已将模式注册在schmema注册表中的Physical主题下。

 curl -X POST \
  http://localhost:8081/subjects/physics/versions \
  -H 'accept: application/vnd.schemaregistry.v1+json' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/vnd.schemaregistry.v1+json' \
  -H 'postman-token: 5ce4bab3-6d0c-2940-d7dd-8f901b0b6fec' \
  -d '{"schema":"{\"type\":\"record\",\"name\":\"physics\",\"fields\":[{\"name\":\"ATTRIBUTE1\",\"type\":\"string\",\"default\":\"\"},{\"name\":\"ATTRIBUTE2\",\"type\":\"long\",\"default\":0},{\"name\":\"ATTRIBUTE3\",\"type\":\"string\",\"default\":\"\"},{\"name\":\"ATTRIBUTE4\",\"type\":\"string\",\"default\":\"\"},{\"name\":\"ATTRIBUTE5\",\"type\":\"string\",\"default\":\"\"}]}"}'

我的Kafka Producer代码与此类似。

SchemaRegistryClient schemaRegistryClient = new CachedSchemaRegistryClient("http://localhost:8081", 20);
        Schema schema = schemaRegistryClient.getBySubjectAndId("physics", ID);

        Producer<String, GenericRecord> producer = new KafkaProducer<String, GenericRecord>(kafkaProps);
        GenericRecord inputRecord = new GenericData.Record(schema);
        inputRecord.put("ATTRIBUTE1", "val1");
        inputRecord.put("ATTRIBUTE2", System.currentTimeMillis());
        inputRecord.put("ATTRIBUTE3", "val3");

        ProducerRecord<String, GenericRecord> recordData = new ProducerRecord<String, GenericRecord>(topic,
                subjectName, inputRecord);
        producer.send(recordData).get();

兼容性:向后 序列化器:KafkaAvroSerializer

运行Kafka生产者时,我遇到了异常。

Exception in thread "main" org.apache.kafka.common.errors.SerializationException: Error serializing Avro message
Caused by: java.lang.NullPointerException: null of string in field ATTRIBUTE4 of physics
    at org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:145)
    at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:139)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:75)
    at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:62)
    at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:92)
    at kafka.MyKafkaAvroSerializer.serialize(MyKafkaAvroSerializer.java:27)
    at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:453)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:430)
    at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:353)
    at kafka.MyAvroProducerV3.main(MyAvroProducerV3.java:62)
Caused by: java.lang.NullPointerException
    at org.apache.avro.io.Encoder.writeString(Encoder.java:121)
    at org.apache.avro.generic.GenericDatumWriter.writeString(GenericDatumWriter.java:267)
    at org.apache.avro.generic.GenericDatumWriter.writeString(GenericDatumWriter.java:262)
    at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:128)

如果我要发送额外的属性,即。来自Kafka Producer的ATTRIBUTE6,然后引发异常,表明ATTRIBUTE6未注册。没关系。

由于我已经注册了5个属性(ATTRIBUTE1,ATTRIBUTE2,ATTRIBUTE3,ATTRIBUTE4,ATTRIBUTE5),因此,如果我仅发送3个属性(即ATTRIBUTE1,ATTRIBUTE2,ATTRIBUTE3),则消费者应该只接收这3个属性和默认值(例如- 1)为失踪者。但是为什么我要超越例外?

我想念什么吗?

0 个答案:

没有答案