Java Code Examples for org.apache.pulsar.client.api.TypedMessageBuilder#key()

The following examples show how to use org.apache.pulsar.client.api.TypedMessageBuilder#key() . 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: PulsarSinkBuilder.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
private void add(E item) {
    TypedMessageBuilder<M> messageBuilder = producer.newMessage()
                                                    .value(extractValueFn.apply(item));
    if (extractKeyFn != null) {
        messageBuilder = messageBuilder.key(extractKeyFn.apply(item));
    }
    if (extractPropertiesFn != null) {
        messageBuilder = messageBuilder.properties(extractPropertiesFn.apply(item));
    }
    if (extractTimestampFn != null) {
        messageBuilder.eventTime(extractTimestampFn.apply(item));
    }
    messageBuilder.sendAsync()
                  .thenApply(CompletableFuture::completedFuture)
                  .exceptionally(t -> {
                      ExceptionUtil.sneakyThrow(t);
                      return null;
                  });
}
 
Example 2
Source File: FlinkPulsarProducer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(T value, Context context) throws Exception {
    checkErroneous();

    byte[] serializedValue = schema.serialize(value);

    TypedMessageBuilder<byte[]> msgBuilder = producer.newMessage();
    if (null != context.timestamp()) {
        msgBuilder = msgBuilder.eventTime(context.timestamp());
    }
    String msgKey = flinkPulsarKeyExtractor.getKey(value);
    if (null != msgKey) {
        msgBuilder = msgBuilder.key(msgKey);
    }

    if (flushOnCheckpoint) {
        synchronized (pendingRecordsLock) {
            pendingRecords++;
        }
    }
    msgBuilder.value(serializedValue)
            .properties(this.flinkPulsarPropertiesExtractor.getProperties(value))
            .sendAsync()
            .thenApply(successCallback)
            .exceptionally(failureCallback);
}
 
Example 3
Source File: TypedMessageBuilderPublish.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Override
public Void process(String input, Context context) {
    String publishTopic = (String) context.getUserConfigValueOrDefault("publish-topic", "publishtopic");
    String output = String.format("%s!", input);

    Map<String, String> properties = new HashMap<>();
    properties.put("input_topic", context.getCurrentRecord().getTopicName().get());
    properties.putAll(context.getCurrentRecord().getProperties());

    try {
        TypedMessageBuilder messageBuilder = context.newOutputMessage(publishTopic, Schema.STRING).
                value(output).properties(properties);
        if (context.getCurrentRecord().getKey().isPresent()){
            messageBuilder.key(context.getCurrentRecord().getKey().get());
        }
        messageBuilder.eventTime(System.currentTimeMillis()).sendAsync();
    } catch (PulsarClientException e) {
        context.getLogger().error(e.toString());
    }
    return null;
}
 
Example 4
Source File: PulsarBenchmarkProducer.java    From openmessaging-benchmark with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> sendAsync(Optional<String> key, byte[] payload) {
    TypedMessageBuilder<byte[]> msgBuilder = producer.newMessage().value(payload);
    if (key.isPresent()) {
        msgBuilder.key(key.get());
    }

    return msgBuilder.sendAsync().thenApply(msgId -> null);
}
 
Example 5
Source File: PulsarManager.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void send(final byte[] msg)  {
    if (producer != null) {
        String newKey = null;

        if(key != null && key.contains("${")) {
            newKey = getLoggerContext().getConfiguration().getStrSubstitutor().replace(key);
        } else if (key != null) {
            newKey = key;
        }


        TypedMessageBuilder<byte[]> messageBuilder = producer.newMessage()
                .value(msg);

        if (newKey != null) {
            messageBuilder.key(newKey);
        }

        if (syncSend) {
            try {
                messageBuilder.send();
            } catch (PulsarClientException e) {
                LOGGER.error("Unable to write to Pulsar in appender [" + getName() + "]", e);
            }
        } else {
            messageBuilder.sendAsync()
                .exceptionally(cause -> {
                    LOGGER.error("Unable to write to Pulsar in appender [" + getName() + "]", cause);
                    return null;
                });
        }
    }
}
 
