2

AVRO デシリアライザーによって作成されたレコードを取り、ElasticSearch に送信したいと考えています。これを行うには、カスタム コードを作成する必要があることに気付きました。

LITERAL オプションを使用すると、GenericRecord を使用するための最初のステップである JSON スキーマが得られます。しかし、AVRO Java API を見てみると、GenericRecord を 1 つのレコードに使用する方法がわかりません。すべての例で DataFileReader を使用しています。

つまり、Flume イベントからフィールドを取得できません。

誰もこれを以前にやったことがありますか?ティア。

4

1 に答える 1

2

私はそれを理解することができました。私は次のことをしました:

// Get the schema
String strSchema = event.getHeader("flume.avro.schema.literal");
// Get the body
byte[] body = event.getBody();

// Create the avro schema
Schema schema = Schema.Parser.parse(strSchema);

// Get the decoder to use to get the "record" from the event stream in object form
BinaryDecoder decoder = DecoderFactory.binaryDecoder(body, null); 

// Get the datum reader
GenericDatumReader reader = new GenericDatumReader(schema);

// Get the Avro record in object form
GenericRecord record = reader.read(null, decoder);

// Now you can iterate over the fields
for (Schema.Field field : schema.getFields()) {
   Object value = record.get(field.name());

   // Code to add field to JSON to send to ElasticSearch not listed
   // ...

} // for (Schema.Field field : schema.getFields()) {

これはうまくいきます。

于 2014-06-12T00:23:47.207 に答える