Search in sources :

Example 1 with SchemaInfoImpl

use of org.apache.pulsar.client.impl.schema.SchemaInfoImpl in project pulsar by apache.

the class TopicsBase method getSchemaData.

// Build schemaData from passed in schema string.
private SchemaData getSchemaData(String keySchema, String valueSchema) {
    try {
        SchemaInfoImpl valueSchemaInfo = (valueSchema == null || valueSchema.isEmpty()) ? (SchemaInfoImpl) StringSchema.utf8().getSchemaInfo() : ObjectMapperFactory.getThreadLocal().readValue(valueSchema, SchemaInfoImpl.class);
        if (null == valueSchemaInfo.getName()) {
            valueSchemaInfo.setName(valueSchemaInfo.getType().toString());
        }
        // Value schema only
        if (keySchema == null || keySchema.isEmpty()) {
            return SchemaData.builder().data(valueSchemaInfo.getSchema()).isDeleted(false).user("Rest Producer").timestamp(System.currentTimeMillis()).type(valueSchemaInfo.getType()).props(valueSchemaInfo.getProperties()).build();
        } else {
            // Key_Value schema
            SchemaInfoImpl keySchemaInfo = ObjectMapperFactory.getThreadLocal().readValue(keySchema, SchemaInfoImpl.class);
            if (null == keySchemaInfo.getName()) {
                keySchemaInfo.setName(keySchemaInfo.getType().toString());
            }
            SchemaInfo schemaInfo = KeyValueSchemaInfo.encodeKeyValueSchemaInfo("KVSchema-" + topicName.getPartitionedTopicName(), keySchemaInfo, valueSchemaInfo, KeyValueEncodingType.SEPARATED);
            return SchemaData.builder().data(schemaInfo.getSchema()).isDeleted(false).user("Rest Producer").timestamp(System.currentTimeMillis()).type(schemaInfo.getType()).props(schemaInfo.getProperties()).build();
        }
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug("Fail to parse schema info for rest produce request with key schema {} and value schema {}", keySchema, valueSchema);
        }
        return null;
    }
}
Also used : SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) IOException(java.io.IOException) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) KeyValueSchemaInfo(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo)

Example 2 with SchemaInfoImpl

use of org.apache.pulsar.client.impl.schema.SchemaInfoImpl in project pulsar-flink by streamnative.

the class FlinkPulsarTableITest method sendProtobufMessage.

private void sendProtobufMessage(String topic, int count) throws PulsarClientException {
    Map<String, String> properties = new HashMap<>();
    properties.put("__PARSING_INFO__", "[{\"number\":1,\"name\":\"a\",\"type\":\"INT32\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":2,\"name\":\"b\",\"type\":\"INT64\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":3,\"name\":\"c\",\"type\":\"BOOL\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":4,\"name\":\"d\",\"type\":\"FLOAT\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":5,\"name\":\"e\",\"type\":\"DOUBLE\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":6,\"name\":\"f\",\"type\":\"STRING\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":7,\"name\":\"g\",\"type\":\"BYTES\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":8,\"name\":\"h\",\"type\":\"ENUM\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null},{\"number\":9,\"name\":\"f_abc_7d\",\"type\":\"INT32\",\"label\":\"LABEL_OPTIONAL\",\"definition\":null}]");
    properties.put("__alwaysAllowNull", "true");
    properties.put("__jsr310ConversionEnabled", "false");
    final SchemaInfoImpl schemaInfo = SchemaInfoImpl.builder().schema(ProtobufNativeSchemaUtils.serialize(SchemaData.descriptor)).name("simpleTest1").type(SchemaType.PROTOBUF_NATIVE).properties(properties).build();
    final Schema<?> schema = Schema.getSchema(schemaInfo);
    try (final Producer<byte[]> producer = getPulsarClient().newProducer(Schema.AUTO_PRODUCE_BYTES(schema)).topic(topic).create()) {
        for (int i = 0; i < count; i++) {
            producer.send(SchemaData.protobufData);
        }
    }
}
Also used : SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString)

Example 3 with SchemaInfoImpl

use of org.apache.pulsar.client.impl.schema.SchemaInfoImpl in project pulsar-flink by streamnative.

the class SimpleSchemaTranslator method avroSchema2PulsarSchema.

