如何基于HashKey和range键在DynamoDB中进行查询? [英] How to do Query in DynamoDB on the basis of HashKey and range Key?
问题描述
我是 DynamoDb
的新手。我只是想知道我们如何使用 hashKey
和 rangeKey
在DynamoDB中的表上进行查询。
假设我的表是 TestTable
,它的模式如下:
1.Id(String类型的HK)
2日期(String类型的RK)
3名称(String类型的属性)
现在如果我想基于 hashKey
此处是 Id
,我们进行查询
为:
比方说,我的查询是获取具有 Id = 123的所有商品。
TestTable testTable = new TestTable();
testTable.setId( 123);
DynamoDBQueryExpression< TestTable> queryExpression =新的DynamoDBQueryExpression< TestTable>()
.withHashKeyValues(TestTable)
.withConsistentRead(false);
现在我想获取所有具有 Id = 123和Date的项目= 1234
。
如何在 DynamoDB
中查询此内容 p>
我正在使用 java
作为编程语言。
前一段时间,我写了一篇有关使用AWS Java SDK进行DynamoDB查询和索引的文章: http://labs.journwe.com/2013/12/15/dynamodb-secondary-indexes/
在您的情况下,它应该像这样工作(请参阅 http:// docs。 aws.amazon.com/amazondynamodb/latest/developerguide/JavaQueryScanORMModelExample.html ):
AmazonDynamoDBClient客户端=新的AmazonDynamoDBClient (新的ProfileCredentialsProvider());
DynamoDBMapper映射器=新的DynamoDBMapper(客户端);
字符串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));
字符串twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo);
条件rangeKeyCondition =新Condition()
.withComparisonOperator(ComparisonOperator.GT.toString())
.withAttributeValueList(new AttributeValue()。withS(twoWeeksAgoStr.toString()));
Reply ReplyKey = new Reply();
replyKey.setId(hashKey);
DynamoDBQueryExpression< Reply> queryExpression =新的DynamoDBQueryExpression< Reply>()
.withHashKeyValues(replyKey)
.withRangeKeyCondition( ReplyDateTime,rangeKeyCondition);
List< Reply> LatestReplies = mapper.query(Reply.class,queryExpression);
请查看DynamoDB文档的Java Object Persistence Model部分以获得更多信息。
I am new to DynamoDb
stuff. I just want to know how can we query on a table in DynamoDB with the hashKey
and rangeKey
.
Let's say my Table is TestTable
and it's schema is something like this:
1.Id (HK of type String)
2 Date (RK of type String )
3 Name (attribute of type String)
Now If I want to query on this table on the basis of hashKey
which is Id
here, we make a query
as :
Let's say my query is to get all Items having Id ="123".
TestTable testTable = new TestTable();
testTable.setId("123");
DynamoDBQueryExpression<TestTable> queryExpression = new DynamoDBQueryExpression<TestTable>()
.withHashKeyValues(TestTable)
.withConsistentRead(false);
Now I want to get all Items having Id ="123" and Date ="1234"
.
How can I query this thing in DynamoDB
I am using java
as my programming language.
I wrote an article about DynamoDB queries and indexing using the AWS Java SDK some time ago: http://labs.journwe.com/2013/12/15/dynamodb-secondary-indexes/
In your case, it should work like this (see 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);
Check out the Java Object Persistence Model section of the DynamoDB docs for more info.
这篇关于如何基于HashKey和range键在DynamoDB中进行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!