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);
}
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;
}
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.";
}
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));
}
}
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";
}
}
Aggregations