Database Reference
In-Depth Information
Using the AWS SDK for Java
In this section, we are going to see how to use the AWS SDK for Java to perform opera-
tions.
Create table
Before creating a table, you should have thought about how the user is going to use that
table in his/her application. Data modeling in DynamoDB should be application oriented in
order to get the maximum benefit of the NoSQL features. You should also have a proper
idea of the reads and writes you are going to expect for a particular table so that there won't
be any
ProvisionedThroughputExceededException
and all request to Dy-
namoDB will be handled properly.
DynamoDB provides the SDK to create, update, and delete tables using a Java API. We can
also scan or query a table and get table description using a simple Java API. The following
are the steps to create a table in AWS DynamoDB. The SDK includes two versions of
APIs, Versions 1 and 2, so make sure that while you are importing packages in Eclipse, you
point to
com.amazonaws.services.dynamodbv2
rather than
com.amazonaws.services.dynamodb
. Perform the following steps:
1. Create an instance of
AmazonDynamoDBClient
and initialize it using your
AWS credentials, that is, your access key and secret key:
AmazonDynamoDBClient dynamoDBClient = new
AmazonDynamoDBClient(
new
ClasspathPropertiesFileCredentialsProvider());
By default, AWS looks for the
properties
file name
AwsCreden-
tials.properties
in
classpath
, which should have two properties set:
accessKey
and
secretKey
.
2. Create an instance of
CreateTableRequest
and provide information such as
table name, key schema, and provision throughput details, as shown in the follow-
ing code:
// Key attribute definition
ArrayList<AttributeDefinition> attrDefList= new
ArrayList<AttributeDefinition>();