DynamoDB - amresh087/newronaRepos GitHub Wiki
Amazon DynamoDB is a fully managed NoSQL database service provided by AWS. It's known for its scalability, performance, and low-latency data access. Below is an example of how you can use DynamoDB with Java:
Step 1: Set Up DynamoDB Table
First, you need to create a table in DynamoDB. You can do this via the AWS Management Console or using the AWS SDKs/APIs. Let's say we want to create a table named "Books" with a primary key "ISBN".
Step 2: Configure AWS SDK Add the AWS SDK for Java to your project's dependencies. If you're using Maven, add the following to your pom.xml:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.12.164</version> <!-- or the latest version -->
</dependency>
Step 3: Perform CRUD Operations
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.PrimaryKey;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;
import com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec;
import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec;
import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
public class DynamoDBExample {
public static void main(String[] args) {
// Initialize DynamoDB client
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
DynamoDB dynamoDB = new DynamoDB(client);
// Get reference to the "Books" table
Table table = dynamoDB.getTable("Books");
// Add item
addBook(table, "1234567890", "Title", "Author");
// Retrieve item
retrieveBook(table, "1234567890");
// Update item
updateBook(table, "1234567890", "New Title");
// Delete item
deleteBook(table, "1234567890");
}
private static void addBook(Table table, String isbn, String title, String author) {
Item item = new Item()
.withPrimaryKey("ISBN", isbn)
.withString("Title", title)
.withString("Author", author);
table.putItem(item);
System.out.println("Book added: " + item.toJSONPretty());
}
private static void retrieveBook(Table table, String isbn) {
GetItemSpec getItemSpec = new GetItemSpec().withPrimaryKey("ISBN", isbn);
Item item = table.getItem(getItemSpec);
System.out.println("Retrieved book: " + item.toJSONPretty());
}
private static void updateBook(Table table, String isbn, String newTitle) {
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("ISBN", isbn)
.withUpdateExpression("set #t = :val1")
.withNameMap("#t", "Title")
.withValueMap(":val1", newTitle);
table.updateItem(updateItemSpec);
System.out.println("Book updated");
}
private static void deleteBook(Table table, String isbn) {
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("ISBN", isbn);
table.deleteItem(deleteItemSpec);
System.out.println("Book deleted");
}
}
We initialize a DynamoDB client. We perform CRUD operations (add, retrieve, update, delete) on the "Books" table. The methods addBook, retrieveBook, updateBook, deleteBook handle each operation respectively.