Search in sources :

Example 1 with GenericRecord

use of org.apache.pulsar.client.api.schema.GenericRecord in project pulsar by apache.

the class GenericAvroSchemaTest method testDecodeWithMultiVersioningSupport.

@Test
public void testDecodeWithMultiVersioningSupport() {
    MultiVersionSchemaInfoProvider provider = mock(MultiVersionSchemaInfoProvider.class);
    readerSchema.setSchemaInfoProvider(provider);
    when(provider.getSchemaByVersion(any(byte[].class))).thenReturn(CompletableFuture.completedFuture(writerSchema.getSchemaInfo()));
    GenericRecord dataForWriter = writerSchema.newRecordBuilder().set("field1", SchemaTestUtils.TEST_MULTI_VERSION_SCHEMA_STRING).set("field3", 0).build();
    GenericRecord record = readerSchema.decode(writerSchema.encode(dataForWriter), new byte[10]);
    Assert.assertEquals(SchemaTestUtils.TEST_MULTI_VERSION_SCHEMA_STRING, record.getField("field1"));
    Assert.assertEquals(0, record.getField("field3"));
    Assert.assertEquals(SchemaTestUtils.TEST_MULTI_VERSION_SCHEMA_DEFAULT_STRING, record.getField("fieldUnableNull"));
}
Also used : GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Test(org.testng.annotations.Test)

Example 2 with GenericRecord

use of org.apache.pulsar.client.api.schema.GenericRecord in project pulsar by apache.

the class GenericProtobufNativeReaderTest method testGetNativeRecord.

@Test
public void testGetNativeRecord() {
    message = TestMessage.newBuilder().setStringField(STRING_FIELD_VLUE).setDoubleField(DOUBLE_FIELD_VLUE).build();
    GenericProtobufNativeReader genericProtobufNativeReader = new GenericProtobufNativeReader(genericProtobufNativeSchema.getProtobufNativeSchema());
    GenericRecord record = genericProtobufNativeReader.read(message.toByteArray());
    assertEquals(record.getField("stringField"), STRING_FIELD_VLUE);
    assertEquals(record.getField("doubleField"), DOUBLE_FIELD_VLUE);
    assertEquals(SchemaType.PROTOBUF_NATIVE, record.getSchemaType());
    DynamicMessage nativeRecord = (DynamicMessage) record.getNativeObject();
    assertEquals(nativeRecord.getField(nativeRecord.getDescriptorForType().findFieldByName("stringField")), STRING_FIELD_VLUE);
    assertEquals(nativeRecord.getField(nativeRecord.getDescriptorForType().findFieldByName("doubleField")), DOUBLE_FIELD_VLUE);
}
Also used : DynamicMessage(com.google.protobuf.DynamicMessage) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Test(org.testng.annotations.Test)

Example 3 with GenericRecord

use of org.apache.pulsar.client.api.schema.GenericRecord in project pulsar by apache.

the class GenericProtobufNativeSchemaTest method testGenericReaderByClazzBasedWriterSchema.

@Test
public void testGenericReaderByClazzBasedWriterSchema() {
    message = TestMessage.newBuilder().setStringField(STRING_FIELD_VLUE).setDoubleField(DOUBLE_FIELD_VLUE).build();
    byte[] clazzBasedProtobufBytes = clazzBasedProtobufNativeSchema.encode(message);
    GenericRecord genericRecord = genericProtobufNativeSchema.decode(clazzBasedProtobufBytes);
    assertEquals(genericRecord.getField("stringField"), STRING_FIELD_VLUE);
    assertEquals(genericRecord.getField("doubleField"), DOUBLE_FIELD_VLUE);
}
Also used : GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) Test(org.testng.annotations.Test)

Example 4 with GenericRecord

use of org.apache.pulsar.client.api.schema.GenericRecord 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 5 with GenericRecord

use of org.apache.pulsar.client.api.schema.GenericRecord in project pulsar by apache.

the class JSONSchemaTest method testJsonGenericRecordBuilder.

