Java Code Examples for org.apache.pulsar.client.api.Schema#decode()
The following examples show how to use
org.apache.pulsar.client.api.Schema#decode() .
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: GenericSchemaImplTest.java From pulsar with Apache License 2.0 | 6 votes |
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 byte[0]); } else { record = decodeSchema.decode(data); } verifyFooRecord(record, i); } }
Example 2
Source File: KeyValueSchemaTest.java From pulsar with Apache License 2.0 | 6 votes |
@Test public void testAllowNullSchemaEncodeAndDecode() { Schema keyValueSchema = Schema.KeyValue(Foo.class, Bar.class); Bar bar = new Bar(); bar.setField1(true); Foo foo = new Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(Color.RED); byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); Assert.assertTrue(encodeBytes.length > 0); KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes); Foo fooBack = keyValue.getKey(); Bar barBack = keyValue.getValue(); assertEquals(foo, fooBack); assertEquals(bar, barBack); }
Example 3
Source File: KeyValueSchemaTest.java From pulsar with Apache License 2.0 | 6 votes |
@Test public void testNotAllowNullSchemaEncodeAndDecode() { Schema keyValueSchema = Schema.KeyValue(JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build()), JSONSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).withAlwaysAllowNull(false).build())); Bar bar = new Bar(); bar.setField1(true); Foo foo = new Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(Color.RED); byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); Assert.assertTrue(encodeBytes.length > 0); KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes); Foo fooBack = keyValue.getKey(); Bar barBack = keyValue.getValue(); assertEquals(foo, fooBack); assertEquals(bar, barBack); }
Example 4
Source File: TypedMessageBuilderImplTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testDefaultValue() { producerBase = mock(ProducerBase.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 = Schema.KeyValue(fooSchema, barSchema); TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema); SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo(); foo.setField1("field1"); foo.setField2("field2"); SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar(); bar.setField1(true); KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = new KeyValue<>(foo, bar); // Check kv.encoding.type default, not set value TypedMessageBuilderImpl<KeyValue> typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.value(keyValue); ByteBuffer content = typedMessageBuilder.getContent(); byte[] contentByte = new byte[content.remaining()]; content.get(contentByte); KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> decodeKeyValue = keyValueSchema.decode(contentByte); assertEquals(decodeKeyValue.getKey(), foo); assertEquals(decodeKeyValue.getValue(), bar); assertFalse(typedMessageBuilderImpl.hasKey()); }
Example 5
Source File: TypedMessageBuilderImplTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testInlineValue() { producerBase = mock(ProducerBase.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 = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE); TypedMessageBuilderImpl typedMessageBuilderImpl = new TypedMessageBuilderImpl(producerBase, keyValueSchema); SchemaTestUtils.Foo foo = new SchemaTestUtils.Foo(); foo.setField1("field1"); foo.setField2("field2"); SchemaTestUtils.Bar bar = new SchemaTestUtils.Bar(); bar.setField1(true); KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> keyValue = new KeyValue<>(foo, bar); // Check kv.encoding.type INLINE TypedMessageBuilderImpl<KeyValue> typedMessageBuilder = (TypedMessageBuilderImpl)typedMessageBuilderImpl.value(keyValue); ByteBuffer content = typedMessageBuilder.getContent(); byte[] contentByte = new byte[content.remaining()]; content.get(contentByte); KeyValue<SchemaTestUtils.Foo, SchemaTestUtils.Bar> decodeKeyValue = keyValueSchema.decode(contentByte); assertEquals(decodeKeyValue.getKey(), foo); assertEquals(decodeKeyValue.getValue(), bar); assertFalse(typedMessageBuilderImpl.hasKey()); }
Example 6
Source File: GenericSchemaImplTest.java From pulsar with Apache License 2.0 | 5 votes |
private void testEncodeAndDecodeKeyValues(Schema<KeyValue<Foo, Foo>> encodeSchema, Schema<KeyValue<GenericRecord, GenericRecord>> decodeSchema) { int numRecords = 10; for (int i = 0; i < numRecords; i++) { Foo foo = newFoo(i); byte[] data = encodeSchema.encode(new KeyValue<>(foo, foo)); KeyValue<GenericRecord, GenericRecord> kv = decodeSchema.decode(data, new byte[0]); verifyFooRecord(kv.getKey(), i); verifyFooRecord(kv.getValue(), i); } }
Example 7
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 8
Source File: SupportVersioningKeyValueSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testKeyValueDefaultVersioningEncodeDecode() { 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); 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 9
Source File: BytesSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
private void testBytesSchema(Schema<byte[]> schema) { byte[] data = "hello world".getBytes(UTF_8); byte[] serializedData = schema.encode(data); assertSame(data, serializedData); byte[] deserializedData = schema.decode(serializedData); assertSame(data, deserializedData); ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(deserializedData.length); byteBuf.writeBytes(deserializedData); assertEquals(data, ((BytesSchema)schema).decode(byteBuf)); }
Example 10
Source File: KeyValueSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testDefaultKeyValueEncodingTypeSchemaEncodeAndDecode() { AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build()); AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build()); Schema<KeyValue<Foo, Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema); Bar bar = new Bar(); bar.setField1(true); Foo foo = new Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(Color.RED); // Check kv.encoding.type default not set value byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); Assert.assertTrue(encodeBytes.length > 0); KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes); Foo fooBack = keyValue.getKey(); Bar barBack = keyValue.getValue(); assertEquals(foo, fooBack); assertEquals(bar, barBack); }
Example 11
Source File: KeyValueSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testInlineKeyValueEncodingTypeSchemaEncodeAndDecode() { AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build()); AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build()); Schema<KeyValue<Foo, Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.INLINE); Bar bar = new Bar(); bar.setField1(true); Foo foo = new Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(Color.RED); // Check kv.encoding.type INLINE byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); Assert.assertTrue(encodeBytes.length > 0); KeyValue<Foo, Bar> keyValue = (KeyValue<Foo, Bar>) keyValueSchema.decode(encodeBytes); Foo fooBack = keyValue.getKey(); Bar barBack = keyValue.getValue(); assertEquals(foo, fooBack); assertEquals(bar, barBack); }
Example 12
Source File: KeyValueSchemaTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode() { AvroSchema<Foo> fooSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build()); AvroSchema<Bar> barSchema = AvroSchema.of(SchemaDefinition.<Bar>builder().withPojo(Bar.class).build()); Schema<KeyValue<Foo, Bar>> keyValueSchema = Schema.KeyValue(fooSchema, barSchema, KeyValueEncodingType.SEPARATED); Bar bar = new Bar(); bar.setField1(true); Foo foo = new Foo(); foo.setField1("field1"); foo.setField2("field2"); foo.setField3(3); foo.setField4(bar); foo.setColor(Color.RED); // Check kv.encoding.type SEPARATED byte[] encodeBytes = keyValueSchema.encode(new KeyValue(foo, bar)); Assert.assertTrue(encodeBytes.length > 0); try { keyValueSchema.decode(encodeBytes); Assert.fail("This method cannot be used under this SEPARATED encoding type"); } catch (SchemaSerializationException e) { Assert.assertTrue(e.getMessage().contains("This method cannot be used under this SEPARATED encoding type")); } KeyValue<Foo, Bar> keyValue = ((KeyValueSchema)keyValueSchema).decode(fooSchema.encode(foo), encodeBytes, null); Foo fooBack = keyValue.getKey(); Bar barBack = keyValue.getValue(); assertEquals(foo, fooBack); assertEquals(bar, barBack); }
Example 13
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 14
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")); }