TomWhiteの本Hadoop:The DefinitiveGuideでApacheAvroを学習しているときに、エラーが発生しました。
この例には3つのステップがあります。
Avroスキーマファイルを作成します(
Pair.avsc
){ "type":"record", "name":"Pair", "doc":"A pair of strings.", "fields":[ { "name":"left", "type":"string" }, { "name":"right", "type":"string" } ] }
スキーマファイルをコンパイルして、Javaクラス(
Pair.java
)を作成します。$ java -jar $AVRO_HOME/avro-tools-1.6.2.jar compile schema src/main/resources/Pair.avsc src/main/java/
SpecificDatumWriter<Pair>
およびを使用して、SpecificDatumReader<Pair>
データをシリアル化/逆シリアル化します。
元のメソッド例はtestPairSpecific()
https://github.com/tomwhite/hadoop-book/blob/master/avro/src/main/java/AvroTest.javaにあります。
サンプルコード( https://github.com/philipjkim/avro-examples/blob/master/src/test/java/org/sooo/AvroTest.java )を書き直しましたcreatePairAndSerializeThenDeserialize()
。これは、元のコードとほぼ同じです。違いは次のとおりです。
- 私が使用したAvroのバージョンは、元の1.3.2では1.6.2です。
Pair.java
avro-tools.jarによって作成された内容は異なります(元の: https ://github.com/tomwhite/hadoop-book/blob/master/avro/src/main/java/Pair.java 、私のもの:https:// github.com/philipjkim/avro-examples/blob/master/src/main/java/org/sooo/Pair.java)
テストを実行した後、エラーが発生しました:
java.lang.NullPointerException
at java.lang.String.replace(String.java:2228)
at org.apache.avro.specific.SpecificData.createSchema(SpecificData.java:195)
at org.apache.avro.specific.SpecificData.getSchema(SpecificData.java:140)
at org.apache.avro.specific.SpecificDatumWriter.<init>(SpecificDatumWriter.java:33)
at org.sooo.AvroTest.createPairAndSerializeThenDeserialize(AvroTest.java:86)
...
AvroTest.createPairAndSerializeThenDeserialize()
は:
@Test
public void createPairAndSerializeThenDeserialize() throws IOException {
// given
Pair datum = new Pair();
datum.setLeft(new Utf8("L"));
datum.setRight(new Utf8("R"));
// serialize
ByteArrayOutputStream out = new ByteArrayOutputStream();
DatumWriter<Pair> writer = new SpecificDatumWriter<Pair>(Pair.class); /* Line 86 */
Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
writer.write(datum, encoder);
encoder.flush();
out.close();
// deserialize
DatumReader<Pair> reader = new SpecificDatumReader<Pair>(Pair.class);
Decoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(),
null);
Pair result = reader.read(null, decoder);
// then
assertThat(result.getLeft().toString(), is("L"));
assertThat(result.getRight().toString(), is("R"));
}
この例の何が問題になっているのか知りたいのですが。コメントありがとうございます。
参考までに、私のサンプルプロジェクトリポジトリはhttps://github.com/philipjkim/avro-examplesです。