Sample
DynamoDBQueryExpression<Reply> queryExpression = new DynamoDBQueryExpression<Reply>()
.withHashKeyValues(replyKey)
.withRangeKeyCondition("ReplyDateTime", rangeKeyCondition);
List<Reply> betweenReplies = mapper.query(Reply.class, queryExpression);
for (Reply reply : betweenReplies) {
System.out.format("Id=%s, Message=%s, PostedBy=%s %n, PostedDateTime=%s %n",
reply.getId(), reply.getMessage(), reply.getPostedBy(), reply.getReplyDateTime() );
}
インスタンス生成
BasicAWSCredentials ac = new BasicAWSCredentials(access_key, secret_keykey);
AmazonDynamoDBClient client = new AmazonDynamoDBClient(ac);
データの取得(1件取得)
//検索のキーを指定
Key key = new Key(new AttributeValue().withN("5"));
//リクエストの生成
GetItemRequest request = new GetItemRequest("testdb", key);
//実行
GetItemResult item = client.getItem(request);
System.out.println(item);
//結果
{Item: {id={N: 5, }, 名前={S: しんさん5, }}, ConsumedCapacityUnits: 0.5, }
検索条件を入れる(複数件検索)
//リクエストの生成
ScanRequest request = new ScanRequest("testdb");
//取得する項目指定
request.withAttributesToGet("id", "名前");
//検索条件
Map<String, Condition> filter = new HashMap<String, Condition>();
filter.put("名前",
new Condition().
withComparisonOperator(ComparisonOperator.BETWEEN).//検索方法
withAttributeValueList(new AttributeValue().withS("更新3"), new AttributeValue().withS("更新7")));
request.withScanFilter(filter);
//実行
ScanResult result = client.scan(request);
//確認
for (Map<String, AttributeValue> row : result.getItems()) {
System.out.printf("id:%s, 名前:%s%n" ,
row.get("id").getN() ,
row.get("名前").getS());
}
検索条件を入れる2(複数件検索)
private static void FindUnansweredThread(DynamoDBMapper mapper, String forumName, String threadSubject) throws Exception{
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
//リクエストの生成
//ScanRequest request = new ScanRequest("testdb");
//取得する項目指定
//request.withAttributesToGet("id", "名前");
//検索条件を作る
Condition scanCondition = new Condition()
.withComparisonOperator(ComparisonOperator.EQ.toString())
.withAttributeValueList(new AttributeValue().withN("0"));
Condition scanCondition2 = new Condition().
withComparisonOperator(ComparisonOperator.BETWEEN).
withAttributeValueList(new AttributeValue().withS("更新3"), new AttributeValue().withS("更新7")));
long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L);
Date twoWeeksAgo = new Date();
twoWeeksAgo.setTime(twoWeeksAgoMilli);
String twoWeeksAgoStr = dateFormatter.format(twoWeeksAgo);
Condition scanCondition3 = new Condition()
.withComparisonOperator(ComparisonOperator.GT.toString())
.withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr.toString()));
//検索対象とする属性をKey,検索条件をValueとしてMapに保存
Map<String, Condition> scanFilter = new HashMap<String, Condition>();
scanFilter.put("Key1", scanCondition);
scanFilter.put("Key2", scanCondition2);
scanFilter.put("Key3", scanCondition3);
//request.withScanFilter(scanFilter);
//実行
//ScanResult result = client.scan(request);
List<Thread> unansweredThreads = mapper.scan(Thread.class, scanExpression);
//確認
for (Thread thread : unansweredThreads) {
System.out.format("Thread=%s, subject=%s, LastPostedDateTime=%s %n",
thread.getForumName(), thread.getSubject(), thread.getLastPostedDateTime() );
}
最終更新:2013年07月22日 02:05