快速查询包含键的表(DynamoDB 和 Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9792529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:42:03  来源:igfitidea点击:

Quickly query a table if it contains a key (DynamoDB and Java)

javaamazon-web-servicesamazon-dynamodb

提问by Chen Harel

I have a table with a hash and range complex key.
I can query an item using GetItemfrom AWS SDK for Java. The GetItemreturns null if it doesn't find the object, or the item as a Map<String, AttributeValue>.
I am looking for the fastest approach to check whether the object does exist
I was thinking maybe supplying a .withAttributesToGetsuch as:

我有一个带有哈希和范围复杂键的表。
我可以使用GetItemAWS SDK for Java查询项目。将GetItem返回null如果没有找到对象,或作为项目Map<String, AttributeValue>
我正在寻找检查对象是否确实存在的最快方法,
我想可能会提供.withAttributesToGet诸如:

GetItemResult result =  dbClient.getItem(new GetItemRequest().
    withTableName(TABLE_NAME).
        withKey(new Key(new AttributeValue().withS(hashKey),
                        new AttributeValue().withS(rangeKey))).
        withAttributesToGet(new ArrayList<String>()));
Map<String, AttributeValue> item = result.getItem();
return (item != null);

Another optimization is to not use the SDK JSON parser and parse the response myself to quickly check if the item has returned.

另一个优化是不使用 SDK JSON 解析器并自己解析响应以快速检查项目是否已返回。

Thanks

谢谢

采纳答案by Sony Kadavan

I think there is negligible difference in speed between "getting" and checking if it exists. You can go ahead and use the GetItem itself. If the item is potentially too large, then limit the attributes being returned.

我认为“获取”和检查它是否存在之间的速度差异可以忽略不计。您可以继续使用 GetItem 本身。如果项目可能太大,则限制返回的属性。

The bottle neck is in latency to reach the Dynaamo DB servers (REST API) and in fetching from the index. So Getting and checking will be similar speed. Ensure that your server issuing the call is in the same region as Dynamo DB - This has max impact on the speed.

瓶颈在于到达 Dynaamo 数据库服务器 (REST API) 和从索引中获取的延迟。所以获取和检查将是相似的速度。确保发出调用的服务器与 Dynamo DB 位于同一区域 - 这对速度影响最大。

回答by Ashwin Patti

By mentioning only the hash key as attributes to get, you could have better performance and don't waste your throughput.

通过仅提及哈希键作为要获取的属性,您可以获得更好的性能并且不会浪费您的吞吐量。