Example usage for com.amazonaws.services.dynamodbv2.model PutItemRequest PutItemRequest

List of usage examples for com.amazonaws.services.dynamodbv2.model PutItemRequest PutItemRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model PutItemRequest PutItemRequest.

Prototype

public PutItemRequest(String tableName, java.util.Map<String, AttributeValue> item) 

Source Link

Document

Constructs a new PutItemRequest object.

Usage

From source file:NYSELoad.java

License:Open Source License

private static void putItem(String tableName, Map<String, AttributeValue> item) {
    PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
    dynamoDB.putItem(putItemRequest);/*ww w  . j a  v  a 2s  .  co m*/
}

From source file:AmazonDynamoDBSample_PutThrottled.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();/*from   ww  w  .j a  v  a 2 s . c  o m*/

    try {
        String tableName = "my-favorite-movies-table";

        // Create a table with a primary hash key named 'name', which holds
        // a string
        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition().withAttributeName("name")
                        .withAttributeType(ScalarAttributeType.S))
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L));

        // Create table if it does not exist yet
        TableUtils.createTableIfNotExists(dynamoDB, createTableRequest);
        // wait for the table to move into ACTIVE state
        TableUtils.waitUntilActive(dynamoDB, tableName);

        // Describe our new table
        DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
        System.out.println("Table Description: " + tableDescription);

        // Add an item
        Map<String, AttributeValue> item = newItem("Bill & Ted's Excellent Adventure", 1989, "****", "James",
                "Sara");
        PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

        // Add another item
        item = newItem("Airplane", 1980, "*****", "James", "Billy Bob");
        putItemRequest = new PutItemRequest(tableName, item);
        putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

        Thread[] thrds = new Thread[16];
        for (int i = 0; i < thrds.length; i++) {
            thrds[i] = newItemCreationThread(tableName, i);
            thrds[i].start();
        }
        for (Thread thrd : thrds) {
            thrd.join();
        }

        // Scan items for movies with a year attribute greater than 1985
        HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
        Condition condition = new Condition().withComparisonOperator(ComparisonOperator.GT.toString())
                .withAttributeValueList(new AttributeValue().withN("1985"));
        scanFilter.put("year", condition);
        ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
        ScanResult scanResult = dynamoDB.scan(scanRequest);
        System.out.println("Result: " + scanResult);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:AmazonDynamoDBSample_PutThrottled.java

License:Open Source License

private static Thread newItemCreationThread(String tableName, final int thrdNo) {
    final int ITEM_COUNTS = 1000 * 1000;
    return new Thread(() -> {
        for (int c = 0; c < ITEM_COUNTS; c++) {
            Map<String, AttributeValue> item = new HashMap<>();
            String pk = String.format("pk name: thrd %d creates %dth item", thrdNo, c);
            item.put("name", new AttributeValue(pk));

            PutItemRequest req = new PutItemRequest(tableName, item);
            req.setReturnValues(ReturnValue.ALL_OLD);
            PutItemResult res = null;//w w w  .  j  av  a 2 s . com
            long t = System.currentTimeMillis();
            try {
                res = dynamoDB.putItem(req);
            } catch (Exception exob) {
                exob.printStackTrace();
            }
            assert 0 == res.getAttributes().size();
            System.out.printf("%s. takes %d ms\n", pk, System.currentTimeMillis() - t);
        }
    });
}

