Friday, October 12, 2018

Steps To Build Web Server Docker Image - 2 min job


I assume you must be knowing what is Docker, Container and their concept.
Here I am giving only basic steps to develop Docker Image for web based application. This tutorial will give you very basic temple on how to develop web based Docker Image.

Please use comment box if you have any question or suggestion.

Step 1: Create one HTML file, say index.html


Binod Suman

This web page from Docker / Kubernetes

Current time




Step 2: Create one Docker file


FROM centos:latest
RUN yum -y install httpd
COPY index.html /var/www/html/
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]
EXPOSE 80



Step 3: Build Docker Image, use below command
docker build -t hello-web .
NOTE: one dot is there at last in above command. I assume index.html and Docker are in the folder that why I used dot else you can give path of Dockerfile as well.

Now your Docker Image ready to use. How to use:

docker run -p 3123:80 -it hello-web:latest

Here 3123 is host machine port and 80 port where application is running in container.

Go your web brower:
http://locahost:3123

You will get output, like some thing:

This web page from Docker / Kubernetes

Current time

10/12/2018, 11:58:36 PM

I have already push this image to docker hub, you can directly pull from there:
docker pull binodsuman/hello-web
docker run -p 3123:80 -it binodsuman/hello-web:latest





Friday, February 23, 2018

Connect Amazon S3 from Java code. Amazon S3 with Java. Amazon S3 Java code example using the AWS SDK. Upload single or multiple object using AWS Java SDK


Login to aws.amazon.com
Create one user from IAM and give access these things:
AWSConnector
AmazonS3FullAccess

And copy "Access Key" and "Secret Access Key" and it will using in your java code to establish connection.

Create one Maven Project in any Eclipse and add this below one dependency:


    com.amazonaws
    aws-java-sdk
    1.9.2


Then use below code to create bucket, create folder and upload some text file.

package AmazonS3.amazondemo;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;

public class App
{

static String accessKey = "**********************************";
static String secretAccessKey = "********************************************";


    public static void main( String[] args )
    {
        System.out.println( "Demo for Amazon S3 connectivity from Standalone Java code!" );
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretAccessKey);
       
        AmazonS3 amazonS3Client = new AmazonS3Client(awsCredentials);
       
        System.out.println("Bucket is being created ..................");
        String bucketName = "bucktbyjavacode";
        amazonS3Client.createBucket(bucketName);
       
        for(Bucket bucket : amazonS3Client.listBuckets()){
        System.out.println("Bucket in Amazon S3 :"+bucket.getName());
        }
       
        System.out.println("Folder is being created ................");
        String newFolderName = "Folder_by_Java_Code";
        App demo = new App();
        //demo.createNewFolder(bucketName, newFolderName, amazonS3Client);
        String fileName = "demotextfile.txt";
        amazonS3Client.putObject(new PutObjectRequest(bucketName, fileName, new File("D:\\demotextfile.txt")));
       
        System.out.println("All Done ............");
       
    }
   
    private void createNewFolder(String bucketName, String newFolderName, AmazonS3 amazonClient){
    ObjectMetadata metaData = new ObjectMetadata();
    metaData.setContentLength(0);
   
    InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
   
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, newFolderName, emptyContent, metaData);
   
    amazonClient.putObject(putObjectRequest);
    }
}

Thursday, February 15, 2018

How to Call R from Java using Rserve, R Programming through Java



