Database Reference
In-Depth Information
Using the AWS SDK for Java
Earlier in this section, we saw how to manipulate tables using the Java API; now, let's learn
how to manipulate items from a certain table.
Put item
This method allows us to store an item in a DynamoDB table. To put the item in the Dy-
namoDB table, you just need to create PutItemRequest and call the putItem method
with the provided details. To call the putItem method, you should first initialize the Dy-
namoDB client, which we have already seen in the Table operations section, as shown in
the following code:
AmazonDynamoDBClient dynamoDBClient = new
AmazonDynamoDBClient(
new ClasspathPropertiesFileCredentialsProvider());
// Create Map of String and AttributeValue and store the
data in it.
Map<String, AttributeValue> item = new HashMap<String,
AttributeValue>();
item.put("bookId", new AttributeValue().withN("2001"));
item.put("name", new AttributeValue().withS("Mastering
DynamoDB"));
item.put("isbn", new
AttributeValue().withS("2222-222-222"));
item.put("authors", new
AttributeValue().withSS(Arrays.asList("Tanmay Deshpande")));
// Initiate with PutItemRequest with table name and item to
be added
PutItemRequest putItemRequest = new
PutItemRequest().withTableName("Book").withItem(item);
// Call put item method from dynamodb client
PutItemResult putItemresult =
dynamoDBClient.putItem(putItemRequest);
Search WWH ::




Custom Search