Java Code Examples for com.amazonaws.services.dynamodbv2.model.QueryResult#getItems()
The following examples show how to use
com.amazonaws.services.dynamodbv2.model.QueryResult#getItems() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LowLevelQuery.java From aws-dynamodb-examples with Apache License 2.0 | 8 votes |
private static void findRepliesForAThread(String forumName, String threadSubject) { String replyId = forumName + "#" + threadSubject; Condition hashKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(replyId)); Map<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("Id", hashKeyCondition); QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withKeyConditions(keyConditions); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 2
Source File: LowLevelQuery.java From aws-dynamodb-examples with Apache License 2.0 | 6 votes |
private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) { Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject); Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); expressionAttributeValues.put(":val", new AttributeValue().withS("User B")); QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withKeyConditions(keyConditions) .withFilterExpression("PostedBy = :val") .withExpressionAttributeValues(expressionAttributeValues) .withProjectionExpression("Message, ReplyDateTime, PostedBy"); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 3
Source File: LowLevelQuery.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void findRepliesForAThread(String forumName, String threadSubject) { String replyId = forumName + "#" + threadSubject; Condition partitionKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS(replyId)); Map<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("Id", partitionKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withKeyConditions(keyConditions); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 4
Source File: LowLevelQuery.java From aws-dynamodb-examples with Apache License 2.0 | 6 votes |
private static void findRepliesInLast15DaysWithConfig(String forumName, String threadSubject) { long twoWeeksAgoMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L); Date twoWeeksAgo = new Date(); twoWeeksAgo.setTime(twoWeeksAgoMilli); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String twoWeeksAgoStr = df.format(twoWeeksAgo); Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr)); Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject); keyConditions.put("ReplyDateTime", rangeKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName(tableName) .withKeyConditions(keyConditions) .withProjectionExpression("Message, ReplyDateTime, PostedBy"); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 5
Source File: LowLevelQuery.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void findRepliesInLast15DaysWithConfig(String forumName, String threadSubject) { long twoWeeksAgoMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L); Date twoWeeksAgo = new Date(); twoWeeksAgo.setTime(twoWeeksAgoMilli); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String twoWeeksAgoStr = df.format(twoWeeksAgo); Condition sortKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr)); Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject); keyConditions.put("ReplyDateTime", sortKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withKeyConditions(keyConditions) .withProjectionExpression("Message, ReplyDateTime, PostedBy"); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 6
Source File: LowLevelQuery.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) { long startDateMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L); long endDateMilli = (new Date()).getTime() - (5L * 24L * 60L * 60L * 1000L); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String startDate = df.format(startDateMilli); String endDate = df.format(endDateMilli); Condition sortKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.BETWEEN.toString()) .withAttributeValueList(new AttributeValue().withS(startDate), new AttributeValue().withS(endDate)); Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject); keyConditions.put("ReplyDateTime", sortKeyCondition); QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withKeyConditions(keyConditions) .withProjectionExpression("Message, ReplyDateTime, PostedBy"); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 7
Source File: LowLevelQuery.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) { Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject); Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>(); expressionAttributeValues.put(":val", new AttributeValue().withS("User B")); QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withKeyConditions(keyConditions) .withFilterExpression("PostedBy = :val").withExpressionAttributeValues(expressionAttributeValues) .withProjectionExpression("Message, ReplyDateTime, PostedBy"); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 8
Source File: LowLevelQuery.java From aws-dynamodb-examples with Apache License 2.0 | 6 votes |
private static void findRepliesForAThreadSpecifyOptionalLimit(String forumName, String threadSubject) { Map<String, AttributeValue> lastEvaluatedKey = null; do { QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withKeyConditions(makeReplyKeyConditions(forumName, threadSubject)) .withLimit(1) .withExclusiveStartKey(lastEvaluatedKey); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } lastEvaluatedKey = result.getLastEvaluatedKey(); } while (lastEvaluatedKey != null); }
Example 9
Source File: QueryRecordReadRequest.java From emr-dynamodb-connector with Apache License 2.0 | 5 votes |
@Override protected PageResults<Map<String, AttributeValue>> fetchPage(RequestLimit lim) { // Read from DynamoDB RetryResult<QueryResult> retryResult = context.getClient().queryTable(tableName, context .getSplit().getFilterPushdown(), lastEvaluatedKey, lim.items, context.getReporter()); QueryResult result = retryResult.result; int retries = retryResult.retries; return new PageResults<>(result.getItems(), result.getLastEvaluatedKey(), result .getConsumedCapacity().getCapacityUnits(), retries); }
Example 10
Source File: LowLevelQuery.java From aws-dynamodb-examples with Apache License 2.0 | 5 votes |
private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) { long startDateMilli = (new Date()).getTime() - (15L*24L*60L*60L*1000L); long endDateMilli = (new Date()).getTime() - (5L*24L*60L*60L*1000L); java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String startDate = df.format(startDateMilli); String endDate = df.format(endDateMilli); Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.BETWEEN.toString()) .withAttributeValueList( new AttributeValue().withS(startDate), new AttributeValue().withS(endDate)); Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject); keyConditions.put("ReplyDateTime", rangeKeyCondition); QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withKeyConditions(keyConditions) .withProjectionExpression("Message, ReplyDateTime, PostedBy"); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } }
Example 11
Source File: DynamoDBMetadataDeleter.java From geowave with Apache License 2.0 | 5 votes |
@Override public boolean delete(final MetadataQuery metadata) { // the nature of metadata deleter is that primary ID is always // well-defined and it is deleting a single entry at a time final String tableName = operations.getMetadataTableName(metadataType); final QueryRequest queryRequest = new QueryRequest(tableName); if (metadata.hasSecondaryId()) { queryRequest.withFilterExpression( DynamoDBOperations.METADATA_SECONDARY_ID_KEY + " = :secVal").addExpressionAttributeValuesEntry( ":secVal", new AttributeValue().withB(ByteBuffer.wrap(metadata.getSecondaryId()))); } queryRequest.withKeyConditionExpression( DynamoDBOperations.METADATA_PRIMARY_ID_KEY + " = :priVal").addExpressionAttributeValuesEntry( ":priVal", new AttributeValue().withB(ByteBuffer.wrap(metadata.getPrimaryId()))); final QueryResult queryResult = operations.getClient().query(queryRequest); for (final Map<String, AttributeValue> entry : queryResult.getItems()) { final Map<String, AttributeValue> key = new HashMap<>(); key.put( DynamoDBOperations.METADATA_PRIMARY_ID_KEY, entry.get(DynamoDBOperations.METADATA_PRIMARY_ID_KEY)); key.put( DynamoDBOperations.METADATA_TIMESTAMP_KEY, entry.get(DynamoDBOperations.METADATA_TIMESTAMP_KEY)); operations.getClient().deleteItem(tableName, key); } return true; }
Example 12
Source File: MultiRecordIterator.java From dynamodb-janusgraph-storage-backend with Apache License 2.0 | 5 votes |
private StaticRecordIterator buildRecordIteratorFromQueryResult(final QueryResult queryResult) { final List<Entry> entries = Lists.newLinkedList(); for (Map<String, AttributeValue> item : queryResult.getItems()) { // DynamoDB's between includes the end of the range, but Titan's slice queries expect the end key to be exclusive final Entry entry = new EntryBuilder(item).slice(rangeKeySliceQuery.getSliceStart(), rangeKeySliceQuery.getSliceEnd()) .build(); if (entry != null) { entries.add(entry); } } return new StaticRecordIterator(entries); }
Example 13
Source File: LowLevelQuery.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
private static void findRepliesForAThreadSpecifyOptionalLimit(String forumName, String threadSubject) { Map<String, AttributeValue> lastEvaluatedKey = null; do { QueryRequest queryRequest = new QueryRequest().withTableName(tableName) .withKeyConditions(makeReplyKeyConditions(forumName, threadSubject)).withLimit(1) .withExclusiveStartKey(lastEvaluatedKey); QueryResult result = client.query(queryRequest); for (Map<String, AttributeValue> item : result.getItems()) { printItem(item); } lastEvaluatedKey = result.getLastEvaluatedKey(); } while (lastEvaluatedKey != null); }
Example 14
Source File: LowLevelLocalSecondaryIndexExample.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
public static void query(String indexName) { System.out.println("\n***********************************************************\n"); System.out.println("Querying table " + tableName + "..."); QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withConsistentRead(true) .withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); HashMap<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("CustomerId", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("[email protected]"))); if (indexName == "IsOpenIndex") { System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open."); System.out.println("Only a user-specified list of attributes are returned\n"); queryRequest.setIndexName(indexName); keyConditions.put("IsOpen", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withN("1"))); // ProjectionExpression queryRequest.setProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus"); } else if (indexName == "OrderCreationDateIndex") { System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2013."); System.out.println("Only the projected attributes are returned\n"); queryRequest.setIndexName(indexName); keyConditions.put("OrderCreationDate", new Condition().withComparisonOperator(ComparisonOperator.GT) .withAttributeValueList(new AttributeValue().withN("20130131"))); // Select queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES); } else { System.out.println("\nNo index: All of Bob's orders, by OrderId:\n"); } queryRequest.setKeyConditions(keyConditions); QueryResult result = client.query(queryRequest); List<Map<String, AttributeValue>> items = result.getItems(); Iterator<Map<String, AttributeValue>> itemsIter = items.iterator(); while (itemsIter.hasNext()) { Map<String, AttributeValue> currentItem = itemsIter.next(); Iterator<String> currentItemIter = currentItem.keySet().iterator(); while (currentItemIter.hasNext()) { String attr = (String) currentItemIter.next(); if (attr == "OrderId" || attr == "IsOpen" || attr == "OrderCreationDate") { System.out.println(attr + "---> " + currentItem.get(attr).getN()); } else { System.out.println(attr + "---> " + currentItem.get(attr).getS()); } } System.out.println(); } System.out.println("\nConsumed capacity: " + result.getConsumedCapacity() + "\n"); }
Example 15
Source File: DynamoDBServiceImpl2.java From Serverless-Programming-Cookbook with MIT License | 5 votes |
@Override public final Response query(final Request request) { final Map<String, AttributeValue> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":" + request.getPartitionKey(), new AttributeValue(request.getPartitionKeyValue())); final String keyConditionExpression = request.getPartitionKey() + "=:" + request.getPartitionKey(); QueryRequest queryRequest = new QueryRequest() .withTableName(request.getTableName()) .withKeyConditionExpression(keyConditionExpression) .withExpressionAttributeValues(expressionAttributeValues); StringBuilder filterExpressionBuilder; if (request.getFilterData() != null) { filterExpressionBuilder = new StringBuilder(); processFilterData(request, filterExpressionBuilder, expressionAttributeValues); //Add to QueryRequest. queryRequest.withFilterExpression(filterExpressionBuilder.toString()); } final QueryResult queryResult = dynamoDBClient.query(queryRequest); final StringBuilder response = new StringBuilder(); response.append("PK of items read with query (V2): "); for (Map<String, AttributeValue> item : queryResult.getItems()) { response.append(prepareKeyStr(item, request)); } return new Response(response.toString(), null); }
Example 16
Source File: LowLevelLocalSecondaryIndexExample.java From aws-dynamodb-examples with Apache License 2.0 | 4 votes |
public static void query(String indexName) { System.out.println("\n***********************************************************\n"); System.out.println("Querying table " + tableName + "..."); QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withConsistentRead(true).withScanIndexForward(true) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); HashMap<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put( "CustomerId", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue() .withS("[email protected]"))); if (indexName == "IsOpenIndex") { System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open."); System.out.println("Only a user-specified list of attributes are returned\n"); queryRequest.setIndexName(indexName); keyConditions.put( "IsOpen", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withN("1"))); // ProjectionExpression queryRequest.setProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus"); } else if (indexName == "OrderCreationDateIndex") { System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2013."); System.out.println("Only the projected attributes are returned\n"); queryRequest.setIndexName(indexName); keyConditions.put("OrderCreationDate", new Condition() .withComparisonOperator(ComparisonOperator.GT) .withAttributeValueList(new AttributeValue() .withN("20130131"))); // Select queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES); } else { System.out.println("\nNo index: All of Bob's orders, by OrderId:\n"); } queryRequest.setKeyConditions(keyConditions); QueryResult result = client.query(queryRequest); List<Map<String, AttributeValue>> items = result.getItems(); Iterator<Map<String, AttributeValue>> itemsIter = items.iterator(); while (itemsIter.hasNext()) { Map<String, AttributeValue> currentItem = itemsIter.next(); Iterator<String> currentItemIter = currentItem.keySet().iterator(); while (currentItemIter.hasNext()) { String attr = (String) currentItemIter.next(); if (attr == "OrderId" || attr == "IsOpen" || attr == "OrderCreationDate") { System.out.println(attr + "---> " + currentItem.get(attr).getN()); } else { System.out.println(attr + "---> " + currentItem.get(attr).getS()); } } System.out.println(); } System.out.println("\nConsumed capacity: " + result.getConsumedCapacity() + "\n"); }
Example 17
Source File: LowLevelGlobalSecondaryIndexExample.java From aws-doc-sdk-examples with Apache License 2.0 | 4 votes |
public static void queryIndex(String indexName) { System.out.println("\n***********************************************************\n"); System.out.print("Querying index " + indexName + "..."); QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withIndexName(indexName) .withScanIndexForward(true); HashMap<String, Condition> keyConditions = new HashMap<String, Condition>(); if (indexName == "CreateDateIndex") { System.out.println("Issues filed on 2013-11-01"); keyConditions.put("CreateDate", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("2013-11-01"))); keyConditions.put("IssueId", new Condition().withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS("A-"))); } else if (indexName == "TitleIndex") { System.out.println("Compilation errors"); keyConditions.put("Title", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("Compilation error"))); keyConditions.put("IssueId", new Condition().withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS("A-"))); // Select queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES); } else if (indexName == "DueDateIndex") { System.out.println("Items that are due on 2013-11-30"); keyConditions.put("DueDate", new Condition().withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("2013-11-30"))); // Select queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES); } else { System.out.println("\nNo valid index name provided"); return; } queryRequest.setKeyConditions(keyConditions); QueryResult result = client.query(queryRequest); List<Map<String, AttributeValue>> items = result.getItems(); Iterator<Map<String, AttributeValue>> itemsIter = items.iterator(); System.out.println(); while (itemsIter.hasNext()) { Map<String, AttributeValue> currentItem = itemsIter.next(); Iterator<String> currentItemIter = currentItem.keySet().iterator(); while (currentItemIter.hasNext()) { String attr = (String) currentItemIter.next(); if (attr == "Priority") { System.out.println(attr + "---> " + currentItem.get(attr).getN()); } else { System.out.println(attr + "---> " + currentItem.get(attr).getS()); } } System.out.println(); } }
Example 18
Source File: LowLevelGlobalSecondaryIndexExample.java From aws-dynamodb-examples with Apache License 2.0 | 4 votes |
public static void queryIndex(String indexName) { System.out.println ("\n***********************************************************\n"); System.out.print("Querying index " + indexName + "..."); QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withIndexName(indexName) .withScanIndexForward(true); HashMap<String, Condition> keyConditions = new HashMap<String, Condition>(); if (indexName == "CreateDateIndex") { System.out.println("Issues filed on 2013-11-01"); keyConditions.put("CreateDate",new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue() .withS("2013-11-01"))); keyConditions.put("IssueId",new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS("A-"))); } else if (indexName == "TitleIndex") { System.out.println("Compilation errors"); keyConditions.put("Title",new Condition() .withComparisonOperator( ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue() .withS("Compilation error"))); keyConditions.put("IssueId", new Condition() .withComparisonOperator(ComparisonOperator.BEGINS_WITH) .withAttributeValueList(new AttributeValue().withS("A-"))); // Select queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES); } else if (indexName == "DueDateIndex") { System.out.println("Items that are due on 2013-11-30"); keyConditions.put("DueDate",new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList(new AttributeValue().withS("2013-11-30"))); // Select queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES); } else { System.out.println("\nNo valid index name provided"); return; } queryRequest.setKeyConditions(keyConditions); QueryResult result = client.query(queryRequest); List<Map<String, AttributeValue>> items = result.getItems(); Iterator<Map<String, AttributeValue>> itemsIter = items.iterator(); System.out.println(); while (itemsIter.hasNext()) { Map<String, AttributeValue> currentItem = itemsIter.next(); Iterator<String> currentItemIter = currentItem.keySet().iterator(); while (currentItemIter.hasNext()) { String attr = (String) currentItemIter.next(); if (attr == "Priority" ) { System.out.println(attr + "---> " + currentItem.get(attr).getN()); } else { System.out.println(attr + "---> " + currentItem.get(attr).getS()); } } System.out.println(); } }