查询DynamoDB [英] Querying DynamoDB
问题描述
我有一个DynamoDB表,其中包含一个字母数字字符串作为哈希键(例如 d4ed6962-3ec2-4312-a480-96ecbb48c9da)。我需要根据表中的另一个字段查询表,因此我需要查询以选择所有键,例如我的字段x在日期x和日期y之间。
I've got a DynamoDB table with a an alpha-numeric string as a hash key (e.g. "d4ed6962-3ec2-4312-a480-96ecbb48c9da"). I need to query the table based on another field in the table, hence I need my query to select all the keys such as my field x is between dat x and date y.
我知道我需要一个哈希键条件和一个范围键条件,但是我很难组成一个哈希键条件,该条件不能将查询绑定到特定ID。
I know I need a condition on the hash key and another on a range key, however I struggle to compose a hash key condition that does not bind my query to specific IDs.
我认为我可以根据ID为NOT_NULL来避免出现冗余情况,但是当我使用它时,会出现错误:
I thought I could get away with a redundant condition based on the ID being NOT_NULL, but when I use it I get the error:
不支持查询关键条件
Query key condition not supported
以下是我正在使用的条件,是否知道如何实现此目标?
Below is the conditions I am using, any idea how to achieve this goal?
Condition hashKeyCondition = new Condition()
.withComparisonOperator(ComparisonOperator.NOT_NULL.toString());
Condition rangeCondition = new Condition()
.withComparisonOperator(ComparisonOperator.BETWEEN.toString())
.withAttributeValueList(new AttributeValue().withS(dateFormatter.print(lastScanTime())),
new AttributeValue().withS(dateFormatter.print(currentScanTime)));
Map<String, Condition> keyConditions = new HashMap<String, Condition>();
keyConditions.put("userId", hashKeyCondition);
keyConditions.put("lastAccesTime", rangeCondition);
在此先感谢大家的帮助。
Thanks in advance to everyone helping.
推荐答案
在DynamoDB中,您可以使用3种api获取项目:
In DynamoDB you can get items with 3 api:
。 扫描(灵活但价格昂贵),
. Scan (flexible but expensive),
。 查询(不太灵活:您必须指定哈希,但是便宜些)
. Query (less flexible: you have to specify an hash, but less expensive)
。 GetItem (由哈希表示,如果您的表有一个,则由范围)
. GetItem (by Hash and, if your table has one, by range)
实现您想要的唯一方法是:
The only way to achieve what you want is by either:
-
使用扫描,速度慢或昂贵。
Use Scan, and be slow or expensive.
使用另一个表(B)作为上一个表(A)的索引,例如:
Use another table (B) as an index to the previous one (A) like:
B.HASH ='VALUES'
B.RANGE = userid
B.lastAccesTime = lastAccesTime(带有二级索引)
B.HASH = 'VALUES'
B.RANGE = userid
B.lastAccesTime = lastAccesTime (with a secondary index)
现在您必须在写入时保持该索引,但是可以将其与Query操作一起使用,将
用于获取您的userId。查询B:hash = VALUES,在x和y之间的lastaccessTime,选择用户ID。
Now you have to maintain that index on writes, but you can use it with the Query operation, to get your userIds. Query B: hash='VALUES', lastaccessTime between x and y, select userid.
希望这会有所帮助。
这篇关于查询DynamoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!