如何在DynamoDB中根据HashKey和range Key进行查询?

2023-12-26

我是新来的DynamoDb东西。我只是想知道我们如何使用以下命令查询 DynamoDB 中的表hashKey and rangeKey.

假设我的桌子是TestTable它的架构是这样的:

1.Id (HK of type String)
2 Date (RK of type String )
3 Name (attribute of type String)

现在如果我想根据以下内容查询该表hashKey这是Id在这里,我们做了一个query as :

假设我的查询是获取所有具有Id ="123".

TestTable testTable = new TestTable();
testTable.setId("123");

DynamoDBQueryExpression<TestTable> queryExpression = new DynamoDBQueryExpression<TestTable>()
                                                                .withHashKeyValues(TestTable)
                                                                .withConsistentRead(false);

现在我想获得所有具有Id ="123" and Date ="1234".

我怎样才能查询这个东西DynamoDB

我在用java作为我的编程语言。


我前段时间写了一篇关于使用 AWS Java SDK 进行 DynamoDB 查询和索引的文章:http://labs.journwe.com/2013/12/15/dynamodb-secondary-indexes/ http://labs.journwe.com/2013/12/15/dynamodb-secondary-indexes/

在你的情况下,它应该像这样工作(参见http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html):

AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
DynamoDBMapper mapper = new DynamoDBMapper(client);

String hashKey = "123";
long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
Date twoWeeksAgo = new Date();
twoWeeksAgo.setTime(twoWeeksAgoMilli);
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
String twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo);            
Condition rangeKeyCondition = new Condition()
        .withComparisonOperator(ComparisonOperator.GT.toString())
        .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr.toString()));

Reply replyKey = new Reply();
replyKey.setId(hashKey);

DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
        .withHashKeyValues(replyKey)
        .withRangeKeyCondition("ReplyDateTime", rangeKeyCondition);

List<Reply> latestReplies = mapper.query(Reply.class, queryExpression);

查看 DynamoDB 文档的 Java 对象持久性模型部分以了解更多信息。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在DynamoDB中根据HashKey和range Key进行查询? 的相关文章

随机推荐