Java Code Examples for org.apache.pulsar.client.api.Message#getKey()

The following examples show how to use org.apache.pulsar.client.api.Message#getKey() . 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: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private K getKey(String topic, Message<byte[]> msg) {
    if (!msg.hasKey()) {
        return null;
    }

    if (keySchema instanceof PulsarKafkaSchema) {
        PulsarKafkaSchema<K> pulsarKafkaSchema = (PulsarKafkaSchema) keySchema;
        Deserializer<K> kafkaDeserializer = pulsarKafkaSchema.getKafkaDeserializer();
        if (kafkaDeserializer instanceof StringDeserializer) {
            return (K) msg.getKey();
        }
        pulsarKafkaSchema.setTopic(topic);
    }
    // Assume base64 encoding
    byte[] data = Base64.getDecoder().decode(msg.getKey());
    return keySchema.decode(data);

}
 
Example 2
Source File: PulsarKafkaConsumer.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private K getKey(String topic, Message<byte[]> msg) {
    if (!msg.hasKey()) {
        return null;
    }

    if (keySchema instanceof PulsarKafkaSchema) {
        PulsarKafkaSchema<K> pulsarKafkaSchema = (PulsarKafkaSchema) keySchema;
        Deserializer<K> kafkaDeserializer = pulsarKafkaSchema.getKafkaDeserializer();
        if (kafkaDeserializer instanceof StringDeserializer) {
            return (K) msg.getKey();
        }
        pulsarKafkaSchema.setTopic(topic);
    }
    // Assume base64 encoding
    byte[] data = Base64.getDecoder().decode(msg.getKey());
    return keySchema.decode(data);

}
 
Example 3
Source File: PulsarAppenderTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAppendWithKey() {
    final Appender appender = ctx.getConfiguration().getAppender("PulsarAppenderWithKey");
    final LogEvent logEvent = createLogEvent();
    appender.append(logEvent);
    Message<byte[]> item;
    synchronized (history) {
        assertEquals(1, history.size());
        item = history.get(0);
    }
    assertNotNull(item);
    String msgKey = item.getKey();
    assertEquals(msgKey, "key");
    assertEquals(LOG_MESSAGE, new String(item.getData(), StandardCharsets.UTF_8));
}
 
Example 4
Source File: CmdConsume.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Interprets the message to create a string representation
 *
 * @param message
 *            The message to interpret
 * @param displayHex
 *            Whether to display BytesMessages in hexdump style, ignored for simple text messages
 * @return String representation of the message
 */
private String interpretMessage(Message<byte[]> message, boolean displayHex) throws IOException {
    StringBuilder sb = new StringBuilder();

    String properties = Arrays.toString(message.getProperties().entrySet().toArray());

    String data;
    byte[] msgData = message.getData();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    if (!displayHex) {
        data = new String(msgData);
    } else {
        HexDump.dump(msgData, 0, out, 0);
        data = new String(out.toByteArray());
    }

    String key = null;
    if (message.hasKey()) {
        key = message.getKey();
    }

    sb.append("key:[").append(key).append("], ");
    sb.append("properties:").append(properties).append(", ");
    sb.append("content:").append(data);

    return sb.toString();
}
 
Example 5
Source File: ConsumerIterator.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public PulsarMessageAndMetadata<K, V> next() {

    Message<byte[]> msg = receivedMessages.poll();
    if (msg == null) {
        try {
            msg = consumer.receive();
        } catch (PulsarClientException e) {
            log.warn("Failed to receive message for {}-{}, {}", consumer.getTopic(), consumer.getSubscription(),
                    e.getMessage(), e);
            throw new RuntimeException(
                    "failed to receive message from " + consumer.getTopic() + "-" + consumer.getSubscription());
        }
    }

    int partition = TopicName.getPartitionIndex(consumer.getTopic());
    long offset = MessageIdUtils.getOffset(msg.getMessageId());
    String key = msg.getKey();
    byte[] value = msg.getValue();

    K desKey = null;
    V desValue = null;

    if (StringUtils.isNotBlank(key)) {
        if (keyDeSerializer.isPresent() && keyDeSerializer.get() instanceof StringDecoder) {
            desKey = (K) key;
        } else {
            byte[] decodedBytes = Base64.getDecoder().decode(key);
            desKey = keyDeSerializer.isPresent() ? keyDeSerializer.get().fromBytes(decodedBytes)
                    : (K) DEFAULT_DECODER.fromBytes(decodedBytes);
        }
    }

    if (value != null) {
        desValue = valueDeSerializer.isPresent() ? valueDeSerializer.get().fromBytes(msg.getData())
                : (V) DEFAULT_DECODER.fromBytes(msg.getData());
    }

    PulsarMessageAndMetadata<K, V> msgAndMetadata = new PulsarMessageAndMetadata<>(consumer.getTopic(), partition,
            null, offset, keyDeSerializer.orElse(null), valueDeSerializer.orElse(null), desKey, desValue);

    if (isAutoCommit) {
        // Commit the offset of previously dequeued messages
        consumer.acknowledgeCumulativeAsync(msg);
    }

    lastConsumedMessageId = msg.getMessageId();
    return msgAndMetadata;
}