Java 如何在DynamoDB中基于HashKey和range Key做Query?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30840093/
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
How to do Query in DynamoDB on the basis of HashKey and range Key?
提问by Geek_To_Learn
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
.
我是新手DynamoDb
。我只想知道我们如何使用hashKey
和查询 DynamoDB 中的表rangeKey
。
Let's say my Table is TestTable
and it's schema is something like this:
假设我的 Table 是TestTable
这样的,它的架构是这样的:
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 :
现在,如果我想在此基础上对这个表查询hashKey
是Id
在这里,我们做出query
如下:
Let's say my query is to get all Items having Id ="123".
假设我的查询是获取所有具有 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"
.
现在我想获得所有具有Id ="123" and Date ="1234"
.
How can I query this thing in DynamoDB
我如何查询这个东西 DynamoDB
I am using java
as my programming language.
我使用java
作为我的编程语言。
回答by Markus Klems
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/
前段时间我写了一篇关于使用 AWS Java SDK 进行 DynamoDB 查询和索引的文章: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):
在您的情况下,它应该像这样工作(参见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.
查看 DynamoDB 文档的 Java 对象持久性模型部分了解更多信息。
回答by Mingliang Liu
If you're not using index, you should have exact one item, if any, whose Id ="123"
and Date ="1234"
.
如果你没有使用索引,你应该有一个确切的项目,如果有的话,其Id ="123"
和Date ="1234"
。
For the exact hash key and range key (in case of =
operator), you don't need a query operation. Please have a loot at GetItem
API, which is nifty. See documentation page.
对于确切的散列键和范围键(在=
运算符的情况下),您不需要查询操作。请在GetItem
API 上抢夺,这很漂亮。请参阅文档页面。
And if you need non-EQ operator for range key (e.g. >=
), you can use key condition expressionin Query
operation: Id = :id and Date >= :date
.
如果您需要范围键的非 EQ 运算符(例如>=
),您可以在操作中使用键条件表达式Query
:Id = :id and Date >= :date
。
回答by Nikita
You could use dynamoDbMapper.load() as following:
您可以使用 dynamoDbMapper.load() 如下:
TestTable testTable = new TestTable();
testTable.setId("123");
testTable.setDate("1234");
TestTable result = dynamoDBMapper.load(testTable);