Kafka in Windows installation run and Getting Started instance (JAVA) __java

Source: Internet
Author: User
Tags serialization zookeeper

First, install JDK and zooeleeper here omitted

Second, installation and Operation Kafka

Download

Http://kafka.apache.org/downloads.html

After the download to any directory, the author is D:\Java\Tool\kafka_2.11-0.10.0.1

1. Enter the Kafka configuration directory, D:\Java\Tool\kafka_2.11-0.10.0.1
2. Edit the file "Server.properties"
3. Find and edit Log.dirs=d:\java\tool\kafka_2.11-0.10.0.1\kafka-log, here's the list of their own modified into their favorite
4. Find and edit zookeeper.connect=localhost:2181. Indicates local run
5. Kafka will run on the 9092 port by default and connect the zookeeper default port: 2181.

Run:

Important: Make sure that the zookeeper instance is ready and starting to run before starting the Kafka server.

1. Enter Kafka installation directory D:\Java\Tool\kafka_2.11-0.10.0.1
2. Press the shift+ right button and select the "Open command Window" option to open the command line.
3. Enter now

. \bin\windows\kafka-server-start.bat. \config\server.properties  

and enter.

Third, testing

The zookeeper and Kafka on the top are always open.

(1), create theme 1. Enter Kafka installation directory D:\Java\Tool\kafka_2.11-0.10.0.1
2. Press the shift+ right button and select the "Open command Window" option to open the command line.
3. Enter now

. \bin\windows\kafka-topics.bat--create--zookeeper localhost:2181--replication-factor 1--partitions 1--topic linlin  


Be careful not to close the window.

(2) Creating producers

1. Enter Kafka installation directory D:\Java\Tool\kafka_2.11-0.10.0.1
2. Press the shift+ right button and select the "Open command Window" option to open the command line.
3. Enter now

. \binwindows\kafka-console-producer.bat--broker-list localhost:9092--topic Linlin


Be careful not to close the window.

(3) Create consumer

1. Enter Kafka installation directory D:\Java\Tool\kafka_2.11-0.10.0.1
2. Press the shift+ right button and select the "Open command Window" option to open the command line.
3. Enter now

. \bin\windows\kafka-console-consumer.bat--zookeeper localhost:2181--topic Linlin


Be careful not to close the window.

then enter the contents in window 2nd, and finally remember to return


Iv. Examples of Kafka entry

1. The entire project catalogue is as follows:


2.pom file

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < Modelversion>4.0.0</modelversion> <groupId>com.lin</groupId> <artifactid>kafka-demo </artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <grou Pid>org.apache.kafka</groupid> <artifactId>kafka_2.10</artifactId> <version>0.8.2.0 </version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifacti d>zkclient</artifactid> <version>0.10</version> </dependency> <dependency> ;groupid>com.alibaba</groupid> <artifactId>fastjson</artifactId> <version>1.2.4</ version> </dependency> </dependencies> &LT;/PROJECT&GT 

3. Producer Kafkaproducer

Package com.lin.demo.producer;
Import Kafka.javaapi.producer.Producer;
Import Kafka.producer.KeyedMessage;
Import Kafka.producer.ProducerConfig;

Import java.util.Properties;
    public class Kafkaproducer {private Final producer<string, string> Producer;

    Public final static String TOPIC = "Test-topic";
        Private Kafkaproducer () {Properties props = new properties ();

        The Kafka Port Props.put ("Metadata.broker.list", "localhost:9092") are configured here;
        Configures the serialization class Props.put for value ("Serializer.class", "Kafka.serializer.StringEncoder");

        Configures the serialization class Props.put ("Key.serializer.class", "Kafka.serializer.StringEncoder") of the key;

        Props.put ("Request.required.acks", "-1");
    Producer = new producer<string, string> (new Producerconfig (props));
        } void produce () {int messageno = 1000;

        Final int COUNT = 10000;
         while (Messageno < COUNT) {String key = string.valueof (Messageno);   String data = "Hello Kafka message" + key;
            Producer.send (New keyedmessage<string, string> (TOPIC, key, data));
            SYSTEM.OUT.PRINTLN (data);
        messageno++;
    } public static void Main (string[] args) {new Kafkaproducer (). produce ();
 }
}

Run, Result:

4. Consumer Kafkaconsumer

Package Com.lin.demo.consumer;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;
Import Com.lin.demo.producer.KafkaProducer;
Import Kafka.consumer.ConsumerConfig;
Import Kafka.consumer.ConsumerIterator;
Import Kafka.consumer.KafkaStream;
Import Kafka.javaapi.consumer.ConsumerConnector;
Import Kafka.serializer.StringDecoder;
Import kafka.utils.VerifiableProperties;
 /** * Created by Yz.shi on 2018/4/11.

    * * Public class Kafkaconsumer {private final consumerconnector consumer;
        Private Kafkaconsumer () {Properties props = new properties ();

        Zookeeper Configuration props.put ("Zookeeper.connect", "localhost:2181");

        Group represents a consumer group Props.put ("Group.id", "Jwd-group");
        ZK Connection Timeout Props.put ("zookeeper.session.timeout.ms", "4000");
        Props.put ("zookeeper.sync.time.ms", "200");
        Props.put ("Rebalance.max.retries", "5");
        Props.put ("rebalance.backoff.ms", "1200"); prOps.put ("auto.commit.interval.ms", "1000");
        Props.put ("Auto.offset.reset", "smallest");

        Serialization class Props.put ("Serializer.class", "Kafka.serializer.StringEncoder");

        Consumerconfig config = new Consumerconfig (props);
    Consumer = kafka.consumer.Consumer.createJavaConsumerConnector (config);
        } void Consume () {map<string, integer> topiccountmap = new hashmap<string, integer> ();

        Topiccountmap.put (Kafkaproducer.topic, New Integer (1));
        Stringdecoder Keydecoder = new Stringdecoder (new Verifiableproperties ());

        Stringdecoder Valuedecoder = new Stringdecoder (new Verifiableproperties ()); Map<string, list<kafkastream<string, string>>> consumermap = Consumer.createmessagestrea
        MS (Topiccountmap,keydecoder,valuedecoder);
        kafkastream<string, string> stream = Consumermap.get (kafkaproducer.topic). Get (0); Consumeriterator<string, string> it = StreaM.iterator (); while (It.hasnext ()) System.out.println ("<<<<<<<<<<<<<<<<<& lt;<<<<<<<<<<<<<<<<<<<<<<< "+ it.next (). Message () + <<<<<<<<<<<<<<<<<<<<<<<<
    <<<<<<<<<<<<<<<<< ");
    public static void Main (string[] args) {new Kafkaconsumer (). consume ();
 }
}

Run Result:


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.