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


                      }