How to describe table in dynamoDB using Java – DynamoDB DescribeTable
5 years ago Lalit Bhagtani 0
In this tutorial, we will learn about how to describe a table in dynamoDB (DescribeTable) using java language.
DynamoDB
Amazon DynamoDB is a fully managed NoSQL database services offered by Amazon as part of its Amazon Web Service (AWS) portfolio. It provides fast and predictable performance with seamless scalability. DynamoDB is a key-value datastore, where each item (row) in a table is a key-value pair. The primary key is the only required attribute for an item and uniquely identifies each item. DynamoDB is schema-less, which means that each item in a table can have any number of attributes and the types of attributes can vary from item to item.
Project Setup
Create a simple maven project in your favorite IDE and add below mentioned dependency in your pom.xml file.
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.524</version>
</dependency>
For latest version of aws library, check this page.
DynamoDB Connection
Create an object of AmazonDynamoDB ( com.amazonaws.services.dynamodbv2.AmazonDynamoDB ) class for sending a client request to dynamoDB. To get instance of this class, we will use AmazonDynamoDBClientBuilder builder class. It requires three important parameters :-
- Region :- It is a region where dynamoDB table will be stored.
- ACCESS_KEY :- It is a access key for using dynamoDB. You can generate this key, using aws management console.
- SECRET_KEY :- It is a secret key of above mentioned access key.
Here is a code example :-
AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder.standard()
.withRegion(Regions.AP_SOUTH_1)
.withCredentials(new AWSStaticCredentialsProvider
(new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY")))
.build();
DescribeTable
Steps to create and send DescribeTableRequest to dynamoDB are as follows:-
- Instantiate DescribeTableRequest object, this object will be used to send the client request to dynamoDB.
- Set name of the table, in our case JOB.
- Invoke the describeTable method on AmazonDynamoDB object by passing table DescribeTableRequest object as an argument.
Example
import java.util.List; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.Regions; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.DescribeTableResult; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughputDescription; import com.amazonaws.services.dynamodbv2.model.TableDescription; public class DescribeTable { public static void main(String[] args) { /* Create DynamoDB Client Object */ AmazonDynamoDB dynamoDB = AmazonDynamoDBClientBuilder .standard() .withRegion(Regions.AP_SOUTH_1) .withCredentials(new AWSStaticCredentialsProvider (new BasicAWSCredentials("ACCESS_KEY","SECRET_KEY"))) .build(); /* Create an Object of DescribeTableRequest */ DescribeTableRequest request = new DescribeTableRequest(); /* Setting Table Name */ request.setTableName("JOB"); try { /* Send Describe Table Request */ DescribeTableResult result = dynamoDB.describeTable(request); System.out.println("Status : " + result.getSdkHttpMetadata().getHttpStatusCode()); /* Get Table Information */ TableDescription tableInfo = result.getTable(); if (tableInfo != null) { System.out.println("Table name : " + tableInfo.getTableName()); System.out.println("Status : " + tableInfo.getTableStatus()); System.out.println("Item count : " + tableInfo.getItemCount().longValue()); System.out.println("Size (bytes) : " + tableInfo.getTableSizeBytes().longValue()); ProvisionedThroughputDescription throughputInfo = tableInfo.getProvisionedThroughput(); System.out.println("Read Capacity : " + throughputInfo.getReadCapacityUnits().longValue()); System.out.println("Write Capacity : " + throughputInfo.getWriteCapacityUnits().longValue()); List<AttributeDefinition> attributes = tableInfo.getAttributeDefinitions(); System.out.println("Attributes"); for (AttributeDefinition a : attributes) { System.out.println(a.getAttributeName() + " " + a.getAttributeType()); } } } catch (AmazonServiceException e) { System.out.println(e.getErrorMessage()); } } }
References :-
That’s all for how to describe a table in dynamoDB (DescribeTable) using java language. If you liked it, please share your thoughts in comments section and share it with others too.