See part 2 in the series.

In telecommunications, there is the term “last mile”. It refers to getting the connection to customer. It’s the last mile between the company’s infrastructure and the customer’s location.

We have similar issues in Big Data. We don’t just have a last mile problem; we have a first mile problem too. We have an issue with getting large amounts of data into our system (first mile). Then, we have an issue getting large amounts data out of our system and into the hands of our customers (last mile).

Before Kafka

Before Kafka came along, we didn’t have a great way of moving data in real-time. Apache Flume was one of the most commonly used technologies. It could accept and process the data in a limited fashion, but lacked a great place to place the data. The data was often written in real-time to a file in HDFS.

After Kafka

Now that we have Apache Kafka, we have a great of not just moving data, but storing it and processing it. Having Kafka in the technology stack is really eliminating the problems of the first and last mile.

First Mile Code

I have an extensive Kafka example in my GitHub. One of the programs is an example of the first mile code. This program takes in JMX metrics and produces them to Kafka. The main work is done in the JXMThread.java class.

Here is the code for the run method:

@Override
public void run() {
    logger.debug("Running producer thread");

    while (true) {
        try {
            String json = getJMX();

            produce(json);

            Thread.sleep(sleepTime);
        } catch (Exception e) {
            logger.error("Error in JMXThread", e);
        }

    }
}

The run method pulls the latest JMX metrics and creates a JSON string. This JSON string is then passed into the produce method. Finally, the thread sleeps before polling again.

private void produce(String json) {
    ProducerRecord<String, String> record = new ProducerRecord<String, String>(
            topic, host, json);
    producer.send(record);

    logger.debug("Produced");
}

The produce method takes in the JSON formatted string and creates a ProducerRecord object. This object wraps information about what you want to send to Kafka like the topic, key, and value. Choosing topics, keys, and values require concerted design effort.

Finally, the send method is called to send the message to Kafka.

This completes the first mile of the journey. The data is in Kafka now and we can start processing and gaining value from the data.

See part 2 in the series.

Share This