使用 java sdk 从具有散列范围架构的给定散列键查询 DynamoDB 中的所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30969594/
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-11-02 17:54:42  来源:igfitidea点击:

Query all items in DynamoDB from a given hash key with a hash-range schema using java sdk

javaamazon-web-servicesamazon-dynamodb

提问by mnewton

EDIT:I was actually incorrect. I was querying the table when I meant to query an index which explains my error. Vikdor's solution is a valid one though.

编辑:我实际上是不正确的。当我打算查询解释我的错误的索引时,我正在查询表。不过,Vikdor 的解决方案是有效的。

ORIGINAL:I have a table with a Hash-Range key schema in DynamoDB. I need to be able to get all items associated with a specific hash key but it seems to require a range key condition. My issue is I want EVERY range key but there is no wildcard option. As of right now my range key is a string and the only way I could think to do this is by querying all range keys greater or equal to the smallest ascii characters I can use since the documentation says it sorts based on ascii character values.

原文:我在 DynamoDB 中有一个带有 Hash-Range 键架构的表。我需要能够获取与特定哈希键关联的所有项目,但它似乎需要范围键条件。我的问题是我想要每个范围键,但没有通配符选项。截至目前,我的范围键是一个字符串,我能想到的唯一方法是查询大于或等于我可以使用的最小 ascii 字符的所有范围键,因为文档说它基于 ascii 字符值进行排序。

I looked into scanning but it appears that simply will read the entire table which is NOT an option.

我研究了扫描,但似乎只会读取整个表格,这不是一个选项。

Is there any better way to query for all values of a hash key or can anyone confirm that using the method with the ascii character will work?

有没有更好的方法来查询哈希键的所有值,或者任何人都可以确认使用带有 ascii 字符的方法是否有效?

回答by Vikdor

but it seems to require a range key condition.

但它似乎需要一个范围键条件。

This doesn't sound to be true.

这听起来不像是真的。

I use DynamoDBMapperand use DynamoDBQueryExpressionto query all the records with a given HashKey as follows:

我使用DynamoDBMapper并使用DynamoDBQueryExpression查询具有给定 HashKey 的所有记录,如下所示:

DynamoDBQueryExpression<DomainObject> query = 
    new DynamoDBQueryExpression<DomainObject>();
DomainObject hashKeyValues = new DomainObject();
hashKeyValues.setHashKey(hashKeyValue);
query.setHashKeyValues(hashKeyValues);
// getMapper() returns a DynamoDBMapper object with the appropriate 
// AmazonDynamoDBClient object.
List<DomainObject> results = getMapper().query(query);

HTH.

哈。

回答by Max

You can use DynamoDB's query API, which allows you to query the database based conditional expressions using the hash/range keys. You can see examples of the API here. Here is a relevant example:

您可以使用 DynamoDB 的查询 API,它允许您使用哈希/范围键查询基于数据库的条件表达式。您可以在此处查看 API 示例。这是一个相关的例子:

ItemCollection<QueryOutcome> items = table.query("theHashFieldName", "theHashFieldToQuery");

You can also query using more complex expressions. E.g.:

您还可以使用更复杂的表达式进行查询。例如:

DynamoDB dynamoDB = new DynamoDB(
    new AmazonDynamoDBClient(new ProfileCredentialsProvider()));

Table table = dynamoDB.getTable("TableName");

QuerySpec spec = new QuerySpec()
    .withKeyConditionExpression("Id = :v_id")
    .withValueMap(new ValueMap()
        .withString(":v_id", "TheId"));

ItemCollection<QueryOutcome> items = table.query(spec);

Iterator<Item> iterator = items.iterator();
Item item = null;
while (iterator.hasNext()) {
    item = iterator.next();
    System.out.println(item.toJSONPretty());
}