use of org.apache.pulsar.client.impl.schema.AutoConsumeSchema in project pulsar by apache.
the class GenericSchemaImplTest method testEncodeAndDecodeGenericRecord.
private void testEncodeAndDecodeGenericRecord(Schema<Foo> encodeSchema, Schema<GenericRecord> decodeSchema) {
int numRecords = 10;
for (int i = 0; i < numRecords; i++) {
Foo foo = newFoo(i);
byte[] data = encodeSchema.encode(foo);
log.info("Decoding : {}", new String(data, UTF_8));
GenericRecord record;
if (decodeSchema instanceof AutoConsumeSchema) {
record = decodeSchema.decode(data, new LongSchemaVersion(0L).bytes());
} else {
record = decodeSchema.decode(data);
}
verifyFooRecord(record, i);
}
}
use of org.apache.pulsar.client.impl.schema.AutoConsumeSchema in project pulsar by apache.
the class GenericSchemaImplTest method testAutoAvroSchema.
@Test
public void testAutoAvroSchema() {
// configure encode schema
Schema<Foo> encodeSchema = Schema.AVRO(Foo.class);
// configure the schema info provider
MultiVersionSchemaInfoProvider multiVersionGenericSchemaProvider = mock(MultiVersionSchemaInfoProvider.class);
when(multiVersionGenericSchemaProvider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(encodeSchema.getSchemaInfo()));
// configure decode schema
AutoConsumeSchema decodeSchema = new AutoConsumeSchema();
decodeSchema.configureSchemaInfo("test-topic", "topic", encodeSchema.getSchemaInfo());
decodeSchema.setSchemaInfoProvider(multiVersionGenericSchemaProvider);
testEncodeAndDecodeGenericRecord(encodeSchema, decodeSchema);
}
use of org.apache.pulsar.client.impl.schema.AutoConsumeSchema in project pulsar by apache.
the class MessageImpl method getKeyValueBySchemaVersion.
private T getKeyValueBySchemaVersion() {
KeyValueSchemaImpl kvSchema = getKeyValueSchema();
byte[] schemaVersion = getSchemaVersion();
if (kvSchema.getKeyValueEncodingType() == KeyValueEncodingType.SEPARATED) {
org.apache.pulsar.common.schema.KeyValue keyValue = (org.apache.pulsar.common.schema.KeyValue) kvSchema.decode(getKeyBytes(), getData(), schemaVersion);
if (schema instanceof AutoConsumeSchema) {
return (T) AutoConsumeSchema.wrapPrimitiveObject(keyValue, ((AutoConsumeSchema) schema).getSchemaInfo(schemaVersion).getType(), schemaVersion);
} else {
return (T) keyValue;
}
} else {
return decode(schemaVersion);
}
}
use of org.apache.pulsar.client.impl.schema.AutoConsumeSchema in project pulsar by apache.
the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.
@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
if (schema != null && schema.supportSchemaVersioning()) {
final SchemaInfoProvider schemaInfoProvider;
try {
schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
} catch (ExecutionException e) {
log.error("Failed to load schema info provider for topic {}", topicName, e);
return FutureUtil.failedFuture(e.getCause());
}
schema = schema.clone();
if (schema.requireFetchingSchemaInfo()) {
@SuppressWarnings("rawtypes") Schema finalSchema = schema;
return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
if (null == schemaInfo) {
if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
// no schema info is found
return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
}
}
try {
log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
} catch (RuntimeException re) {
return FutureUtil.failedFuture(re);
}
finalSchema.setSchemaInfoProvider(schemaInfoProvider);
return CompletableFuture.completedFuture(finalSchema);
});
} else {
schema.setSchemaInfoProvider(schemaInfoProvider);
}
}
return CompletableFuture.completedFuture(schema);
}
use of org.apache.pulsar.client.impl.schema.AutoConsumeSchema in project pulsar by apache.
the class InfluxDBSinkTest method testAvroSchema.
@Test
public void testAvroSchema() {
AvroSchema<Cpu> schema = AvroSchema.of(Cpu.class);
AutoConsumeSchema autoConsumeSchema = new AutoConsumeSchema();
autoConsumeSchema.setSchema(GenericSchemaImpl.of(schema.getSchemaInfo()));
GenericSchema<GenericRecord> genericAvroSchema = GenericSchemaImpl.of(autoConsumeSchema.getSchemaInfo());
assertTrue(genericAvroSchema instanceof GenericAvroSchema);
byte[] bytes = schema.encode(cpu);
GenericRecord record = genericAvroSchema.decode(bytes);
assertEquals(record.getField("measurement"), "cpu");
assertEquals(record.getField("timestamp"), timestamp);
assertEquals(((Map) record.getField("tags")).get(new Utf8("host")).toString(), "server-1");
assertEquals(((Map) record.getField("fields")).get(new Utf8("value")), 10);
}
Aggregations