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();
                                  }
                               } 
                           }


                      }