Search in sources :

Example 16 with ItemCollection

use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIGlobalSecondaryIndexExample method queryIndex.

public static void queryIndex(String indexName) {
    Table table = dynamoDB.getTable(tableName);
    System.out.println("\n***********************************************************\n");
    System.out.print("Querying index " + indexName + "...");
    Index index = table.getIndex(indexName);
    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();
    if (indexName == "CreateDateIndex") {
        System.out.println("Issues filed on 2013-11-01");
        querySpec.withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_date", "2013-11-01").withString(":v_issue", "A-"));
        items = index.query(querySpec);
    } else if (indexName == "TitleIndex") {
        System.out.println("Compilation errors");
        querySpec.withKeyConditionExpression("Title = :v_title and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_title", "Compilation error").withString(":v_issue", "A-"));
        items = index.query(querySpec);
    } else if (indexName == "DueDateIndex") {
        System.out.println("Items that are due on 2013-11-30");
        querySpec.withKeyConditionExpression("DueDate = :v_date").withValueMap(new ValueMap().withString(":v_date", "2013-11-30"));
        items = index.query(querySpec);
    } else {
        System.out.println("\nNo valid index name provided");
        return;
    }
    Iterator<Item> iterator = items.iterator();
    System.out.println("Query: printing results...");
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) Index(com.amazonaws.services.dynamodbv2.document.Index) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 17 with ItemCollection

use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPILocalSecondaryIndexExample method query.

public static void query(String indexName) {
    Table table = dynamoDB.getTable(tableName);
    System.out.println("\n***********************************************************\n");
    System.out.println("Querying table " + tableName + "...");
    QuerySpec querySpec = new QuerySpec().withConsistentRead(true).withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
    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");
        Index index = table.getIndex(indexName);
        querySpec.withKeyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_isopen", 1));
        querySpec.withProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");
        ItemCollection<QueryOutcome> items = index.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    } else if (indexName == "OrderCreationDateIndex") {
        System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2015.");
        System.out.println("Only the projected attributes are returned\n");
        Index index = table.getIndex(indexName);
        querySpec.withKeyConditionExpression("CustomerId = :v_custid and OrderCreationDate >= :v_orddate").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_orddate", 20150131));
        querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);
        ItemCollection<QueryOutcome> items = index.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    } else {
        System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
        querySpec.withKeyConditionExpression("CustomerId = :v_custid").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com"));
        ItemCollection<QueryOutcome> items = table.query(querySpec);
        Iterator<Item> iterator = items.iterator();
        System.out.println("Query: printing results...");
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) Iterator(java.util.Iterator) LocalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex) Index(com.amazonaws.services.dynamodbv2.document.Index) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) ItemCollection(com.amazonaws.services.dynamodbv2.document.ItemCollection) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 18 with ItemCollection

use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIQuery method findRepliesPostedWithinTimePeriod.

private static void findRepliesPostedWithinTimePeriod(String forumName, String threadSubject) {
    Table table = dynamoDB.getTable(tableName);
    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);
    String replyId = forumName + "#" + threadSubject;
    QuerySpec spec = new QuerySpec().withProjectionExpression("Message, ReplyDateTime, PostedBy").withKeyConditionExpression("Id = :v_id and ReplyDateTime between :v_start_dt and :v_end_dt").withValueMap(new ValueMap().withString(":v_id", replyId).withString(":v_start_dt", startDate).withString(":v_end_dt", endDate));
    ItemCollection<QueryOutcome> items = table.query(spec);
    System.out.println("\nfindRepliesPostedWithinTimePeriod results:");
    Iterator<Item> iterator = items.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) SimpleDateFormat(java.text.SimpleDateFormat) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) Date(java.util.Date) Item(com.amazonaws.services.dynamodbv2.document.Item) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) SimpleDateFormat(java.text.SimpleDateFormat)

Example 19 with ItemCollection

use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPIQuery method findRepliesUsingAFilterExpression.

private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) {
    Table table = dynamoDB.getTable(tableName);
    String replyId = forumName + "#" + threadSubject;
    QuerySpec spec = new QuerySpec().withProjectionExpression("Message, ReplyDateTime, PostedBy").withKeyConditionExpression("Id = :v_id").withFilterExpression("PostedBy = :v_postedby").withValueMap(new ValueMap().withString(":v_id", replyId).withString(":v_postedby", "User B"));
    ItemCollection<QueryOutcome> items = table.query(spec);
    System.out.println("\nfindRepliesUsingAFilterExpression results:");
    Iterator<Item> iterator = items.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next().toJSONPretty());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QueryOutcome(com.amazonaws.services.dynamodbv2.document.QueryOutcome) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)

Example 20 with ItemCollection

use of com.amazonaws.services.dynamodbv2.document.ItemCollection in project aws-doc-sdk-examples by awsdocs.

the class MoviesScan method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Movies");
    ScanSpec scanSpec = new ScanSpec().withProjectionExpression("#yr, title, info.rating").withFilterExpression("#yr between :start_yr and :end_yr").withNameMap(new NameMap().with("#yr", "year")).withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959));
    try {
        ItemCollection<ScanOutcome> items = table.scan(scanSpec);
        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            Item item = iter.next();
            System.out.println(item.toString());
        }
    } catch (Exception e) {
        System.err.println("Unable to scan the table:");
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) ScanOutcome(com.amazonaws.services.dynamodbv2.document.ScanOutcome) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) NameMap(com.amazonaws.services.dynamodbv2.document.utils.NameMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) ScanSpec(com.amazonaws.services.dynamodbv2.document.spec.ScanSpec)

Aggregations

QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)18 Item (com.amazonaws.services.dynamodbv2.document.Item)13 Table (com.amazonaws.services.dynamodbv2.document.Table)13 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)11 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10 Test (org.testng.annotations.Test)6 Date (java.util.Date)5 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 ScanOutcome (com.amazonaws.services.dynamodbv2.document.ScanOutcome)3 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 Index (com.amazonaws.services.dynamodbv2.document.Index)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2 TransactionConflictException (com.amazonaws.services.dynamodbv2.model.TransactionConflictException)2 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)2