List

Apache Kafka is a highly-scalable publish-subscribe messaging system that can serve as the data backbone in distributed applications. With Kafka’s Producer-Consumer model it becomes easy to implement multiple data consumers that do live monitoring as well persistent data storage for later analysis.

STEP 1: Installation, the best way to install the latest version of the Kafka server on OS X and to keep it up to date is via Homebrew.

1
$ brew install kafka

this will also install all dependencies, like zookeeper which is required to run the server.

STEP 2: we need to start Zookeeper before we can start Kafka.

1
$ zkserver start

start_zookeeperSTEP 3: Once Zookeeper is up start the Kafka server.

1
$ kafka-server-start.sh /usr/local/etc/kafka/server.properties

kafka_start

STEP 4: Start a consumer, Kafka also has a command line consumer that will dump out messages to standard out.

1
2
$ kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
Sending a message in Kafka

STEP 5: Send some messages, Kafka comes with a command line client producer that will take input from console and send it out as messages to the Kafka cluster. By default each line will be sent as a separate message. The topic test is created automatically when messages are sent to it. Omitting logging you should see something like this:

1
2
$ kafka-console-producer.sh --broker-list localhost:9092 --topic test
Sending a message in Kafka

messages

Run each of the above commands in different terminal, then you should now be able to type messages into the producer terminal and see them appear in the consumer terminal. Both of these command line tools have additional options.

STEP 6: Setting up a multi-broker cluster

So far we have been running against a single broker, but that’s no fun. For Kafka, a single broker is just a cluster of size one, so nothing much changes other than starting a few more broker instances. But just to get feel for it, let’s expand our cluster to three nodes (still all on our local machine).

First we make a config file for each of the brokers:

1
2
cp config/server.properties config/server-1.properties
cp config/server.properties config/server-2.properties

Now edit these new files and set the following properties:

1
2
3
4
5
6
7
8
9
config/server-1.properties:
    broker.id=1
    port=9093
    log.dir=/tmp/kafka-logs-1
 
config/server-2.properties:
    broker.id=2
    port=9094
    log.dir=/tmp/kafka-logs-2

The broker.id property is the unique and permanent name of each node in the cluster. We have to override the port and log directory only because we are running these all on the same machine and we want to keep the brokers from trying to all register on the same port or overwrite each others data.

We already have Zookeeper and our single node started, so we just need to start the two new nodes. However, this time we have to override the JMX port used by java too to avoid clashes with the running node:

1
2
3
4
JMX_PORT=9997 bin/kafka-server-start.sh config/server-1.properties
...
JMX_PORT=9998 bin/kafka-server-start.sh config/server-2.properties
...

Now create a new topic with a replication factor of three:

1
bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 3 --partition 1 --topic my-replicated-topic

Okay but now that we have a cluster how can we know which broker is doing what? To see that run the “list topics” command:

1
2
3
bin/kafka-list-topic.sh --zookeeper localhost:2181
topic: my-replicated-topic  partition: 0  leader: 1  replicas: 1,2,0  isr: 1,2,0
topic: test                 partition: 0  leader: 0  replicas: 0      isr: 0

Here is an explanation of output:

  • “leader” is the node responsible for all reads and writes for the given partition. Each node would be the leader for a randomly selected portion of the partitions.
  • “replicas” is the list of nodes that are supposed to server the log for this partition regardless of whether they are currently alive.
  • “isr” is the set of “in-sync” replicas. This is the subset of the replicas list that is currently alive and caught-up to the leader.

Note that both topics we created have only a single partition (partition 0). The original topic has no replicas and so it is only present on the leader (node 0), the replicated topic is present on all three nodes with node 1 currently acting as leader and all replicas in sync.

As before let’s publish a few messages message:

1
2
3
4
5
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my-replicated-topic
...
my test message 1
my test message 2
^C

Now consume this message:

1
2
3
4
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2

Now let’s test out fault-tolerance. Kill the broker acting as leader for this topic’s only partition:

1
pkill -9 -f server-1.properties

Leadership should switch to one of the slaves:

1
2
3
4
bin/kafka-list-topic.sh --zookeeper localhost:2181
...
topic: my-replicated-topic  partition: 0    leader: 2   replicas: 1,2,0 isr: 2
topic: test partition: 0    leader: 0   replicas: 0 isr: 0

And the messages should still be available for consumption even though the leader that took the writes originally is down:

1
2
3
4
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic my-replicated-topic
...
my test message 1
my test message 2

The post Installing Kafka on Mac OSX appeared first on The Big Data Blog.

Source: Installing Kafka on Mac OSX

Leave a Reply

Your email address will not be published. Required fields are marked *

  Posts

1 2 3
February 17th, 2016

Kaggle Competition Past Winner Solutions

We learn more from code, and from great code. Not necessarily always the 1st ranking solution, because we also learn […]

February 7th, 2016

Installing Kafka on Mac OSX

Apache Kafka is a highly-scalable publish-subscribe messaging system that can serve as the data backbone in distributed applications. With Kafka’s […]

February 5th, 2016

Lucene In-Memory Search Example and Sample Code

More sample code: https://github.com/fnp/pylucene/tree/master/samples/LuceneInAction  Sample code import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.*; […]

February 5th, 2016

PYLUCENE 3.0 IN 60 SECONDS — TUTORIAL and SAMPLE CODE

I pro­vide a basic index­ing and retrieval code using the PyLucene 3.0 API.Lucene In Action (2nd Ed) cov­ers Lucene 3.0, but […]

January 29th, 2016

NiFi: Thinking Differently About DataFlow

Recently a question was posed to the Apache NiFi (Incubating) Developer Mailing List about how best to use Apache NiFi […]

January 29th, 2016

Apache Nifi (aka HDF) data flow across data center

Short Description: This article provides a step by step overview of how to setup cross data center data flow using […]

January 24th, 2016

Accurately Measuring Model Prediction Error

When assessing the quality of a model, being able to accurately measure its prediction error is of key importance. Often, […]

January 9th, 2016

TIME SERIES FORECASTING – TAKING KAGGLE ROSSMANN CHALLENGE AS EXAMPLE

A time series is a sequence of data points, typically consisting of successive measurements made over a time interval. Forecasting […]

January 7th, 2016

Getting Started with Markov Chains

There are number of R packages devoted to sophisticated applications of Markov chains. These include msm and SemiMarkov for fitting […]

December 26th, 2015

Hadoop filesystem at Twitter

Twitter runs multiple large Hadoop clusters that are among the biggest in the world. Hadoop is at the core of […]