gpt4 book ai didi

scala - DynamoDB PutItemOutcome#getPutItemResult() 返回空对象

转载 作者:行者123 更新时间:2023-12-04 18:00:36 24 4
gpt4 key购买 nike

我正在尝试在本地运行 AWS Dynamo 教程中的以下示例,Step 3: Put, Update, and Delete an Item .

在我的例子中,它是:

val client: AmazonDynamoDBClient = new AmazonDynamoDBClient().withEndpoint("http://localhost:7777")

val dynamoDB: DynamoDB = new DynamoDB(client)

val table: Table = dynamoDB.getTable("Catalog")

try {

val rating: java.util.List[Float] = new java.util.LinkedList[Float]()

rating.add(1)

val newItem: Item = new Item().withPrimaryKey("Title", "Title here").withInt("Country", 1).
withList("Ratings", rating)

val outcome: PutItemOutcome = table.putItem(newItem)

System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult)

} catch {

case exception: Exception => System.out.println(exception.getMessage)

}

输出是:

PutItem succeeded: {}

在本地 DynamoDB 控制台中:

var params = { 
TableName: "Catalog",
Key: {
"Title":"Title Here",
}
};

docClient.get(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});

输出:

{ "Item": { "Title": "Title Here", "Ratings": [ 1 ], "Country": 1 } }

最佳答案

您需要在 PutItem 请求中将 ReturnValues 设置为 ALL_OLD 以获得返回值,但即便如此,它也只会包含被替换的值。

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-ReturnValues

对于您的代码,您需要执行类似替换的操作

val outcome: PutItemOutcome = table.putItem(newItem)

val putItemSpec: PutItemSpec = new PutItemSpec()
.withItem(newItem)
.withReturnValues(ReturnValue.ALL_OLD)
val outcome: PutItemOutcome = table.putItem(putItemSpec)

关于scala - DynamoDB PutItemOutcome#getPutItemResult() 返回空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36058982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com