Producers
1  Packagecom;2 Importjava.util.Properties; 3 ImportJava.util.concurrent.TimeUnit; 4   5 ImportKafka.javaapi.producer.Producer; 6 ImportKafka.producer.KeyedMessage; 7 ImportKafka.producer.ProducerConfig; 8 ImportKafka.serializer.StringEncoder; 9   Ten    One    A    -  Public classKafkaproducerextendsthread{ -    the     PrivateString topic;  -        -      Publickafkaproducer (String topic) { -         Super();  +          This. Topic =topic;  -     }   +        A        at @Override -      Public voidrun () { -Producer Producer =Createproducer ();  -         intI=0;  -          while(true){   -Producer.send (NewKeyedmessage<integer, string> (topic, "message:" + i++));  inSYSTEM.OUT.PRINTLN ("message:" +i); -             Try {   toTimeUnit.SECONDS.sleep (1);  +}Catch(interruptedexception e) { - E.printstacktrace ();  the             }   *         }   $     }  Panax Notoginseng    -     PrivateProducer Createproducer () { theProperties Properties =NewProperties ();  +Properties.put ("Zookeeper.connect", "192.168.130.130:2181");//declaration ZK AProperties.put ("Serializer.class", Stringencoder.class. GetName ());  theProperties.put ("Metadata.broker.list", "192.168.130.130:9092");//declaring Kafka broker +         return NewProducer<integer, String> (NewProducerconfig (properties));  -      }   $        $        -      Public Static voidMain (string[] args) { -         NewKafkaproducer ("Test"). Start ();//using the Kafka cluster to create a good theme test the        //System.out.println ("111"); -           Wuyi     }   the         - }   Wu   
Consumers
1  Packagecom;2 ImportJava.util.HashMap; 3 Importjava.util.List; 4 ImportJava.util.Map; 5 Importjava.util.Properties; 6   7 ImportKafka.consumer.Consumer; 8 ImportKafka.consumer.ConsumerConfig; 9 ImportKafka.consumer.ConsumerIterator; Ten ImportKafka.consumer.KafkaStream;  One ImportKafka.javaapi.consumer.ConsumerConnector;  A    -    -    the    - /**  - * Receive Data - * received by: Message:10 + received by: Message:11 - received by: Message:12 + received by: Message:13 A received by: Message:14 at  * @authorZM -  *  -  */   -  Public classKafkaconsumerextendsthread{ -    -     PrivateString topic;  in        -      Publickafkaconsumer (String topic) { to         Super();  +          This. Topic =topic;  -     }   the        *        $ @OverridePanax Notoginseng      Public voidrun () { -Consumerconnector consumer =Createconsumer ();  themap<string, integer> topiccountmap =NewHashmap<string, integer>();  +Topiccountmap.put (topic, 1);//get one data from a topic at a time AMap<string, list<kafkastream<byte[],byte[]>>> Messagestreams =Consumer.createmessagestreams (TOPICCOUNTMAP);  thekafkastream<byte[],byte[]> stream = Messagestreams.get (topic). get (0);//get the data received each time +consumeriterator<byte[],byte[]> iterator =Stream.iterator ();  -           while(Iterator.hasnext ()) { $String message =NewString (Iterator.next (). message ());  $System.out.println ("Received:" +message);  -          }   -     }   the    -     Privateconsumerconnector Createconsumer () {WuyiProperties Properties =NewProperties ();  the         //Set submission Time -Properties.put ("auto.commit.interval.ms", "1000"); WuProperties.put ("Zookeeper.connect", "192.168.130.130:2181");//declaration ZK -Properties.put ("Group.id", "group2");//You must use a different group name, and you cannot access topic data within the same group if both the producer and the consumer are in the same group About         returnConsumer.createjavaconsumerconnector (NewConsumerconfig (properties));  $      }   -        -        -      Public Static voidMain (string[] args) { A         NewKafkaconsumer ("Test"). Start ();//using the Kafka cluster to create a good theme test +            the     }   -         $}
----FIX: Turn off the Linux firewall
---/etc/init.d/iptables status gets a series of messages stating that the firewall is open
---/etc/rc.d/init.d/iptables stop to shut down the firewall
----Enter the command in the Kafka Bin directory to view the consumption status
[Email protected] bin]$/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker--zkconnect 192.168.130.130:2181-- Group Group1
Attention
Kafka Java Instance