Java Code Examples for kafka.message.MessageAndOffset#message()
The following examples show how to use
kafka.message.MessageAndOffset#message() .
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: KafkaBoltTest.java From storm-kafka-0.8-plus with Apache License 2.0 | 6 votes |
private boolean verifyMessage(String key, String message) { long lastMessageOffset = KafkaUtils.getOffset(simpleConsumer, kafkaConfig.topic, 0, OffsetRequest.LatestTime()) - 1; ByteBufferMessageSet messageAndOffsets = KafkaUtils.fetchMessages(kafkaConfig, simpleConsumer, new Partition(Broker.fromString(broker.getBrokerConnectionString()), 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; }
Example 2
Source File: AbstractExactlyOnceKafkaOutputOperator.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
private void initializeLastProcessingOffset() { // read last received kafka message TopicMetadata tm = KafkaMetadataUtil.getTopicMetadata(Sets.newHashSet((String)getConfigProperties().get(KafkaMetadataUtil.PRODUCER_PROP_BROKERLIST)), this.getTopic()); if (tm == null) { throw new RuntimeException("Failed to retrieve topic metadata"); } partitionNum = tm.partitionsMetadata().size(); lastMsgs = new HashMap<Integer, Pair<byte[],byte[]>>(partitionNum); for (PartitionMetadata pm : tm.partitionsMetadata()) { String leadBroker = pm.leader().host(); int port = pm.leader().port(); String clientName = this.getClass().getName().replace('$', '.') + "_Client_" + tm.topic() + "_" + pm.partitionId(); SimpleConsumer consumer = new SimpleConsumer(leadBroker, port, 100000, 64 * 1024, clientName); long readOffset = KafkaMetadataUtil.getLastOffset(consumer, tm.topic(), pm.partitionId(), kafka.api.OffsetRequest.LatestTime(), clientName); FetchRequest req = new FetchRequestBuilder().clientId(clientName).addFetch(tm.topic(), pm.partitionId(), readOffset - 1, 100000).build(); FetchResponse fetchResponse = consumer.fetch(req); for (MessageAndOffset messageAndOffset : fetchResponse.messageSet(tm.topic(), pm.partitionId())) { Message m = messageAndOffset.message(); ByteBuffer payload = m.payload(); ByteBuffer key = m.key(); byte[] valueBytes = new byte[payload.limit()]; byte[] keyBytes = new byte[key.limit()]; payload.get(valueBytes); key.get(keyBytes); lastMsgs.put(pm.partitionId(), new Pair<byte[], byte[]>(keyBytes, valueBytes)); } } }
Example 3
Source File: KafkaReader.java From HiveKa with Apache License 2.0 | 5 votes |
/** * Fetches the next Kafka message and stuffs the results into the key and * value * * @param key * @param payload * @param pKey * @return true if there exists more events * @throws IOException */ public boolean getNext(KafkaKey key, BytesWritable payload ,BytesWritable pKey) throws IOException { if (hasNext()) { MessageAndOffset msgAndOffset = messageIter.next(); Message message = msgAndOffset.message(); ByteBuffer buf = message.payload(); int origSize = buf.remaining(); byte[] bytes = new byte[origSize]; buf.get(bytes, buf.position(), origSize); payload.set(bytes, 0, origSize); buf = message.key(); if(buf != null){ origSize = buf.remaining(); bytes = new byte[origSize]; buf.get(bytes, buf.position(), origSize); pKey.set(bytes, 0, origSize); } key.clear(); key.set(kafkaRequest.getTopic(), kafkaRequest.getLeaderId(), kafkaRequest.getPartition(), currentOffset, msgAndOffset.offset() + 1, message.checksum()); key.setMessageSize(msgAndOffset.message().size()); currentOffset = msgAndOffset.offset() + 1; // increase offset currentCount++; // increase count return true; } else { return false; } }
Example 4
Source File: KafkaPartitionReader.java From Scribengin with GNU Affero General Public License v3.0 | 5 votes |
byte[] getCurrentMessagePayload() { while(currentMessageSetIterator.hasNext()) { MessageAndOffset messageAndOffset = currentMessageSetIterator.next(); if (messageAndOffset.offset() < currentOffset) continue; //old offset, ignore Message message = messageAndOffset.message(); ByteBuffer payload = message.payload(); byte[] bytes = new byte[payload.limit()]; payload.get(bytes); currentOffset = messageAndOffset.nextOffset(); return bytes; } return null; }