Database Reference
In-Depth Information
All other attributes (
BookTitle
,
Author
,
PubDate
,
Publisher
,
Edition
, and
Language
) getters will be followed by the
@DynamoDBAttrib-
ute(attributeName="")
annotation with the corresponding table attribute name.
We have seen how to represent primary key and non-key attributes. Now, let's see how an
item can be put on the DynamoDB table using a high-level API. Take a look at the follow-
ing code:
AmazonDynamoDBClient dynamoDBClient = new
AmazonDynamoDBClient(
new ProfileCredentialsProvider());
DynamoDBMapper dynamoDBMapper = new
DynamoDBMapper(dynamoDBClient);
BookEntity book = new BookEntity();
book.setBookTitle("Hadoop");
book.setAuthor("Tom White");
book.setEdition(new Integer(1));
book.setPubDate("2012-12-28");
book.setPublisher("o'reilly");
book.setLanguage(new
HashSet<String>(Arrays.asList("English")));
dynamoDBMapper.save(book);
The preceding code will put an item with the given details. The first line of the code cre-
ates a client for DynamoDB and the second line will create a
DynamoDBMapper
class
for this client. Then, the next seven lines will create an instance of the
BookEntity
class (mapped to the
Tbl_Book
table) and populate it with the corresponding data. Then,
the last line will call the save method available in the
DynamoDBMapper
class, which
will put this item in the table. Let's see how we can perform a query operation using the
high-level API:
BookEntity bookEntity = new BookEntity();
bookEntity.setBookTitle("Hadoop");
bookEntity.setPubDate("2012-12-28");
DynamoDBQueryExpression<BookEntity> query
= new DynamoDBQueryExpression<BookEntity>()
.withHashKeyValues(bookEntity);
query.setHashKeyValues(bookEntity);
List<BookEntity> bookList
= dynamoDBMapper.query(BookEntity.class, query);