Java Connection Kafka operation, stand-alone version Kafka
The code is recorded as follows
1. Maven Add Dependency configuration:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactid>kafka_2.10</ artifactid>
<version>0.10.2.0</version>
</dependency>
2, Java code implementation:
Package Com.sam.project.kafka;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;
Import Java.util.concurrent.TimeUnit;
Import Org.apache.kafka.common.security.JaasUtils;
Import Kafka.admin.AdminUtils;
Import Kafka.admin.RackAwareMode;
Import Kafka.consumer.Consumer;
Import Kafka.consumer.ConsumerConfig;
Import Kafka.consumer.ConsumerIterator;
Import Kafka.consumer.KafkaStream;
Import kafka.consumer.Whitelist;
Import Kafka.javaapi.consumer.ConsumerConnector;
Import Kafka.message.MessageAndMetadata;
Import Kafka.server.ConfigType;
Import Kafka.utils.ZkUtils;
Import Kafka.javaapi.producer.Producer;
Import Kafka.producer.KeyedMessage;
Import Kafka.producer.ProducerConfig;
Import Kafka.serializer.StringEncoder;
/** * Test Kafka * * public class APP {private final static String URL = "192.168.1.114:2181";
Private final static String NAME = "Test_topic"; Create theme private static void Createtopic () {zkutils zkutils = ZkutIls.apply (URL, 30000, 30000, jaasutils.iszksecurityenabled ()); Create a single partition single pair of T1 topic adminutils.createtopic (Zkutils, NAME, 1, 1, New Properties (), rackawaremode.enforced$.
module$);
Zkutils.close ();
System.out.println ("Create success!"); //delete theme (not completely removed) private static void Deletetopic () {zkutils zkutils = zkutils.apply (URL, 30000, 30000, Jaasutils.isz
Ksecurityenabled ());
Delete topic ' T1 ' adminutils.deletetopic (zkutils, NAME);
Zkutils.close ();
System.out.println ("Delete successful!"); }//modify theme private static void Edittopic () {zkutils zkutils = zkutils.apply (URL, 30000, 30000, jaasutils.iszksecurity
Enabled ());
Properties props = Adminutils.fetchentityconfig (Zkutils, Configtype.topic (), NAME);
Increase the topic level attribute Props.put ("Min.cleanable.dirty.ratio", "0.3");
Deletes the topic level attribute Props.remove ("max.message.bytes");
Modify the properties of the topic ' Test ' adminutils.changetopicconfig (zkutils, NAME, props);
Zkutils.close (); }//subject read private static void Querytopic () {zkutils zkutils = ZkutIls.apply (URL, 30000, 30000, jaasutils.iszksecurityenabled ());
Gets the Topic property property of Topic ' test ' props = Adminutils.fetchentityconfig (Zkutils, Configtype.topic (), NAME);
Query Topic-level Properties Iterator it = Props.entryset (). iterator ();
while (It.hasnext ()) {Map.entry Entry = (map.entry) it.next ();
Object key = Entry.getkey ();
Object value = Entry.getvalue ();
SYSTEM.OUT.PRINTLN (key + "=" + value);
} zkutils.close ();
/** * @Description: Producer * * private static void producer () {Properties Properties = new properties (); Properties.put ("Zookeeper.connect", "192.168.1.114:2181");/Declaration ZK Properties.put ("Serializer.class", StringEncod
Er.class.getName ()); Properties.put ("Metadata.broker.list", "192.168.1.114:9092")//Declaration Kafka broker Producer Producer = new producer<
Integer, string> (New Producerconfig (properties));
int i=0; while (true) {producer.send (new Keyedmessage<integer, string>(NAME, "message:" + i++));
try {TimeUnit.SECONDS.sleep (1);
catch (Interruptedexception e) {e.printstacktrace (); }}/** * @Description: Consumer */private static void customer () {try {properties Properties = N
EW Properties ();
Properties.put ("Zookeeper.connect", URL);
Properties.put ("Auto.commit.enable", "true");
Properties.put ("auto.commit.interval.ms", "60000");
Properties.put ("Group.id", "test_topic");
Consumerconfig consumerconfig = new Consumerconfig (properties);
Consumerconnector javaconsumerconnector = Consumer.createjavaconsumerconnector (consumerconfig);
Topic filter Whitelist Whitelist = new Whitelist ("Test_topic"); List<kafkastream<byte[], byte[]>> partitions = javaconsumerconnector. Createmessagestreamsbyfilter (
Whitelist);
if (partitions = = null) {System.out.println ("empty!");
TimeUnit.SECONDS.sleep (1);
} System.out.println ("Partitions:" +partitions.size ()); Consumer message for (kafkastream<byte[], byte[]> partition:partitions) {consumeriterator<byte[], byte[]> it
Erator = Partition.iterator ();
while (Iterator.hasnext ()) {messageandmetadata<byte[], byte[]> next = Iterator.next ();
System.out.println ("Partiton:" + next.partition ());
System.out.println ("offset:" + next.offset ());
System.out.println ("received message:" + New String (Next.message (), "Utf-8")); Exception e) {//Todo:handle Exception}} public static void Main (string[] args) {Createt
OPIc ();
Deletetopic ();
Edittopic ();
Querytopic ();
Producer ();
Customer ();
}
}
The above code implementation Java Connection Kafka theme topic operation, simple realization of producers and consumers.