Java Code Examples for org.apache.pulsar.client.api.Schema#getSchemaInfo()

The following examples show how to use org.apache.pulsar.client.api.Schema#getSchemaInfo() . 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: AdminApiSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private <T> void testSchemaInfoApi(Schema<T> schema,
                                   String topicName) throws Exception {
    SchemaInfo si = schema.getSchemaInfo();
    admin.schemas().createSchema(topicName, si);
    log.info("Upload schema to topic {} : {}", topicName, si);

    SchemaInfo readSi = admin.schemas().getSchemaInfo(topicName);
    log.info("Read schema of topic {} : {}", topicName, readSi);

    assertEquals(si, readSi);

    readSi = admin.schemas().getSchemaInfo(topicName + "-partition-0");
    log.info("Read schema of topic {} : {}", topicName, readSi);

    assertEquals(si, readSi);

}
 
Example 2
Source File: AdminApiSchemaTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private <T> void testSchemaInfoWithVersionApi(Schema<T> schema,
                                   String topicName) throws Exception {
    SchemaInfo si = schema.getSchemaInfo();
    admin.schemas().createSchema(topicName, si);
    log.info("Upload schema to topic {} : {}", topicName, si);

    SchemaInfoWithVersion readSi = admin.schemas().getSchemaInfoWithVersion(topicName);
    log.info("Read schema of topic {} : {}", topicName, readSi);

    assertEquals(si, readSi.getSchemaInfo());
    assertEquals(0, readSi.getVersion());

    readSi = admin.schemas().getSchemaInfoWithVersion(topicName + "-partition-0");
    log.info("Read schema of topic {} : {}", topicName, readSi);

    assertEquals(si, readSi.getSchemaInfo());
    assertEquals(0, readSi.getVersion());

}
 
Example 3
Source File: KeyValueSchemaInfoTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "encodingTypes")
public void encodeDecodeKeyValueSchemaInfo(KeyValueEncodingType encodingType) {
    Schema<KeyValue<Foo, Bar>> kvSchema = Schema.KeyValue(
        FOO_SCHEMA,
        BAR_SCHEMA,
        encodingType
    );
    SchemaInfo kvSchemaInfo = kvSchema.getSchemaInfo();
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(kvSchemaInfo),
        encodingType);

    SchemaInfo encodedSchemaInfo =
        DefaultImplementation.encodeKeyValueSchemaInfo(FOO_SCHEMA, BAR_SCHEMA, encodingType);
    assertEquals(encodedSchemaInfo, kvSchemaInfo);
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(encodedSchemaInfo),
        encodingType);

    KeyValue<SchemaInfo, SchemaInfo> schemaInfoKeyValue =
        DefaultImplementation.decodeKeyValueSchemaInfo(kvSchemaInfo);

    assertEquals(schemaInfoKeyValue.getKey(), FOO_SCHEMA.getSchemaInfo());
    assertEquals(schemaInfoKeyValue.getValue(), BAR_SCHEMA.getSchemaInfo());
}
 
Example 4
Source File: AutoProduceBytesSchema.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public AutoProduceBytesSchema(Schema<T> schema) {
    this.schema = schema;
    SchemaInfo schemaInfo = schema.getSchemaInfo();
    this.requireSchemaValidation = schemaInfo != null
                                   && schemaInfo.getType() != SchemaType.BYTES
                                   && schemaInfo.getType() != SchemaType.NONE;
}
 
Example 5
Source File: KeyValueSchemaInfoTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "encodingTypes")
public void encodeDecodeNestedKeyValueSchemaInfo(KeyValueEncodingType encodingType) {
    Schema<KeyValue<String, Bar>> nestedSchema =
        Schema.KeyValue(Schema.STRING, BAR_SCHEMA, KeyValueEncodingType.INLINE);
    Schema<KeyValue<Foo, KeyValue<String, Bar>>> kvSchema = Schema.KeyValue(
        FOO_SCHEMA,
        nestedSchema,
        encodingType
    );
    SchemaInfo kvSchemaInfo = kvSchema.getSchemaInfo();
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(kvSchemaInfo),
        encodingType);

    SchemaInfo encodedSchemaInfo =
        DefaultImplementation.encodeKeyValueSchemaInfo(
            FOO_SCHEMA,
            nestedSchema,
            encodingType);
    assertEquals(encodedSchemaInfo, kvSchemaInfo);
    assertEquals(
        DefaultImplementation.decodeKeyValueEncodingType(encodedSchemaInfo),
        encodingType);

    KeyValue<SchemaInfo, SchemaInfo> schemaInfoKeyValue =
        DefaultImplementation.decodeKeyValueSchemaInfo(kvSchemaInfo);

    assertEquals(schemaInfoKeyValue.getKey(), FOO_SCHEMA.getSchemaInfo());
    assertEquals(schemaInfoKeyValue.getValue().getType(), SchemaType.KEY_VALUE);
    KeyValue<SchemaInfo, SchemaInfo> nestedSchemaInfoKeyValue =
        DefaultImplementation.decodeKeyValueSchemaInfo(schemaInfoKeyValue.getValue());

    assertEquals(nestedSchemaInfoKeyValue.getKey(), Schema.STRING.getSchemaInfo());
    assertEquals(nestedSchemaInfoKeyValue.getValue(), BAR_SCHEMA.getSchemaInfo());
}
 
Example 6
Source File: SchemaBuilderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: SchemaBuilderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: AutoProduceBytesSchema.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public void setSchema(Schema<T> schema) {
    this.schema = schema;
    this.requireSchemaValidation = schema.getSchemaInfo() != null
        && SchemaType.BYTES != schema.getSchemaInfo().getType()
        && SchemaType.NONE != schema.getSchemaInfo().getType();
}