Java Code Examples for kafka.consumer.KafkaStream#iterator()
The following examples show how to use
kafka.consumer.KafkaStream#iterator() .
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: KafkaOffsetGetter.java From Kafka-Insight with Apache License 2.0 | 6 votes |
/** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread. * <p> * The general contract of the method <code>run</code> is that it may * take any action whatsoever. * * @see Thread#run() */ @Override public void run() { ConsumerConnector consumerConnector = KafkaUtils.createConsumerConnector(zkAddr, group); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(CONSUMER_OFFSET_TOPIC, new Integer(1)); KafkaStream<byte[], byte[]> offsetMsgStream = consumerConnector.createMessageStreams(topicCountMap).get(CONSUMER_OFFSET_TOPIC).get(0); ConsumerIterator<byte[], byte[]> it = offsetMsgStream.iterator(); while (true) { MessageAndMetadata<byte[], byte[]> offsetMsg = it.next(); if (ByteBuffer.wrap(offsetMsg.key()).getShort() < 2) { try { GroupTopicPartition commitKey = readMessageKey(ByteBuffer.wrap(offsetMsg.key())); if (offsetMsg.message() == null) { continue; } kafka.common.OffsetAndMetadata commitValue = readMessageValue(ByteBuffer.wrap(offsetMsg.message())); kafkaConsumerOffsets.put(commitKey, commitValue); } catch (Exception e) { e.printStackTrace(); } } } }
Example 2
Source File: KafkaWorker.java From elasticsearch-river-kafka with Apache License 2.0 | 6 votes |
/** * Consumes the messages from the partition via specified stream. */ private void consumeMessagesAndAddToBulkProcessor(final KafkaStream stream) { try { // by default it waits forever for message, but there is timeout configured final ConsumerIterator<byte[], byte[]> consumerIterator = stream.iterator(); // Consume all the messages of the stream (partition) while (consumerIterator.hasNext() && consume) { final MessageAndMetadata messageAndMetadata = consumerIterator.next(); logMessage(messageAndMetadata); elasticsearchProducer.addMessagesToBulkProcessor(messageAndMetadata); // StatsD reporting stats.messagesReceived.incrementAndGet(); stats.lastCommitOffsetByPartitionId.put(messageAndMetadata.partition(), messageAndMetadata.offset()); } } catch (ConsumerTimeoutException ex) { logger.debug("Nothing to be consumed for now. Consume flag is: {}", consume); } }
Example 3
Source File: LegacyKafkaMessageIterator.java From secor with Apache License 2.0 | 6 votes |
@Override public void init(SecorConfig config) throws UnknownHostException { this.mConfig = config; mConsumerConnector = Consumer.createJavaConsumerConnector(createConsumerConfig()); if (!mConfig.getKafkaTopicBlacklist().isEmpty() && !mConfig.getKafkaTopicFilter().isEmpty()) { throw new RuntimeException("Topic filter and blacklist cannot be both specified."); } TopicFilter topicFilter = !mConfig.getKafkaTopicBlacklist().isEmpty() ? new Blacklist(mConfig.getKafkaTopicBlacklist()) : new Whitelist(mConfig.getKafkaTopicFilter()); LOG.debug("Use TopicFilter {}({})", topicFilter.getClass(), topicFilter); List<KafkaStream<byte[], byte[]>> streams = mConsumerConnector.createMessageStreamsByFilter(topicFilter); KafkaStream<byte[], byte[]> stream = streams.get(0); mIterator = stream.iterator(); mKafkaMessageTimestampFactory = new KafkaMessageTimestampFactory(mConfig.getKafkaMessageTimestampClass()); }
Example 4
Source File: KafkaConsumer.java From flume-ng-kafka-sink with Apache License 2.0 | 6 votes |
public MessageAndMetadata getNextMessage(String topic){ List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic); KafkaStream stream = streams.get(0); // it has only a single stream, because there is only one consumer final ConsumerIterator<byte[], byte[]> it = stream.iterator(); int counter = 0; while (!it.hasNext()){ // Wait time >= 10s, so return null and exit if(counter == 5){ logger.error("0 messages available to fetch for the topic " + topic); return null; } // wait till a message is published. this is a blocking call. try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { // ignore } counter++; } return it.next(); }
Example 5
Source File: FastKafkaSource.java From fraud-detection-tutorial with Apache License 2.0 | 6 votes |
public synchronized void start() { log.info("Starting {}...", this); try { this.consumer = KafkaSourceUtil.getConsumer(this.kafkaProps); } catch (Exception var6) { throw new FlumeException("Unable to create consumer. Check whether the ZooKeeper server is up and that the Flume agent can connect to it.", var6); } HashMap topicCountMap = new HashMap(); topicCountMap.put(this.topic, Integer.valueOf(1)); try { Map e = this.consumer.createMessageStreams(topicCountMap); List topicList = (List)e.get(this.topic); KafkaStream stream = (KafkaStream)topicList.get(0); this.it = stream.iterator(); } catch (Exception var5) { throw new FlumeException("Unable to get message iterator from Kafka", var5); } log.info("Kafka source {} started.", this.getName()); this.counter.start(); super.start(); }
Example 6
Source File: KafkaConsumer.java From sqoop-on-spark with Apache License 2.0 | 6 votes |
public MessageAndMetadata getNextMessage(String topic) { List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic); // it has only a single stream, because there is only one consumer KafkaStream stream = streams.get(0); final ConsumerIterator<byte[], byte[]> it = stream.iterator(); int counter = 0; try { if (it.hasNext()) { return it.next(); } else { return null; } } catch (ConsumerTimeoutException e) { logger.error("0 messages available to fetch for the topic " + topic); return null; } }
Example 7
Source File: KafkaProducerServiceIntegrationTest.java From vertx-kafka-service with Apache License 2.0 | 6 votes |
private void consumeMessages() { final Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(TOPIC, 1); final StringDecoder decoder = new StringDecoder(new VerifiableProperties()); final Map<String, List<KafkaStream<String, String>>> consumerMap = consumer.createMessageStreams(topicCountMap, decoder, decoder); final KafkaStream<String, String> stream = consumerMap.get(TOPIC).get(0); final ConsumerIterator<String, String> iterator = stream.iterator(); Thread kafkaMessageReceiverThread = new Thread( () -> { while (iterator.hasNext()) { String msg = iterator.next().message(); msg = msg == null ? "<null>" : msg; System.out.println("got message: " + msg); messagesReceived.add(msg); } }, "kafkaMessageReceiverThread" ); kafkaMessageReceiverThread.start(); }
Example 8
Source File: KafkaMqCollect.java From light_drtc with Apache License 2.0 | 6 votes |
public void collectMq(){ Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(Constants.kfTopic, new Integer(1)); StringDecoder keyDecoder = new StringDecoder(new VerifiableProperties()); StringDecoder valueDecoder = new StringDecoder(new VerifiableProperties()); Map<String, List<KafkaStream<String, String>>> consumerMap = consumer.createMessageStreams(topicCountMap,keyDecoder,valueDecoder); KafkaStream<String, String> stream = consumerMap.get(Constants.kfTopic).get(0); ConsumerIterator<String, String> it = stream.iterator(); MessageAndMetadata<String, String> msgMeta; while (it.hasNext()){ msgMeta = it.next(); super.mqTimer.parseMqText(msgMeta.key(), msgMeta.message()); //System.out.println(msgMeta.key()+"\t"+msgMeta.message()); } }
Example 9
Source File: KafkaReceiver.java From koper with Apache License 2.0 | 6 votes |
private void processStreamsByTopic(String topicKeys, List<KafkaStream<byte[], byte[]>> streamList) { // init stream thread pool ExecutorService streamPool = Executors.newFixedThreadPool(partitions); String[] topics = StringUtils.split(topicKeys, ","); if (log.isDebugEnabled()) log.debug("准备处理消息流集合 KafkaStreamList,topic count={},topics={}, partitions/topic={}", topics.length, topicKeys, partitions); //遍历stream AtomicInteger index = new AtomicInteger(0); for (KafkaStream<byte[], byte[]> stream : streamList) { Thread streamThread = new Thread() { @Override public void run() { int i = index.getAndAdd(1); if (log.isDebugEnabled()) log.debug("处理消息流KafkaStream -- No.={}, partitions={}", i, partitions + ":" + i); ConsumerIterator<byte[], byte[]> consumerIterator = stream.iterator(); processStreamByConsumer(topicKeys, consumerIterator); } }; streamPool.execute(streamThread); } }
Example 10
Source File: KafkaDataProvider.java From linden with Apache License 2.0 | 6 votes |
public KafkaDataProvider(String zookeeper, String topic, String groupId) { super(MessageAndMetadata.class); Properties props = new Properties(); props.put("zookeeper.connect", zookeeper); props.put("group.id", groupId); props.put("zookeeper.session.timeout.ms", "30000"); props.put("auto.commit.interval.ms", "1000"); props.put("fetch.message.max.bytes", "4194304"); consumer = kafka.consumer.Consumer.createJavaConsumerConnector(new ConsumerConfig(props)); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, 1); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); iter = stream.iterator(); }
Example 11
Source File: KafkaSourceOp.java From PoseidonX with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override public void initialize() throws StreamingException { ConsumerConfig consumerConfig = new ConsumerConfig(kafkaProperties); consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig); Map<String, Integer> topicCountMap = Maps.newHashMap(); topicCountMap.put(topic, TOPIC_COUNT); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); consumerIterator = stream.iterator(); }
Example 12
Source File: ReleaseDecryptConsumerServiceImpl.java From OpenIoE with Apache License 2.0 | 5 votes |
@Override public void run() { Map<String, Integer> topicMap = new HashMap<String, Integer>(); topicMap.put(CryptoIntegrationConstants.TOPIC_CRYPTO_RELEASE_DECRYPT_CC, new Integer(1)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector.createMessageStreams(topicMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(CryptoIntegrationConstants.TOPIC_CRYPTO_RELEASE_DECRYPT_CC).get(0); ConsumerIterator<byte[], byte[]> it = stream.iterator(); while(it.hasNext()) { String sessionId = new String(it.next().message()); cryptoService.releaseDecryptCCVolatile(sessionId); } }
Example 13
Source File: DemoHighLevelConsumer.java From KafkaExample with Apache License 2.0 | 5 votes |
public static void main(String[] args) { args = new String[] { "zookeeper0:2181/kafka", "topic1", "group2", "consumer1" }; if (args == null || args.length != 4) { System.err.println("Usage:\n\tjava -jar kafka_consumer.jar ${zookeeper_list} ${topic_name} ${group_name} ${consumer_id}"); System.exit(1); } String zk = args[0]; String topic = args[1]; String groupid = args[2]; String consumerid = args[3]; Properties props = new Properties(); props.put("zookeeper.connect", zk); props.put("group.id", groupid); props.put("client.id", "test"); props.put("consumer.id", consumerid); props.put("auto.offset.reset", "largest"); props.put("auto.commit.enable", "false"); props.put("auto.commit.interval.ms", "60000"); ConsumerConfig consumerConfig = new ConsumerConfig(props); ConsumerConnector consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, 1); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumerConnector.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream1 = consumerMap.get(topic).get(0); ConsumerIterator<byte[], byte[]> interator = stream1.iterator(); while (interator.hasNext()) { MessageAndMetadata<byte[], byte[]> messageAndMetadata = interator.next(); String message = String.format( "Topic:%s, GroupID:%s, Consumer ID:%s, PartitionID:%s, Offset:%s, Message Key:%s, Message Payload: %s", messageAndMetadata.topic(), groupid, consumerid, messageAndMetadata.partition(), messageAndMetadata.offset(), new String(messageAndMetadata.key()), new String(messageAndMetadata.message())); System.out.println(message); consumerConnector.commitOffsets(); } }
Example 14
Source File: KafkaTestConsumer.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
@Override public void run() { Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, new Integer(1)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); ConsumerIterator<byte[], byte[]> it = stream.iterator(); logger.debug("Inside consumer::run receiveCount= {}", receiveCount); while (it.hasNext() & isAlive) { Message msg = new Message(it.next().message()); if (latch != null) { latch.countDown(); } if (getMessage(msg).equals(KafkaOperatorTestBase.END_TUPLE)) { break; } holdingBuffer.add(msg); receiveCount++; logger.debug("Consuming {}, receiveCount= {}", getMessage(msg), receiveCount); try { Thread.sleep(50); } catch (InterruptedException e) { break; } } logger.debug("DONE consuming"); }
Example 15
Source File: KafkaDemoClient.java From iotplatform with Apache License 2.0 | 5 votes |
private static ConsumerIterator<String, String> buildConsumer(String topic) { Map<String, Integer> topicCountMap = new HashMap<>(); topicCountMap.put(topic, 1); ConsumerConfig consumerConfig = new ConsumerConfig(consumerProperties()); ConsumerConnector consumerConnector = Consumer.createJavaConsumerConnector(consumerConfig); Map<String, List<KafkaStream<String, String>>> consumers = consumerConnector.createMessageStreams(topicCountMap, new StringDecoder(null), new StringDecoder(null)); KafkaStream<String, String> stream = consumers.get(topic).get(0); return stream.iterator(); }
Example 16
Source File: KafkaComponent.java From metron with Apache License 2.0 | 5 votes |
public ConsumerIterator<byte[], byte[]> getStreamIterator(String topic, String group, String consumerName) { // setup simple consumer Properties consumerProperties = TestUtils.createConsumerProperties(zookeeperConnectString, group, consumerName, -1); consumer = kafka.consumer.Consumer.createJavaConsumerConnector(new ConsumerConfig(consumerProperties)); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, 1); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); ConsumerIterator<byte[], byte[]> iterator = stream.iterator(); return iterator; }
Example 17
Source File: KafkaConsumer.java From blog_demos with Apache License 2.0 | 5 votes |
/** * 启动一个consumer * @param topic */ public void startConsume(String topic){ Properties props = new Properties(); props.put("zookeeper.connect", zkConnect); props.put("group.id", groupId); props.put("zookeeper.session.timeout.ms", "40000"); props.put("zookeeper.sync.time.ms", "200"); props.put("auto.commit.interval.ms", "1000"); ConsumerConnector consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(props)); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, new Integer(1)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); final ConsumerIterator<byte[], byte[]> it = stream.iterator(); Runnable executor = new Runnable() { @Override public void run() { while (it.hasNext()) { System.out.println("************** receive:" + new String(it.next().message())); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; new Thread(executor).start(); }
Example 18
Source File: KafkaConsumer.java From blog_demos with Apache License 2.0 | 5 votes |
/** * 启动一个consumer * @param topic */ public void startConsume(String topic){ Properties props = new Properties(); props.put("zookeeper.connect", zkConnect); props.put("group.id", groupId); props.put("zookeeper.session.timeout.ms", "40000"); props.put("zookeeper.sync.time.ms", "200"); props.put("auto.commit.interval.ms", "1000"); ConsumerConnector consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(props)); Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(topic, new Integer(1)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap); KafkaStream<byte[], byte[]> stream = consumerMap.get(topic).get(0); final ConsumerIterator<byte[], byte[]> it = stream.iterator(); Runnable executor = new Runnable() { @Override public void run() { while (it.hasNext()) { System.out.println("************** receive:" + new String(it.next().message())); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; new Thread(executor).start(); }
Example 19
Source File: KafkaMessageProcessorIT.java From mod-kafka with Apache License 2.0 | 5 votes |
private void consumeMessages() { final Map<String, Integer> topicCountMap = new HashMap<String, Integer>(); topicCountMap.put(KafkaProperties.DEFAULT_TOPIC, 1); final StringDecoder decoder = new StringDecoder(new VerifiableProperties()); final Map<String, List<KafkaStream<String, String>>> consumerMap = consumer.createMessageStreams(topicCountMap, decoder, decoder); final KafkaStream<String, String> stream = consumerMap.get(KafkaProperties.DEFAULT_TOPIC).get(0); final ConsumerIterator<String, String> iterator = stream.iterator(); Thread kafkaMessageReceiverThread = new Thread( new Runnable() { @Override public void run() { while (iterator.hasNext()) { String msg = iterator.next().message(); msg = msg == null ? "<null>" : msg; System.out.println("got message: " + msg); messagesReceived.add(msg); } } }, "kafkaMessageReceiverThread" ); kafkaMessageReceiverThread.start(); }
Example 20
Source File: ThrottlingManagerEstimatorConsumerFactory.java From warp10-platform with Apache License 2.0 | 4 votes |
@Override public Runnable getConsumer(final KafkaSynchronizedConsumerPool pool, final KafkaStream<byte[], byte[]> stream) { return new Runnable() { @Override public void run() { ConsumerIterator<byte[],byte[]> iter = stream.iterator(); // Iterate on the messages TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory()); KafkaOffsetCounters counters = pool.getCounters(); try { while (iter.hasNext()) { // // Since the call to 'next' may block, we need to first // check that there is a message available // boolean nonEmpty = iter.nonEmpty(); if (nonEmpty) { MessageAndMetadata<byte[], byte[]> msg = iter.next(); counters.count(msg.partition(), msg.offset()); byte[] data = msg.message(); Sensision.update(SensisionConstants.CLASS_WARP_INGRESS_KAFKA_THROTTLING_IN_MESSAGES, Sensision.EMPTY_LABELS, 1); Sensision.update(SensisionConstants.CLASS_WARP_INGRESS_KAFKA_THROTTLING_IN_BYTES, Sensision.EMPTY_LABELS, data.length); if (null != macKey) { data = CryptoUtils.removeMAC(macKey, data); } // Skip data whose MAC was not verified successfully if (null == data) { Sensision.update(SensisionConstants.CLASS_WARP_INGRESS_KAFKA_THROTTLING_IN_INVALIDMACS, Sensision.EMPTY_LABELS, 1); continue; } // // Update throttling manager // try { ThrottlingManager.fuse(HyperLogLogPlus.fromBytes(data)); Sensision.update(SensisionConstants.CLASS_WARP_INGRESS_THROTLLING_FUSIONS, Sensision.EMPTY_LABELS, 1); } catch (Exception e) { Sensision.update(SensisionConstants.CLASS_WARP_INGRESS_THROTLLING_FUSIONS_FAILED, Sensision.EMPTY_LABELS, 1); } } } } catch (Throwable t) { t.printStackTrace(System.err); } finally { // Set abort to true in case we exit the 'run' method pool.getAbort().set(true); } } }; }