AWS无服务器Lambda Java从Amazon DynamoDB检索所有项目,没有变量类型 &g



我编写了一个lambda函数,它扫描dynamoDB中的所有项,但它通过其类型检索它们。我怎样才能去掉它们?我应该尝试以适合我的方式解析数据还是有更好的方法?

public class Get implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
DynamoDB dynamoDB = new DynamoDB(client);
ScanRequest scanRequest = new ScanRequest()
.withTableName("xxx");
static final int STATUS_CODE_NO_CONTENT = 204;
static final int STATUS_CODE_CREATED = 201;
static final int STATUS_CODE_FAILED = 400;
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
Map<String, String> map = request.getPathParameters();
ContactDetails contactDetails = null;
APIGatewayProxyResponseEvent response = null;
int code = STATUS_CODE_NO_CONTENT;
try {
ScanResult result = client.scan(scanRequest);
code = STATUS_CODE_CREATED;
response = new APIGatewayProxyResponseEvent().withStatusCode(code).withIsBase64Encoded(Boolean.FALSE).withHeaders(Collections.emptyMap()).withBody(new Gson().toJson(result.getItems()));
} catch (Exception e) {
code = STATUS_CODE_FAILED;
response = new APIGatewayProxyResponseEvent().withStatusCode(code).withIsBase64Encoded(Boolean.FALSE).withHeaders(Collections.emptyMap()).withBody(e.toString());
}
return response;
}
}

反应:[{"kontent"{"s"ys"},"id":{"s"ys"},{"id"{"s"asdassa"})需要去做[{"id"ys","kontent":"ys"},{"id"asdassa"]

您需要打开从result.getItems()返回的AttributeValue实例。对于字符串来说很简单,但也有更复杂的数据类型。

实现此方法,然后用它包裹result.getItems(),即convertItems(result.getItems())

List<Map<String, Object>> convertItems(List<Map<String, AttributeValue>> items) {
List<Map<String, Object>> newItems = new ArrayList<>(items.size());
for (Map<String, AttributeValue> item : items) { 
Map<String, Object> newItem = new HashMap<>();
item.forEach((key, value) -> {
switch(value.type()) {
case S:
newItem.put(key, value.s());
break;
// Any other types you want to support besides string
}
});
newItems.add(newItem);
}
return newItems;
}

肯定有更好的方法来执行Amazon DynamoDB从AWS Lambda函数内部进行CRUD操作。

离开AWS SDK for Java V1V2,。Amazon强烈建议开发者在使用JAVA SDK和Enhanced Client执行Amazon DynamoDB CRUD操作时使用V2。

software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient

详情请参见:

https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/dynamodb-enhanced-client.html

该客户端在AWS Lambda函数中工作得很好。有关如何在使用Java运行时Lambda API的AWS Lambda函数中使用此客户机的详细信息,请参阅本文:

创建使用个人防护设备检测图像的AWS Lambda函数

本例创建一个Java Lambda函数,该函数使用Amazon Rekognition服务检测图像中的PPE信息,并在Amazon DynamoDB表中创建一条记录。这向您展示了使用增强型客户机从Lambda函数内执行CRUD操作的最佳实践.

当您使用增强客户端和扫描时操作,扫描方法返回Java对象。你不必像使用V1那样使用逻辑和getItems。这是使用这个客户端最好的部分之一。

要获取数据,只需调用属于Java对象的方法。例如,下面是一个Scan方法,返回Customer要获取数据,只需调用属于Customer的方法对象-例如getCustName():

public static void scan( DynamoDbEnhancedClient enhancedClient) {
try{
DynamoDbTable<Customer> custTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
Iterator<Customer> results = custTable.scan().items().iterator();
while (results.hasNext()) {
Customer rec = results.next();
System.out.println("The record id is "+rec.getId());
System.out.println("The name is " +rec.getCustName());
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("Done");
}

最新更新