Search in sources :

Example 11 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-doc-sdk-examples by awsdocs.

the class Query method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    Query <table> <partitionkey> <partitionkeyvalue>\n\n" + "Where:\n" + "    table - the table to put the item in.\n" + "    partitionkey  - partition key name of the table.\n" + "    partitionkeyvalue - value of the partition key that should match.\n\n" + "Example:\n" + "    Query GreetingsTable Language eng \n";
    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String partition_key_name = args[1];
    String partition_key_val = args[2];
    String partition_alias = "#a";
    System.out.format("Querying %s", table_name);
    System.out.println("");
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    // set up an alias for the partition key name in case it's a reserved word
    HashMap<String, String> attrNameAlias = new HashMap<String, String>();
    attrNameAlias.put(partition_alias, partition_key_name);
    // set up mapping of the partition name with the value
    HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
    attrValues.put(":" + partition_key_name, new AttributeValue().withS(partition_key_val));
    QueryRequest queryReq = new QueryRequest().withTableName(table_name).withKeyConditionExpression(partition_alias + " = :" + partition_key_name).withExpressionAttributeNames(attrNameAlias).withExpressionAttributeValues(attrValues);
    try {
        QueryResult response = ddb.query(queryReq);
        System.out.println(response.getCount());
    } catch (AmazonDynamoDBException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 12 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-doc-sdk-examples by awsdocs.

the class LowLevelQuery method findRepliesForAThread.

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);
    }
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap)

Example 13 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project aws-doc-sdk-examples by awsdocs.

the class LowLevelQuery method findRepliesPostedWithinTimePeriod.

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);
    }
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) SimpleDateFormat(java.text.SimpleDateFormat)

Example 14 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project camel by apache.

the class QueryCommand method execute.

@Override
public void execute() {
    QueryResult result = ddbClient.query(new QueryRequest().withTableName(determineTableName()).withAttributesToGet(determineAttributeNames()).withConsistentRead(determineConsistentRead()).withExclusiveStartKey(determineStartKey()).withKeyConditions(determineKeyConditions()).withExclusiveStartKey(determineStartKey()).withLimit(determineLimit()).withScanIndexForward(determineScanIndexForward()));
    Map tmp = new HashMap<>();
    tmp.put(DdbConstants.ITEMS, result.getItems());
    tmp.put(DdbConstants.LAST_EVALUATED_KEY, result.getLastEvaluatedKey());
    tmp.put(DdbConstants.CONSUMED_CAPACITY, result.getConsumedCapacity());
    tmp.put(DdbConstants.COUNT, result.getCount());
    addToResults(tmp);
}
Also used : QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 15 with QueryResult

use of com.ibm.watson.discovery.v2.model.QueryResult in project camel by apache.

the class AmazonDDBClientMock method query.

@SuppressWarnings("unchecked")
@Override
public QueryResult query(QueryRequest queryRequest) {
    this.queryRequest = queryRequest;
    ConsumedCapacity consumed = new ConsumedCapacity();
    consumed.setCapacityUnits(1.0);
    Map<String, AttributeValue> lastEvaluatedKey = new HashMap<String, AttributeValue>();
    lastEvaluatedKey.put("1", new AttributeValue("LAST_KEY"));
    return new QueryResult().withConsumedCapacity(consumed).withCount(1).withItems(getAttributes()).withLastEvaluatedKey(lastEvaluatedKey);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) HashMap(java.util.HashMap) ConsumedCapacity(com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)

Aggregations

QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)21 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)19 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)14 HashMap (java.util.HashMap)9 Condition (com.amazonaws.services.dynamodbv2.model.Condition)8 Map (java.util.Map)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)4 AmazonClientException (com.amazonaws.AmazonClientException)2 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)2 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)2 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Iterator (java.util.Iterator)2 AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 Document (com.amazonaws.mobileconnectors.dynamodbv2.document.datatype.Document)1 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)1 ComparisonOperator (com.amazonaws.services.dynamodbv2.model.ComparisonOperator)1 ImmutableMap (com.google.common.collect.ImmutableMap)1