Home » Java » amazon web services – How can I include and use a command line tool in my my java lambda function?-Exceptionshub

amazon web services – How can I include and use a command line tool in my my java lambda function?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to do some data transformation using a command-line tool (Tarql). I am unsure on how to include the command line tool in my deployment package. I was thinking of saving it in a tmp folder so that I could work with it through ProcessBuilder but I don’t know how to include it in my deployment package in the first place.

Another issue that I am facing is I don’t know how to save files from an s3 bucket to the tmp folder.

My Code so far:

public class LambdaTarqlExecution implements RequestHandler<S3Event, String> {
    private static Logger log = LoggerFactory.getLogger(LambdaTarqlExecution.class);
    private AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();
    public LambdaTarqlExecution() {
    }
    // Test purpose only.
    LambdaTarqlExecution(AmazonS3 s3) {
        this.s3 = s3;
    }

    @Override
    public String handleRequest(S3Event event, Context context) {
        context.getLogger().log("Received event: " + event);
        // Get the object from the event and show its content type
        S3EventNotification.S3EventNotificationRecord record=event.getRecords().get(0);
        String bucket = record.getS3().getBucket().getName();

        try {

            ObjectMetadata object1 =  s3.getObject(new GetObjectRequest(bucket, "GeoMappings.sparql"),new File("/tmp/"));
            ObjectMetadata object2 =  s3.getObject(new GetObjectRequest(bucket, "Countries.csv"),new File("/tmp/"));
            String[] command;
            ProcessBuilder pb = new ProcessBuilder("/tmp/tarql.bat", "--write-base", "--base","ontologies.se.com/Procurment", "/tmp/GeoMappings.sparql", "/tmp/Countries.csv", ">", "/tmp/geo.ttl");
            PutObjectResult result = s3.putObject("turtleFiles", "geo.ttl", "/tmp/geo.ttl");

        } catch (AmazonServiceException e) {
            // The call was transmitted successfully, but Amazon S3 couldn't process 
            // it, so it returned an error response.
            e.printStackTrace();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
            e.printStackTrace();
        }        

        return "sucess";
   }
}
How to&Answers: