package com.amazonaws.codesamples.datamodeling;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
public class ObjectPersistenceCRUDExample {
static AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
public static void main(String[] args) throws IOException {
testCRUDOperations();
System.out.println("Example complete!");
}
@DynamoDBTable(tableName="ProductCatalog")
public static class CatalogItem {
private Integer id;
private String title;
private String ISBN;
private Set bookAuthors;
@DynamoDBHashKey(attributeName="Id")
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
@DynamoDBAttribute(attributeName="Title")
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
@DynamoDBAttribute(attributeName="ISBN")
public String getISBN() { return ISBN; }
public void setISBN(String ISBN) { this.ISBN = ISBN;}
@DynamoDBAttribute(attributeName = "Authors")
public Set getBookAuthors() { return bookAuthors; }
public void setBookAuthors(Set bookAuthors) { this.bookAuthors = bookAuthors; }
@Override
public String toString() {
return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors
+ ", id=" + id + ", title=" + title + "]";
}
}
private static void testCRUDOperations() {
CatalogItem item = new CatalogItem();
item.setId(601);
item.setTitle("Book 601");
item.setISBN("611-1111111111");
item.setBookAuthors(new HashSet(Arrays.asList("Author1", "Author2")));
// Save the item (book).
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(item);
// Retrieve the item.
CatalogItem itemRetrieved = mapper.load(CatalogItem.class, 601);
System.out.println("Item retrieved:");
System.out.println(itemRetrieved);
// Update the item.
itemRetrieved.setISBN("622-2222222222");
itemRetrieved.setBookAuthors(new HashSet(Arrays.asList("Author1", "Author3")));
mapper.save(itemRetrieved);
System.out.println("Item updated:");
System.out.println(itemRetrieved);
// Retrieve the updated item.
DynamoDBMapperConfig config = new DynamoDBMapperConfig(DynamoDBMapperConfig.ConsistentReads.CONSISTENT);
CatalogItem updatedItem = mapper.load(CatalogItem.class, 601, config);
System.out.println("Retrieved the previously updated item:");
System.out.println(updatedItem);
// Delete the item.
mapper.delete(updatedItem);
// Try to retrieve deleted item.
CatalogItem deletedItem = mapper.load(CatalogItem.class, updatedItem.getId(), config);
if (deletedItem == null) {
System.out.println("Done - Sample item is deleted.");
}
}
}
Showing posts with label dynamo db. Show all posts
Showing posts with label dynamo db. Show all posts
AWS Dynamo DB operations
Migrating from Mongodb to Dynamo db
referernce: https://www.codementor.io/devops/tutorial/handling-date-and-datetime-in-dynamodb
Dynamo Db does not support multiple indexes
It supports only Hash key, hash key + range key only
Does not support native date, DateTime object
Advantage:
No worries of scaling, monitoring, security updates.
average good response.
Use unix time stamp, store as Number
Combine multiple mongo db key if required
Database denormalization
Based on query patter, create schema
Dynamo DB is NO SQL db
Queries are slow
GetItem call as fast
Terminology
Table , Item, Attribute, Primary Key, hash attribute value, hash and range attribute values
secondary index on non key attibute value
Start the dynamo db locally
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -help
Dynamo db port: 8000
Create table
var params = {
TableName : "Music",
KeySchema:[
{AttributeName:"Artist", KeyType:"Hash"},
{AttributeName:"SongTitle", KeyType:"Range"}
],
AttributeDefinitions:[
{AttibuteName:"Artist", AttibuteType:"S"},
{AttibuteName:"SongTitle", AttibuteType:"S"},
],
ProvisionedThroughput:{
ReadCapacityUnits:1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function(err, data){
});
TableStatus is should be Active
2)
var params = { TableName: "Music" }; dynamodb.describeTable(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
3)
var params = {}; dynamodb.listTables(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
4)
var params = { TableName: "Music", Item: { "Artist":"No One You Know", "SongTitle":"Call Me Today", "AlbumTitle":"Somewhat Famous", "Year": 2015, "Price": 2.14, "Genre": "Country", "Tags": { "Composers": [ "Smith", "Jones", "Davis" ], "LengthInSeconds": 214 } } }; dynamodb.putItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
primarykey is must, remaining attribute are optional
5)Condition Expressoin
var params = { TableName: "Music", Item: { "Artist":"No One You Know", "SongTitle":"Call Me Today", "AlbumTitle":"Somewhat Famous", "Year": 2015, "Price": 2.14, "Genre": "Country", "Tags": { "Composers": [ "Smith", "Jones", "Davis" ], "LengthInSeconds": 214 } }, "ConditionExpression": "attribute_not_exists(Artist) and attribute_not_exists(SongTitle)" };
6)Batch insert
var params = { RequestItems: { "Music": [ { PutRequest: { Item: { "Artist": "No One You Know", "SongTitle": "My Dog Spot", "AlbumTitle":"Hey Now", "Price": 1.98, "Genre": "Country", "CriticRating": 8.4 } } }, { PutRequest: { Item: { "Artist": "No One You Know", "SongTitle": "Somewhere Down The Road", "AlbumTitle":"Somewhat Famous", "Genre": "Country", "CriticRating": 8.4, "Year": 1984 } } }
]}
7)Dynamo db data types
String, number, float, map, list
8)
var params = { TableName: "Music", Key: { "Artist": "No One You Know", "SongTitle": "Call Me Today" } }; dynamodb.getItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
9)
var params = { TableName: "Music", Key: { "Artist": "No One You Know", "SongTitle": "Call Me Today" }, ProjectionExpression: "AlbumTitle" };
10)
var params = { TableName: "Music", KeyConditionExpression: "Artist = :artist", ExpressionAttributeValues: { ":artist": "No One You Know" } }; dynamodb.query(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
12)
var params = { TableName: "Music", AttributeDefinitions:[ {AttributeName: "Genre", AttributeType: "S"}, {AttributeName: "Price", AttributeType: "N"} ], GlobalSecondaryIndexUpdates: [ { Create: { IndexName: "GenreAndPriceIndex", KeySchema: [ {AttributeName: "Genre", KeyType: "HASH"}, {AttributeName: "Price", KeyType: "RANGE"}, ], Projection: { "ProjectionType": "ALL" }, ProvisionedThroughput: { "ReadCapacityUnits": 1,"WriteCapacityUnits": 1 } } } ] }; dynamodb.updateTable(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
13)
var params = { TableName: "Music", Key: { "Artist":"No One You Know", "SongTitle":"Call Me Today" }, UpdateExpression: "SET RecordLabel = :label", ExpressionAttributeValues: { ":label": "Global Records" }, ReturnValues: "ALL_NEW" }; dynamodb.updateItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
15)Automatic counter
15)
var params = { TableName: "Music", Key: { Artist: "The Acme Band", SongTitle: "Look Out, World" } }; dynamodb.deleteItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/BestPractices.html
refernce: http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Java.html
Dynamo Db does not support multiple indexes
It supports only Hash key, hash key + range key only
Does not support native date, DateTime object
Advantage:
No worries of scaling, monitoring, security updates.
average good response.
Use unix time stamp, store as Number
Combine multiple mongo db key if required
Database denormalization
Based on query patter, create schema
Dynamo DB is NO SQL db
Queries are slow
GetItem call as fast
Terminology
Table , Item, Attribute, Primary Key, hash attribute value, hash and range attribute values
secondary index on non key attibute value
Start the dynamo db locally
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -help
Dynamo db port: 8000
Create table
var params = {
TableName : "Music",
KeySchema:[
{AttributeName:"Artist", KeyType:"Hash"},
{AttributeName:"SongTitle", KeyType:"Range"}
],
AttributeDefinitions:[
{AttibuteName:"Artist", AttibuteType:"S"},
{AttibuteName:"SongTitle", AttibuteType:"S"},
],
ProvisionedThroughput:{
ReadCapacityUnits:1,
WriteCapacityUnits: 1
}
};
dynamodb.createTable(params, function(err, data){
});
TableStatus is should be Active
2)
var params = { TableName: "Music" }; dynamodb.describeTable(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
3)
var params = {}; dynamodb.listTables(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
4)
var params = { TableName: "Music", Item: { "Artist":"No One You Know", "SongTitle":"Call Me Today", "AlbumTitle":"Somewhat Famous", "Year": 2015, "Price": 2.14, "Genre": "Country", "Tags": { "Composers": [ "Smith", "Jones", "Davis" ], "LengthInSeconds": 214 } } }; dynamodb.putItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
primarykey is must, remaining attribute are optional
5)Condition Expressoin
var params = { TableName: "Music", Item: { "Artist":"No One You Know", "SongTitle":"Call Me Today", "AlbumTitle":"Somewhat Famous", "Year": 2015, "Price": 2.14, "Genre": "Country", "Tags": { "Composers": [ "Smith", "Jones", "Davis" ], "LengthInSeconds": 214 } }, "ConditionExpression": "attribute_not_exists(Artist) and attribute_not_exists(SongTitle)" };
6)Batch insert
var params = { RequestItems: { "Music": [ { PutRequest: { Item: { "Artist": "No One You Know", "SongTitle": "My Dog Spot", "AlbumTitle":"Hey Now", "Price": 1.98, "Genre": "Country", "CriticRating": 8.4 } } }, { PutRequest: { Item: { "Artist": "No One You Know", "SongTitle": "Somewhere Down The Road", "AlbumTitle":"Somewhat Famous", "Genre": "Country", "CriticRating": 8.4, "Year": 1984 } } }
]}
7)Dynamo db data types
String, number, float, map, list
8)
var params = { TableName: "Music", Key: { "Artist": "No One You Know", "SongTitle": "Call Me Today" } }; dynamodb.getItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
9)
var params = { TableName: "Music", Key: { "Artist": "No One You Know", "SongTitle": "Call Me Today" }, ProjectionExpression: "AlbumTitle" };
10)
var params = { TableName: "Music", KeyConditionExpression: "Artist = :artist", ExpressionAttributeValues: { ":artist": "No One You Know" } }; dynamodb.query(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
11)
var params = {
TableName: "Music"
};
dynamodb.scan(params, function(err, data) {
if (err)
console.log(JSON.stringify(err, null, 2));
else
console.log(JSON.stringify(data, null, 2));
});
Operations12)
var params = { TableName: "Music", AttributeDefinitions:[ {AttributeName: "Genre", AttributeType: "S"}, {AttributeName: "Price", AttributeType: "N"} ], GlobalSecondaryIndexUpdates: [ { Create: { IndexName: "GenreAndPriceIndex", KeySchema: [ {AttributeName: "Genre", KeyType: "HASH"}, {AttributeName: "Price", KeyType: "RANGE"}, ], Projection: { "ProjectionType": "ALL" }, ProvisionedThroughput: { "ReadCapacityUnits": 1,"WriteCapacityUnits": 1 } } } ] }; dynamodb.updateTable(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
13)
var params = { TableName: "Music", Key: { "Artist":"No One You Know", "SongTitle":"Call Me Today" }, UpdateExpression: "SET RecordLabel = :label", ExpressionAttributeValues: { ":label": "Global Records" }, ReturnValues: "ALL_NEW" }; dynamodb.updateItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
15)Automatic counter
15)
var params = { TableName: "Music", Key: { Artist: "The Acme Band", SongTitle: "Look Out, World" } }; dynamodb.deleteItem(params, function(err, data) { if (err) console.log(JSON.stringify(err, null, 2)); else console.log(JSON.stringify(data, null, 2)); });
refernce: http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Java.html
Subscribe to:
Posts (Atom)