Java Code Examples for org.apache.pulsar.client.api.Schema#setSchemaInfoProvider()
The following examples show how to use
org.apache.pulsar.client.api.Schema#setSchemaInfoProvider() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PulsarClientImpl.java From pulsar with Apache License 2.0 | 5 votes |
@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()) { Schema finalSchema = schema; return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> { if (null == schemaInfo) { if (!(finalSchema instanceof AutoConsumeSchema)) { // 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); }
Example 2
Source File: AvroSchema.java From pulsar with Apache License 2.0 | 5 votes |
@Override public Schema<T> clone() { Schema<T> schema = new AvroSchema<>(schemaInfo, pojoClassLoader); if (schemaInfoProvider != null) { schema.setSchemaInfoProvider(schemaInfoProvider); } return schema; }
Example 3
Source File: AutoConsumeSchema.java From pulsar with Apache License 2.0 | 5 votes |
@Override public Schema<GenericRecord> clone() { Schema<GenericRecord> schema = Schema.AUTO_CONSUME(); if (this.schema != null) { schema.configureSchemaInfo(topicName, componentName, this.schema.getSchemaInfo()); } else { schema.configureSchemaInfo(topicName, componentName, null); } if (schemaInfoProvider != null) { schema.setSchemaInfoProvider(schemaInfoProvider); } return schema; }
Example 4
Source File: GenericSchemaImplTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testKeyValueSchema() { // configure the schema info provider MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class); GenericSchema genericAvroSchema = GenericSchemaImpl.of(Schema.AVRO(Foo.class).getSchemaInfo()); when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))) .thenReturn(CompletableFuture.completedFuture( KeyValueSchemaInfo.encodeKeyValueSchemaInfo( genericAvroSchema, genericAvroSchema, KeyValueEncodingType.INLINE ) )); List<Schema<Foo>> encodeSchemas = Lists.newArrayList( Schema.JSON(Foo.class), Schema.AVRO(Foo.class) ); for (Schema<Foo> keySchema : encodeSchemas) { for (Schema<Foo> valueSchema : encodeSchemas) { // configure encode schema Schema<KeyValue<Foo, Foo>> kvSchema = KeyValueSchema.of( keySchema, valueSchema ); // configure decode schema Schema<KeyValue<GenericRecord, GenericRecord>> decodeSchema = KeyValueSchema.of( Schema.AUTO_CONSUME(), Schema.AUTO_CONSUME() ); decodeSchema.configureSchemaInfo( "test-topic", "topic",kvSchema.getSchemaInfo() ); decodeSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider); testEncodeAndDecodeKeyValues(kvSchema, decodeSchema); } } }
Example 5
Source File: SupportVersioningKeyValueSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testKeyValueVersioningEncodeDecode() { MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class); AvroSchema<SchemaTestUtils.Foo> fooSchema = AvroSchema.of( SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build()); AvroSchema<SchemaTestUtils.Bar> barSchema = AvroSchema.of( SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build()); Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = KeyValueSchema.of( fooSchema, barSchema); keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider); when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))) .thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo())); SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar(); bar.setField1(true); SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(SchemaTestUtils.Color.RED); byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = keyValueSchema.decode( encodeBytes, new byte[10]); Assert.assertEquals(keyValue.getKey().getField1(), foo.getField1()); Assert.assertEquals(keyValue.getKey().getField2(), foo.getField2()); Assert.assertEquals(keyValue.getKey().getField3(), foo.getField3()); Assert.assertEquals(keyValue.getKey().getField4(), foo.getField4()); Assert.assertEquals(keyValue.getKey().getColor(), foo.getColor()); Assert.assertTrue(keyValue.getValue().isField1()); Assert.assertEquals( KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.INLINE); }
Example 6
Source File: SupportVersioningKeyValueSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testSeparateKeyValueVersioningEncodeDecode() { MultiVersionSchemaInfoProvider multiVersionSchemaInfoProvider = mock(MultiVersionSchemaInfoProvider.class); AvroSchema<SchemaTestUtils.Foo> fooSchema = AvroSchema.of( SchemaDefinition.<SchemaTestUtils.Foo>builder().withPojo(SchemaTestUtils.Foo.class).build()); AvroSchema<SchemaTestUtils.Bar> barSchema = AvroSchema.of( SchemaDefinition.<SchemaTestUtils.Bar>builder().withPojo(SchemaTestUtils.Bar.class).build()); Schema<KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar>> keyValueSchema = KeyValueSchema.of( fooSchema, barSchema, KeyValueEncodingType.SEPARATED); keyValueSchema.setSchemaInfoProvider(multiVersionSchemaInfoProvider); when(multiVersionSchemaInfoProvider.getSchemaByVersion(any(byte[].class))) .thenReturn(CompletableFuture.completedFuture(keyValueSchema.getSchemaInfo())); SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar(); bar.setField1(true); SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(SchemaTestUtils.Color.RED); byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = ((KeyValueSchema)keyValueSchema).decode( fooSchema.encode(foo), encodeBytes, new byte[10]); Assert.assertTrue(keyValue.getValue().isField1()); Assert.assertEquals( KeyValueEncodingType.valueOf(keyValueSchema.getSchemaInfo().getProperties().get("kv.encoding.type")), KeyValueEncodingType.SEPARATED); }