Thursday, 15 February 2018

Apache Nifi - I

In this post, we will look at another tool, Apache Nifi, that is touted as supporting powerful and scalable directed graphs of data routing, transformation, and system mediation logic. More details on Apache Nifi can be found here. One of the outstanding features of this tool is the web interface that can be used for design, control, feedback, and monitoring purposes. All ETL experts out there coming from "small data" background and familiar with tools like Informatica PowerCenter, Oracle Data Integrator Enterprise Edition, Microsoft SSIS, IBM InfoSphere DataStage, etc will enjoy working on Apache Nifi. Apache NiFi is based on technology previously called "Niagara Files". Hence, the name Nifi that is short for "Niagara Files"

The first post on this topic is dedicated to installation. This is in line with our strategy - we try to understand tools by starting from scratch and not overwhelm the readers with complex configurations on the first run. We will then try to add more concepts and features of the product in latter posts. We will install Apache Nifi on Windows and we will use this environment for all the work in this post. One of the prerequisites for installation of Nifi is Java. Also, make sure the JAVA_HOME of PATH Environment variables are set correctly to point to the Java installation. You can make sure it is installed by running below command:

java -version









The binaries for Apache Nifi can be downloaded here. We will work with the latest release, 1.5.0. On this page, click here to download the binaries. After downloading nifi-1.5.0-bin.zip, drop it into a folder called nifi as shown below:









 Right click on the file click on Extract all ...

















Change the installation directory to D:\ as shown below and click on Extract:



















The files are extracted as shown below:




















Navigate as shown below to see the installed folders:










On the command line, enter below commands to run Nifi:

D:\>
D:\>cd D:\nifi-1.5.0\bin\
D:\nifi-1.5.0\bin>run-nifi











A series of output statements are seen that will end with s line like "INFO [main] org.apache.nifi.bootstrap.Command Launched Apache NiFi with Process ID 9048" as shown below:


















This means that Nifi is up and running. One can verify that Nifi is running by running status command as follows:

D:\>
D:\>cd D:\nifi-1.5.0\bin\
D:\nifi-1.5.0\bin>status-nifi









A series of output statements are seen that will end with s line like "[main] INFO org.apache.nifi.bootstrap.Command - Apache NiFi is currently running, listening to Bootstrap on port 49643, PID=9048" as shown below:



















Lastly, navigate to http://localhost:8080/ to open the Nifi Designer window as shown below:


















Click on nifi as a final step to see below application:














Alternately, you can navigate to http://localhost:8080/nifi/ directly. 

To bring down the application, click CTRL+C on the window where the application was started.

This complete the installation steps for Apache Nifi on Windows

Sunday, 4 February 2018

Apache Kafka - IV

In the third part of Apache Kafka, we will write a producer in Java. We will use the same environment that we set up in this post.

In addition, we are using IntelliJ IDEA, Community Edition (Version: 2017.3.4, Build: 173.4548.28, Released: January 30, 2018) as IDE for the Java program. A screenshot is shown below:














In this post, we will write a producer in Java. This will read a file and publish the contents to a topic. A consumer will subscribe to this topic and output the message to the console

We invoke zookeeper and kafka as shown below. Then, we create a topic called topic1:

Start Zookeeper:

Open first terminal:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\zookeeper-server-start.bat config\zookeeper.properties

Start Kafka:

Next, open another terminal and start Kafka as shown below:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\kafka-server-start.bat config\server.properties


Create Topic:

In a third terminal, let us now proceed to create a topic called topic1 having a replication factor as 1 and number of partitions as 1:

F:\>
F:\>cd kafka_2.12-1.0.0F:\kafka_2.12-1.0.0>bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic topic1 

Write below code in Intellij IDEA and run it:

import java.util.Properties;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

public class customProducer {
    private static final String topic= "topic1";
    public static void main(String[] args) {
        String fileName = "C:\\kafka_2.12-1.0.0\\NOTICE";

        Properties properties = new Properties();

        // kafka bootstrap server properties        properties.setProperty("bootstrap.servers", "127.0.0.1:9092");
        properties.setProperty("key.serializer", StringSerializer.class.getName());
        properties.setProperty("value.serializer", StringSerializer.class.getName());

        // producer properties        properties.setProperty("acks", "1");
        properties.setProperty("retries", "3");
        properties.setProperty("linger.ms", "1");

        KafkaProducer<String,String> producer = 
                                 new org.apache.kafka.clients.producer.KafkaProducer<String,String>(properties);

        try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

            String line;
            while ((line = br.readLine()) != null) {
                ProducerRecord<String,String> producerRecord =
                        new ProducerRecord<String,String>(topic, line);
                producer.send(producerRecord);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        producer.close();
    }
}


Above code reads the NOTICE file under Kafka Home and publishes the file contents to topic called topic1. In a way, the code above simulates the FileSource Connector under Kafka Connect in the sense that it reads a file and publishes to a topic

Next, we start a consumer to receive the messages published to the topic1:

Start Consumer:

On another, start a console consumer as follows:

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic topic1 --from-beginning

Output is shown below:




 








The contents to NOTICE file is shown below for reference:


















The messages are identical to the contents of file above
 

Sunday, 28 January 2018

Apache Kafka - III

In the third part of Apache Kafka, we continue with more commands. We will use the same environment that we set up in this post

Let us restart Zookeeper and Kafka we have done in previous post with below commands:

Start Zookeeper:

Open first terminal:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\zookeeper-server-start.bat config\zookeeper.properties

Start Kafka:

Next, open another terminal and start Kafka as shown below:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\kafka-server-start.bat config\server.properties


Create Topic:

In a third terminal, let us now proceed to create a topic called topic1 having a replication factor as 1 and number of partitions as 3:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic topic1

Start Producer:

In a fourth terminal, let us start a producer with below command as shown:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic topic1


In a fifth terminal, increase the retention time (in milliseconds) of messages as shown below:

F:\>
F:\>cd kafka_2.12-1.0.0

F:\kafka_2.12-1.0.0>bin\windows\kafka-configs --zookeeper localhost:2181 --entity-type topics --entity-name topic1 --alter --add-config retention.ms=1800000

In the fourth terminal, add the following messages:

>Message 1
>Message 2
>Message 3
>Message 4
>Message 5
>Message 6
>Message 7
>Message 8
>Message 9
>Message 10
>Message 11
>Messgae 12
>Message 13


Start Consumer:

On a sixth terminal, start a console consumer as follows:

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic topic1 --from-beginning

Output is shown below:

F:\kafka_2.12-1.0.0>bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic topic1 --from-beginning
Message 1
Message 4
Message 7
Message 10
Message 13
Message 2
Message 5
Message 8
Message 11
Message 3
Message 6
Message 9
Messgae 12


Note that the order of messages is not the same.  You can see the messages through the command we ran in the post:

F:\kafka_2.12-1.0.0>bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic topic1 --from-beginning
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
Message 2
Message 5
Message 8
Message 11
Message 3
Message 6
Message 9
Messgae 12
Message 1
Message 4
Message 7
Message 10
Message 13


To see the messages in a partition, run below command:

bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic topic1 --partition 0 --from-beginning

Output is shown below:

F:\kafka_2.12-1.0.0>bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic topic1 --partition 0 --from-beginning
Message 2
Message 5
Message 8
Message 11


Note that the messages within a partition are ordered 

This concludes the third post on Apache Kafka