0

AWS 提供此 Java 代码以在 rekognition 中执行视频分析。但是,在 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);
    }
}

如何更正此代码,以免公共类显示错误?

4

1 回答 1

-1

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

于 2018-12-26T01:12:05.210 回答