Java Code Examples for org.apache.pulsar.client.impl.TypedMessageBuilderImpl#property()

The following examples show how to use org.apache.pulsar.client.impl.TypedMessageBuilderImpl#property() . 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: MessageRecordUtils.java    From kop with Apache License 2.0 5 votes vote down vote up
public static MessageImpl<byte[]> recordToEntry(Record record) {
    @SuppressWarnings("unchecked")
    TypedMessageBuilderImpl<byte[]> builder = new TypedMessageBuilderImpl(null, Schema.BYTES);

    // key
    if (record.hasKey()) {
        byte[] key = new byte[record.keySize()];
        record.key().get(key);
        builder.keyBytes(key);
        // reuse ordering key to avoid converting string < > bytes
        builder.orderingKey(key);
    }

    // value
    if (record.hasValue()) {
        byte[] value = new byte[record.valueSize()];
        record.value().get(value);
        builder.value(value);
    } else {
        builder.value(new byte[0]);
    }

    // sequence
    if (record.sequence() >= 0) {
        builder.sequenceId(record.sequence());
    }

    // timestamp
    if (record.timestamp() >= 0) {
        builder.eventTime(record.timestamp());
    }

    // header
    for (Header h : record.headers()) {
        builder.property(h.key(),
            new String(h.value(), UTF_8));
    }

    return (MessageImpl<byte[]>) builder.getMessage();
}
 
Example 2
Source File: KafkaProducerInterceptorWrapper.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a Kafka {@link ProducerRecord} to a Pulsar {@link Message}.
 *
 * @param producerRecord Kafka record to be convert.
 * @return Pulsar message.
 */
private Message<byte[]> toPulsarMessage(ProducerRecord<K, V> producerRecord) {
    TypedMessageBuilderImpl typedMessageBuilder = new TypedMessageBuilderImpl(null, scheme);
    typedMessageBuilder.key(serializeKey(topic, producerRecord.key()));
    if (valueSchema instanceof PulsarKafkaSchema) {
        ((PulsarKafkaSchema<V>) valueSchema).setTopic(topic);
    }
    typedMessageBuilder.value(valueSchema.encode(producerRecord.value()));
    typedMessageBuilder.eventTime(eventTime);
    typedMessageBuilder.property(KafkaMessageRouter.PARTITION_ID, partitionID);
    return typedMessageBuilder.getMessage();
}