Search in sources :

Example 1 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project crunch by cloudera.

the class SpecificAvroGroupByTest method createPersonAvroFile.

private void createPersonAvroFile(File avroFile) throws IOException {
    Builder person = Person.newBuilder();
    person.setAge(40);
    person.setName("Bob");
    List<CharSequence> siblingNames = Lists.newArrayList();
    siblingNames.add("Bob" + "1");
    siblingNames.add("Bob" + "2");
    person.setSiblingnames(siblingNames);
    FileOutputStream outputStream = new FileOutputStream(avroFile);
    SpecificDatumWriter<Person> writer = new SpecificDatumWriter<Person>(Person.class);
    DataFileWriter<Person> dataFileWriter = new DataFileWriter<Person>(writer);
    dataFileWriter.create(Person.SCHEMA$, outputStream);
    dataFileWriter.append(person.build());
    dataFileWriter.close();
    outputStream.close();
}
Also used : Builder(org.apache.crunch.test.Person.Builder) FileOutputStream(java.io.FileOutputStream) DataFileWriter(org.apache.avro.file.DataFileWriter) Person(org.apache.crunch.test.Person) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 2 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project flink by apache.

the class AvroRecordInputFormatTest method writeTestFile.

public static void writeTestFile(File testFile) throws IOException {
    ArrayList<CharSequence> stringArray = new ArrayList<CharSequence>();
    stringArray.add(TEST_ARRAY_STRING_1);
    stringArray.add(TEST_ARRAY_STRING_2);
    ArrayList<Boolean> booleanArray = new ArrayList<Boolean>();
    booleanArray.add(TEST_ARRAY_BOOLEAN_1);
    booleanArray.add(TEST_ARRAY_BOOLEAN_2);
    HashMap<CharSequence, Long> longMap = new HashMap<CharSequence, Long>();
    longMap.put(TEST_MAP_KEY1, TEST_MAP_VALUE1);
    longMap.put(TEST_MAP_KEY2, TEST_MAP_VALUE2);
    Address addr = new Address();
    addr.setNum(TEST_NUM);
    addr.setStreet(TEST_STREET);
    addr.setCity(TEST_CITY);
    addr.setState(TEST_STATE);
    addr.setZip(TEST_ZIP);
    User user1 = new User();
    user1.setName(TEST_NAME);
    user1.setFavoriteNumber(256);
    user1.setTypeDoubleTest(123.45d);
    user1.setTypeBoolTest(true);
    user1.setTypeArrayString(stringArray);
    user1.setTypeArrayBoolean(booleanArray);
    user1.setTypeEnum(TEST_ENUM_COLOR);
    user1.setTypeMap(longMap);
    user1.setTypeNested(addr);
    // Construct via builder
    User user2 = User.newBuilder().setName("Charlie").setFavoriteColor("blue").setFavoriteNumber(null).setTypeBoolTest(false).setTypeDoubleTest(1.337d).setTypeNullTest(null).setTypeLongTest(1337L).setTypeArrayString(new ArrayList<CharSequence>()).setTypeArrayBoolean(new ArrayList<Boolean>()).setTypeNullableArray(null).setTypeEnum(Colors.RED).setTypeMap(new HashMap<CharSequence, Long>()).setTypeFixed(null).setTypeUnion(null).setTypeNested(Address.newBuilder().setNum(TEST_NUM).setStreet(TEST_STREET).setCity(TEST_CITY).setState(TEST_STATE).setZip(TEST_ZIP).build()).build();
    DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
    DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
    dataFileWriter.create(user1.getSchema(), testFile);
    dataFileWriter.append(user1);
    dataFileWriter.append(user2);
    dataFileWriter.close();
}
Also used : User(org.apache.flink.api.io.avro.generated.User) Address(org.apache.flink.api.io.avro.generated.Address) DataFileWriter(org.apache.avro.file.DataFileWriter) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 3 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project flink by apache.

the class AvroSplittableInputFormatTest method createFiles.

