2016-12-15 3 views
1

Avro & 카프카를 사용하여 개체 목록을 보내려고합니다. 내 주요 문제는 목록 크기가 자주 변경 될 수 있으므로 빌드 방법을 모른다는 것입니다. Avro 스키마는 잘 알려진 구조를 의미합니다.avro & kafka를 사용하여 개체 목록을 보내는 방법

누구나 어떻게 할 수 있는지 알고 있습니까?

public class AvroObj { 

    private List<TestObj> list; 

    public List<TestObj> getList() { 
     return list; 
    } 

    public void setList(List<TestObj> list) { 
     this.list = list; 
    } 
} 

스키마 생성 : ReflectDatumWriter를 사용하여 바이트

Schema schema = ReflectData.get().getSchema(AvroObj.class); 

직렬화를 (내가 제공

+0

브로가,지도를 아라스 같은 몇 가지 복잡한 유형을 지원합니다. 이렇게하면 유연성이 향상됩니다. http://stackoverflow.com/questions/39232395/kafka-kstreams-processing-timeouts/39237089#39237089 –

답변

0

나는 예를 들어 목록 클래스, 을 할 수있는 가장 쉬운 방법이라고 생각 ClassCastException를 잡을 수있는 다른 데이텀 작성자가 발생합니다.

try(ByteArrayOutputStream out = new ByteArrayOutputStream()) { 
      BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); 
      DatumWriter<AvroObj> writer = new ReflectDatumWriter<>(schema); 
      writer.write(avroObj, encoder); 
      encoder.flush(); 
      byte[] bytes = out.toByteArray(); 
     } 

다음 kafka 제작자와 바이트를 보냅니다.

deserialise는 소비자가받은 바이트 :

DatumReader<AvroObj> reader1 = new ReflectDatumReader<AvroObj>(schema); 
     Decoder decoder = DecoderFactory.get().binaryDecoder(bytes, null); 
     AvroObj decodedAvroObj = reader1.read(null, decoder); 
관련 문제