本文整理匯總了Java中org.apache.avro.specific.SpecificData類的典型用法代碼示例。如果您正苦於以下問題:Java SpecificData類的具體用法?Java SpecificData怎麽用?Java SpecificData使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SpecificData類屬於org.apache.avro.specific包,在下文中一共展示了SpecificData類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testAvroCoderStrings
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@Test
public void testAvroCoderStrings() {
// Custom Strings in Records
assertDeterministic(AvroCoder.of(SchemaBuilder.record("someRecord").fields()
.name("string").prop(SpecificData.CLASS_PROP, "java.lang.String")
.type().stringType().noDefault()
.endRecord()));
assertNonDeterministic(AvroCoder.of(SchemaBuilder.record("someRecord").fields()
.name("string").prop(SpecificData.CLASS_PROP, "unknownString")
.type().stringType().noDefault()
.endRecord()),
reason("someRecord.string", "unknownString is not known to be deterministic"));
// Custom Strings in Unions
assertNonDeterministic(AvroCoder.of(SchemaBuilder.unionOf()
.intType().and()
.record("someRecord").fields()
.name("someField").prop(SpecificData.CLASS_PROP, "unknownString")
.type().stringType().noDefault().endRecord()
.endUnion()),
reason("someRecord.someField", "unknownString is not known to be deterministic"));
}
示例2: init
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@PostConstruct
public void init() {
Properties props = new Properties();
props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-streams-processor");
props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka:9092");
props.put(StreamsConfig.ZOOKEEPER_CONNECT_CONFIG, "zookeeper:2181");
props.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://schema-registry:8081");
KStreamBuilder builder = new KStreamBuilder();
builder.stream("tweets")
.map((k, v) -> {
Tweet tweet = (Tweet) SpecificData.get().deepCopy(Tweet.getClassSchema(), v);
return new KeyValue<>(tweet.getId(), tweet.getText().toString());
})
.to(Serdes.Long(), Serdes.String(), "processed-tweets");
KafkaStreams streams = new KafkaStreams(builder, props);
streams.start();
}
示例3: initializeAvro
import org.apache.avro.specific.SpecificData; //導入依賴的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();
}
示例4: deserialize
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public T deserialize(String topic, byte[] data) {
LOGGER.debug("data to deserialize='{}'", DatatypeConverter.printHexBinary(data));
try {
// get the schema
Schema schema = targetType.newInstance().getSchema();
Injection<GenericRecord, byte[]> genericRecordInjection = GenericAvroCodecs.toBinary(schema);
GenericRecord genericRecord = genericRecordInjection.invert((byte[]) data).get();
T result = (T) SpecificData.get().deepCopy(schema, genericRecord);
LOGGER.debug("data='{}'", result);
return result;
} catch (Exception e) {
throw new SerializationException(
"Can't deserialize data [" + Arrays.toString(data) + "] from topic [" + topic + "]", e);
}
}
示例5: extractParams
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
/**
* Extracts parameters from RPC call to List or converts to object of appropriate type
* if only one parameter set.
*
* @param message Avro message
* @param request Avro request
* @param singleParameter Indicates that called method has single parameter
* @param dataResolver Extracts type of parameters in call
* @return Parameters of RPC method invocation
*/
private static Object extractParams(Protocol.Message message, Object request, boolean singleParameter, SpecificData dataResolver) {
if (singleParameter) {
Schema.Field field = message.getRequest().getFields().get(0);
return dataResolver.getField(request, field.name(), field.pos());
} else {
int i = 0;
Object[] params = new Object[message.getRequest().getFields().size()];
for (Schema.Field param : message.getRequest().getFields()) {
params[i] = dataResolver.getField(request, param.name(), param.pos());
i++;
}
return params;
}
}
示例6: applyDiff
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
public static GenericRecord applyDiff(GenericRecord avroObj, RecordDiff diff, Schema schema) throws IOException {
GenericRecord modifiedAvroObj = createGenericRecordWithSchema(schema, avroObj);
Map<String, Object> diffFields = diff.getDiffFields();
List<Schema.Field> fields = schema.getFields();
for (Schema.Field field : fields) {
if (diffFields.containsKey(field.name())) {
GenericRecord fieldsValue = (GenericRecord) diffFields.get(field.name());
Class<? extends GenericRecord> fieldsValueClass = fieldsValue.getClass();
if (fieldsValueClass.isAssignableFrom(PrimitiveDiff.class)) {
AvroDiffPrimitive.applyPrimitiveDiff(field, avroObj, fieldsValue, modifiedAvroObj, null);
} else if (fieldsValueClass.isAssignableFrom(MapDiff.class)) {
AvroDiffMap.applyMapDiff(field, avroObj, fieldsValue, modifiedAvroObj);
} else if (fieldsValueClass.isAssignableFrom(ArrayDiff.class)) {
AvroDiffArray.applyArrayDiff(field, avroObj, fieldsValue, modifiedAvroObj);
} else if (fieldsValueClass.isAssignableFrom(RecordDiff.class)) {
GenericRecord recordField = (GenericRecord) modifiedAvroObj.get(field.pos());
GenericRecord genericRecord = applyDiff(recordField, (RecordDiff) fieldsValue, recordField.getSchema());
modifiedAvroObj.put(field.pos(), genericRecord);
} else {
LOGGER.error("Field from RecordDiff has unknown type.");
}
} else {
modifiedAvroObj.put(field.pos(), avroObj.get(field.pos()));
}
}
return SpecificData.get().deepCopy(schema, modifiedAvroObj);
}
示例7: EventReader
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
/**
* Create a new Event Reader
* @param in
* @throws IOException
*/
@SuppressWarnings("deprecation")
public EventReader(DataInputStream in) throws IOException {
this.in = in;
this.version = in.readLine();
if (!EventWriter.VERSION.equals(version)) {
throw new IOException("Incompatible event log version: "+version);
}
Schema myschema = new SpecificData(Event.class.getClassLoader()).getSchema(Event.class);
this.schema = Schema.parse(in.readLine());
this.reader = new SpecificDatumReader(schema, myschema);
this.decoder = DecoderFactory.get().jsonDecoder(schema, in);
}
示例8: readObject
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
this.recordClazz = (Class<? extends SpecificRecord>) ois.readObject();
this.schema = SpecificData.get().getSchema(recordClazz);
this.datumWriter = new SpecificDatumWriter<>(schema);
this.arrayOutputStream = new ByteArrayOutputStream();
this.encoder = EncoderFactory.get().binaryEncoder(arrayOutputStream, null);
}
示例9: _testSerDes
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
private void _testSerDes(Long id, Number serdesProtocolVersion) throws Exception {
SchemaMetadata schemaMetadata =
new SchemaMetadata.Builder("random-" + System.currentTimeMillis())
.schemaGroup("custom")
.type(AvroSchemaProvider.TYPE)
.compatibility(SchemaCompatibility.BACKWARD)
.build();
SchemaIdVersion schemaIdVersion = new SchemaIdVersion(1L, 1, id);
Device input = new Device(1L, "device", 1, System.currentTimeMillis());
SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(id, input.getName().toString(), schemaIdVersion.getVersion(),
input.getSchema().toString(),
System.currentTimeMillis(),
"some device");
new Expectations() {
{
mockSchemaRegistryClient.getSchemaMetadataInfo(anyString);
result = new SchemaMetadataInfo(schemaMetadata); minTimes=0; maxTimes=1;
mockSchemaRegistryClient.addSchemaVersion(withInstanceOf(SchemaMetadata.class), withInstanceOf(SchemaVersion.class));
result = schemaIdVersion; minTimes=0; maxTimes=1;
mockSchemaRegistryClient.getSchemaVersionInfo(withInstanceOf(SchemaVersionKey.class));
result = schemaVersionInfo; minTimes=0; maxTimes=1;
}
};
AvroSnapshotSerializer serializer = new AvroSnapshotSerializer();
serializer.init(Collections.singletonMap(SERDES_PROTOCOL_VERSION, serdesProtocolVersion));
AvroSnapshotDeserializer deserializer = new AvroSnapshotDeserializer();
deserializer.init(Collections.emptyMap());
byte[] serializedData = serializer.serialize(input, schemaMetadata);
Object deserializedObj = deserializer.deserialize(new ByteArrayInputStream(serializedData), null);
Assert.assertTrue(SpecificData.get().compare(input, deserializedObj, input.getSchema()) == 0);
}
示例10: assertAvroObjs
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
public static void assertAvroObjs(Object expected, Object given) {
if (expected instanceof byte[]) {
Assert.assertArrayEquals((byte[]) expected, (byte[]) given);
} else if(expected instanceof SpecificRecord) {
Assert.assertTrue(SpecificData.get().compare(expected, given, ((SpecificRecord) expected).getSchema()) == 0);
} else {
Assert.assertEquals(expected, given);
}
}
示例11: AvroRowDeserializationSchema
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
/**
* Creates a Avro deserialization schema for the given record.
*
* @param recordClazz Avro record class used to deserialize Avro's record to Flink's row
*/
public AvroRowDeserializationSchema(Class<? extends SpecificRecord> recordClazz) {
Preconditions.checkNotNull(recordClazz, "Avro record class must not be null.");
this.schema = SpecificData.get().getSchema(recordClazz);
this.datumReader = new SpecificDatumReader<>(schema);
this.record = (SpecificRecord) SpecificData.newInstance(recordClazz, schema);
this.inputStream = new MutableByteArrayInputStream();
this.decoder = DecoderFactory.get().binaryDecoder(inputStream, null);
}
示例12: writeExternal
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
示例13: readExternal
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}
示例14: writeExternal
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@Override
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException {
WRITER$.write(this, SpecificData.getEncoder(out));
}
示例15: readExternal
import org.apache.avro.specific.SpecificData; //導入依賴的package包/類
@Override
public void readExternal(java.io.ObjectInput in) throws java.io.IOException {
READER$.read(this, SpecificData.getDecoder(in));
}