Search in sources :

Example 41 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project voldemort by voldemort.

the class ClientConfigUtil method readMultipleClientConfigAvro.

/**
     * Parses a string that contains multiple fat client configs in avro format
     * 
     * @param configAvro Input string of avro format, that contains config for
     *        multiple stores
     * @return Map of store names to store config properties
     */
@SuppressWarnings("unchecked")
public static Map<String, Properties> readMultipleClientConfigAvro(String configAvro) {
    Map<String, Properties> mapStoreToProps = Maps.newHashMap();
    try {
        JsonDecoder decoder = new JsonDecoder(CLIENT_CONFIGS_AVRO_SCHEMA, configAvro);
        GenericDatumReader<Object> datumReader = new GenericDatumReader<Object>(CLIENT_CONFIGS_AVRO_SCHEMA);
        Map<Utf8, Map<Utf8, Utf8>> storeConfigs = (Map<Utf8, Map<Utf8, Utf8>>) datumReader.read(null, decoder);
        // Store config props to return back
        for (Utf8 storeName : storeConfigs.keySet()) {
            Properties props = new Properties();
            Map<Utf8, Utf8> singleConfig = storeConfigs.get(storeName);
            for (Utf8 key : singleConfig.keySet()) {
                props.put(key.toString(), singleConfig.get(key).toString());
            }
            if (storeName == null || storeName.length() == 0) {
                throw new Exception("Invalid store name found!");
            }
            mapStoreToProps.put(storeName.toString(), props);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mapStoreToProps;
}
Also used : JsonDecoder(org.apache.avro.io.JsonDecoder) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Utf8(org.apache.avro.util.Utf8) Properties(java.util.Properties) Map(java.util.Map)

Example 42 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader 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)

Example 43 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project beam by apache.

the class AvroIOTest method testMetadata.

@Test
@SuppressWarnings("unchecked")
@Category(NeedsRunner.class)
public void testMetadata() throws Exception {
    List<GenericClass> values = ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
    File outputFile = tmpFolder.newFile("output.avro");
    p.apply(Create.of(values)).apply(AvroIO.write(GenericClass.class).to(outputFile.getAbsolutePath()).withoutSharding().withMetadata(ImmutableMap.<String, Object>of("stringKey", "stringValue", "longKey", 100L, "bytesKey", "bytesValue".getBytes())));
    p.run();
    DataFileStream dataFileStream = new DataFileStream(new FileInputStream(outputFile), new GenericDatumReader());
    assertEquals("stringValue", dataFileStream.getMetaString("stringKey"));
    assertEquals(100L, dataFileStream.getMetaLong("longKey"));
    assertArrayEquals("bytesValue".getBytes(), dataFileStream.getMeta("bytesKey"));
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) DataFileStream(org.apache.avro.file.DataFileStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 44 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project beam by apache.

the class AvroIOTest method testAvroIONullCodecWriteAndReadASingleFile.

@Test
@SuppressWarnings("unchecked")
@Category(NeedsRunner.class)
public void testAvroIONullCodecWriteAndReadASingleFile() throws Throwable {
    List<GenericClass> values = ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
    File outputFile = tmpFolder.newFile("output.avro");
    p.apply(Create.of(values)).apply(AvroIO.write(GenericClass.class).to(outputFile.getAbsolutePath()).withoutSharding().withCodec(CodecFactory.nullCodec()));
    p.run();
    PCollection<GenericClass> input = p.apply(AvroIO.read(GenericClass.class).from(outputFile.getAbsolutePath()));
    PAssert.that(input).containsInAnyOrder(values);
    p.run();
    DataFileStream dataFileStream = new DataFileStream(new FileInputStream(outputFile), new GenericDatumReader());
    assertEquals("null", dataFileStream.getMetaString("avro.codec"));
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) DataFileStream(org.apache.avro.file.DataFileStream) File(java.io.File) FileInputStream(java.io.FileInputStream) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 45 with GenericDatumReader

use of org.apache.avro.generic.GenericDatumReader in project samza by apache.

the class AvroFileHdfsReader method open.

@Override
public void open(String pathStr, String singleFileOffset) {
    LOG.info(String.format("%s: Open file [%s] with file offset [%s] for read", systemStreamPartition, pathStr, singleFileOffset));
    Path path = new Path(pathStr);
    try {
        AvroFSInput input = new AvroFSInput(FileContext.getFileContext(path.toUri()), path);
        fileReader = new DataFileReader<>(input, new GenericDatumReader<>());
        seek(singleFileOffset);
    } catch (IOException e) {
        throw new SamzaException(e);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) IOException(java.io.IOException) AvroFSInput(org.apache.hadoop.fs.AvroFSInput) SamzaException(org.apache.samza.SamzaException)

Aggregations

GenericDatumReader (org.apache.avro.generic.GenericDatumReader)47 GenericRecord (org.apache.avro.generic.GenericRecord)32 Schema (org.apache.avro.Schema)20 IOException (java.io.IOException)15 File (java.io.File)10 DataFileStream (org.apache.avro.file.DataFileStream)10 Decoder (org.apache.avro.io.Decoder)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 GenericData (org.apache.avro.generic.GenericData)7 DataFileReader (org.apache.avro.file.DataFileReader)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 JsonDecoder (org.apache.avro.io.JsonDecoder)5 ParseException (io.druid.java.util.common.parsers.ParseException)4 FileInputStream (java.io.FileInputStream)4 DataFileWriter (org.apache.avro.file.DataFileWriter)4 GenericDatumWriter (org.apache.avro.generic.GenericDatumWriter)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 Map (java.util.Map)3