static GenericSchema<GenericRecord> avroSchema2PulsarSchema(Schema avroSchema) {
    byte[] schemaBytes = avroSchema.toString().getBytes(StandardCharsets.UTF_8);
    SchemaInfoImpl si = new SchemaInfoImpl();
    si.setName("Avro");
    si.setSchema(schemaBytes);
    si.setType(SchemaType.AVRO);
    return org.apache.pulsar.client.api.Schema.generic(si);
}
Also used : SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl)

Example 4 with SchemaInfoImpl

use of org.apache.pulsar.client.impl.schema.SchemaInfoImpl in project incubator-pulsar by apache.

the class TopicsBase method getSchemaData.

// Build schemaData from passed in schema string.
private SchemaData getSchemaData(String keySchema, String valueSchema) {
    try {
        SchemaInfoImpl valueSchemaInfo = (valueSchema == null || valueSchema.isEmpty()) ? (SchemaInfoImpl) StringSchema.utf8().getSchemaInfo() : ObjectMapperFactory.getThreadLocal().readValue(valueSchema, SchemaInfoImpl.class);
        if (null == valueSchemaInfo.getName()) {
            valueSchemaInfo.setName(valueSchemaInfo.getType().toString());
        }
        // Value schema only
        if (keySchema == null || keySchema.isEmpty()) {
            return SchemaData.builder().data(valueSchemaInfo.getSchema()).isDeleted(false).user("Rest Producer").timestamp(System.currentTimeMillis()).type(valueSchemaInfo.getType()).props(valueSchemaInfo.getProperties()).build();
        } else {
            // Key_Value schema
            SchemaInfoImpl keySchemaInfo = ObjectMapperFactory.getThreadLocal().readValue(keySchema, SchemaInfoImpl.class);
            if (null == keySchemaInfo.getName()) {
                keySchemaInfo.setName(keySchemaInfo.getType().toString());
            }
            SchemaInfo schemaInfo = KeyValueSchemaInfo.encodeKeyValueSchemaInfo("KVSchema-" + topicName.getPartitionedTopicName(), keySchemaInfo, valueSchemaInfo, KeyValueEncodingType.SEPARATED);
            return SchemaData.builder().data(schemaInfo.getSchema()).isDeleted(false).user("Rest Producer").timestamp(System.currentTimeMillis()).type(schemaInfo.getType()).props(schemaInfo.getProperties()).build();
        }
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug("Fail to parse schema info for rest produce request with key schema {} and value schema {}", keySchema, valueSchema);
        }
        return null;
    }
}
Also used : SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) IOException(java.io.IOException) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) KeyValueSchemaInfo(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo)

Example 5 with SchemaInfoImpl

use of org.apache.pulsar.client.impl.schema.SchemaInfoImpl in project flink by splunk.

the class PulsarSchema method readObject.

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    // Name
    String name = ois.readUTF();
    // Schema
    int byteLen = ois.readInt();
    byte[] schemaBytes = new byte[byteLen];
    int read = ois.read(schemaBytes);
    checkState(read == byteLen);
    // Type
    int typeIdx = ois.readInt();
    SchemaType type = SchemaType.valueOf(typeIdx);
    // Properties
    int propSize = ois.readInt();
    Map<String, String> properties = new HashMap<>(propSize);
    for (int i = 0; i < propSize; i++) {
        properties.put(ois.readUTF(), ois.readUTF());
    }
    this.schemaInfo = new SchemaInfoImpl(name, schemaBytes, type, properties);
    this.schema = createSchema(schemaInfo);
}
Also used : SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) HashMap(java.util.HashMap) SchemaType(org.apache.pulsar.common.schema.SchemaType)

Aggregations

SchemaInfoImpl (org.apache.pulsar.client.impl.schema.SchemaInfoImpl)11 HashMap (java.util.HashMap)4 IOException (java.io.IOException)3 KeyValueSchemaInfo (org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo)3 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)3 SchemaType (org.apache.pulsar.common.schema.SchemaType)3 ByteString (com.google.protobuf.ByteString)1 FlinkSchema (org.apache.flink.streaming.connectors.pulsar.serialization.FlinkSchema)1 DataType (org.apache.flink.table.types.DataType)1 FieldsDataType (org.apache.flink.table.types.FieldsDataType)1 RowType (org.apache.flink.table.types.logical.RowType)1 Schema (org.apache.pulsar.shade.org.apache.avro.Schema)1