Example usage for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient putItem

List of usage examples for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient putItem

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2 AmazonDynamoDBClient putItem.

Prototype

@Override
public PutItemResult putItem(PutItemRequest request) 

Source Link

Document

Creates a new item, or replaces an old item with a new item.

Usage

From source file:awslabs.lab22.SolutionCode.java

License:Open Source License

@Override
public void createAccountItem(AmazonDynamoDBClient ddbClient, String tableName, Account account) {
    // Create a HashMap<String, AttributeValue> object to hold the attributes of the item to add.
    Map<String, AttributeValue> itemAttributes = new HashMap<String, AttributeValue>();
    // Add the required items (Company and Email) from the account parameter to the attribute HashMap.
    itemAttributes.put("Company", new AttributeValue().withS(account.getCompany()));
    itemAttributes.put("Email", new AttributeValue().withS(account.getEmail()));

    // Check the account parameter and add all values that aren't empty strings ("") to the attribute HashMap.
    if (!account.getFirst().equals("")) {
        itemAttributes.put("First", new AttributeValue().withS(account.getFirst()));
    }//  w ww .jav a2 s . co m
    if (!account.getLast().equals("")) {
        itemAttributes.put("Last", new AttributeValue().withS(account.getLast()));
    }
    if (!account.getAge().equals("")) {
        itemAttributes.put("Age", new AttributeValue().withN(account.getAge()));
    }

    // Construct a PutItemRequest object to put the attributes into the specified table.
    PutItemRequest putItemRequest = new PutItemRequest().withTableName(tableName).withItem(itemAttributes);

    // Submit the request using the putItem method of the ddbClient object.
    ddbClient.putItem(putItemRequest);
}

From source file:awslabs.lab22.StudentCode.java

License:Open Source License

/**
 * Create a DynamoDB item from the values specified in the account parameter. The names of the attributes in the
 * item should match the corresponding property names in the Account object. Don't add attributes for fields in the
 * Account object that are empty./*from   w  w  w.j  a  va 2  s. c  o  m*/
 * 
 * Since the Company and Email attributes are part of the table key, those will always be provided in the Account
 * object when this method is called. This method will be called multiple times by the code controlling the lab.
 * 
 * Important: Even thought the Account.Age property is passed to you as a string, add it to the item as a numerical
 * value.
 * 
 * @param ddbClient The DynamoDB client object.
 * @param tableName The name of the table to add the item to.
 * @param account The Account object containing the data to add.
 */
@Override
public void createAccountItem(AmazonDynamoDBClient ddbClient, String tableName, Account account) {
    // TODO: Replace this call to the super class with your own implementation of the method.
    ddbClient.setRegion(Region.getRegion(Regions.US_EAST_1));
    Map<String, AttributeValue> items = new HashMap<String, AttributeValue>();
    items.put("Company", new AttributeValue().withS(account.getCompany()));
    items.put("Email", new AttributeValue().withS(account.getEmail()));

    if (account.getFirst() != null) {
        items.put("First", new AttributeValue().withS(account.getFirst()));
    }

    if (account.getLast() != null) {
        items.put("Last", new AttributeValue().withS(account.getLast()));
    }

    if (account.getAge() != null) {
        items.put("Age", new AttributeValue().withN(account.getAge()));
    }

    PutItemRequest request = new PutItemRequest().withTableName(tableName).withItem(items);
    ddbClient.putItem(request);
}

From source file:awslabs.lab51.SolutionCode.java

License:Open Source License

@Override
public void addImage(AmazonDynamoDBClient dynamoDbClient, String tableName, AmazonS3Client s3Client,
        String bucketName, String imageKey, String filePath) {

    try {//from w w w.  ja v  a  2  s. c  o m
        File file = new File(filePath);
        if (file.exists()) {
            s3Client.putObject(bucketName, imageKey, file);

            PutItemRequest putItemRequest = new PutItemRequest().withTableName(tableName);
            putItemRequest.addItemEntry("Key", new AttributeValue(imageKey));
            putItemRequest.addItemEntry("Bucket", new AttributeValue(bucketName));
            dynamoDbClient.putItem(putItemRequest);
            labController.logMessageToPage("Added imageKey: " + imageKey);
        } else {
            labController.logMessageToPage(
                    "Image doesn't exist on disk. Skipped: " + imageKey + "[" + filePath + "]");
        }
    } catch (Exception ex) {
        labController.logMessageToPage("addImage Error: " + ex.getMessage());
    }

}