Package com.amazonaws.services.dynamodbv2.document

Examples of com.amazonaws.services.dynamodbv2.document.Table


* Sample code to put items to a DynamoDB table.
*/
public class B_PutItemTest extends AbstractQuickStart {
    @Test
    public void howToPutItems() {
        Table table = dynamo.getTable(TABLE_NAME);
        Item item = newItem();
        int intAttrVal = item.getInt("intAttr");
        // Creates 10 items of range key values from 1 to 10
        for (int i=1; i <= 10; i++) {
            PutItemOutcome result = table.putItem(
                item.withInt(RANGE_KEY_NAME, i)
                .withInt("intAttr", intAttrVal+i)
            );
            System.out.println(result);
        }
View Full Code Here


*/
public class Z_DeleteTableTest extends AbstractQuickStart {
//    @Test
    public void howToDeleteTable() throws InterruptedException {
        String TABLE_NAME = "myTableForMidLevelApi";
        Table table = dynamo.getTable(TABLE_NAME);
        // Wait for the table to become active or deleted
        TableDescription desc = table.waitForActiveOrDelete();
        if (desc == null) {
            System.out.println("Table " + table.getTableName()
                    + " does not exist.");
        } else {
            table.delete();
            // No need to wait, but you could
            table.waitForDelete();
            System.out.println("Table " + table.getTableName()
                    + " has been deleted");
        }
    }
View Full Code Here

                +   "} ,"
                +   "\"children\" :"
                +   "[ \"SJB\" , \"ASB\" , \"CGB\" , \"BGB\" , \"GTB\" ]"
                + "}"
                ;
        Table table = dynamo.getTable(TABLE_NAME);
        Item item = new Item()
            .withPrimaryKey(HASH_KEY_NAME, "howToPutItems_withJSONDoc", RANGE_KEY_NAME, 1)
            // Store JSON document
            .withJSON("document", json);
        table.putItem(item);
        // Retrieve the entire document and the entire document only
        Item documentItem = table.getItem(new GetItemSpec()
            .withPrimaryKey(HASH_KEY_NAME, "howToPutItems_withJSONDoc", RANGE_KEY_NAME, 1)
            .withAttributesToGet("document"));
        System.out.println(documentItem.getJSON("document"));
        // Output: {"last_name":"Barr","children":["SJB","ASB","CGB","BGB","GTB"],"first_name":"Jeff","person_id":123,"current_city":"Tokyo","next_haircut":{"month":10,"year":2014,"day":30}}
        System.out.println(documentItem.getJSONPretty("document"));
        // Output:
//        {
//            "last_name" : "Barr",
//            "children" : [ "SJB", "ASB", "CGB", "BGB", "GTB" ],
//            "first_name" : "Jeff",
//            "person_id" : 123,
//            "current_city" : "Tokyo",
//            "next_haircut" : {
//              "month" : 10,
//              "year" : 2014,
//              "day" : 30
//            }
//        }        // Retrieve part of a document. Perhaps I need the next_haircut and nothing else
        Item partialDocItem = table.getItem(new GetItemSpec()
            .withPrimaryKey(HASH_KEY_NAME, "howToPutItems_withJSONDoc", RANGE_KEY_NAME, 1)
            .withProjectionExpression("document.next_haircut"))
            ;
        System.out.println(partialDocItem);
        // Output: { Item: {document={next_haircut={month=10, year=2014, day=30}}} }
        // I can update part of a document. Here's how I would change my current_city back to Seattle:
        table.updateItem(new UpdateItemSpec()
            .withPrimaryKey(HASH_KEY_NAME, "howToPutItems_withJSONDoc", RANGE_KEY_NAME, 1)
            .withUpdateExpression("SET document.current_city = :city")
            .withValueMap(new ValueMap().withString(":city", "Seattle"))
        );
        // Retrieve the entire item
        Item itemUpdated = table.getItem(HASH_KEY_NAME, "howToPutItems_withJSONDoc", RANGE_KEY_NAME, 1);
        System.out.println(itemUpdated);
        // Output: { Item: {document={last_name=Bar, children=[SJB, ASB, CGB, BGB, GTB], first_name=Jeff, current_city=Seattle, next_haircut={month=10, year=2014, day=30}}, myRangeKey=1, myHashKey=B_PutItemJsonTest} }
        System.out.println(itemUpdated.getJSONPretty("document"));
        // Output:
//        {
View Full Code Here

                +   "} ,"
                +   "\"children\" :"
                +   "[ \"SJB\" , \"ASB\" , \"CGB\" , \"BGB\" , \"GTB\" ]"
                + "}"
                ;
        Table table = dynamo.getTable(TABLE_NAME);
        Item item = Item.fromJSON(json)
             // We don't even need to set the primary key if it's already included in the JSON document
            .withPrimaryKey(HASH_KEY_NAME, "howToPut_TopLevelJSON", RANGE_KEY_NAME, 1);
        table.putItem(item);
        // Retrieve the entire document and the entire document only
        Item documentItem = table.getItem(new GetItemSpec()
            .withPrimaryKey(HASH_KEY_NAME, "howToPut_TopLevelJSON", RANGE_KEY_NAME, 1));
        System.out.println(documentItem.toJSON());
        // Output: {"first_name":"Jeff","myRangeKey":1,"person_id":123,"current_city":"Tokyo","next_haircut":{"month":10,"year":2014,"day":30},"last_name":"Barr","children":["SJB","ASB","CGB","BGB","GTB"],"myHashKey":"howToPut_TopLevelJSON"}
        System.out.println(documentItem.toJSONPretty());
        // Output:
//        {
//            "first_name" : "Jeff",
//            "myRangeKey" : 1,
//            "person_id" : 123,
//            "current_city" : "Tokyo",
//            "next_haircut" : {
//              "month" : 10,
//              "year" : 2014,
//              "day" : 30
//            },
//            "last_name" : "Barr",
//            "children" : [ "SJB", "ASB", "CGB", "BGB", "GTB" ],
//            "myHashKey" : "howToPut_TopLevelJSON"
//          }
        // Retrieve part of a document. Perhaps I need the next_haircut and nothing else
        Item partialDocItem = table.getItem(new GetItemSpec()
            .withPrimaryKey(HASH_KEY_NAME, "howToPut_TopLevelJSON", RANGE_KEY_NAME, 1)
            .withProjectionExpression("next_haircut"))
            ;
        System.out.println(partialDocItem);
        // Output: { Item: {next_haircut={month=10, year=2014, day=30}} }
        // I can update part of a document. Here's how I would change my current_city back to Seattle:
        table.updateItem(new UpdateItemSpec()
            .withPrimaryKey(HASH_KEY_NAME, "howToPut_TopLevelJSON", RANGE_KEY_NAME, 1)
            .withUpdateExpression("SET current_city = :city")
            .withValueMap(new ValueMap().withString(":city", "Seattle"))
        );
        // Retrieve the entire item
        Item itemUpdated = table.getItem(HASH_KEY_NAME, "howToPut_TopLevelJSON", RANGE_KEY_NAME, 1);
        System.out.println(itemUpdated);
        // Output: { Item: {first_name=Jeff, myRangeKey=1, person_id=123, current_city=Seattle, next_haircut={month=10, year=2014, day=30}, last_name=Barr, children=[SJB, ASB, CGB, BGB, GTB], myHashKey=howToPut_TopLevelJSON} }
        System.out.println(itemUpdated.toJSONPretty());
        // Output:
//        {
View Full Code Here

        new B_PutItemTest().howToPutItems();
    }

    @Test
    public void howToGetItemOutcomes() {
        Table table = dynamo.getTable(TABLE_NAME);
        for (int i=1; i <= 10; i++) {
            GetItemOutcome outcome = table.getItemOutcome(
                HASH_KEY_NAME, "foo", RANGE_KEY_NAME, i);
            Item item = outcome.getItem();
            System.out.println("========== item " + i + " ==========");
            System.out.println(item);
            byte[] binary = item.getBinary("binary");
View Full Code Here

        }
    }

    @Test
    public void howToUseProjectionExpression() {
        Table table = dynamo.getTable(TABLE_NAME);
        for (int i=1; i <= 10; i++) {
            GetItemOutcome outcome = table.getItemOutcome(
                HASH_KEY_NAME, "foo", RANGE_KEY_NAME, i,
                // Here is the projection expression to select 3 attributes
                // to be returned.
                // This expression requires attribute name substitution for
                // "binary" which is a reserved word in DynamoDB
View Full Code Here

        }
    }

    @Test
    public void howToUseGetItemSpec() {
        Table table = dynamo.getTable(TABLE_NAME);
        for (int i=1; i <= 10; i++) {
            GetItemOutcome outcome = table.getItemOutcome(new GetItemSpec()
                .withPrimaryKey(HASH_KEY_NAME, "foo", RANGE_KEY_NAME, i)
                .withProjectionExpression("#binary, intAttr, stringAttr")
                .withNameMap(new NameMap().with("#binary", "binary")));
            Item item = outcome.getItem();
            System.out.println("========== item " + i + " ==========");
View Full Code Here

        }
    }

    @Test
    public void getNonExistentItem() {
        Table table = dynamo.getTable(TABLE_NAME);
        GetItemOutcome outcome = table.getItemOutcome(
            HASH_KEY_NAME, "bar", RANGE_KEY_NAME, 99);
        Item item = outcome.getItem();
        Assert.assertNull(item);
        // "Outcome" allows access to the low level result
        System.out.println("low level result: " + outcome.getGetItemResult());
View Full Code Here

        createTable(dynamo);
        fillInData(dynamo);
    }

    private static void createTable(DynamoDB dynamo) throws InterruptedException {
        Table table = dynamo.getTable(TABLE_NAME);
        TableDescription desc = table.waitForActiveOrDelete();
        if (desc == null) {
            // table doesn't exist; let's create it
            KeySchemaElement hashKey =
                new KeySchemaElement(HASH_KEY, KeyType.HASH);
            KeySchemaElement rangeKey =
                new KeySchemaElement(RANGE_KEY, KeyType.RANGE);
            CreateTableRequest createTableRequest =
                new CreateTableRequest(TABLE_NAME, Arrays.asList(hashKey, rangeKey))
                .withAttributeDefinitions(
                    new AttributeDefinition(HASH_KEY, ScalarAttributeType.N),
                    new AttributeDefinition(RANGE_KEY, ScalarAttributeType.S))
                .withProvisionedThroughput(
                    new ProvisionedThroughput(READ_CAPACITY, WRITE_CAPACITY));
            table = dynamo.createTable(createTableRequest);
            table.waitForActive();
        }
    }
View Full Code Here

            table.waitForActive();
        }
    }

    private static void fillInData(DynamoDB dynamo) {
        Table table = dynamo.getTable(TABLE_NAME);
        table.putItem(new Item().withLong(HASH_KEY, FIRST_CUSTOMER_ID)
            .withString(RANGE_KEY, ADDRESS_TYPE_WORK)
            .withString("AddressLine1", "1918 8th Aven")
            .withString("city", "seattle")
            .withString("state", "WA")
            .withInt("zipcode", 98104));
        table.putItem(new Item().withLong(HASH_KEY, FIRST_CUSTOMER_ID)
            .withString(RANGE_KEY, ADDRESS_TYPE_HOME)
            .withString("AddressLine1", "15606 NE 40th ST")
            .withString("city", "redmond")
            .withString("state", "WA")
            .withInt("zipcode", 98052));
View Full Code Here

TOP

Related Classes of com.amazonaws.services.dynamodbv2.document.Table

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.