Amazon DynamoDB Integration
This article helps any new developer who wants to connect to Amazon DynamoDB — keep reading to find out more!
Join the DZone community and get the full member experience.
Join For FreeThis article helps any new developer who wants to connect to Amazon DynamoDB.
Prerequisites:
- Developer should have working IDE
- The developer should have an AWS account and have DynamoDb service added.
- A sample DynamoDb Table to be created.
Configuration Needed:
In your class-path, you should have awsconfiguration.json created. For android, these files should reside in the project folder -> res/raw folder.
Below are sample code-snippet
{ "Version": "0.1.0", "IdentityManager": { "Default": {} }, "CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "us-east-1:XXXXXXXXXXXXXX", "Region": "us-east-1" } } }, "CognitoUserPool": { "Default": { "AppClientId": "XXXXXXXXXXX", "PoolId": "us-east-1_XXXXXXX", "Region": "us-east-1" } } }
Note: Masked PoolId, AppClientId in the above snippet, the developer can get these details in his AWS account when he created CognitoIdentity Pool.
Code to Connect DynamoDb:
xxxxxxxxxx
private static final String TABLE_NAME = "abcde_sample";
private final String identityPoolId="us-east-1:XXXXXXXXXXXXXXX";
private final CognitoCachingCredentialsProvider credentialsProvider;
private static volatile DynamoDBAccess instance;
private AmazonDynamoDBClient dbClient;
private Table dbTable;
private DynamoDBAccess(Context context){
credentialsProvider = new CognitoCachingCredentialsProvider(
context,
identityPoolId, // Identity pool ID
Regions.US_EAST_1 // Region
);
// Create a connection to DynamoDB
dbClient = new AmazonDynamoDBClient(credentialsProvider);
int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > 8)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}
dbTable = Table.loadTable(dbClient, TABLE_NAME);
}
The required dependencies needed to be added to the grade or maven file. Below are sample dependencies needed to be added in Gradle:
xxxxxxxxxx
implementation 'com.amazonaws:aws-android-sdk-core:2.19.0'
implementation 'com.amazonaws:aws-android-sdk-s3:2.6.+'
implementation 'com.amazonaws:aws-android-sdk-ddb:2.6.+'
implementation 'com.amazonaws:aws-android-sdk-core:2.4.4'
implementation 'com.amazonaws:aws-android-sdk-ddb:2.4.4'
implementation 'com.amazonaws:aws-android-sdk-ddb-document:2.4.4'
implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.16.12'
Once all the above code and configurations are met, we can test the code with a sample data ingestion. Below is a sample code-snippet for ingesting into the Amazon DynamoDb table.
xxxxxxxxxx
public void ingest(Document req) throws Exception {
try {
dbTable.putItem(req);
} catch (Exception e){
e.printStackTrace();
throw new Exception();
}
Log.i("Data Ingested successfully....");
}
You have to convert your object model to Document which is basically followed by Key-value and then the call ingest method.
Once we run, you can check the data in the Amazon DynamDB table. Sample screenshot.
You are now good to go. Happy learning!
Opinions expressed by DZone contributors are their own.
Comments