Database Reference
In-Depth Information
In the following function, we will try to put two items (
item1
and
item2
, each of type
Map<String, AttributeValue>
) into the table (whose name is taken as an input
parameter). As we discussed earlier (the
getTableKeySchema
method), every item
must have primary key attributes (
BookTitle
and
Author
attributes). So both the
items have these two attributes.
In the first item (
item1
), including primary key attributes, we are adding four more at-
tributes, namely
Publisher
(
String
type),
PubDate
(
String
type),
Language
(
StringSet
type), and
Edition
(
Number
type). In order to add the attributes we
must call the correct method of the
AttributeValue
class, depending on the type of
attribute we need to put.
In the second item (
item2
), we also add the same attributes for another topic, with the
additional attribute named
Pages
(
Number
type).
private static void putItems(String tableName) {
Map<String, AttributeValue> item1 = new HashMap<String,
AttributeValue>();
item1.put("BookTitle", new
AttributeValue().withS("SCJP"));
item1.put("Author", new AttributeValue().withS("Kathy"));
item1.put("Publisher", new AttributeValue().withS("TMH"));
item1.put("PubDate", new
AttributeValue().withS("28-Dec-09"));
item1.put("Language", new AttributeValue()
.withSS(Arrays.asList("English", "German")));
item1.put("Edition", new AttributeValue().withN("1"));
PutItemRequest putItemRequest = new PutItemRequest()
.withTableName(tableName)
.withItem(item1);
client.putItem(putItemRequest);
Map<String, AttributeValue> item2 = new HashMap<String,
AttributeValue>();
item2.put("BookTitle", new
AttributeValue().withS("Inferno"));
item2.put("Author", new
AttributeValue().withS("DanBrown"));