Wednesday, May 22, 2019

Installing Apache Kafka and Zookeeper on Windows

In this article, I’ll talk about how to Install, configure and start Apache Zookeeper and Apache Kafka Server on Windows OS.

Prerequisite

  • JRE running on your machine and path must set to Environment Variable
  • Any Zip tool like 7-zip, WinZip or WinRAR.
  • Download and Extract Apache Zookeeper using 7-zip.
  • Download and Extract Apache Kafka using 7-zip

ZooKeeper Installation Instructions:

  • Go to the conf directory of your Zookeeper. For me its under D:\Softwares\apache-zookeeper-3.5.5
  • Copy and rename zoo_sample.cfg to zoo.cfg file.
  • Open and Edit dataDIr=/tmp/zookeeper to dataDir=D:\Softwares\apache-zookeeper-3.5.5

  • Add entries in System Environment Variables
  • ZOOKEEPER_HOME=D:\Softwares\apache-zookeeper-3.5.5
  • Append D:\Softwares\apache-zookeeper-3.5.5\bin to PATH system variable.
  • Open command prompt and type zkserver to start the Zookeeper application.
  • You can easily edit the default port ( 2181) in zoo.cfg file.

Congratulations, ZooKeeper is up and running on port 2181.


Installing Apache Kafka Instructions
  • Please make sure that the Zookeeper instance is up and running before starting any kafka server.
  • Go to the Kafka Installation directory (D:\Softwares\apache-tomcat-8.5.37) and run below command.
  • .\bin\windows\kafka-server-start.bat .\config\server.properties

Congratulations, Apache kafka is up and running on port 9092
  

Create Topic
To create a topic, we need to run the kafka-topics script

  • Go to Apache kafka installation directory and navigate to bin\windows folder (D:\Softwares\kafka_2.11-2.2.0\bin\windows).
  • Execute below command to create topic where <TOPIC_NAME> should be name of your topic
  • kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic <TOPIC_NAME>




Create Producer

Now the topic has been created, Lets start a producer so that we can published some message against sales topic.

·       Go to Apache kafka installation directory and navigate to bin\windows folder (D:\Softwares\kafka_2.11-2.2.0\bin\windows).
·       Execute: kafka-console-producer.bat --broker-list localhost:9092 --topic sales






Create Consumer
Let’s create a consumer which should subscribe to sales topic to get all the messages.

·       Go to Apache kafka installation directory and navigate to bin\windows folder (D:\Softwares\kafka_2.11-2.2.0\bin\windows).
·       Execute: kafka-console-consumer.bat --bootstrap-server localhost:2181 --topic sales








By running all four components (zookeeper, kafka, producer, and consumer) in different terminals, we will be able to enter messages from the producer’s terminal and can see them appearing in the consumer’s terminal and If everything works fine, you will be able to push and see messages.


Bonus Installation:

There are multiple UI applications which can be use for monitoring apache kafka. It will display the information like total brokers, topics, partition and even lets you view messages as well.
One of the tools is Kafdrop which we will install right now and see all those details on UI.

Requirements:
  • Java 8
  • Kafka (0.8.1 or 0.8.2 is known to work)
  • Zookeeper (3.4.5 or later)
  • Download and Extract Kafdrop using 7-zip.
Installation
·       Go to the Kafdrop directory and run mvn clean package which will generate an executable JAR in target directory.
·       Go to target directory and run
o   java -jar ./target/kafdrop-<version>.jar --zookeeper.connect=localhost:2181
·       Open a browser and navigate to http://localhost:9000 to view the Kafdrop UI.  















Tuesday, April 23, 2019

JAVA 11 – New String methods


JAVA 11 – New String methods

In this blog, we will discuss about all six new methods which got introduced with JDK 11.

  • isBlank(): returns true if string is empty otherwise false. White spaces code will also be considered as empty string.

// Java 8 Predefine Functional Interface
             // The best way to learn JAVA 8 is to implement it wherever you can...
             BiConsumer<String, String> display = (msg1, msg2) -> System.out.println(msg1 + msg2);

             // local variable type inference
             var msg = "Welcome to waheedtechblog page";
             // String msg = "Welcome to waheedtechblog page"; both are same

             // Will return true if string is empty or any white spaces
             if (msg.isBlank()) {
                    display.accept("msg attribute is emty", msg);
             } else {
                    display.accept("1. Hello, ", msg);
             }

String emptyMsg = "";
display.accept("2. Is msg Empty: ", String.valueOf(emptyMsg.isBlank()));

             String whitespacesMsg = "\n\t";
display.accept("2. Is msg empty: ", String.valueOf(whitespacesMsg.isBlank()));
Output:
            1. Hello, Welcome to waheedtechblog page
2. Is msg Empty: true
3. Is msg empty: true

  • lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as \n, \r etc.

            String lineMsg = "Welcome\nto\nwaheedtechblog\npage";
       display.accept("3. Line Msg: \n", lineMsg);
       // line returns streams of line extracted from the String
       List<String> lines = lineMsg.lines().collect(Collectors.toList());
       System.out.println("4. line as list: " + lines);
Output:
       3. Line Msg:
Welcome
to
waheedtechblog
page
4. line as list: [Welcome, to, waheedtechblog, page]

  • repeat(n): This method returns the concatenated string of original string repeated the number of times in the argument.

