简体   繁体   English

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

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

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. ORIGINAL:我在DynamoDB中有一个带有Hash-Range键模式的表。 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. 截至目前我的范围键是一个字符串,我能想到的唯一方法是查询所有范围键大于或等于我可以使用的最小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字符的方法是否有效?

but it seems to require a range key condition. 但它似乎需要一个范围关键条件。

This doesn't sound to be true. 这听起来并非如此。

I use DynamoDBMapper and use DynamoDBQueryExpression to 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. HTH。

You can use DynamoDB's query API, which allows you to query the database based conditional expressions using the hash/range keys. 您可以使用DynamoDB的查询API,它允许您使用哈希/范围键查询基于数据库的条件表达式。 You can see examples of the API here . 您可以在此处查看API的示例。 Here is a relevant example: 这是一个相关的例子:

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

You can also query using more complex expressions. 您还可以使用更复杂的表达式进行查询。 Eg: 例如:

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM