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

标签 scala amazon-dynamodb dynamo-local

我正在尝试在本地运行 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/

相关文章:

amazon-dynamodb - 在本地 DynamoDB 中创建表时出错

amazon-web-services - 流支持本地dynamodb?

java - 如何重定向到在 play 框架中有 post 方法的 Action

scala - 系统内存 259522560 必须至少为 4.718592E8。请使用更大的堆大小

java - Spark : replace null values in dataframe with mean of column

amazon-dynamodb - 如何在单个请求中放置多个项目?

ios - 对适用于 iOS 的 dynamoDB (AWS) 执行扫描或查询

scala - Scala 构造函数中有局部值/变量吗?

node.js - 从 dynamodb 中删除项目时清空数据对象

amazon-web-services - AWS 环境变量提供商不接受凭据