use of org.apache.avro.generic.GenericDatumWriter in project crunch by cloudera.
the class AvroFileReaderFactoryTest method populateGenericFile.
private void populateGenericFile(List<GenericRecord> genericRecords, Schema outputSchema) throws IOException {
FileOutputStream outputStream = new FileOutputStream(this.avroFile);
GenericDatumWriter<GenericRecord> genericDatumWriter = new GenericDatumWriter<GenericRecord>(outputSchema);
DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<GenericRecord>(genericDatumWriter);
dataFileWriter.create(outputSchema, outputStream);
for (GenericRecord record : genericRecords) {
dataFileWriter.append(record);
}
dataFileWriter.close();
outputStream.close();
}
use of org.apache.avro.generic.GenericDatumWriter in project trevni by cutting.
the class RandomData method main.
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: RandomData <schemafile> <outputfile> <count>");
System.exit(-1);
}
Schema sch = Schema.parse(new File(args[0]));
DataFileWriter<Object> writer = new DataFileWriter<Object>(new GenericDatumWriter<Object>()).create(sch, new File(args[1]));
try {
for (Object datum : new RandomData(sch, Integer.parseInt(args[2]))) {
writer.append(datum);
}
} finally {
writer.close();
}
}
use of org.apache.avro.generic.GenericDatumWriter in project druid by druid-io.
the class AvroStreamInputRowParserTest method testParse.
@Test
public void testParse() throws SchemaValidationException, IOException {
// serde test
Repository repository = new InMemoryRepository(null);
AvroStreamInputRowParser parser = new AvroStreamInputRowParser(PARSE_SPEC, new SchemaRepoBasedAvroBytesDecoder<String, Integer>(new Avro1124SubjectAndIdConverter(TOPIC), repository));
ByteBufferInputRowParser parser2 = jsonMapper.readValue(jsonMapper.writeValueAsString(parser), ByteBufferInputRowParser.class);
repository = ((SchemaRepoBasedAvroBytesDecoder) ((AvroStreamInputRowParser) parser2).getAvroBytesDecoder()).getSchemaRepository();
// prepare data
GenericRecord someAvroDatum = buildSomeAvroDatum();
// encode schema id
Avro1124SubjectAndIdConverter converter = new Avro1124SubjectAndIdConverter(TOPIC);
TypedSchemaRepository<Integer, Schema, String> repositoryClient = new TypedSchemaRepository<Integer, Schema, String>(repository, new IntegerConverter(), new AvroSchemaConverter(), new IdentityConverter());
Integer id = repositoryClient.registerSchema(TOPIC, SomeAvroDatum.getClassSchema());
ByteBuffer byteBuffer = ByteBuffer.allocate(4);
converter.putSubjectAndId(TOPIC, id, byteBuffer);
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(byteBuffer.array());
// encode data
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(someAvroDatum.getSchema());
// write avro datum to bytes
writer.write(someAvroDatum, EncoderFactory.get().directBinaryEncoder(out, null));
InputRow inputRow = parser2.parse(ByteBuffer.wrap(out.toByteArray()));
assertInputRowCorrect(inputRow);
}
use of org.apache.avro.generic.GenericDatumWriter in project druid by druid-io.
the class InlineSchemasAvroBytesDecoderTest method testParse.
@Test
public void testParse() throws Exception {
GenericRecord someAvroDatum = AvroStreamInputRowParserTest.buildSomeAvroDatum();
Schema schema = SomeAvroDatum.getClassSchema();
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(new byte[] { 1 });
out.write(ByteBuffer.allocate(4).putInt(10).array());
DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);
writer.write(someAvroDatum, EncoderFactory.get().directBinaryEncoder(out, null));
GenericRecord actual = new InlineSchemasAvroBytesDecoder(ImmutableMap.of(10, schema)).parse(ByteBuffer.wrap(out.toByteArray()));
Assert.assertEquals(someAvroDatum.get("id"), actual.get("id"));
}
use of org.apache.avro.generic.GenericDatumWriter in project h2o-3 by h2oai.
the class AvroFileGenerator method generateUnionTypes.
public static File generateUnionTypes(String filename, int nrows) throws IOException {
File parentDir = Files.createTempDir();
File f = new File(parentDir, filename);
DatumWriter<GenericRecord> w = new GenericDatumWriter<GenericRecord>();
DataFileWriter<GenericRecord> dw = new DataFileWriter<GenericRecord>(w);
// Based on SchemaBuilder javadoc:
// * The below two field declarations are equivalent:
// * <pre>
// * .name("f").type().unionOf().nullType().and().longType().endUnion().nullDefault()
// * .name("f").type().optional().longType()
// * </pre>
Schema schema = SchemaBuilder.builder().record("test_union_types").fields().name("CUString").type().optional().stringType().name("CUBytes").type().optional().bytesType().name("CUInt").type().optional().intType().name("CULong").type().optional().longType().name("CUFloat").type().optional().floatType().name("CUDouble").type().optional().doubleType().name("CUBoolean").type().optional().booleanType().endRecord();
try {
dw.create(schema, f);
for (int i = 0; i < nrows; i++) {
GenericRecord gr = new GenericData.Record(schema);
gr.put("CUString", i == 0 ? null : String.valueOf(i));
gr.put("CUBytes", i == 0 ? null : ByteBuffer.wrap(StringUtils.toBytes(i)));
gr.put("CUInt", i == 0 ? null : i);
gr.put("CULong", i == 0 ? null : Long.valueOf(i));
gr.put("CUFloat", i == 0 ? null : Float.valueOf(i));
gr.put("CUDouble", i == 0 ? null : Double.valueOf(i));
gr.put("CUBoolean", i == 0 ? null : (i & 1) == 1);
dw.append(gr);
}
return f;
} finally {
dw.close();
;
}
}
Aggregations