Java Code Examples for org.apache.pulsar.client.api.Schema#AVRO
The following examples show how to use
org.apache.pulsar.client.api.Schema#AVRO .
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: ProxyTest.java From pulsar with Apache License 2.0 | 6 votes |
@Test public void testGetSchema() throws Exception { @Cleanup PulsarClient client = PulsarClient.builder().serviceUrl(proxyService.getServiceUrl()) .build(); Schema<Foo> schema = Schema.AVRO(Foo.class); try { try (Producer<Foo> ignored = client.newProducer(schema).topic("persistent://sample/test/local/get-schema") .create()) { } } catch (Exception ex) { Assert.fail("Should not have failed since can acquire LookupRequestSemaphore"); } byte[] schemaVersion = new byte[8]; byte b = new Long(0l).byteValue(); for (int i = 0; i<8; i++){ schemaVersion[i] = b; } SchemaInfo schemaInfo = ((PulsarClientImpl) client).getLookup() .getSchema(TopicName.get("persistent://sample/test/local/get-schema"), schemaVersion).get().orElse(null); Assert.assertEquals(new String(schemaInfo.getSchema()), new String(schema.getSchemaInfo().getSchema())); }
Example 2
Source File: GenericSchemaImplTest.java From pulsar with Apache License 2.0 | 6 votes |
@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); }
Example 3
Source File: AdminApiSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@DataProvider(name = "schemas") public Object[][] schemas() { return new Object[][] { { Schema.BOOL }, { Schema.INT8 }, { Schema.INT16 }, { Schema.INT32 }, { Schema.INT64 }, { StringSchema.utf8() }, { new StringSchema(US_ASCII) }, { Schema.FLOAT }, { Schema.DOUBLE }, { Schema.DATE }, { Schema.TIME }, { Schema.TIMESTAMP }, { Schema.AVRO( SchemaDefinition.builder() .withPojo(Foo.class) .withProperties(PROPS) .build() ) }, { Schema.JSON( SchemaDefinition.builder() .withPojo(Foo.class) .withProperties(PROPS) .build() )}, { Schema.KeyValue( StringSchema.utf8(), new StringSchema(US_ASCII) )} }; }
Example 4
Source File: SchemaDataValidatorTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test(dataProvider = "structSchemas") public void testStructValidatorSuccess(SchemaType type) throws Exception { Schema<Foo> schema = Schema.AVRO(Foo.class); SchemaData data = SchemaData.builder() .type(type) .data(schema.getSchemaInfo().getSchema()) .build(); SchemaDataValidator.validateSchemaData(data); }
Example 5
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testAllOptionalFieldsSchema() { RecordSchemaBuilder recordSchemaBuilder = SchemaBuilder.record("org.apache.pulsar.client.impl.schema.SchemaBuilderTest.AllOptionalFields"); recordSchemaBuilder.field("intField") .type(SchemaType.INT32).optional(); recordSchemaBuilder.field("longField") .type(SchemaType.INT64).optional(); recordSchemaBuilder.field("stringField") .type(SchemaType.STRING).optional(); recordSchemaBuilder.field("boolField") .type(SchemaType.BOOLEAN).optional(); recordSchemaBuilder.field("floatField") .type(SchemaType.FLOAT).optional(); recordSchemaBuilder.field("doubleField") .type(SchemaType.DOUBLE).optional(); SchemaInfo schemaInfo = recordSchemaBuilder.build( SchemaType.AVRO ); Schema<AllOptionalFields> pojoSchema = Schema.AVRO(AllOptionalFields.class); SchemaInfo pojoSchemaInfo = pojoSchema.getSchemaInfo(); org.apache.avro.Schema avroSchema = new org.apache.avro.Schema.Parser().parse( new String(schemaInfo.getSchema(), UTF_8) ); org.apache.avro.Schema avroPojoSchema = new org.apache.avro.Schema.Parser().parse( new String(pojoSchemaInfo.getSchema(), UTF_8) ); assertEquals(avroPojoSchema, avroSchema); }
Example 6
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testAllPrimitiveFieldsSchema() { RecordSchemaBuilder recordSchemaBuilder = SchemaBuilder.record("org.apache.pulsar.client.impl.schema.SchemaBuilderTest.AllPrimitiveFields"); recordSchemaBuilder.field("intField") .type(SchemaType.INT32); recordSchemaBuilder.field("longField") .type(SchemaType.INT64); recordSchemaBuilder.field("boolField") .type(SchemaType.BOOLEAN); recordSchemaBuilder.field("floatField") .type(SchemaType.FLOAT); recordSchemaBuilder.field("doubleField") .type(SchemaType.DOUBLE); SchemaInfo schemaInfo = recordSchemaBuilder.build( SchemaType.AVRO ); Schema<AllPrimitiveFields> pojoSchema = Schema.AVRO(AllPrimitiveFields.class); SchemaInfo pojoSchemaInfo = pojoSchema.getSchemaInfo(); org.apache.avro.Schema avroSchema = new org.apache.avro.Schema.Parser().parse( new String(schemaInfo.getSchema(), UTF_8) ); org.apache.avro.Schema avroPojoSchema = new org.apache.avro.Schema.Parser().parse( new String(pojoSchemaInfo.getSchema(), UTF_8) ); assertEquals(avroPojoSchema, avroSchema); }
Example 7
Source File: FlinkPulsarSink.java From pulsar-flink with Apache License 2.0 | 4 votes |
@Override protected Schema<T> getPulsarSchema() { return Schema.AVRO(recordClazz); }
Example 8
Source File: GenericSchemaImplTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericAvroSchema() { Schema<Foo> encodeSchema = Schema.AVRO(Foo.class); GenericSchema decodeSchema = GenericSchemaImpl.of(encodeSchema.getSchemaInfo()); testEncodeAndDecodeGenericRecord(encodeSchema, decodeSchema); }
Example 9
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericRecordBuilderAvroByFieldnamePojo() { RecordSchemaBuilder people1SchemaBuilder = SchemaBuilder.record("People1"); people1SchemaBuilder.field("age").type(SchemaType.INT32); people1SchemaBuilder.field("height").type(SchemaType.INT32); people1SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people1SchemaInfo = people1SchemaBuilder.build(SchemaType.AVRO); GenericSchema people1Schema = Schema.generic(people1SchemaInfo); GenericRecordBuilder people1RecordBuilder = people1Schema.newRecordBuilder(); people1RecordBuilder.set("age", 20); people1RecordBuilder.set("height", 180); people1RecordBuilder.set("name", "people1"); GenericRecord people1GenericRecord = people1RecordBuilder.build(); RecordSchemaBuilder people2SchemaBuilder = SchemaBuilder.record("People2"); people2SchemaBuilder.field("age").type(SchemaType.INT32); people2SchemaBuilder.field("height").type(SchemaType.INT32); people2SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people2SchemaInfo = people2SchemaBuilder.build(SchemaType.AVRO); GenericSchema people2Schema = Schema.generic(people2SchemaInfo); GenericRecordBuilder people2RecordBuilder = people2Schema.newRecordBuilder(); people2RecordBuilder.set("age", 20); people2RecordBuilder.set("height", 180); people2RecordBuilder.set("name", "people2"); GenericRecord people2GenericRecord = people2RecordBuilder.build(); RecordSchemaBuilder peopleSchemaBuilder = SchemaBuilder.record("People"); peopleSchemaBuilder.field("people1", people1Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("people2", people2Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo schemaInfo = peopleSchemaBuilder.build(SchemaType.AVRO); GenericSchema peopleSchema = Schema.generic(schemaInfo); GenericRecordBuilder peopleRecordBuilder = peopleSchema.newRecordBuilder(); peopleRecordBuilder.set("people1", people1GenericRecord); peopleRecordBuilder.set("people2", people2GenericRecord); peopleRecordBuilder.set("name", "people"); GenericRecord peopleRecord = peopleRecordBuilder.build(); byte[] peopleEncode = peopleSchema.encode(peopleRecord); Schema<People> peopleDecodeSchema = Schema.AVRO( SchemaDefinition.<People>builder().withPojo(People.class).withAlwaysAllowNull(false).build()); People people = peopleDecodeSchema.decode(peopleEncode); assertEquals(people.name, peopleRecord.getField("name")); assertEquals(people.getPeople1().age, people1GenericRecord.getField("age")); assertEquals(people.getPeople1().height, people1GenericRecord.getField("height")); assertEquals(people.getPeople1().name, people1GenericRecord.getField("name")); assertEquals(people.getPeople2().age, people2GenericRecord.getField("age")); assertEquals(people.getPeople2().height, people2GenericRecord.getField("height")); assertEquals(people.getPeople2().name, people2GenericRecord.getField("name")); }
Example 10
Source File: SchemaBuilderTest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testGenericRecordBuilderAvroByFieldIndexPojo() { RecordSchemaBuilder people1SchemaBuilder = SchemaBuilder.record("People1"); people1SchemaBuilder.field("age").type(SchemaType.INT32); people1SchemaBuilder.field("height").type(SchemaType.INT32); people1SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people1SchemaInfo = people1SchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> people1Schema = Schema.generic(people1SchemaInfo); GenericRecordBuilder people1RecordBuilder = people1Schema.newRecordBuilder(); people1RecordBuilder.set(people1Schema.getFields().get(0), 20); people1RecordBuilder.set(people1Schema.getFields().get(1), 180); people1RecordBuilder.set(people1Schema.getFields().get(2), "people1"); GenericRecord people1GenericRecord = people1RecordBuilder.build(); RecordSchemaBuilder people2SchemaBuilder = SchemaBuilder.record("People2"); people2SchemaBuilder.field("age").type(SchemaType.INT32); people2SchemaBuilder.field("height").type(SchemaType.INT32); people2SchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo people2SchemaInfo = people2SchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> people2Schema = Schema.generic(people2SchemaInfo); GenericRecordBuilder people2RecordBuilder = people2Schema.newRecordBuilder(); people2RecordBuilder.set(people2Schema.getFields().get(0), 20); people2RecordBuilder.set(people2Schema.getFields().get(1), 180); people2RecordBuilder.set(people2Schema.getFields().get(2), "people2"); GenericRecord people2GenericRecord = people2RecordBuilder.build(); RecordSchemaBuilder peopleSchemaBuilder = SchemaBuilder.record("People"); peopleSchemaBuilder.field("people1", people1Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("people2", people2Schema).type(SchemaType.AVRO); peopleSchemaBuilder.field("name").type(SchemaType.STRING); SchemaInfo schemaInfo = peopleSchemaBuilder.build(SchemaType.AVRO); GenericSchema<GenericRecord> peopleSchema = Schema.generic(schemaInfo); GenericRecordBuilder peopleRecordBuilder = peopleSchema.newRecordBuilder(); peopleRecordBuilder.set(peopleSchema.getFields().get(0), people1GenericRecord); peopleRecordBuilder.set(peopleSchema.getFields().get(1), people2GenericRecord); peopleRecordBuilder.set(peopleSchema.getFields().get(2), "people"); GenericRecord peopleRecord = peopleRecordBuilder.build(); byte[] peopleEncode = peopleSchema.encode(peopleRecord); Schema<People> peopleDecodeSchema = Schema.AVRO( SchemaDefinition.<People>builder().withPojo(People.class).withAlwaysAllowNull(false).build()); People people = peopleDecodeSchema.decode(peopleEncode); assertEquals(people.name, peopleRecord.getField("name")); assertEquals(people.getPeople1().age, people1GenericRecord.getField("age")); assertEquals(people.getPeople1().height, people1GenericRecord.getField("height")); assertEquals(people.getPeople1().name, people1GenericRecord.getField("name")); assertEquals(people.getPeople2().age, people2GenericRecord.getField("age")); assertEquals(people.getPeople2().height, people2GenericRecord.getField("height")); assertEquals(people.getPeople2().name, people2GenericRecord.getField("name")); }