From source file:ProductCategoryPriceIndex.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();/*from   ww  w. ja va2s .c  o m*/

    try {
        String tableName = "product_cat_pi";

        // Create table if it does not exist yet
        if (!Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is does not exist");
        }

        // Describe our new table
        DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
        System.out.println("Table Description: " + tableDescription);

        // Add an item
        Map<String, AttributeValue> item = newItem("TV", 2, "TV Type one", "TV Type two");
        PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:DynamoDB.java

License:Open Source License

public void addItem(String msgID, String msg) {
    // Add an item           
    Map<String, AttributeValue> item = newItem(msgID, msg);
    PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

}

From source file:AmazonDynamoDBSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();//w  w w  .j a va  2s  .c o m

    try {
        String tableName = "storyTable";

        // Create table if it does not exist yet
        if (Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is already ACTIVE");
        } else {
            // Create a table with a primary hash key named 'name', which holds a string
            CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                    .withKeySchema(
                            new KeySchemaElement().withAttributeName("storyID").withKeyType(KeyType.HASH))
                    .withAttributeDefinitions(new AttributeDefinition().withAttributeName("storyID")
                            .withAttributeType(ScalarAttributeType.N))
                    .withProvisionedThroughput(
                            new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L));
            TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest)
                    .getTableDescription();
            System.out.println("Created Table: " + createdTableDescription);

            // Wait for it to become active
            System.out.println("Waiting for " + tableName + " to become ACTIVE...");
            Tables.awaitTableToBecomeActive(dynamoDB, tableName);
        }

        // Describe our new table
        DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
        System.out.println("Table Description: " + tableDescription);
        Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
        System.out.println(currentTimestamp);
        ByteBuffer b = createBody();
        int id1 = 1;
        int id2 = 2;
        double d1 = 0;
        double d2 = 1;
        Timestamp inputTimestamp = currentTimestamp;
        // Add an item
        Map<String, AttributeValue> item = newItem(id1, "JAM", "This is JAM", b, inputTimestamp, inputTimestamp,
                d1, d2);
        System.out.println("here");

        PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
        System.out.println("here2");
        System.out.println(item);
        System.out.println(putItemRequest);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("here3");
        System.out.println("Result: " + putItemResult);

        // Add another item
        item = newItem(id2, "JAM2", "This is JAM2", b, inputTimestamp, inputTimestamp, d1, d2);
        putItemRequest = new PutItemRequest(tableName, item);
        putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

        // Scan items for movies with a year attribute greater than 1985
        //            HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
        //            Condition condition = new Condition()
        //                .withComparisonOperator(ComparisonOperator.GT)
        //                .withAttributeValueList(new AttributeValue());
        //            scanFilter.put("year", condition);
        //            ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
        //            ScanResult scanResult = dynamoDB.scan(scanRequest);
        //            System.out.println("Result: " + scanResult);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:sqsAlertPersist.java

License:Open Source License

public static void main(String[] args) throws Exception {

    // get credentials
    String user = "jreilly";
    AWSCredentials credentials = whgHelper.getCred(user);

    // use credentials to set access to SQS
    AmazonSQS sqs = whgHelper.setQueueAccess(credentials);

    // define queue that messages will be retrieved from
    String thisQueue = "alertPersist";
    String nextQueue = "alertCache";

    // set access to database with credentials
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    dynamoDB.setRegion(usEast1);//from   w  w  w . ja v  a 2 s  . co  m

    // check for table, create one if missing
    String tableName = "alerts";
    whgHelper.setTable(dynamoDB, tableName);

    while (1 > 0) {

        // pull list of current messages (up to 10) in the queue
        List<Message> messages = whgHelper.getMessagesFromQueue(thisQueue, sqs);
        System.out.println("Count of messages in " + thisQueue + ": " + messages.size());

        try {

            for (Message message : messages) {

                whgHelper.printMessage(message);
                for (Entry<String, String> entry : message.getAttributes().entrySet()) {
                    whgHelper.printMessageEntry(entry);
                }

                // Add an item to DynamoDB table
                Map<String, AttributeValue> item = whgHelper.newAlert(message.getBody());
                PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
                PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
                System.out.println();
                System.out.println("Result: " + putItemResult);

                // then send message to cache queue
                System.out.println("Sending messages to next queue.");
                sqs.sendMessage(new SendMessageRequest(nextQueue, message.getBody()));

                // delete message after sending to persist queue
                System.out.println("Deleting message from this queue.\n");
                String messageRecieptHandle = message.getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(thisQueue, messageRecieptHandle));

            }
            Thread.sleep(20000); // do nothing for 2000 miliseconds (2 second)

        } catch (AmazonServiceException ase) {
            whgHelper.errorMessagesAse(ase);
        } catch (AmazonClientException ace) {
            whgHelper.errorMessagesAce(ace);
        }
    }
}

