Search in sources :

Example 6 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project aws-doc-sdk-examples by awsdocs.

the class JobCompletionHandler method GetResultsLabels.

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);
}
Also used : GetLabelDetectionRequest(com.amazonaws.services.rekognition.model.GetLabelDetectionRequest) AmazonRekognition(com.amazonaws.services.rekognition.AmazonRekognition) GetLabelDetectionResult(com.amazonaws.services.rekognition.model.GetLabelDetectionResult) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) VideoMetadata(com.amazonaws.services.rekognition.model.VideoMetadata) LabelDetection(com.amazonaws.services.rekognition.model.LabelDetection)

Example 7 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project aws-doc-sdk-examples by awsdocs.

the class JobCompletionHandler method handleRequest.

@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;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project aws-doc-sdk-examples by awsdocs.

the class HandlerStoreData method handleRequest.

@Override
public String handleRequest(String event, Context context) {
    LambdaLogger logger = context.getLogger();
    String xml = event;
    DynamoDBService storeData = new DynamoDBService();
    try {
        storeData.injectETLData(xml);
        logger.log("data stored:");
    } catch (JDOMException | IOException e) {
        e.printStackTrace();
    }
    return "Data is stored successfully.";
}
Also used : IOException(java.io.IOException) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger) JDOMException(org.jdom2.JDOMException)

Example 9 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project djl-demo by deepjavalibrary.

the class Handler method handleRequest.

@Override
public void handleRequest(InputStream is, OutputStream os, Context context) throws IOException {
    LambdaLogger logger = context.getLogger();
    String input = Utils.toString(is);
    try {
        Request request = GSON.fromJson(input, Request.class);
        String url = request.getInputImageUrl();
        String artifactId = request.getArtifactId();
        Map<String, String> filters = request.getFilters();
        Criteria<Image, Classifications> criteria = Criteria.builder().setTypes(Image.class, Classifications.class).optArtifactId(artifactId).optFilters(filters).build();
        try (ZooModel<Image, Classifications> model = criteria.loadModel();
            Predictor<Image, Classifications> predictor = model.newPredictor()) {
            Image image = ImageFactory.getInstance().fromUrl(url);
            List<Classifications.Classification> result = predictor.predict(image).topK(5);
            os.write(GSON.toJson(result).getBytes(StandardCharsets.UTF_8));
        }
    } catch (RuntimeException | ModelException | TranslateException e) {
        logger.log("Failed handle input: " + input);
        logger.log(e.toString());
        String msg = "{\"status\": \"invoke failed: " + e.toString() + "\"}";
        os.write(msg.getBytes(StandardCharsets.UTF_8));
    }
}
Also used : Classifications(ai.djl.modality.Classifications) ModelException(ai.djl.ModelException) TranslateException(ai.djl.translate.TranslateException) Image(ai.djl.modality.cv.Image) LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Example 10 with LambdaLogger

use of com.amazonaws.services.lambda.runtime.LambdaLogger in project amazon-keyspaces-examples by aws-samples.

the class Handler method handleRequest.

@Override
public String handleRequest(Map<String, String> event, Context context) {
    LambdaLogger logger = context.getLogger();
    try {
        logger.log("Printing environment settings:");
        logger.log("CONTEXT: " + gson.toJson(context));
        // process event
        logger.log("EVENT: " + gson.toJson(event));
        logger.log("EVENT TYPE: " + event.getClass().toString());
        int success;
        if (shouldLoginUsingSSC()) {
            success = KeyspacesSSCSession.Instance(logger).validateConnection();
        } else {
            success = KeyspacesSigV4Session.Instance(logger).validateConnection();
        }
        if (success == 200) {
            return "200 OK";
        }
        return "400 error";
    } catch (Exception e) {
        e.printStackTrace();
        return "400 error";
    }
}
Also used : LambdaLogger(com.amazonaws.services.lambda.runtime.LambdaLogger)

Aggregations

LambdaLogger (com.amazonaws.services.lambda.runtime.LambdaLogger)59 HashMap (java.util.HashMap)10 Date (java.util.Date)9 List (java.util.List)8 Map (java.util.Map)8 Context (com.amazonaws.services.lambda.runtime.Context)6 ApiAuthorizer (com.formkiq.lambda.apigateway.ApiAuthorizer)6 ApiGatewayRequestEvent (com.formkiq.lambda.apigateway.ApiGatewayRequestEvent)6 ApiGatewayRequestEventUtil (com.formkiq.lambda.apigateway.ApiGatewayRequestEventUtil)6 ApiGatewayRequestHandler (com.formkiq.lambda.apigateway.ApiGatewayRequestHandler)6 ApiRequestHandlerResponse (com.formkiq.lambda.apigateway.ApiRequestHandlerResponse)6 IOException (java.io.IOException)6 SC_OK (com.formkiq.lambda.apigateway.ApiResponseStatus.SC_OK)5 AwsServiceCache (com.formkiq.lambda.apigateway.AwsServiceCache)5 BadException (com.formkiq.lambda.apigateway.exception.BadException)5 DynamicObject (com.formkiq.stacks.common.objects.DynamicObject)5 Collectors (java.util.stream.Collectors)5 ClientContext (com.amazonaws.services.lambda.runtime.ClientContext)4 CognitoIdentity (com.amazonaws.services.lambda.runtime.CognitoIdentity)4 ApiMapResponse (com.formkiq.lambda.apigateway.ApiMapResponse)4