Search in sources :

Example 1 with AutoConsumeSchema

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);
    }
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion)

Example 2 with AutoConsumeSchema

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);
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) Test(org.testng.annotations.Test)

Example 3 with AutoConsumeSchema

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);
    }
}
Also used : KeyValue(org.apache.pulsar.common.api.proto.KeyValue) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchemaImpl(org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl)

Example 4 with AutoConsumeSchema

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);
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) AutoProduceBytesSchema(org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider)

Example 5 with AutoConsumeSchema

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);
}
Also used : GenericAvroSchema(org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) Utf8(org.apache.avro.util.Utf8) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

AutoConsumeSchema (org.apache.pulsar.client.impl.schema.AutoConsumeSchema)34 Test (org.testng.annotations.Test)24 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)18 Foo (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo)12 GenericAvroSchema (org.apache.pulsar.client.impl.schema.generic.GenericAvroSchema)10 HashMap (java.util.HashMap)8 MessageImpl (org.apache.pulsar.client.impl.MessageImpl)7 GenericSchema (org.apache.pulsar.client.api.schema.GenericSchema)6 LongSchemaVersion (org.apache.pulsar.common.schema.LongSchemaVersion)6 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Utf8 (org.apache.avro.util.Utf8)2 Get (org.apache.hadoop.hbase.client.Get)2 Result (org.apache.hadoop.hbase.client.Result)2 Table (org.apache.hadoop.hbase.client.Table)2 Consumer (org.apache.pulsar.client.api.Consumer)2 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)2 Schema (org.apache.pulsar.client.api.Schema)2