Search in sources :

Example 16 with SchemaSerializationException

use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.

the class AvroWriter method write.

@Override
public synchronized byte[] write(T message) {
    byte[] outputBytes = null;
    try {
        writer.write(message, this.encoder);
    } catch (Exception e) {
        throw new SchemaSerializationException(e);
    } finally {
        try {
            this.encoder.flush();
            outputBytes = this.byteArrayOutputStream.toByteArray();
        } catch (Exception ex) {
            throw new SchemaSerializationException(ex);
        }
        this.byteArrayOutputStream.reset();
    }
    return outputBytes;
}
Also used : SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException)

Example 17 with SchemaSerializationException

use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.

the class GenericAvroWriter method write.

@Override
public synchronized byte[] write(GenericRecord message) {
    try {
        writer.write(((GenericAvroRecord) message).getAvroRecord(), this.encoder);
        this.encoder.flush();
        return this.byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        throw new SchemaSerializationException(e);
    } finally {
        this.byteArrayOutputStream.reset();
    }
}
Also used : SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException)

Example 18 with SchemaSerializationException

use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.

the class JSONSchemaTest method testNotAllowNullEncodeAndDecode.

@Test
public void testNotAllowNullEncodeAndDecode() {
    JSONSchema<Foo> jsonSchema = JSONSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    Foo foo1 = new Foo();
    foo1.setField1("foo1");
    foo1.setField2("bar1");
    foo1.setField4(new Bar());
    foo1.setFieldUnableNull("notNull");
    Foo foo2 = new Foo();
    foo2.setField1("foo2");
    foo2.setField2("bar2");
    byte[] bytes1 = jsonSchema.encode(foo1);
    Foo object1 = jsonSchema.decode(bytes1);
    Assert.assertTrue(bytes1.length > 0);
    assertEquals(object1, foo1);
    try {
        jsonSchema.encode(foo2);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SchemaSerializationException);
    }
}
Also used : NestedBar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.NestedBar) Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) DerivedFoo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.DerivedFoo) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) JSONException(org.json.JSONException) SchemaValidationException(org.apache.avro.SchemaValidationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.testng.annotations.Test)

Example 19 with SchemaSerializationException

use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.

the class KeyValueSchemaTest method testSeparatedKeyValueEncodingTypeSchemaEncodeAndDecode.

@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 = ((KeyValueSchemaImpl) keyValueSchema).decode(fooSchema.encode(foo), encodeBytes, null);
    Foo fooBack = keyValue.getKey();
    Bar barBack = keyValue.getValue();
    assertEquals(foo, fooBack);
    assertEquals(bar, barBack);
}
Also used : Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) KeyValue(org.apache.pulsar.common.schema.KeyValue) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) Test(org.testng.annotations.Test)

Example 20 with SchemaSerializationException

use of org.apache.pulsar.client.api.SchemaSerializationException in project pulsar by yahoo.

the class AvroSchemaTest method testNotAllowNullEncodeAndDecode.

@Test
public void testNotAllowNullEncodeAndDecode() {
    AvroSchema<Foo> avroSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
    Foo foo1 = new Foo();
    foo1.setField1("foo1");
    foo1.setField2("bar1");
    foo1.setField4(new Bar());
    foo1.setFieldUnableNull("notNull");
    Foo foo2 = new Foo();
    foo2.setField1("foo2");
    foo2.setField2("bar2");
    byte[] bytes1 = avroSchema.encode(foo1);
    Foo object1 = avroSchema.decode(bytes1);
    Assert.assertTrue(bytes1.length > 0);
    assertEquals(object1, foo1);
    try {
        avroSchema.encode(foo2);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SchemaSerializationException);
    }
}
Also used : Bar(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar) Foo(org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) JSONException(org.json.JSONException) SchemaValidationException(org.apache.avro.SchemaValidationException) Test(org.testng.annotations.Test)

Aggregations

SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)66 IOException (java.io.IOException)21 Test (org.testng.annotations.Test)15 List (java.util.List)12 GenericRecord (org.apache.pulsar.client.api.schema.GenericRecord)12 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)12 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 FileDescriptorProto (com.google.protobuf.DescriptorProtos.FileDescriptorProto)9 Arrays (java.util.Arrays)9 HashMap (java.util.HashMap)9 Map (java.util.Map)9 TimeUnit (java.util.concurrent.TimeUnit)9 SchemaValidationException (org.apache.avro.SchemaValidationException)9 Bar (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Bar)9 Foo (org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo)9 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)9 ProtobufNativeSchemaData (org.apache.pulsar.common.protocol.schema.ProtobufNativeSchemaData)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)6