Example 6
Source File: PulsarSink.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Override
public void write(Record<T> record) {
    TypedMessageBuilder<T> msg = pulsarSinkProcessor.newMessage(record);

    if (record.getKey().isPresent() && !(record.getSchema() instanceof KeyValueSchema &&
            ((KeyValueSchema) record.getSchema()).getKeyValueEncodingType() == KeyValueEncodingType.SEPARATED)) {
        msg.key(record.getKey().get());
    }

    msg.value(record.getValue());

    if (!record.getProperties().isEmpty() && pulsarSinkConfig.isForwardSourceMessageProperty()) {
        msg.properties(record.getProperties());
    }

    SinkRecord<T> sinkRecord = (SinkRecord<T>) record;
    if (sinkRecord.getSourceRecord() instanceof PulsarRecord) {
        PulsarRecord<T> pulsarRecord = (PulsarRecord<T>) sinkRecord.getSourceRecord();
        // forward user properties to sink-topic
        msg.property("__pfn_input_topic__", pulsarRecord.getTopicName().get())
           .property("__pfn_input_msg_id__",
                     new String(Base64.getEncoder().encode(pulsarRecord.getMessageId().toByteArray())));
    } else {
        // It is coming from some source
        Optional<Long> eventTime = sinkRecord.getSourceRecord().getEventTime();
        eventTime.ifPresent(msg::eventTime);
    }

    pulsarSinkProcessor.sendOutputMessage(msg, record);
}
 
Example 7
Source File: TupleToMessageMapper.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Set the value on a message builder to prepare the message to be published from the Bolt.
 *
 * @param tuple
 * @return
 */
default TypedMessageBuilder<byte[]> toMessage(TypedMessageBuilder<byte[]> msgBuilder, Tuple tuple) {
    // Default implementation provided for backward compatibility
    Message<byte[]> msg = toMessage(tuple);
    msgBuilder.value(msg.getData())
        .properties(msg.getProperties());
    if (msg.hasKey()) {
        msgBuilder.key(msg.getKey());
    }
    return msgBuilder;
}
 
Example 8
Source File: CmdProduce.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private int publish(String topic) {
    int numMessagesSent = 0;
    int returnCode = 0;

    try {
        PulsarClient client = clientBuilder.build();
        ProducerBuilder<byte[]> producerBuilder = client.newProducer().topic(topic);
        if (this.chunkingAllowed) {
            producerBuilder.enableChunking(true);
            producerBuilder.enableBatching(false);
        }
        Producer<byte[]> producer = producerBuilder.create();

        List<byte[]> messageBodies = generateMessageBodies(this.messages, this.messageFileNames);
        RateLimiter limiter = (this.publishRate > 0) ? RateLimiter.create(this.publishRate) : null;

        Map<String, String> kvMap = new HashMap<>();
        for (String property : properties) {
            String [] kv = property.split("=");
            kvMap.put(kv[0], kv[1]);
        }

        for (int i = 0; i < this.numTimesProduce; i++) {
            for (byte[] content : messageBodies) {
                if (limiter != null) {
                    limiter.acquire();
                }

                TypedMessageBuilder<byte[]> message = producer.newMessage();

                if (!kvMap.isEmpty()) {
                    message.properties(kvMap);
                }

                if (key != null && !key.isEmpty()) {
                    message.key(key);
                }

                message.value(content).send();

                numMessagesSent++;
            }
        }
        client.close();
    } catch (Exception e) {
        LOG.error("Error while producing messages");
        LOG.error(e.getMessage(), e);
        returnCode = -1;
    } finally {
        LOG.info("{} messages successfully produced", numMessagesSent);
    }

    return returnCode;
}