Kafka Java sample

Source: Internet
Author: User
The Kafka version I use is: 0.7.2

The JDK version is: 1.6.0_20

Http://kafka.apache.org/07/quickstart.html The official example is not very complete, the following code is my supplement and can be run after compiling.


Producer Code

[Java]  View Plain copy import java.util.*;   import kafka.message.message;   Import  kafka.producer.ProducerConfig;   import kafka.javaapi.producer.producer;   Import  kafka.javaapi.producer.ProducerData;      public class producersample {              public static void main (string[)  args)  {           producersample ps =  new producersample ();               Properties props = new properties ();            props.put ("Zk.connect",  "127.0.0.1:2181");            props.put ("Serializer.class",  "Kafka.serializer.StringEncoder");          &Nbsp;    producerconfig config = new producerconfig (props);            producer<string, string> producer =  new Producer<String, String> (config);            producerdata<string, string> data = new producerdata<string,  String> ("Test-topic",  "Test-message2");            producer.send (data);           producer.close ();        }  }  

Consumer Code

[Java] view plain copy import Java.nio.ByteBuffer;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.