A hands-on Kafka setup in plain Java: a producer, a consumer, and a real-world data source (Twitter's streaming API) feeding the producer instead of a hardcoded message.
Producer— configuresKafkaProducerwith bootstrap servers + string (de)serializers, sends aProducerRecordtofirst_topics, and flushes/closes cleanly.Consumer— configuresKafkaConsumerwith a consumer group (my-app) andearliestoffset reset, subscribes tofirst_topics, and polls in a loop.TwitterProducer— the interesting piece: opens a filtered Twitter stream via the Hosebird Client (hbc), buffers incoming tweets on aBlockingQueue, and republishes each one onto thetwitter_tweetsKafka topic through aKafkaProducer, with an async callback for send errors.
Java, Apache Kafka client, Twitter Hosebird Client (hbc), Maven
- Start a local Kafka broker on
127.0.0.1:9092 - Run
Consumerto subscribe to the topic you care about (first_topicsortwitter_tweets) - Run
Producerfor a simple hello-world message, orTwitterProducer(with Twitter API credentials) to stream live tweets into Kafka