Code Monkey home page Code Monkey logo

as_spark_kafka_gradle's Introduction

kafka producer and consumer example in scala and java

This is tested locally with

  Gradle 2.0, 
  Java 1.8.0_65
  Scala 2.11.7
  Zookeeper 3.4.6
  Kafka 2.10-0.9.0.0
  Spark 1.6.0       : https://jaceklaskowski.gitbooks.io/mastering-apache-spark/content/spark-building-from-sources.html

start zookeeper

if you have installed zookeeper, start it, or run the command:

bin/zkServer.sh start conf/zoo.cfg

start kafka with default configuration

> bin/kafka-server-start.sh config/server.properties

create a topic

> bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 10 --topic test_topic

Download and package this example

> git clone https://github.com/iamon3/as_spark_kafka_gradle.git
> cd as_spark_kafka_gradle
> gradle clean build

it will package compiled classes and its dependencies into a jar.

cd into jar location

> cd build/lib/

run the consumer

This example contains two consumers written in Java and in scala. You can run this for java:

> java -cp kafka_example-0.1.0-SNAPSHOT.jar com.colobu.kafka.ConsumerExample localhost:2181 group1 test_topic 10 0

or this for scala:

> java -cp kafka_example-0.1.0-SNAPSHOT.jar com.colobu.kafka.ScalaConsumerExample localhost:2181 group1 test_topic 10 0

Stop the consumer Ctrl+C

Deploy and run the consumer in Spark

> cp kafka_example-0.1.0-SNAPSHOT.jar ${SPARK_INSTALLATION_DIR}/bin
> cd ${SPARK_INSTALLATION_DIR}/bin
> sh spark-submit --class com.colobu.kafka.ScalaConsumerExample \
> --master local[8] \
> kafka_example-0.1.0-SNAPSH.jar localhost:2181 group1 test_topic 10 0 group1 test_topic 10 0

Push messages to Kafka

> ${KAFKA_INSTALLTION_DIR}/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test_topic 
> Hi
> Hello
> What's Up.

These messages will be consumed and will be displayed on spark terminal.

run the producer

This example also contains two producers written in Java and in scala. you can run this for java:

> java -cp kafka_example-0.1.0-SNAPSHOT.jar com.colobu.kafka.ProducerExample 10000 colobu localhost:9092

or this for scala

> java -cp kafka_example-0.1.0-SNAPSHOT.jar com.colobu.kafka.ScalaProducerExample 10000 colobu localhost:9092

as_spark_kafka_gradle's People

Contributors

iamon3 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.