本文整理汇总了Java中org.apache.avro.generic.GenericDatumReader类的典型用法代码示例。如果您正苦于以下问题:Java GenericDatumReader类的具体用法?Java GenericDatumReader怎么用?Java GenericDatumReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
GenericDatumReader类属于org.apache.avro.generic包,在下文中一共展示了GenericDatumReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readAvroFile
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
/**
* Reads in binary Avro-encoded entities using the schema stored in the file
* and prints them out.
*/
public static void readAvroFile(File file) throws IOException {
GenericDatumReader datum = new GenericDatumReader();
DataFileReader reader = new DataFileReader(file, datum);
GenericData.Record record = new GenericData.Record(reader.getSchema());
while (reader.hasNext()) {
reader.next(record);
System.out.println("Name " + record.get("name") + " on "
+ record.get("Meetup_date") + " attending "
+ record.get("going") + " organized by "
+ record.get("organizer") + " on " + record.get("topics"));
}
reader.close();
}
示例2: convertAvroToJson
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
static void convertAvroToJson(InputStream inputStream, OutputStream outputStream, Schema schema)
throws IOException {
DatumReader<Object> reader = new GenericDatumReader<>(schema);
DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
BinaryDecoder binaryDecoder = DecoderFactory.get().binaryDecoder(inputStream, null);
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, outputStream, true);
Object datum = null;
while (!binaryDecoder.isEnd()) {
datum = reader.read(datum, binaryDecoder);
writer.write(datum, jsonEncoder);
jsonEncoder.flush();
}
outputStream.flush();
}
示例3: AvroFileInputStream
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
public AvroFileInputStream(FileStatus status) throws IOException {
pos = 0;
buffer = new byte[0];
GenericDatumReader<Object> reader = new GenericDatumReader<Object>();
FileContext fc = FileContext.getFileContext(new Configuration());
fileReader =
DataFileReader.openReader(new AvroFSInput(fc, status.getPath()),reader);
Schema schema = fileReader.getSchema();
writer = new GenericDatumWriter<Object>(schema);
output = new ByteArrayOutputStream();
JsonGenerator generator =
new JsonFactory().createJsonGenerator(output, JsonEncoding.UTF8);
MinimalPrettyPrinter prettyPrinter = new MinimalPrettyPrinter();
prettyPrinter.setRootValueSeparator(System.getProperty("line.separator"));
generator.setPrettyPrinter(prettyPrinter);
encoder = EncoderFactory.get().jsonEncoder(schema, generator);
}
示例4: testGenericRecord
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
@Test
public void testGenericRecord() throws IOException {
final Path outputPath = new Path(File.createTempFile("avro-output-file", "generic.avro").getAbsolutePath());
final AvroOutputFormat<GenericRecord> outputFormat = new AvroOutputFormat<>(outputPath, GenericRecord.class);
Schema schema = new Schema.Parser().parse("{\"type\":\"record\", \"name\":\"user\", \"fields\": [{\"name\":\"user_name\", \"type\":\"string\"}, {\"name\":\"favorite_number\", \"type\":\"int\"}, {\"name\":\"favorite_color\", \"type\":\"string\"}]}");
outputFormat.setWriteMode(FileSystem.WriteMode.OVERWRITE);
outputFormat.setSchema(schema);
output(outputFormat, schema);
GenericDatumReader<GenericRecord> reader = new GenericDatumReader<>(schema);
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new File(outputPath.getPath()), reader);
while (dataFileReader.hasNext()) {
GenericRecord record = dataFileReader.next();
assertEquals(record.get("user_name").toString(), "testUser");
assertEquals(record.get("favorite_number"), 1);
assertEquals(record.get("favorite_color").toString(), "blue");
}
//cleanup
FileSystem fs = FileSystem.getLocalFileSystem();
fs.delete(outputPath, false);
}
示例5: processSinglex
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
/**
* Process singlex.
*
* @throws Exception the exception
*/
public void processSinglex() throws Exception {
int base = (int) System.currentTimeMillis();
User user = User.newBuilder().setName("name" + base).setFavoriteColor("color" + base).setFavoriteNumber(base)
.build();
DatumWriter<GenericRecord> datumWriterUser = new GenericDatumWriter<GenericRecord>(User.getClassSchema());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] byteData = null;
try {
BinaryEncoder binaryEncoder = EncoderFactory.get().binaryEncoder(baos, null);
datumWriterUser.write(user, binaryEncoder);
binaryEncoder.flush();
byteData = baos.toByteArray();
} finally {
baos.close();
}
System.out.println(byteData.length);
DatumReader<GenericRecord> datumReaderUser = new GenericDatumReader<GenericRecord>( User.getClassSchema());
GenericRecord genericRecord = datumReaderUser.read(null, DecoderFactory.get().binaryDecoder(byteData, null) );
System.out.println(genericRecord);
System.out.println( genericRecord.get("name"));
}
示例6: checkNumeric
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
private void checkNumeric(String type, Object value) throws Exception {
String def =
"{\"type\":\"record\",\"name\":\"X\",\"fields\":"
+"[{\"type\":\""+type+"\",\"name\":\"n\"}]}";
Schema schema = Schema.parse(def);
DatumReader<GenericRecord> reader =
new GenericDatumReader<GenericRecord>(schema);
String[] records = {"{\"n\":1}", "{\"n\":1.0}"};
for (String record : records) {
Decoder decoder = new ExtendedJsonDecoder(schema, record);
GenericRecord r = reader.read(null, decoder);
Assert.assertEquals(value, r.get("n"));
}
}
示例7: initialize
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
private void initialize() throws IOException, NoSuchAlgorithmException {
SeekableResettableInputBridge in = new SeekableResettableInputBridge(ris);
long pos = in.tell();
in.seek(0L);
fileReader = new DataFileReader<GenericRecord>(in,
new GenericDatumReader<GenericRecord>());
fileReader.sync(pos);
schema = fileReader.getSchema();
datumWriter = new GenericDatumWriter(schema);
out = new ByteArrayOutputStream();
encoder = EncoderFactory.get().binaryEncoder(out, encoder);
schemaHash = SchemaNormalization.parsingFingerprint("CRC-64-AVRO", schema);
schemaHashString = Hex.encodeHexString(schemaHash);
}
示例8: convertJsonToAvro
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
static void convertJsonToAvro(InputStream inputStream, OutputStream outputStream, Schema schema)
throws IOException {
DatumReader<Object> reader = new GenericDatumReader<>(schema);
DatumWriter<Object> writer = new GenericDatumWriter<>(schema);
Encoder binaryEncoder = EncoderFactory.get().binaryEncoder(outputStream, null);
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, inputStream);
Object datum = null;
while (true) {
try {
datum = reader.read(datum, jsonDecoder);
} catch (EOFException eofException) {
break;
}
writer.write(datum, binaryEncoder);
binaryEncoder.flush();
}
outputStream.flush();
}
示例9: testCompressFile
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
@Test
public void testCompressFile() throws Exception {
String avroCodec = "snappy";
localProps.put(StorageSinkConnectorConfig.AVRO_CODEC_CONFIG, avroCodec);
setUp();
task = new S3SinkTask(connectorConfig, context, storage, partitioner, format, SYSTEM_TIME);
List<SinkRecord> sinkRecords = createRecords(7);
// Perform write
task.put(sinkRecords);
task.close(context.assignment());
task.stop();
List<S3ObjectSummary> summaries = listObjects(S3_TEST_BUCKET_NAME, "/", s3);
for(S3ObjectSummary summary: summaries){
InputStream in = s3.getObject(summary.getBucketName(), summary.getKey()).getObjectContent();
DatumReader<Object> reader = new GenericDatumReader<>();
DataFileStream<Object> streamReader = new DataFileStream<>(in, reader);
// make sure that produced Avro file has proper codec set
Assert.assertEquals(avroCodec, streamReader.getMetaString(StorageSinkConnectorConfig.AVRO_CODEC_CONFIG));
streamReader.close();
}
long[] validOffsets = {0, 3, 6};
verify(sinkRecords, validOffsets);
}
示例10: processAvroMessage
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
/**
* Processes an Avro Blob containing a single message and with no embedded
* schema. This is the pattern when Avro objects are passed over messaging
* infrastructure such as Apache Kafka.
*
* @param avroMessage
* The Blob that holds the single Avro message object
* @param avroKey
* The Blob that holds the single Avro key object (if passed)
* @param outStream
* The stream to which the JSON string must be submitted
* @param outTuple
* The tuple holding the JSON string
* @param messageSchema
* The schema of the Avro messsage object
* @param keySchema
* The schema of the Avro key object
* @throws Exception
*/
private void processAvroMessage(Blob avroMessage, Blob avroKey, StreamingOutput<OutputTuple> outStream,
OutputTuple outTuple, Schema messageSchema, Schema keySchema) throws Exception {
// Deserialize message
GenericDatumReader<GenericRecord> consumer = new GenericDatumReader<GenericRecord>(messageSchema);
ByteArrayInputStream consumedByteArray = new ByteArrayInputStream(avroMessage.getData());
Decoder consumedDecoder = DecoderFactory.get().binaryDecoder(consumedByteArray, null);
GenericRecord consumedDatum = consumer.read(null, consumedDecoder);
if (LOGGER.isTraceEnabled())
LOGGER.log(TraceLevel.TRACE, "JSON representation of Avro message: " + consumedDatum.toString());
outTuple.setString(outputJsonMessage, consumedDatum.toString());
// Deserialize key (if specified)
if (avroKey != null) {
consumer = new GenericDatumReader<GenericRecord>(keySchema);
consumedByteArray = new ByteArrayInputStream(avroKey.getData());
consumedDecoder = DecoderFactory.get().binaryDecoder(consumedByteArray, null);
consumedDatum = consumer.read(null, consumedDecoder);
if (LOGGER.isTraceEnabled())
LOGGER.log(TraceLevel.TRACE, "JSON representation of Avro key: " + consumedDatum.toString());
if (outputJsonKey != null)
outTuple.setString(outputJsonKey, consumedDatum.toString());
}
// Submit new tuple to output port 0
outStream.submit(outTuple);
}
示例11: configure
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
@Override
public void configure(Config config) {
fieldNames = config.getStringList(FIELD_NAMES_CONFIG_NAME);
fieldTypes = config.getStringList(FIELD_TYPES_CONFIG_NAME);
avroSchema = schemaFor(fieldNames, fieldTypes);
reader = new GenericDatumReader<GenericRecord>(avroSchema);
doesAppendRaw = TranslatorUtils.doesAppendRaw(config);
if (doesAppendRaw) {
fieldNames.add(TranslatorUtils.getAppendRawKeyFieldName(config));
fieldTypes.add("binary");
fieldNames.add(TranslatorUtils.getAppendRawValueFieldName(config));
fieldTypes.add("binary");
}
schema = RowUtils.structTypeFor(fieldNames, fieldTypes);
}
示例12: AvroBlock
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
AvroBlock(
byte[] data,
long numRecords,
Mode<T> mode,
String writerSchemaString,
String codec)
throws IOException {
this.mode = mode;
this.numRecords = numRecords;
checkNotNull(writerSchemaString, "writerSchemaString");
Schema writerSchema = internOrParseSchemaString(writerSchemaString);
Schema readerSchema =
internOrParseSchemaString(
MoreObjects.firstNonNull(mode.readerSchemaString, writerSchemaString));
this.reader =
(mode.type == GenericRecord.class)
? new GenericDatumReader<T>(writerSchema, readerSchema)
: new ReflectDatumReader<T>(writerSchema, readerSchema);
this.decoder = DecoderFactory.get().binaryDecoder(decodeAsInputStream(data, codec), null);
}
示例13: testCompressedWriteAndReadASingleFile
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
@Category(NeedsRunner.class)
public void testCompressedWriteAndReadASingleFile() throws Throwable {
List<GenericClass> values =
ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
File outputFile = tmpFolder.newFile("output.avro");
writePipeline.apply(Create.of(values))
.apply(
AvroIO.write(GenericClass.class)
.to(outputFile.getAbsolutePath())
.withoutSharding()
.withCodec(CodecFactory.deflateCodec(9)));
writePipeline.run();
PAssert.that(
readPipeline.apply(AvroIO.read(GenericClass.class).from(outputFile.getAbsolutePath())))
.containsInAnyOrder(values);
readPipeline.run();
try (DataFileStream dataFileStream =
new DataFileStream(new FileInputStream(outputFile), new GenericDatumReader())) {
assertEquals("deflate", dataFileStream.getMetaString("avro.codec"));
}
}
示例14: testWriteThenReadASingleFileWithNullCodec
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
@Category(NeedsRunner.class)
public void testWriteThenReadASingleFileWithNullCodec() throws Throwable {
List<GenericClass> values =
ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
File outputFile = tmpFolder.newFile("output.avro");
writePipeline.apply(Create.of(values))
.apply(
AvroIO.write(GenericClass.class)
.to(outputFile.getAbsolutePath())
.withoutSharding()
.withCodec(CodecFactory.nullCodec()));
writePipeline.run();
PAssert.that(
readPipeline.apply(AvroIO.read(GenericClass.class).from(outputFile.getAbsolutePath())))
.containsInAnyOrder(values);
readPipeline.run();
try (DataFileStream dataFileStream =
new DataFileStream(new FileInputStream(outputFile), new GenericDatumReader())) {
assertEquals("null", dataFileStream.getMetaString("avro.codec"));
}
}
示例15: initReader
import org.apache.avro.generic.GenericDatumReader; //导入依赖的package包/类
private DataFileReader<E> initReader(FileInputSplit split) throws IOException {
DatumReader<E> datumReader;
if (org.apache.avro.generic.GenericRecord.class == avroValueType) {
datumReader = new GenericDatumReader<E>();
} else {
datumReader = org.apache.avro.specific.SpecificRecordBase.class.isAssignableFrom(avroValueType)
? new SpecificDatumReader<E>(avroValueType) : new ReflectDatumReader<E>(avroValueType);
}
if (LOG.isInfoEnabled()) {
LOG.info("Opening split {}", split);
}
SeekableInput in = new FSDataInputStreamWrapper(stream, split.getPath().getFileSystem().getFileStatus(split.getPath()).getLen());
DataFileReader<E> dataFileReader = (DataFileReader) DataFileReader.openReader(in, datumReader);
if (LOG.isDebugEnabled()) {
LOG.debug("Loaded SCHEMA: {}", dataFileReader.getSchema());
}
end = split.getStart() + split.getLength();
recordsReadSinceLastSync = 0;
return dataFileReader;
}