@Before
public void createFiles() throws IOException {
    testFile = File.createTempFile("AvroSplittableInputFormatTest", null);
    ArrayList<CharSequence> stringArray = new ArrayList<CharSequence>();
    stringArray.add(TEST_ARRAY_STRING_1);
    stringArray.add(TEST_ARRAY_STRING_2);
    ArrayList<Boolean> booleanArray = new ArrayList<Boolean>();
    booleanArray.add(TEST_ARRAY_BOOLEAN_1);
    booleanArray.add(TEST_ARRAY_BOOLEAN_2);
    HashMap<CharSequence, Long> longMap = new HashMap<CharSequence, Long>();
    longMap.put(TEST_MAP_KEY1, TEST_MAP_VALUE1);
    longMap.put(TEST_MAP_KEY2, TEST_MAP_VALUE2);
    Address addr = new Address();
    addr.setNum(new Integer(TEST_NUM));
    addr.setStreet(TEST_STREET);
    addr.setCity(TEST_CITY);
    addr.setState(TEST_STATE);
    addr.setZip(TEST_ZIP);
    User user1 = new User();
    user1.setName(TEST_NAME);
    user1.setFavoriteNumber(256);
    user1.setTypeDoubleTest(123.45d);
    user1.setTypeBoolTest(true);
    user1.setTypeArrayString(stringArray);
    user1.setTypeArrayBoolean(booleanArray);
    user1.setTypeEnum(TEST_ENUM_COLOR);
    user1.setTypeMap(longMap);
    user1.setTypeNested(addr);
    // Construct via builder
    User user2 = User.newBuilder().setName(TEST_NAME).setFavoriteColor("blue").setFavoriteNumber(null).setTypeBoolTest(false).setTypeDoubleTest(1.337d).setTypeNullTest(null).setTypeLongTest(1337L).setTypeArrayString(new ArrayList<CharSequence>()).setTypeArrayBoolean(new ArrayList<Boolean>()).setTypeNullableArray(null).setTypeEnum(Colors.RED).setTypeMap(new HashMap<CharSequence, Long>()).setTypeFixed(new Fixed16()).setTypeUnion(123L).setTypeNested(Address.newBuilder().setNum(TEST_NUM).setStreet(TEST_STREET).setCity(TEST_CITY).setState(TEST_STATE).setZip(TEST_ZIP).build()).build();
    DatumWriter<User> userDatumWriter = new SpecificDatumWriter<User>(User.class);
    DataFileWriter<User> dataFileWriter = new DataFileWriter<User>(userDatumWriter);
    dataFileWriter.create(user1.getSchema(), testFile);
    dataFileWriter.append(user1);
    dataFileWriter.append(user2);
    Random rnd = new Random(1337);
    for (int i = 0; i < NUM_RECORDS - 2; i++) {
        User user = new User();
        user.setName(TEST_NAME + rnd.nextInt());
        user.setFavoriteNumber(rnd.nextInt());
        user.setTypeDoubleTest(rnd.nextDouble());
        user.setTypeBoolTest(true);
        user.setTypeArrayString(stringArray);
        user.setTypeArrayBoolean(booleanArray);
        user.setTypeEnum(TEST_ENUM_COLOR);
        user.setTypeMap(longMap);
        Address address = new Address();
        address.setNum(new Integer(TEST_NUM));
        address.setStreet(TEST_STREET);
        address.setCity(TEST_CITY);
        address.setState(TEST_STATE);
        address.setZip(TEST_ZIP);
        user.setTypeNested(address);
        dataFileWriter.append(user);
    }
    dataFileWriter.close();
}
Also used : User(org.apache.flink.api.io.avro.generated.User) Address(org.apache.flink.api.io.avro.generated.Address) HashMap(java.util.HashMap) DataFileWriter(org.apache.avro.file.DataFileWriter) ArrayList(java.util.ArrayList) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter) Fixed16(org.apache.flink.api.io.avro.generated.Fixed16) Random(java.util.Random) Before(org.junit.Before)

Example 4 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project spf4j by zolyfarkas.

the class Converter method save.

public static void save(final File file, final SampleNode collected) throws IOException {
    try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
        final SpecificDatumWriter<ASample> writer = new SpecificDatumWriter<>(ASample.SCHEMA$);
        final BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
        Converter.convert(Method.ROOT, collected, -1, 0, (final ASample object, final long deadline) -> {
            writer.write(object, encoder);
        });
        encoder.flush();
    }
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ASample(org.spf4j.ssdump2.avro.ASample) BufferedOutputStream(java.io.BufferedOutputStream) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Example 5 with SpecificDatumWriter

use of org.apache.avro.specific.SpecificDatumWriter in project spf4j by zolyfarkas.

the class Converter method saveLabeledDumps.

public static void saveLabeledDumps(final File file, final Map<String, SampleNode> collected) throws IOException {
    try (BufferedOutputStream bos = new BufferedOutputStream(Files.newOutputStream(file.toPath()))) {
        final SpecificDatumWriter<ASample> writer = new SpecificDatumWriter<>(ASample.SCHEMA$);
        final BinaryEncoder encoder = EncoderFactory.get().directBinaryEncoder(bos, null);
        encoder.writeMapStart();
        encoder.setItemCount(collected.size());
        for (Map.Entry<String, SampleNode> entry : collected.entrySet()) {
            encoder.startItem();
            encoder.writeString(entry.getKey());
            encoder.writeArrayStart();
            Converter.convert(Method.ROOT, entry.getValue(), -1, 0, (final ASample object, final long deadline) -> {
                encoder.setItemCount(1);
                encoder.startItem();
                writer.write(object, encoder);
            });
            encoder.writeArrayEnd();
        }
        encoder.writeMapEnd();
        encoder.flush();
    }
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ASample(org.spf4j.ssdump2.avro.ASample) SampleNode(org.spf4j.stackmonitor.SampleNode) BufferedOutputStream(java.io.BufferedOutputStream) TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) TIntObjectMap(gnu.trove.map.TIntObjectMap) TMap(gnu.trove.map.TMap) HashMap(java.util.HashMap) Map(java.util.Map) THashMap(gnu.trove.map.hash.THashMap) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Aggregations

SpecificDatumWriter (org.apache.avro.specific.SpecificDatumWriter)46 Schema (org.apache.avro.Schema)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 BinaryEncoder (org.apache.avro.io.BinaryEncoder)12 IOException (java.io.IOException)11 Test (org.junit.Test)11 DataFileWriter (org.apache.avro.file.DataFileWriter)9 GenericRecord (org.apache.avro.generic.GenericRecord)9 Encoder (org.apache.avro.io.Encoder)7 ByteBuffer (java.nio.ByteBuffer)6 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 SpecificRecord (org.apache.avro.specific.SpecificRecord)3 Person (org.apache.crunch.test.Person)3 Avro1124SubjectAndIdConverter (org.apache.druid.data.input.schemarepo.Avro1124SubjectAndIdConverter)3 InMemoryRepository (org.schemarepo.InMemoryRepository)3 Repository (org.schemarepo.Repository)3 TypedSchemaRepository (org.schemarepo.api.TypedSchemaRepository)3 AvroSchemaConverter (org.schemarepo.api.converter.AvroSchemaConverter)3 IdentityConverter (org.schemarepo.api.converter.IdentityConverter)3