Search in sources :

Example 1 with GenericJsonWriter

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

the class TopicsBase method encodeWithSchema.

// Encode message with corresponding schema, do necessary conversion before encoding
private byte[] encodeWithSchema(String input, Schema schema) {
    try {
        switch(schema.getSchemaInfo().getType()) {
            case INT8:
                return schema.encode(Byte.parseByte(input));
            case INT16:
                return schema.encode(Short.parseShort(input));
            case INT32:
                return schema.encode(Integer.parseInt(input));
            case INT64:
                return schema.encode(Long.parseLong(input));
            case STRING:
                return schema.encode(input);
            case FLOAT:
                return schema.encode(Float.parseFloat(input));
            case DOUBLE:
                return schema.encode(Double.parseDouble(input));
            case BOOLEAN:
                return schema.encode(Boolean.parseBoolean(input));
            case BYTES:
                return schema.encode(input.getBytes());
            case DATE:
                return schema.encode(DateFormat.getDateInstance().parse(input));
            case TIME:
                return schema.encode(new Time(Long.parseLong(input)));
            case TIMESTAMP:
                return schema.encode(new Timestamp(Long.parseLong(input)));
            case INSTANT:
                return schema.encode(Instant.parse(input));
            case LOCAL_DATE:
                return schema.encode(LocalDate.parse(input));
            case LOCAL_TIME:
                return schema.encode(LocalTime.parse(input));
            case LOCAL_DATE_TIME:
                return schema.encode(LocalDateTime.parse(input));
            case JSON:
                GenericJsonWriter jsonWriter = new GenericJsonWriter();
                return jsonWriter.write(new GenericJsonRecord(null, null, ObjectMapperFactory.getThreadLocal().readTree(input), schema.getSchemaInfo()));
            case AVRO:
                AvroBaseStructSchema avroSchema = ((AvroBaseStructSchema) schema);
                Decoder decoder = DecoderFactory.get().jsonDecoder(avroSchema.getAvroSchema(), input);
                DatumReader<GenericData.Record> reader = new GenericDatumReader(avroSchema.getAvroSchema());
                GenericRecord genericRecord = reader.read(null, decoder);
                GenericAvroWriter avroWriter = new GenericAvroWriter(avroSchema.getAvroSchema());
                return avroWriter.write(new GenericAvroRecord(null, avroSchema.getAvroSchema(), null, genericRecord));
            case PROTOBUF_NATIVE:
            case KEY_VALUE:
            default:
                throw new PulsarClientException.InvalidMessageException("");
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Fail to encode value {} with schema {} for rest produce request", input, new String(schema.getSchemaInfo().getSchema()));
        }
        return new byte[0];
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Time(java.sql.Time) LocalTime(java.time.LocalTime) LocalDateTime(java.time.LocalDateTime) AvroBaseStructSchema(org.apache.pulsar.client.impl.schema.AvroBaseStructSchema) Decoder(org.apache.avro.io.Decoder) GenericAvroWriter(org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter) Timestamp(java.sql.Timestamp) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericRecord(org.apache.avro.generic.GenericRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericJsonWriter(org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with GenericJsonWriter

use of org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter in project pulsar by yahoo.

the class TopicsBase method encodeWithSchema.

// Encode message with corresponding schema, do necessary conversion before encoding
private byte[] encodeWithSchema(String input, Schema schema) {
    try {
        switch(schema.getSchemaInfo().getType()) {
            case INT8:
                return schema.encode(Byte.parseByte(input));
            case INT16:
                return schema.encode(Short.parseShort(input));
            case INT32:
                return schema.encode(Integer.parseInt(input));
            case INT64:
                return schema.encode(Long.parseLong(input));
            case STRING:
                return schema.encode(input);
            case FLOAT:
                return schema.encode(Float.parseFloat(input));
            case DOUBLE:
                return schema.encode(Double.parseDouble(input));
            case BOOLEAN:
                return schema.encode(Boolean.parseBoolean(input));
            case BYTES:
                return schema.encode(input.getBytes());
            case DATE:
                return schema.encode(DateFormat.getDateInstance().parse(input));
            case TIME:
                return schema.encode(new Time(Long.parseLong(input)));
            case TIMESTAMP:
                return schema.encode(new Timestamp(Long.parseLong(input)));
            case INSTANT:
                return schema.encode(Instant.parse(input));
            case LOCAL_DATE:
                return schema.encode(LocalDate.parse(input));
            case LOCAL_TIME:
                return schema.encode(LocalTime.parse(input));
            case LOCAL_DATE_TIME:
                return schema.encode(LocalDateTime.parse(input));
            case JSON:
                GenericJsonWriter jsonWriter = new GenericJsonWriter();
                return jsonWriter.write(new GenericJsonRecord(null, null, ObjectMapperFactory.getThreadLocal().readTree(input), schema.getSchemaInfo()));
            case AVRO:
                AvroBaseStructSchema avroSchema = ((AvroBaseStructSchema) schema);
                Decoder decoder = DecoderFactory.get().jsonDecoder(avroSchema.getAvroSchema(), input);
                DatumReader<GenericData.Record> reader = new GenericDatumReader(avroSchema.getAvroSchema());
                GenericRecord genericRecord = reader.read(null, decoder);
                GenericAvroWriter avroWriter = new GenericAvroWriter(avroSchema.getAvroSchema());
                return avroWriter.write(new GenericAvroRecord(null, avroSchema.getAvroSchema(), null, genericRecord));
            case PROTOBUF_NATIVE:
            case KEY_VALUE:
            default:
                throw new PulsarClientException.InvalidMessageException("");
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Fail to encode value {} with schema {} for rest produce request", input, new String(schema.getSchemaInfo().getSchema()));
        }
        return new byte[0];
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Time(java.sql.Time) LocalTime(java.time.LocalTime) LocalDateTime(java.time.LocalDateTime) AvroBaseStructSchema(org.apache.pulsar.client.impl.schema.AvroBaseStructSchema) Decoder(org.apache.avro.io.Decoder) GenericAvroWriter(org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter) Timestamp(java.sql.Timestamp) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericRecord(org.apache.avro.generic.GenericRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericJsonWriter(org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 3 with GenericJsonWriter

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

the class TopicsBase method encodeWithSchema.

// Encode message with corresponding schema, do necessary conversion before encoding
private byte[] encodeWithSchema(String input, Schema schema) {
    try {
        switch(schema.getSchemaInfo().getType()) {
            case INT8:
                return schema.encode(Byte.parseByte(input));
            case INT16:
                return schema.encode(Short.parseShort(input));
            case INT32:
                return schema.encode(Integer.parseInt(input));
            case INT64:
                return schema.encode(Long.parseLong(input));
            case STRING:
                return schema.encode(input);
            case FLOAT:
                return schema.encode(Float.parseFloat(input));
            case DOUBLE:
                return schema.encode(Double.parseDouble(input));
            case BOOLEAN:
                return schema.encode(Boolean.parseBoolean(input));
            case BYTES:
                return schema.encode(input.getBytes());
            case DATE:
                return schema.encode(DateFormat.getDateInstance().parse(input));
            case TIME:
                return schema.encode(new Time(Long.parseLong(input)));
            case TIMESTAMP:
                return schema.encode(new Timestamp(Long.parseLong(input)));
            case INSTANT:
                return schema.encode(Instant.parse(input));
            case LOCAL_DATE:
                return schema.encode(LocalDate.parse(input));
            case LOCAL_TIME:
                return schema.encode(LocalTime.parse(input));
            case LOCAL_DATE_TIME:
                return schema.encode(LocalDateTime.parse(input));
            case JSON:
                GenericJsonWriter jsonWriter = new GenericJsonWriter();
                return jsonWriter.write(new GenericJsonRecord(null, null, ObjectMapperFactory.getThreadLocal().readTree(input), schema.getSchemaInfo()));
            case AVRO:
                AvroBaseStructSchema avroSchema = ((AvroBaseStructSchema) schema);
                Decoder decoder = DecoderFactory.get().jsonDecoder(avroSchema.getAvroSchema(), input);
                DatumReader<GenericData.Record> reader = new GenericDatumReader(avroSchema.getAvroSchema());
                GenericRecord genericRecord = reader.read(null, decoder);
                GenericAvroWriter avroWriter = new GenericAvroWriter(avroSchema.getAvroSchema());
                return avroWriter.write(new GenericAvroRecord(null, avroSchema.getAvroSchema(), null, genericRecord));
            case PROTOBUF_NATIVE:
            case KEY_VALUE:
            default:
                throw new PulsarClientException.InvalidMessageException("");
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Fail to encode value {} with schema {} for rest produce request", input, new String(schema.getSchemaInfo().getSchema()));
        }
        return new byte[0];
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Time(java.sql.Time) LocalTime(java.time.LocalTime) LocalDateTime(java.time.LocalDateTime) AvroBaseStructSchema(org.apache.pulsar.client.impl.schema.AvroBaseStructSchema) Decoder(org.apache.avro.io.Decoder) GenericAvroWriter(org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter) Timestamp(java.sql.Timestamp) URISyntaxException(java.net.URISyntaxException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericRecord(org.apache.avro.generic.GenericRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) GenericJsonWriter(org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

IOException (java.io.IOException)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 LocalDateTime (java.time.LocalDateTime)3 LocalTime (java.time.LocalTime)3 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)3 GenericRecord (org.apache.avro.generic.GenericRecord)3 Decoder (org.apache.avro.io.Decoder)3 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)3 BrokerServiceException (org.apache.pulsar.broker.service.BrokerServiceException)3 SchemaException (org.apache.pulsar.broker.service.schema.exceptions.SchemaException)3 RestException (org.apache.pulsar.broker.web.RestException)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 AvroBaseStructSchema (org.apache.pulsar.client.impl.schema.AvroBaseStructSchema)3 GenericAvroRecord (org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord)3 GenericAvroWriter (org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter)3 GenericJsonRecord (org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord)3 GenericJsonWriter (org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter)3 URISyntaxException (java.net.URISyntaxException)1