本文整理匯總了Java中org.apache.avro.specific.SpecificRecord類的典型用法代碼示例。如果您正苦於以下問題:Java SpecificRecord類的具體用法?Java SpecificRecord怎麽用?Java SpecificRecord使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SpecificRecord類屬於org.apache.avro.specific包,在下文中一共展示了SpecificRecord類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fromBytes
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T fromBytes( byte[] val, Schema schema
, SpecificDatumReader<T> datumReader, T object)
throws IOException {
Type type = schema.getType();
switch (type) {
case ENUM:
String symbol = schema.getEnumSymbols().get(val[0]);
return (T)Enum.valueOf(ReflectData.get().getClass(schema), symbol);
case STRING: return (T)new Utf8(toString(val));
case BYTES: return (T)ByteBuffer.wrap(val);
case INT: return (T)Integer.valueOf(bytesToVint(val));
case LONG: return (T)Long.valueOf(bytesToVlong(val));
case FLOAT: return (T)Float.valueOf(toFloat(val));
case DOUBLE: return (T)Double.valueOf(toDouble(val));
case BOOLEAN: return (T)Boolean.valueOf(val[0] != 0);
case RECORD: //fall
case MAP:
case ARRAY: return (T)IOUtils.deserialize(val, (SpecificDatumReader<SpecificRecord>)datumReader, schema, (SpecificRecord)object);
default: throw new RuntimeException("Unknown type: "+type);
}
}
示例2: toBytes
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> byte[] toBytes(T o, Schema schema
, SpecificDatumWriter<T> datumWriter)
throws IOException {
Type type = schema.getType();
switch (type) {
case STRING: return toBytes(((Utf8)o).toString()); // TODO: maybe ((Utf8)o).getBytes(); ?
case BYTES: return ((ByteBuffer)o).array();
case INT: return vintToBytes((Integer)o);
case LONG: return vintToBytes((Long)o);
case FLOAT: return toBytes((Float)o);
case DOUBLE: return toBytes((Double)o);
case BOOLEAN: return (Boolean)o ? new byte[] {1} : new byte[] {0};
case ENUM: return new byte[] { (byte)((Enum<?>) o).ordinal() };
case RECORD: //fall
case MAP:
case ARRAY: return IOUtils.serialize((SpecificDatumWriter<SpecificRecord>)datumWriter, schema, (SpecificRecord)o);
default: throw new RuntimeException("Unknown type: "+type);
}
}
示例3: getSimpleTestData
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
/**
* Tests a simple Avro data types without nesting.
*/
public static Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> getSimpleTestData() {
final Address addr = Address.newBuilder()
.setNum(42)
.setStreet("Main Street 42")
.setCity("Test City")
.setState("Test State")
.setZip("12345")
.build();
final Row rowAddr = new Row(5);
rowAddr.setField(0, 42);
rowAddr.setField(1, "Main Street 42");
rowAddr.setField(2, "Test City");
rowAddr.setField(3, "Test State");
rowAddr.setField(4, "12345");
final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> t = new Tuple3<>();
t.f0 = Address.class;
t.f1 = addr;
t.f2 = rowAddr;
return t;
}
示例4: initializeAvro
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
private void initializeAvro() {
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (SpecificRecord.class.isAssignableFrom(type)) {
this.avroData = new SpecificData(cl);
this.schema = this.avroData.getSchema(type);
this.reader = new SpecificDatumReader<>(schema, schema, avroData);
this.writer = new SpecificDatumWriter<>(schema, avroData);
}
else {
final ReflectData reflectData = new ReflectData(cl);
this.avroData = reflectData;
this.schema = this.avroData.getSchema(type);
this.reader = new ReflectDatumReader<>(schema, schema, reflectData);
this.writer = new ReflectDatumWriter<>(schema, reflectData);
}
this.encoder = new DataOutputEncoder();
this.decoder = new DataInputDecoder();
}
示例5: testSerializability
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@Test
public void testSerializability() throws IOException, ClassNotFoundException {
final Tuple3<Class<? extends SpecificRecord>, SpecificRecord, Row> testData = AvroTestUtils.getComplexTestData();
final AvroRowSerializationSchema serOrig = new AvroRowSerializationSchema(testData.f0);
final AvroRowDeserializationSchema deserOrig = new AvroRowDeserializationSchema(testData.f0);
byte[] serBytes = InstantiationUtil.serializeObject(serOrig);
byte[] deserBytes = InstantiationUtil.serializeObject(deserOrig);
AvroRowSerializationSchema serCopy =
InstantiationUtil.deserializeObject(serBytes, Thread.currentThread().getContextClassLoader());
AvroRowDeserializationSchema deserCopy =
InstantiationUtil.deserializeObject(deserBytes, Thread.currentThread().getContextClassLoader());
final byte[] bytes = serCopy.serialize(testData.f2);
deserCopy.deserialize(bytes);
deserCopy.deserialize(bytes);
final Row actual = deserCopy.deserialize(bytes);
assertEquals(testData.f2, actual);
}
示例6: getSpecificRecord
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
public static <T extends AvroTable, A extends SpecificRecord> A getSpecificRecord(Row<T> row){
T table = row.getTable();
Class<A> klass = (Class<A>) table.getKlass();
A specificRecord;
try {
specificRecord = klass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
for (Schema.Field field : specificRecord.getSchema().getFields()) {
specificRecord.put(field.pos(), row.get(table.getColumn(field.name())));
}
return specificRecord;
}
示例7: getDatumWriter
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
private DatumWriter<Object> getDatumWriter(Class<Object> type, Schema schema) {
DatumWriter<Object> writer;
this.logger.debug("Finding correct DatumWriter for type " + type.getName());
if (SpecificRecord.class.isAssignableFrom(type)) {
if (schema != null) {
writer = new SpecificDatumWriter<>(schema);
}
else {
writer = new SpecificDatumWriter<>(type);
}
}
else if (GenericRecord.class.isAssignableFrom(type)) {
writer = new GenericDatumWriter<>(schema);
}
else {
if (schema != null) {
writer = new ReflectDatumWriter<>(schema);
}
else {
writer = new ReflectDatumWriter<>(type);
}
}
return writer;
}
示例8: extractRecords
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
/**
* @param file
* where to read from
* @param schema
* Avro records structure
* @return all values from the file
* @throws IOException
*/
public static <T extends SpecificRecord> List<T> extractRecords(File file, Schema schema) throws IOException {
DatumReader<T> reader = new SpecificDatumReader<>(schema);
DataFileReader<T> fileReader = new DataFileReader<>(file, reader);
List<T> data = new ArrayList<T>();
try {
while (fileReader.hasNext()) {
data.add(fileReader.next());
}
} finally {
if(fileReader != null) {
fileReader.close();
}
}
return data;
}
示例9: loadKeyedRecords
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@Override
public PTable<Tuple3<String, Long, String>, SpecificRecord>
loadKeyedRecords(String inputFormat, Path inputPath, Configuration conf,
Pipeline pipeline, boolean variantsOnly, boolean flatten, String sampleGroup,
Set<String> samples)
throws IOException {
PCollection<Pair<org.bdgenomics.formats.avro.Variant, Collection<Genotype>>> adamRecords
= readVariants(inputFormat, inputPath, conf, pipeline, sampleGroup);
// The data are now loaded into ADAM variant objects; convert to keyed SpecificRecords
ADAMToKeyedSpecificRecordFn converter =
new ADAMToKeyedSpecificRecordFn(variantsOnly, flatten, sampleGroup, samples);
@SuppressWarnings("unchecked")
PType<SpecificRecord> specificPType = Avros.specifics(converter.getSpecificRecordType());
return adamRecords.parallelDo("Convert to keyed SpecificRecords",
converter, Avros.tableOf(KEY_PTYPE, specificPType));
}
示例10: handleEntity
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
/**
* Handles entity by converting it to avro format and writing to output.
* Each entity may consist of many parts: body with updates.
* Optional relations are expected as the last parameters.
*/
private <T extends SpecificRecord> void handleEntity(final String id,
List<QualifiedOafJsonRecord> bodyParts, OafEntityToAvroConverter<T> converter, String outputName,
RelationConversionDTO<?>... relationConversionDTO) throws InterruptedException, IOException {
Oaf oafObj = buildOafObject(bodyParts);
if (oafObj == null) {
log.error("missing 'body' qualifier value for record " + id);
return;
}
if (resultApprover.approve(oafObj)) {
T avroEntity = converter.convert(oafObj.getEntity());
if (avroEntity != null) {
outputs.write(outputName, new AvroKey<T>(avroEntity));
}
// handing relations
if (relationConversionDTO!=null) {
for (RelationConversionDTO<?> currentDTO : relationConversionDTO) {
handleRelation(currentDTO.getOafJsonParts(), currentDTO.getConverter(), currentDTO.getOutputName());
}
}
}
}
示例11: writeTo
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@Override
public void writeTo(Object msg, StringWriter writer) throws Exception {
SpecificRecord record = (SpecificRecord) msg;
ByteArrayOutputStream out = new ByteArrayOutputStream();
_writer.setSchema(record.getSchema());
JsonEncoder encoder = EncoderFactory.get().jsonEncoder(record.getSchema(), out);
_writer.write(msg, encoder);
encoder.flush();
writer.write(out.toString("UTF-8"));
}
示例12: serializeDatum
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
public SerializedDatum serializeDatum(final TDomainClass domainObject) {
try {
final SpecificRecord record = avroRoundTripProjector.toAvro(domainObject);
final Schema schema = record.getSchema();
final SpecificDatumWriter bodyWriter = new SpecificDatumWriter(schema);
final ByteArrayOutputStream bodyByteStream = new ByteArrayOutputStream();
final BinaryEncoder binaryEncoder = EncoderFactory.get().directBinaryEncoder(bodyByteStream, null);
bodyWriter.write(record, binaryEncoder);
binaryEncoder.flush();
bodyByteStream.flush();
final ByteBuffer datumBody = ByteBuffer.wrap(bodyByteStream.toByteArray());
bodyByteStream.close();
final int datumSchemaVersion = datumSchemaRepository.getDatumTypeVersion(schema).getVersion();
return new SerializedDatum(datumBody, new DatumTypeVersion(datumTypeId, datumSchemaVersion));
} catch (final Exception e) {
throw new RuntimeException("Could not create datum body", e);
}
}
示例13: deserializeDatum
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
public TDomainClass deserializeDatum(final SerializedDatum serializedDatum) {
try {
final DatumTypeVersion datumTypeVersion = serializedDatum.getDatumTypeVersion();
final Schema datumWriterSchema = datumSchemaRepository.getSchema(datumTypeVersion);
final Schema datumReaderSchema = datumSchemaRepository.getLatestSchema(datumTypeVersion.getDatumTypeId());
final DatumReader<? extends SpecificRecord> datumReader = new SpecificDatumReader<>(datumWriterSchema,
datumReaderSchema);
final InputStream byteBufferInputStream =
new ByteBufferInputStream(Collections.singletonList(serializedDatum.getPayload()));
final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(byteBufferInputStream, null);
final SpecificRecord record = datumReader.read(null, decoder);
byteBufferInputStream.close();
return avroRoundTripProjector.fromAvro(record);
} catch (final IOException e) {
throw new RuntimeException("Could not deserialize versioned payload to domain object", e);
}
}
示例14: fromBytes
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> T fromBytes( byte[] val, Schema schema
, SpecificDatumReader<T> datumReader, T object)
throws IOException {
Type type = schema.getType();
switch (type) {
case ENUM:
String symbol = schema.getEnumSymbols().get(val[0]);
return (T)Enum.valueOf(ReflectData.get().getClass(schema), symbol);
case STRING: return (T)new Utf8(toString(val));
case BYTES: return (T)ByteBuffer.wrap(val);
case INT: return (T)Integer.valueOf(bytesToVint(val));
case LONG: return (T)Long.valueOf(bytesToVlong(val));
case FLOAT: return (T)Float.valueOf(toFloat(val));
case DOUBLE: return (T)Double.valueOf(toDouble(val));
case BOOLEAN: return (T)Boolean.valueOf(val[0] != 0);
case RECORD: //fall
case MAP:
case ARRAY: return (T)IOUtils.deserialize(val, (SpecificDatumReader<SpecificRecord>)datumReader, (SpecificRecord)object);
default: throw new RuntimeException("Unknown type: "+type);
}
}
示例15: toBytes
import org.apache.avro.specific.SpecificRecord; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static <T> byte[] toBytes(T o, Schema schema
, SpecificDatumWriter<T> datumWriter)
throws IOException {
Type type = schema.getType();
switch (type) {
case STRING: return toBytes(((Utf8)o).toString()); // TODO: maybe ((Utf8)o).getBytes(); ?
case BYTES: return ((ByteBuffer)o).array();
case INT: return vintToBytes((Integer)o);
case LONG: return vintToBytes((Long)o);
case FLOAT: return toBytes((Float)o);
case DOUBLE: return toBytes((Double)o);
case BOOLEAN: return (Boolean)o ? new byte[] {1} : new byte[] {0};
case ENUM: return new byte[] { (byte)((Enum<?>) o).ordinal() };
case RECORD: //fall
case MAP:
case ARRAY: return IOUtils.serialize((SpecificDatumWriter<SpecificRecord>)datumWriter, (SpecificRecord)o);
default: throw new RuntimeException("Unknown type: "+type);
}
}