var repeatMessage = "Welcome";
// returns a new string whose value is the concatenation of this string repeated
             // ‘n’ times.
             display.accept("9. Repeat msg: ", repeatMessage.repeat(5));
Output:
9. Repeat msg: WelcomeWelcomeWelcomeWelcomeWelcome

  • Strip(), StripLeading(),StripTrailing(): These method are used to strip white spaces from the String

                   var stripMsg = "    Welcome to   \t Waheedtechblog page     ";
             display.accept("5. Normal Print: ", "$" + stripMsg + " $");
             // As the name suggests, Strip() methods are used to strip whitespaces from the
             // string.
             display.accept("6. Using Strip(): ", "$" + stripMsg.strip() + "$");
             display.accept("7. Using StripLeading(): ", "$" + stripMsg.stripLeading() + "$");
display.accept("8. Using StripTrailing: ", "$" + stripMsg.stripTrailing() + "$");

Output:
5. Normal Print: $    Welcome to        Waheedtechblog page      $
6. Using Strip(): $Welcome to    Waheedtechblog page$
7. Using StripLeading(): $Welcome to    Waheedtechblog page     $
8. Using StripTrailing: $    Welcome to        Waheedtechblog page$

You can download the source code from my GitHub repository.

Happy Coding…!!!

Sunday, April 21, 2019

Java 10 - Local Variable Type Inference


Java 10 - Local Variable Type Inference

In one of my previous blog, I have already discussed about Type Inference. If you don’t have the idea about Type Inference then you can check my blog here.

If we want to understand Type Inference in one line then we can say that It is the capability of the compiler to automatically detect the datatype of a variable at the compiler time.

What is Local Variable type inference?

Java 10 added new feature that allows the developer to skip the type declaration associated with local variables (those defined inside method definitions, initialization blocks, for-loops, and other blocks like if-else), and the type is inferred by the JDK. It will, then, be the job of the compiler to figure out the datatype of the variable.
Until Java 9, we had to define the type of the local variable.

E.g.: String message = “Welcome to Waheedtechblog.com”;

The above statement is right as that’s how things have been since the inception of java but if you observe, the type of the object at right side is clearing mentioning the type of data that we have defined at left side which makes the variable redundant.
So with Java 10, We can declare a local variable without defining its data type at left side.

            Var message = “Welcome to Waheedtechblog.com”;

Over here, we don’t have to provide the data type of String to local variable message. Compiler will get the datatype based on right hand side value.

Note:
  • This feature is available only for local variables with the initializer. It cannot be used for member variables, method parameters, return types, etc as the initializer is required as without which compiler won’t be able to infer the type.
  • To support backward compatibility, var is not a java reserved keyword. So, we can create variable with name var as it is allowed.

Let’s see where all we can use var

package com.waheedtechblog.typeinference;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class TypeInferenceExample {

       static {
             // static block variable
             var staticVaribale = "var in static block";
             Consumer<String> displayOnConsole = msg -> {
                    System.out.println(msg);
             };
             displayOnConsole.accept(staticVaribale);

       }

       public static void main(String[] args) {

             TypeInferenceExample inferenceExample = new TypeInferenceExample();

             BiConsumer<String, String> displayOnConsole = (str, msg) -> {
                    System.out.println(str + msg);
             };

             // Before Java 10
             String message = "Welcome to Waheedtechblog.com";
             displayOnConsole.accept("Before JDK 10: ", message);

             // Using JDK 10, Local Variable
             var message10 = "Welcome to Waheedtechblog.com";
             displayOnConsole.accept("With JDK 10: ", message);

             // local variable declaration in enhanced loops
             String[] countryName = { "India", "Japan", "UAE", "USA", "UK" };
             displayOnConsole.accept("", "List of Country using enhanced loop");
             for (var country : countryName) {
                    displayOnConsole.accept("", country);
             }

             // basic for loop
             displayOnConsole.accept("", "List of Country using basic loop");
             for (var i = 0; i < countryName.length; i++) {
                    displayOnConsole.accept("", countryName[i]);
             }

             displayOnConsole.accept("Length of string '" + message10 + "' is: ",
                           String.valueOf(inferenceExample.getLenth(message10)));


             // var is not keyword, so you can create variable with name var as well.
     String var = "var is not keyword";
     displayOnConsole.accept("", var);
       }

       // can return var type as well
       private int getLenth(String msg) {
             var length = msg.length();
             return length;
       }

}

Output:
var in static block
Before JDK 10: Welcome to Waheedtechblog.com
With JDK 10: Welcome to Waheedtechblog.com
List of Country using enhanced loop
India
Japan
UAE
USA
UK
List of Country using basic loop
India
Japan
UAE
USA
UK
Length of string 'Welcome to Waheedtechblog.com' is: 29
var is not keyword

Illegal use of Var:

1.    Can’t use as a class field
Class Test {
            Var msg; // not allowed as it is local variable inference
}
2.    Local variable without initialization
Private void getOTP(){
            Var otp ; // cant use without initializer
}
3.    Not allowed as parameter for any methods
Private void getOTP(var secretKey){
            //Error, cannot use var on method parameter
}
4.    Not permitted in method return type
Private var getOTP(String secretKey){
            //Error, Method return type cannot be var
}
5.    Not permitted with variable initialized with ‘NULL’
Private String getOTP(String secretKey){
            Var msg = null; Error, cant initialize as null
}

You can download the source code from my GitHub repository.

Happy Coding..!!!









How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...