Search in sources :

Example 6 with ReflectDatumReader

use of org.apache.avro.reflect.ReflectDatumReader in project flink by apache.

the class AvroOutputFormatITCase method postSubmit.

@Override
protected void postSubmit() throws Exception {
    //compare result for specific user type
    File[] output1;
    File file1 = asFile(outputPath1);
    if (file1.isDirectory()) {
        output1 = file1.listFiles();
        // check for avro ext in dir.
        for (File avroOutput : output1) {
            Assert.assertTrue("Expect extension '.avro'", avroOutput.toString().endsWith(".avro"));
        }
    } else {
        output1 = new File[] { file1 };
    }
    List<String> result1 = new ArrayList<String>();
    DatumReader<User> userDatumReader1 = new SpecificDatumReader<User>(User.class);
    for (File avroOutput : output1) {
        DataFileReader<User> dataFileReader1 = new DataFileReader<User>(avroOutput, userDatumReader1);
        while (dataFileReader1.hasNext()) {
            User user = dataFileReader1.next();
            result1.add(user.getName() + "|" + user.getFavoriteNumber() + "|" + user.getFavoriteColor());
        }
    }
    for (String expectedResult : userData.split("\n")) {
        Assert.assertTrue("expected user " + expectedResult + " not found.", result1.contains(expectedResult));
    }
    //compare result for reflect user type
    File[] output2;
    File file2 = asFile(outputPath2);
    if (file2.isDirectory()) {
        output2 = file2.listFiles();
    } else {
        output2 = new File[] { file2 };
    }
    List<String> result2 = new ArrayList<String>();
    DatumReader<ReflectiveUser> userDatumReader2 = new ReflectDatumReader<ReflectiveUser>(ReflectiveUser.class);
    for (File avroOutput : output2) {
        DataFileReader<ReflectiveUser> dataFileReader2 = new DataFileReader<ReflectiveUser>(avroOutput, userDatumReader2);
        while (dataFileReader2.hasNext()) {
            ReflectiveUser user = dataFileReader2.next();
            result2.add(user.getName() + "|" + user.getFavoriteNumber() + "|" + user.getFavoriteColor());
        }
    }
    for (String expectedResult : userData.split("\n")) {
        Assert.assertTrue("expected user " + expectedResult + " not found.", result2.contains(expectedResult));
    }
}
Also used : User(org.apache.flink.api.io.avro.example.User) ArrayList(java.util.ArrayList) DataFileReader(org.apache.avro.file.DataFileReader) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) ReflectDatumReader(org.apache.avro.reflect.ReflectDatumReader) File(java.io.File)

Example 7 with ReflectDatumReader

use of org.apache.avro.reflect.ReflectDatumReader in project hadoop by apache.

the class AvroTestUtil method testReflect.

public static void testReflect(Object value, Type type, String schema) throws Exception {
    // check that schema matches expected
    Schema s = ReflectData.get().getSchema(type);
    assertEquals(Schema.parse(schema), s);
    // check that value is serialized correctly
    ReflectDatumWriter<Object> writer = new ReflectDatumWriter<Object>(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    writer.write(value, EncoderFactory.get().directBinaryEncoder(out, null));
    ReflectDatumReader<Object> reader = new ReflectDatumReader<Object>(s);
    Object after = reader.read(null, DecoderFactory.get().binaryDecoder(out.toByteArray(), null));
    assertEquals(value, after);
}
Also used : Schema(org.apache.avro.Schema) ReflectDatumWriter(org.apache.avro.reflect.ReflectDatumWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReflectDatumReader(org.apache.avro.reflect.ReflectDatumReader)

Example 8 with ReflectDatumReader

use of org.apache.avro.reflect.ReflectDatumReader in project beam by apache.

the class AvroSource method createDatumReader.

private DatumReader<T> createDatumReader() {
    Schema readSchema = getReadSchema();
    Schema fileSchema = getFileSchema();
    checkNotNull(readSchema, "No read schema has been initialized for source %s", this);
    checkNotNull(fileSchema, "No file schema has been initialized for source %s", this);
    if (type == GenericRecord.class) {
        return new GenericDatumReader<>(fileSchema, readSchema);
    } else {
        return new ReflectDatumReader<>(fileSchema, readSchema);
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ReflectDatumReader(org.apache.avro.reflect.ReflectDatumReader)

Aggregations

ReflectDatumReader (org.apache.avro.reflect.ReflectDatumReader)8 DataFileReader (org.apache.avro.file.DataFileReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Schema (org.apache.avro.Schema)2 ReflectDatumWriter (org.apache.avro.reflect.ReflectDatumWriter)2 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)2 InputSupplier (com.google.common.io.InputSupplier)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 SeekableInput (org.apache.avro.file.SeekableInput)1 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)1 Decoder (org.apache.avro.io.Decoder)1 FSDataInputStreamWrapper (org.apache.flink.api.avro.FSDataInputStreamWrapper)1 User (org.apache.flink.api.io.avro.example.User)1