Package com.amazonaws.services.dynamodbv2.document

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


*/
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


            System.out.println(result);
        }
    }

    private Item newItem() {
        Item item = new Item()
            .withString(HASH_KEY_NAME, "foo")
            .withBinary("binary", new byte[]{1,2,3,4})
            .withBinarySet("binarySet", new byte[]{5,6}, new byte[]{7,8})
            .withBoolean("booleanTrue", true)
            .withBoolean("booleanFalse", false)
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:
//        {
//            "last_name" : "Barr",
//            "children" : [ "SJB", "ASB", "CGB", "BGB", "GTB" ],
//            "first_name" : "Jeff",
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:
//        {
//            "first_name" : "Jeff",
//            "myRangeKey" : 1,
//            "person_id" : 123,
View Full Code Here

                "foo", 1,
                "foo", 2,
                "foo", 3)
            // you can add a bunch of items to put in one go
            .withItemsToPut(
                new Item()
                    .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 111)
                    .withString("someStringAttr", "someStrVal1")
                    .withInt("someIntAttr", 111),
                new Item()
                    .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 222)
                    .withString("someStringAttr", "someStrVal2")
                    .withInt("someIntAttr", 222),
                new Item()
                    .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 333)
                    .withString("someStringAttr", "someStrVal3")
                    .withInt("someIntAttr", 333))
            // or you can take it slow and add one key to delete at a time
            .addHashAndRangePrimaryKeyToDelete(
                HASH_KEY_NAME, "foo", RANGE_KEY_NAME, 4)
            .addHashAndRangePrimaryKeyToDelete(
                HASH_KEY_NAME, "foo", RANGE_KEY_NAME, 5)
            // or you can take it slow and add one item to put at a time
            .addItemToPut(new Item()
                .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 444)
                .withString("someStringAttr", "someStrVal4")
                .withInt("someIntAttr", 444))
            .addItemToPut(new Item()
                .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 555)
                .withString("someStringAttr", "someStrVal5")
                .withInt("someIntAttr", 555))
            ;
        BatchWriteItemOutcome outcome = dynamo.batchWriteItem(tableWriteItems);
View Full Code Here

            new TableWriteItems(TABLE_NAME)
                .withHashAndRangeKeysToDelete(HASH_KEY_NAME, RANGE_KEY_NAME,
                    "foo", 1,
                    "foo", 2)
                .withItemsToPut(
                    new Item()
                        .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 666)
                        .withString("someStringAttr", "someStrVal6")
                        .withInt("someIntAttr", 666),
                    new Item()
                        .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 777)
                        .withString("someStringAttr", "someStrVal7")
                        .withInt("someIntAttr", 777)),
            // 2nd table
            new TableWriteItems(F_UpdateItemTest.TABLE_NAME)
                .withHashAndRangeKeysToDelete(F_UpdateItemTest.HASH_KEY, F_UpdateItemTest.RANGE_KEY,
                        F_UpdateItemTest.FIRST_CUSTOMER_ID, F_UpdateItemTest.ADDRESS_TYPE_HOME,
                        F_UpdateItemTest.FIRST_CUSTOMER_ID, F_UpdateItemTest.ADDRESS_TYPE_WORK)
                .withItemsToPut(
                    new Item()
                        .withPrimaryKey(F_UpdateItemTest.HASH_KEY, 111,
                            F_UpdateItemTest.RANGE_KEY, F_UpdateItemTest.ADDRESS_TYPE_HOME)
                        .withString("AddressLine1", "crazy ave")
                        .withString("city", "crazy city")
                        .withString("state", "XX")
                        .withInt("zipcode", 99199),
                        new Item()
                    .withPrimaryKey(F_UpdateItemTest.HASH_KEY, 111,
                            F_UpdateItemTest.RANGE_KEY, F_UpdateItemTest.ADDRESS_TYPE_WORK)
                        .withString("AddressLine1", "silly ave")
                        .withString("city", "silly city")
                        .withString("state", "YY")
View Full Code Here

                "foo", 1,
                "foo", 2,
                "foo", 3)
            // you can add a bunch of items to put in one go
            .withItemsToPut(
                new Item()
                    .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 111)
                    .withString("someStringAttr", "someStrVal1")
                    .withInt("someIntAttr", 111),
                new Item()
                    .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 222)
                    .withString("someStringAttr", "someStrVal2")
                    .withInt("someIntAttr", 222),
                new Item()
                    .withPrimaryKey(HASH_KEY_NAME, "TestingPutItemInBatch", RANGE_KEY_NAME, 333)
                    .withString("someStringAttr", "someStrVal3")
                    .withInt("someIntAttr", 333));
        // unprocessed items from DynamoDB
        Map<String, List<WriteRequest>> unprocessed = null ;
View Full Code Here

    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");
            System.out.println("binary: " + Arrays.toString(binary));
            Set<byte[]> binarySet = item.getBinarySet("binarySet");
            for (byte[] ba: binarySet) {
                System.out.println("binary set element: " + Arrays.toString(ba));
            }
            boolean bTrue = item.getBoolean("booleanTrue");
            System.out.println("booleanTrue: " + bTrue);
            int intval = item.getInt("intAttr");
            System.out.println("intAttr: " + intval);
            List<Object> listval = item.getList("listAtr");
            System.out.println("listAtr: " + listval);
            Map<String,Object> mapval = item.getMap("mapAttr");
            System.out.println("mapAttr: " + mapval);
            Object nullval = item.get("nullAttr");
            System.out.println("nullAttr: " + nullval);
            BigDecimal numval = item.getNumber("numberAttr");
            System.out.println("numberAttr: " + numval);
            String strval = item.getString("stringAttr");
            System.out.println("stringAttr: " + strval);
            Set<String> strset = item.getStringSet("stringSetAttr");
            System.out.println("stringSetAttr: " + strset);
            // "Outcome" allows access to the low level result
            System.out.println("low level result: " + outcome.getGetItemResult());
        }
    }
View Full Code Here

                // to be returned.
                // This expression requires attribute name substitution for
                // "binary" which is a reserved word in DynamoDB
                "#binary, intAttr, stringAttr",
                new NameMap().with("#binary", "binary"));
            Item item = outcome.getItem();
            System.out.println("========== item " + i + " ==========");
            System.out.println(item);
            byte[] binary = item.getBinary("binary");
            System.out.println("binary: " + Arrays.toString(binary));
            int intval = item.getInt("intAttr");
            System.out.println("intAttr: " + intval);
            Set<String> strset = item.getStringSet("stringSetAttr");
            System.out.println("stringSetAttr: " + strset);
            // "Outcome" allows access to the low level result
            System.out.println("low level result: " + outcome.getGetItemResult());
        }
    }
View Full Code Here

        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 + " ==========");
            System.out.println(item);
            byte[] binary = item.getBinary("binary");
            System.out.println("binary: " + Arrays.toString(binary));
            int intval = item.getInt("intAttr");
            System.out.println("intAttr: " + intval);
            Set<String> strset = item.getStringSet("stringSetAttr");
            System.out.println("stringSetAttr: " + strset);
            // "Outcome" allows access to the low level result
            System.out.println("low level result: " + outcome.getGetItemResult());
        }
    }
View Full Code Here

TOP

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

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.