Gee*_*arn 11 java amazon-dynamodb
我是新手DynamoDb.我只想知道如何使用hashKey和查询DynamoDB中的表rangeKey.
假设我的表是TestTable,它的架构是这样的:
1.Id (HK of type String)
2 Date (RK of type String )
3 Name (attribute of type String)
Run Code Online (Sandbox Code Playgroud)
现在,如果我想在此基础上对这个表查询hashKey是Id在这里,我们做出query如下:
假设我的查询是让所有项目都有 Id ="123".
TestTable testTable = new TestTable();
testTable.setId("123");
DynamoDBQueryExpression<TestTable> queryExpression = new DynamoDBQueryExpression<TestTable>()
.withHashKeyValues(TestTable)
.withConsistentRead(false);
Run Code Online (Sandbox Code Playgroud)
现在我想得到所有物品Id ="123" and Date ="1234".
我怎么能查询这个东西 DynamoDB
我正在使用java我的编程语言.
小智 10
我前段时间使用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 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);
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请查看DynamoDB文档的Java Object Persistence Model部分.
小智 6
你可以使用dynamoDbMapper.load()如下:
TestTable testTable = new TestTable();
testTable.setId("123");
testTable.setDate("1234");
TestTable result = dynamoDBMapper.load(testTable);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26799 次 |
| 最近记录: |