AWS Lamda函数生成cloudwatch编译错误

时间:2018-12-12 09:27:24

标签: eclipse amazon-web-services aws-java-sdk aws-toolkit

AWS提供此Java代码以进行识别中的视频分析。但是,在Eclipse中查看此消息时,会出现错误消息:

The public type JobCompletionHandler must be defined in its own file

在AWS中调用该函数时,它还会在Cloudwatch日志中显示:

Unresolved compilation problem: The public type JobCompletionHandler must be defined in its own file : java.lang.Error java.lang.Error: Unresolved compilation problem: The public type JobCompletionHandler must be defined in its own file at com.amazonaws.lambda.demo.JobCompletionHandler.<init>(LambdaFunctionHandler.java:19) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Met
Unresolved compilation problem:
The public type JobCompletionHandler must be defined in its own file
: java.lang.Error
java.lang.Error: Unresolved compilation problem:
The public type JobCompletionHandler must be defined in its own file

这是AWS提供的全部功能:

//Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazonrekognition-developer-guide/blob/master/LICENSE-SAMPLECODE.)
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
import java.util.List;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.rekognition.AmazonRekognition;
import com.amazonaws.services.rekognition.AmazonRekognitionClientBuilder;
import com.amazonaws.services.rekognition.model.GetLabelDetectionRequest;
import com.amazonaws.services.rekognition.model.GetLabelDetectionResult;
import com.amazonaws.services.rekognition.model.LabelDetection;
import com.amazonaws.services.rekognition.model.LabelDetectionSortBy;
import com.amazonaws.services.rekognition.model.VideoMetadata;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

可以重命名此公共类和文件名。不需要创建单独的文件。

public class JobCompletionHandler implements RequestHandler<SNSEvent, String> {
    @Override
    public String handleRequest(SNSEvent event, Context context) {
        String message = event.getRecords().get(0).getSNS().getMessage();
        LambdaLogger logger = context.getLogger();
        // Parse SNS event for analysis results. Log results
        try {
            ObjectMapper operationResultMapper = new ObjectMapper();
            JsonNode jsonResultTree = operationResultMapper.readTree(message);
            logger.log("Rekognition Video Operation:=========================");
            logger.log("Job id: " + jsonResultTree.get("JobId"));
            logger.log("Status : " + jsonResultTree.get("Status"));
            logger.log("Job tag : " + jsonResultTree.get("JobTag"));
            logger.log("Operation : " + jsonResultTree.get("API"));
            if (jsonResultTree.get("API").asText().equals("StartLabelDetection")) {
                if (jsonResultTree.get("Status").asText().equals("SUCCEEDED")){
                    GetResultsLabels(jsonResultTree.get("JobId").asText(), context);
                }
                else{
                    String errorMessage = "Video analysis failed for job "
                    + jsonResultTree.get("JobId")
                    + "State " + jsonResultTree.get("Status");
                    throw new Exception(errorMessage);
                }
            } else
            logger.log("Operation not StartLabelDetection");
        } catch (Exception e) {
            logger.log("Error: " + e.getMessage());
            throw new RuntimeException (e);
        }
        return message;
    }
    void GetResultsLabels(String startJobId, Context context) throws Exception {
        LambdaLogger logger = context.getLogger();
        AmazonRekognition rek =
        AmazonRekognitionClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
        int maxResults = 1000;
        String paginationToken = null;
        GetLabelDetectionResult labelDetectionResult = null;
        String labels = "";
        Integer labelsCount = 0;
        String label = "";
        String currentLabel = "";
        //Get label detection results and log them.
        do {
            GetLabelDetectionRequest labelDetectionRequest = new
            GetLabelDetectionRequest().withJobId(startJobId)
            .withSortBy(LabelDetectionSortBy.NAME).withMaxResults(maxResults).withNextToken(paginationToken);
            labelDetectionResult = rek.getLabelDetection(labelDetectionRequest);
            paginationToken = labelDetectionResult.getNextToken();
            VideoMetadata videoMetaData = labelDetectionResult.getVideoMetadata();
            // Add labels to log
            List<LabelDetection> detectedLabels = labelDetectionResult.getLabels();
            for (LabelDetection detectedLabel : detectedLabels) {
                label = detectedLabel.getLabel().getName();
                if (label.equals(currentLabel)) {
                    continue;
                }
                labels = labels + label + " / ";
                currentLabel = label;
                labelsCount++;
            }
        } while (labelDetectionResult != null && labelDetectionResult.getNextToken() != null);
        logger.log("Total number of labels : " + labelsCount);
        logger.log("labels : " + labels);
    }
}

我该如何更正此代码,以使公共类没有错误显示?

1 个答案:

答案 0 :(得分:-1)

重命名该类后,它现在可以编译。