@Test
public void testJsonGenericRecordBuilder() {
    JSONSchema<Seller> sellerJsonSchema = JSONSchema.of(Seller.class);
    RecordSchemaBuilder sellerSchemaBuilder = SchemaBuilder.record("seller");
    sellerSchemaBuilder.field("state").type(SchemaType.STRING);
    sellerSchemaBuilder.field("street").type(SchemaType.STRING);
    sellerSchemaBuilder.field("zipCode").type(SchemaType.INT64);
    SchemaInfo sellerSchemaInfo = sellerSchemaBuilder.build(SchemaType.JSON);
    GenericSchemaImpl sellerGenericSchema = GenericSchemaImpl.of(sellerSchemaInfo);
    JSONSchema<PC> pcJsonSchema = JSONSchema.of(PC.class);
    RecordSchemaBuilder pcSchemaBuilder = SchemaBuilder.record("pc");
    pcSchemaBuilder.field("brand").type(SchemaType.STRING);
    pcSchemaBuilder.field("model").type(SchemaType.STRING);
    pcSchemaBuilder.field("gpu").type(SchemaType.STRING);
    pcSchemaBuilder.field("year").type(SchemaType.INT64);
    pcSchemaBuilder.field("seller", sellerGenericSchema).type(SchemaType.JSON).optional();
    SchemaInfo pcGenericSchemaInfo = pcSchemaBuilder.build(SchemaType.JSON);
    GenericSchemaImpl pcGenericSchema = GenericSchemaImpl.of(pcGenericSchemaInfo);
    Seller seller = new Seller("USA", "oakstreet", 9999);
    PC pc = new PC("dell", "g3", 2020, GPU.AMD, seller);
    byte[] bytes = pcJsonSchema.encode(pc);
    Assert.assertTrue(bytes.length > 0);
    Object pc2 = pcJsonSchema.decode(bytes);
    assertEquals(pc, pc2);
    GenericRecord sellerRecord = sellerGenericSchema.newRecordBuilder().set("state", "USA").set("street", "oakstreet").set("zipCode", 9999).build();
    GenericRecord pcRecord = pcGenericSchema.newRecordBuilder().set("brand", "dell").set("model", "g3").set("year", 2020).set("gpu", GPU.AMD).set("seller", sellerRecord).build();
    byte[] bytes3 = pcGenericSchema.encode(pcRecord);
    Assert.assertTrue(bytes3.length > 0);
    GenericRecord pc3Record = pcGenericSchema.decode(bytes3);
    for (Field field : pc3Record.getFields()) {
        assertTrue(pcGenericSchema.getFields().contains(field));
    }
    assertEquals("dell", pc3Record.getField("brand"));
    assertEquals("g3", pc3Record.getField("model"));
    assertEquals(2020, pc3Record.getField("year"));
    assertEquals(GPU.AMD.toString(), pc3Record.getField("gpu"));
    GenericRecord seller3Record = (GenericRecord) pc3Record.getField("seller");
    assertEquals("USA", seller3Record.getField("state"));
    assertEquals("oakstreet", seller3Record.getField("street"));
    assertEquals(9999, seller3Record.getField("zipCode"));
    assertTrue(pc3Record instanceof GenericJsonRecord);
    Assertions.assertThatCode(() -> pc3Record.getField("I_DO_NOT_EXIST")).doesNotThrowAnyException();
}
Also used : GenericSchemaImpl(org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl) Field(org.apache.pulsar.client.api.schema.Field) RecordSchemaBuilder(org.apache.pulsar.client.api.schema.RecordSchemaBuilder) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericRecord(org.apache.pulsar.client.api.schema.GenericRecord) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) Test(org.testng.annotations.Test)

Aggregations

GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)136 Test (org.testng.annotations.Test)43 PulsarClient (org.apache.pulsar.client.api.PulsarClient)25 Test (org.junit.jupiter.api.Test)25 Message (org.apache.pulsar.client.api.Message)18 KeyValue (org.apache.pulsar.common.schema.KeyValue)18 CqlSession (com.datastax.oss.driver.api.core.CqlSession)16 Schema (org.apache.pulsar.client.api.Schema)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Field (org.apache.pulsar.client.api.schema.Field)15 Map (java.util.Map)14 GenericObject (org.apache.pulsar.client.api.schema.GenericObject)14 RecordSchemaBuilder (org.apache.pulsar.client.api.schema.RecordSchemaBuilder)14 Record (org.apache.pulsar.functions.api.Record)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 SchemaType (org.apache.pulsar.common.schema.SchemaType)13 List (java.util.List)12 ChaosNetworkContainer (com.datastax.testcontainers.ChaosNetworkContainer)10