Search in sources :

Example 1 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project storm by apache.

the class TridentKafkaEmitter method fetchMessages.

private ByteBufferMessageSet fetchMessages(SimpleConsumer consumer, Partition partition, long offset) {
    long start = System.currentTimeMillis();
    ByteBufferMessageSet msgs = null;
    msgs = KafkaUtils.fetchMessages(_config, consumer, partition, offset);
    long millis = System.currentTimeMillis() - start;
    _kafkaMeanFetchLatencyMetric.update(millis);
    _kafkaMaxFetchLatencyMetric.update(millis);
    return msgs;
}
Also used : ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet)

Example 2 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project storm by apache.

the class KafkaBoltTest method executeWithByteArrayKeyAndMessageFire.

/* test with fireAndForget option enabled */
@Test
public void executeWithByteArrayKeyAndMessageFire() {
    boolean async = true;
    boolean fireAndForget = true;
    bolt = generateDefaultSerializerBolt(async, fireAndForget, null);
    String keyString = "test-key";
    String messageString = "test-message";
    byte[] key = keyString.getBytes();
    byte[] message = messageString.getBytes();
    Tuple tuple = generateTestTuple(key, message);
    final ByteBufferMessageSet mockMsg = mockSingleMessage(key, message);
    simpleConsumer.close();
    simpleConsumer = mockSimpleConsumer(mockMsg);
    KafkaProducer<?, ?> producer = mock(KafkaProducer.class);
    // do not invoke the callback of send() in order to test whether the bolt handle the fireAndForget option
    // properly.
    doReturn(mock(Future.class)).when(producer).send(any(ProducerRecord.class), any(Callback.class));
    bolt.execute(tuple);
    verify(collector).ack(tuple);
    verifyMessage(keyString, messageString);
}
Also used : Callback(org.apache.kafka.clients.producer.Callback) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) Tuple(org.apache.storm.tuple.Tuple)

Example 3 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project storm by apache.

the class KafkaUtilsTest method generateTuplesWithKeyAndKeyValueScheme.

@Test
public void generateTuplesWithKeyAndKeyValueScheme() {
    config.scheme = new KeyValueSchemeAsMultiScheme(new StringKeyValueScheme());
    config.useStartOffsetTimeIfOffsetOutOfRange = false;
    String value = "value";
    String key = "key";
    createTopicAndSendMessage(key, value);
    ByteBufferMessageSet messageAndOffsets = getLastMessage();
    for (MessageAndOffset msg : messageAndOffsets) {
        Iterable<List<Object>> lists = KafkaUtils.generateTuples(config, msg.message(), config.topic);
        assertEquals(ImmutableMap.of(key, value), lists.iterator().next().get(0));
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) MessageAndOffset(kafka.message.MessageAndOffset) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) Test(org.junit.Test)

Example 4 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project storm by apache.

the class KafkaUtilsTest method fetchMessage.

@Test
public void fetchMessage() throws Exception {
    String value = "test";
    createTopicAndSendMessage(value);
    long offset = KafkaUtils.getOffset(simpleConsumer, config.topic, 0, OffsetRequest.LatestTime()) - 1;
    ByteBufferMessageSet messageAndOffsets = KafkaUtils.fetchMessages(config, simpleConsumer, new Partition(Broker.fromString(broker.getBrokerConnectionString()), TEST_TOPIC, 0), offset);
    String message = new String(Utils.toByteArray(messageAndOffsets.iterator().next().message().payload()));
    assertThat(message, is(equalTo(value)));
}
Also used : ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) Test(org.junit.Test)

Example 5 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project storm by apache.

the class TestUtils method verifyMessage.

public static boolean verifyMessage(String key, String message, KafkaTestBroker broker, SimpleConsumer simpleConsumer) {
    long lastMessageOffset = KafkaUtils.getOffset(simpleConsumer, TestUtils.TOPIC, 0, OffsetRequest.LatestTime()) - 1;
    ByteBufferMessageSet messageAndOffsets = KafkaUtils.fetchMessages(TestUtils.getKafkaConfig(broker), simpleConsumer, new Partition(Broker.fromString(broker.getBrokerConnectionString()), TestUtils.TOPIC, 0), lastMessageOffset);
    MessageAndOffset messageAndOffset = messageAndOffsets.iterator().next();
    Message kafkaMessage = messageAndOffset.message();
    ByteBuffer messageKeyBuffer = kafkaMessage.key();
    String keyString = null;
    String messageString = new String(Utils.toByteArray(kafkaMessage.payload()));
    if (messageKeyBuffer != null) {
        keyString = new String(Utils.toByteArray(messageKeyBuffer));
    }
    assertEquals(key, keyString);
    assertEquals(message, messageString);
    return true;
}
Also used : Message(kafka.message.Message) MessageAndOffset(kafka.message.MessageAndOffset) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBufferMessageSet (kafka.javaapi.message.ByteBufferMessageSet)34 MessageAndOffset (kafka.message.MessageAndOffset)24 ArrayList (java.util.ArrayList)11 List (java.util.List)10 Test (org.junit.Test)10 IOException (java.io.IOException)6 FetchRequest (kafka.api.FetchRequest)5 Message (kafka.message.Message)5 ByteBuffer (java.nio.ByteBuffer)4 FetchRequestBuilder (kafka.api.FetchRequestBuilder)4 FetchResponse (kafka.javaapi.FetchResponse)4 SimpleConsumer (kafka.javaapi.consumer.SimpleConsumer)4 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)3 Future (java.util.concurrent.Future)2 Callback (org.apache.kafka.clients.producer.Callback)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 SchemeAsMultiScheme (org.apache.storm.spout.SchemeAsMultiScheme)2 Tuple (org.apache.storm.tuple.Tuple)2