We will use Rserve software to connect our Java code to R environment. Rserve is a TCP/IP server which allows other programs to use facilities of R  from various languages without the need to initialize R or link against R library. Every connection has a separate workspace and working directory. For more details please refer this page https://www.rforge.net/Rserve/

  1. Install R software in your computer somewhere. (My case it is D:\InstalledSoftware\R-3.4.3)
  2. Start your R environment
  3. Install Rserve package (use command install.packages("Rserve"))
  4. Start Rserve in your R environment (use command library(Rserve))
  5. That’s it from R side.
  6. Now setup your Java project.
  7. Open any Eclipse, create java project say "RJavaConnect"
  8. Add two Rserve related jar file in your RJavaConnect project (REngine.jar and Rserve.jar)
  9. You can get both jar from D:\InstalledSoftware\R-3.4.3\library\Rserve\java (In your case check your R installation path). Else you can add these two dependency in your pom.xml
               
    org.nuiton.thirdparty
      REngine
        1.8-5



                org.nuiton.thirdparty
                  Rserve
                    1.8-5
                         Finally, create one Java Class say Demo.java and execute below code. That’s it :)

                      package pkg;

                      import org.rosuda.REngine.REXP;
                      import org.rosuda.REngine.REXPMismatchException;
                      import org.rosuda.REngine.REngineException;
                      import org.rosuda.REngine.Rserve.RConnection;
                      import org.rosuda.REngine.Rserve.RserveException;



                      public class Demo {

                      RConnection connection = null;

                      public static void main(String[] args) {
                      Demo demo = new Demo();
                      demo.connectToR();
                      }

                      public void connectToR(){

                      try{
                      connection = new RConnection();
                      String vector = "c(1,2,3,4)";
                                  connection.eval("meanVal=mean(" + vector + ")");
                                  double mean = connection.eval("meanVal").asDouble();
                                  System.out.println("The mean of given vector is=" + mean);
                                 
                                   /*
                                   String setwd = "setwd(\"d:/InstalledSoftware/RStudio\")";
                                   connection.eval(setwd);
                                  
                                   vector = "binod<-c p="">
                                   connection.eval(vector);*/
                                 
                                  int[] input = {10,20,30,40,50};
                                  connection.assign("input", input);
                                  REXP output = connection.eval("mean(input)");
                                  System.out.println("Mean of out input data :"+output);
                                  System.out.println("Mean of out input data :"+output.asDouble());
                                 
                                  }catch (RserveException e) {
                                    e.printStackTrace();
                                 }catch (REXPMismatchException e) {
                                  e.printStackTrace();
                                } catch (REngineException e) {
                                     e.printStackTrace();
                                 }finally{
                                       if(connection != null){
                                    connection.close();
                                  }
                               } 
                           }


                      }

                      Saturday, June 18, 2016

                      java.util.Optional, Tired of Null Pointer Exceptions, get rid of Null Pointer Exceptions, Handle NullPointerException in Java


                      See this below java code to add two Integer:

                      public Integer sumOfTwoInteger(Integer a, Integer b){
                            return a + b;
                         }


                      Here, sumOfTwoInteger() method at all not taking care of any of the method arguments, this method will be executed either you pass any valid Integer value or any null value. Even, from where (caller method) you will call this sumOfTwoInteger() method, caller method will be also taking care for data is going to pass to sumOfTwoInteger() method. Finally ends up with this below error message:

                      Integer a = null;
                      Integer b = 10;

                      System.out.println(test.sumOfTwoInteger(a,b));

                      Exception in thread "main" java.lang.NullPointerException
                      at Tester.sumOfTwoInteger(GuavaTester.java:19)
                      at Tester.main(GuavaTester.java:14)

                      Now, how to handle this kind of situation where null check is to be check always and each and every place, before calling method or during method is being executed.

                      User java.util.Optional package.

                      import java.util.Optional;


                      public class Tester {
                         public static void main(String args[]) {
                            Tester guavaTester = new Tester();
                           
                            Integer invalidInput = null;
                           
                           
                            Optional a =  Optional.of(invalidInput);
                            Optional b =  Optional.of(new Integer(10));
                             
                            System.out.println(guavaTester.sumOfTwoInteger(a,b));
                         }

                         public Integer sumOfTwoInteger(Optional a, Optional b){
                           return a.get() + b.get();
                        }

                      }

                      Now, when you will run this code, it will give error at line, where you are trying to create Integer value a.

                      Exception in thread "main" java.lang.NullPointerException
                      at java.util.Objects.requireNonNull(Unknown Source)
                      at java.util.Optional.(Unknown Source)
                      at java.util.Optional.of(Unknown Source)
                      at Tester.main(Tester.java:11)



                      More details at http://www.tutorialspoint.com/guava/index.htm





                      Thursday, April 28, 2016

                      Create index in kibana using curl command, Index creation in Kibana by code, Kibana index by command, create index in Kibana in background


                      Your ElasticSearch Servar and Kibana server should be running during this poc.

                      Just assume,

                      Kibana is running at 10.20.30.40:5601


                      First requirement, you should have same index in your Elasticsearch for which you wanted to create index in Kibana.
                      If you do not have any index in Elasticsearch then first create it.

                      {
                       "_index": "twiteer_one",
                                  "_type": "tweet",
                                  "_id": "2",
                                  "_score": 1,
                                  "_source": {
                                     "nick": "Ishan@gmail.com",
                                     "date": "2015-02-10",
                                     "post": "NCFE",
                                     "other_name": "Ishan Suman"
                                  }
                               },
                               {
                                  "_index": "twiteer_one",
                                  "_type": "tweet",
                                  "_id": "17",
                                  "_score": 1,
                                  "_source": {
                                     "nick": "@binodsuman",
                                     "date": "2014-08-15",
                                     "post": "BigData, Kibana, ES, ML",
                                     "other_name": "Binod Suman"
                                  }
                      }

                      I hope your Elasticsearch must be running while using below command.

                      Now suppose you want to create index with name twiteer_one in your kibana, then use this below command in lunux flavor machine. (This command gives some error while executing in Windows Machine).

                      curl -H  "Content-Type: application/json" -XPOST -i -H "kbn-version:4.4.2" http://10.20.30.40:5601/elasticsearch/.kibana/index-pattern/twiteer_one?op_type=create -d '{"title": "twiteer_two", "timeFieldName": "date"}'

                      After successfully execute this command, come in Kibana and check.

                      Kibana -> Settings -> Indices, your twiteer_two index should be here.









                      Wednesday, April 27, 2016

                      Export and import via curl kibana’s dashboards and visualizations, Kibana Dashboard export into another Elasticsearch, command line load dashboard, How to save dashboard as json file, Location of custom Kibana dashboards in ElasticSearch


                      First in create some index and data in first ElasticSearch. As per my example:
                      {
                       "_index": "twiteer_one",
                                  "_type": "tweet",
                                  "_id": "2",
                                  "_score": 1,
                                  "_source": {
                                     "nick": "Ishan@gmail.com",
                                     "date": "2015-02-10",
                                     "post": "NCFE",
                                     "other_name": "Ishan Suman"
                                  }
                               },
                               {
                                  "_index": "twiteer_one",
                                  "_type": "tweet",
                                  "_id": "17",
                                  "_score": 1,
                                  "_source": {
                                     "nick": "@binodsuman",
                                     "date": "2014-08-15",
                                     "post": "BigData, Kibana, ES, ML",
                                     "other_name": "Binod Suman"
                                  }

                       - Connect this first ElasticSearch into your kibana dashboard.
                       - Create two visualization using above index "twiteer_one"
                         In my case, First-Visualization and Second-Visualization
                       - Create one Dashboard in kibana using these two visualization.
                         In my case my Dashboard name is "My-Dashboard".
                       
                      - Export all three item (two visualization and one dashboard) individually from Kibana.
                        HOW: In Kibana -> go to Settings -> Objects -> Visualization and
                         * tick on First Visualization and export and save in one folder with name v1.json
                         * tick on Second  Visualization and export and save in one folder with name v2.json
                       
                         - In Kibana -> go to Settings -> Objects -> Dashboard and
                         * tick on My Dashboard and export and save in one folder with name dashboard.json
                       
                       
                        - Stop this first ElasticSearch
                        - Start your second ElasticSearch, make sure you should have same index "twiteer_one" with same set of data.
                        Edit v1.json and remove first four line and your file should like this:
                       
                         {
                            "title": "First Visualization",
                            "visState": "{\"title\":\"New
                       ----
                       -----
                        "searchSourceJSON": "{\"index\":\"twiteer_one\",\"query\":{\"query_string\":{\"query\":\"*\",\"analyze_wildcard\":true}},\"filter\":[]}"
                            }
                          }
                        Edit v2.json and dashboard.json also and remove first four lines.
                       
                        Execute these three curl command:
                        D:\Kibana_Json>curl localhost:9200/.kibana/visualization/First-Visualization -d @v1.json
                        D:\Kibana_Json>curl localhost:9200/.kibana/visualization/Second-Visualization -d @v2.json
                        D:\Kibana_Json>curl localhost:9200/.kibana/dashboard/My-Dashboard -d @dashboard.json
                        Make sure after executing each command you must get "created":true message.
                        {"_index":".kibana","_type":"dashboard","_id":"My-Dashboard","_version":1,"_shards":{"total":2,"successful":1,"failed":0},"created":true}
                      D:\Kibana_Json>

                        Now you connect second ElasticSearch with Kibana and you will be getting all your visualization and Dashboard.
                        Please write if any problem or feedback.
                       
                       

                       

                       
                         
                       
                         

                      Thursday, March 24, 2016

                      SSL enable Server and Client side code, KeyStore, KeyManagerFactory, SSLContext, SSLServerSocketFactory, easy example of Java SSL code, getting start with SSL, What is SSL, Wha is the use of SSL, keytool command, SSL Client java code, How to write SSL enable client side code


                      SSL (Secure Sockets Layer) provides a secure connection between internet browsers and websites, allowing you to transmit private data online. 

                      Server used to have private and public key (key OR certificate). Server creator either can take certificate from some well known Certificate Authority (CA) or generate their own certificate using java keytool command. When client wants to connect server, server send public key to client and handshake happens between both.

                      Using server's public key, all data which go from client to server are encrypted by server's public key and at server side using private key, server can decrypted those client data. But instead always using public and private key, after handshake both client and server use one symmetric key for their encryption and decryption.

                      How to generate your own key using JDK keytool command.
                      keytool -genkey -keystore demokeystore -keyalg RSA
                      I use "password" as password while generating this keystore.


                      SSLServerDemo.java

                      import javax.net.ssl.KeyManagerFactory;
                      import javax.net.ssl.SSLContext;
                      import javax.net.ssl.SSLServerSocket;
                      import javax.net.ssl.SSLServerSocketFactory;
                      import javax.net.ssl.SSLSocket;
                      import javax.net.ssl.TrustManagerFactory;

                      import java.io.BufferedReader;
                      import java.io.FileInputStream;
                      import java.io.InputStream;
                      import java.io.InputStreamReader;
                      import java.security.KeyStore;

                      public class SSLServerDemo {
                          public static void main(String[] arstring) {
                          SSLContext sslContext;
                          System.out.println("** Servlet start with inbuild SSL **");
                              try {
                             
                               InputStream kis = new FileInputStream("demokeystore");
                               KeyStore keyStore = KeyStore.getInstance("jks");
                               keyStore.load(kis, "password".toCharArray());
                               KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
                               keyManagerFactory.init(keyStore, "password".toCharArray());
                                   
                               sslContext = SSLContext.getInstance("TLS");
                               sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
                                  
                               SSLServerSocketFactory sslserversocketfactory = sslContext.getServerSocketFactory();
                               SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(9000);
                               SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();

                               InputStream inputstream = sslsocket.getInputStream();
                               InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
                               BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

                               String string = null;
                               while ((string = bufferedreader.readLine()) != null) {
                                      System.out.println("From SSL Server :"+string);
                                      System.out.flush();
                                  }
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          }
                      }


                      SSLClientDemo.java

                      import javax.net.ssl.KeyManagerFactory;
                      import javax.net.ssl.SSLContext;
                      import javax.net.ssl.SSLServerSocketFactory;
                      import javax.net.ssl.SSLSocket;
                      import javax.net.ssl.SSLSocketFactory;
                      import javax.net.ssl.TrustManagerFactory;

                      import java.io.*;
                      import java.security.KeyStore;

                      public class SSLClientDemo {
                          public static void main(String[] arstring) {
                          SSLContext sslContext;
                              try {
                             
                                InputStream kis = new FileInputStream("demokeystore");
                                  KeyStore trustStore = KeyStore.getInstance("jks");
                                  trustStore.load(kis, "password".toCharArray());
                                  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                                  trustManagerFactory.init(trustStore);
                                           
                                  sslContext = SSLContext.getInstance("TLS");
                                  sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
                                  
                                   SSLSocketFactory sslsocketfactory = sslContext.getSocketFactory();
                                  SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 9000);

                                  InputStream inputstream = System.in;
                                  InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
                                  BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

                                  OutputStream outputstream = sslsocket.getOutputStream();
                                  OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outputstream);
                                  BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);

                                  String string = null;
                                  while ((string = bufferedreader.readLine()) != null) {
                                      bufferedwriter.write(string + '\n');
                                      bufferedwriter.flush();
                                  }
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          }
                      }

                      Compile both java file and run both. Now if you type any message in client side, it will send to server side code.