Search in sources :

Example 31 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project heron by twitter.

the class KafkaUtilsTest method generateTuplesWithValueAndStringMultiSchemeWithTopic.

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

Example 32 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project heron by twitter.

the class KafkaUtilsTest method generateTuplesWithValueSchemeAndKeyValueMessage.

@Test
public void generateTuplesWithValueSchemeAndKeyValueMessage() {
    config.scheme = new SchemeAsMultiScheme(new StringScheme());
    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(value, lists.iterator().next().get(0));
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) MessageAndOffset(kafka.message.MessageAndOffset) SchemeAsMultiScheme(org.apache.storm.spout.SchemeAsMultiScheme) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) Test(org.junit.Test)

Example 33 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project heron by twitter.

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 34 with ByteBufferMessageSet

use of kafka.javaapi.message.ByteBufferMessageSet in project cdap by caskdata.

the class KafkaConsumer method fetchMessages.

/**
   * Fetches Kafka messages from an offset.
   * @param offset message offset to start.
   * @param callback callback to handle the messages fetched.
   * @return number of messages fetched.
   */
public int fetchMessages(long offset, Callback callback) throws OffsetOutOfRangeException {
    ByteBufferMessageSet messageSet = fetchMessageSet(offset);
    int msgCount = 0;
    for (MessageAndOffset msg : messageSet) {
        ++msgCount;
        callback.handle(msg.offset(), msg.message().payload());
    }
    return msgCount;
}
Also used : MessageAndOffset(kafka.message.MessageAndOffset) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet)

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