From source file:br.com.faccilitacorretor.middleware.dynamo.AmazonDynamoDBSample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();//from w w  w. j  av a 2s  .  c o  m

    try {
        String tableName = "my-favorite-movies-table";

        // Create table if it does not exist yet
        if (Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is already ACTIVE");
        } else {
            // Create a table with a primary hash key named 'name', which holds a string
            CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                    .withKeySchema(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH))
                    .withAttributeDefinitions(new AttributeDefinition().withAttributeName("name")
                            .withAttributeType(ScalarAttributeType.S))
                    .withProvisionedThroughput(
                            new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L));
            TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest)
                    .getTableDescription();
            System.out.println("Created Table: " + createdTableDescription);

            // Wait for it to become active
            System.out.println("Waiting for " + tableName + " to become ACTIVE...");
            Tables.awaitTableToBecomeActive(dynamoDB, tableName);
        }

        // Describe our new table
        DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
        System.out.println("Table Description: " + tableDescription);

        // Add an item
        Map<String, AttributeValue> item = newItem("Bill & Ted's Excellent Adventure", 1989, "****", "James",
                "Sara");
        PutItemRequest putItemRequest = new PutItemRequest(tableName, item);
        PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

        // Add another item
        item = newItem("Airplane", 1980, "*****", "James", "Billy Bob");
        putItemRequest = new PutItemRequest(tableName, item);
        putItemResult = dynamoDB.putItem(putItemRequest);
        System.out.println("Result: " + putItemResult);

        // Scan items for movies with a year attribute greater than 1985
        HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
        Condition condition = new Condition().withComparisonOperator(ComparisonOperator.GT.toString())
                .withAttributeValueList(new AttributeValue().withN("1985"));
        scanFilter.put("year", condition);
        ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter);
        ScanResult scanResult = dynamoDB.scan(scanRequest);
        System.out.println("Result: " + scanResult);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:com.app.dynamoDb.DynamoFacebookUsers.java

License:Open Source License

public void insert(String UserID, String UserName, String Email) {
    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("UserID", new AttributeValue(UserID));
    item.put("UserName", new AttributeValue(UserName));
    item.put("Email", new AttributeValue(Email));
    System.out.println("inserted");

    PutItemRequest putItemRequest = new PutItemRequest("FacebookUsers", item);
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

}

From source file:com.app.dynamoDb.DynamoUser.java

License:Open Source License

@SuppressWarnings("null")
public void insert(String UserName, String Password, String Email) {

    ScanRequest scanRequest = new ScanRequest("Users");
    ScanResult scanResult = dynamoDB.scan(scanRequest);
    int s[] = new int[100];
    int i = 0;/*from  w w w.  j a v  a2 s.c om*/

    for (Map<String, AttributeValue> item : scanResult.getItems()) {
        s[i] = Integer.valueOf(item.get("UserID").getS());
        i++;
    }
    Arrays.sort(s);
    int max = s[s.length - 1];
    String userid = String.valueOf(max + 1);

    Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("UserID", new AttributeValue(userid));
    item.put("UserName", new AttributeValue(UserName));
    item.put("Password", new AttributeValue(Password));
    item.put("Email", new AttributeValue(Email));
    this.setUserID(userid);
    this.setUserName(UserName);
    this.setPassword(Password);
    this.setEmail(Email);

    PutItemRequest putItemRequest = new PutItemRequest("Users", item);
    PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

}