首页 > 解决方案 > 如何在我的 java lambda 函数中包含和使用命令行工具?

问题描述

我正在尝试使用命令行工具(Tarql)进行一些数据转换。我不确定如何在我的部署包中包含命令行工具。我正在考虑将它保存在 tmp 文件夹中,以便我可以通过 ProcessBuilder 使用它,但我不知道如何将它包含在我的部署包中。

我面临的另一个问题是我不知道如何将文件从 s3 存储桶保存到 tmp 文件夹。

到目前为止我的代码:

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";
   }
}

标签: javaamazon-web-servicesshellamazon-s3aws-lambda

解决方案


推荐阅读