DynamoDB Java tutorial http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Java.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.chocksaway.util; | |
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; | |
import com.amazonaws.services.dynamodbv2.document.*; | |
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; | |
import com.amazonaws.services.dynamodbv2.document.spec.ScanSpec; | |
import com.amazonaws.services.dynamodbv2.document.utils.NameMap; | |
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap; | |
import com.amazonaws.services.dynamodbv2.model.*; | |
import com.fasterxml.jackson.core.JsonFactory; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.node.ObjectNode; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
/** | |
* Author milesd on 30/07/2016. | |
* | |
* Dynamo DB tutorial http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Java.html | |
*/ | |
public class Dynamo { | |
/** | |
* Create Movies table | |
* | |
* @param client | |
*/ | |
private void createTable(DynamoDB client) { | |
try { | |
final String tableName = "Movies"; | |
System.out.println("Attempting to create table; please wait..."); | |
Table table = client.createTable(tableName, | |
Arrays.asList( | |
new KeySchemaElement("year", KeyType.HASH), //Partition key | |
new KeySchemaElement("title", KeyType.RANGE)), //Sort key | |
Arrays.asList( | |
new AttributeDefinition("year", ScalarAttributeType.N), | |
new AttributeDefinition("title", ScalarAttributeType.S)), | |
new ProvisionedThroughput(10L, 10L)); | |
table.waitForActive(); | |
System.out.println("Success. Table status: " + table.getDescription().getTableStatus()); | |
} catch (Exception e) { | |
System.err.println("Unable to create table: "); | |
System.err.println(e.getMessage()); | |
} | |
} | |
/** | |
* List dynamodb tables | |
* | |
* @param client | |
*/ | |
private void listTables(DynamoDB client) { | |
TableCollection<ListTablesResult> tables = client.listTables(); | |
Iterator<Table> iterator = tables.iterator(); | |
while (iterator.hasNext()) { | |
Table table = iterator.next(); | |
System.out.println(table.getTableName()); | |
} | |
} | |
/** | |
* Load moviedata.json | |
* | |
* @param dynamoDB | |
*/ | |
private void loadData(DynamoDB dynamoDB) { | |
Table table = dynamoDB.getTable("Movies"); | |
JsonParser parser = null; | |
JsonNode rootNode = null; | |
try { | |
parser = new JsonFactory() | |
.createParser(new File("moviedata.json")); | |
rootNode = new ObjectMapper().readTree(parser); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
Iterator<JsonNode> iter = rootNode.iterator(); | |
ObjectNode currentNode; | |
while (iter.hasNext()) { | |
currentNode = (ObjectNode) iter.next(); | |
int year = currentNode.path("year").asInt(); | |
String title = currentNode.path("title").asText(); | |
try { | |
table.putItem(new Item() | |
.withPrimaryKey("year", year, "title", title) | |
.withJSON("info", currentNode.path("info").toString())); | |
System.out.println("PutItem succeeded: " + year + " " + title); | |
} catch (Exception e) { | |
System.err.println("Unable to add movie: " + year + " " + title); | |
System.err.println(e.getMessage()); | |
break; | |
} | |
} | |
try { | |
parser.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Query Movies table | |
* | |
* @param dynamoDB | |
*/ | |
private void queryTable(DynamoDB dynamoDB) { | |
Table table = dynamoDB.getTable("Movies"); | |
HashMap<String, String> nameMap = new HashMap<String, String>(); | |
nameMap.put("#yr", "year"); | |
HashMap<String, Object> valueMap = new HashMap<String, Object>(); | |
valueMap.put(":yyyy", 1985); | |
QuerySpec querySpec = new QuerySpec() | |
.withKeyConditionExpression("#yr = :yyyy") | |
.withNameMap(nameMap) | |
.withValueMap(valueMap); | |
ItemCollection<QueryOutcome> items = null; | |
Iterator<Item> iterator = null; | |
Item item = null; | |
try { | |
System.out.println("Movies from 1985"); | |
items = table.query(querySpec); | |
iterator = items.iterator(); | |
while (iterator.hasNext()) { | |
item = iterator.next(); | |
System.out.println(item.getNumber("year") + ": " | |
+ item.getString("title")); | |
} | |
} catch (Exception e) { | |
System.err.println("Unable to query movies from 1985"); | |
System.err.println(e.getMessage()); | |
} | |
} | |
/** | |
* Scan Movies table | |
* | |
* @param dynamoDB | |
*/ | |
private void scanTable(DynamoDB dynamoDB) { | |
Table table = dynamoDB.getTable("Movies"); | |
ScanSpec scanSpec = new ScanSpec() | |
.withProjectionExpression("#yr, title, info.rating") | |
.withFilterExpression("#yr between :start_yr and :end_yr") | |
.withNameMap(new NameMap().with("#yr", "year")) | |
.withValueMap(new ValueMap().withNumber(":start_yr", 1983).withNumber(":end_yr", 1983)); | |
try { | |
ItemCollection<ScanOutcome> items = table.scan(scanSpec); | |
Iterator<Item> iter = items.iterator(); | |
while (iter.hasNext()) { | |
Item item = iter.next(); | |
System.out.println(item.toString()); | |
} | |
} catch (Exception e) { | |
System.err.println("Unable to scan the table:"); | |
System.err.println(e.getMessage()); | |
} | |
} | |
/** | |
* Get dynamodb client | |
* | |
* @return | |
*/ | |
private DynamoDB getDynamoDBClient() { | |
AmazonDynamoDBClient client = new AmazonDynamoDBClient() | |
.withEndpoint("http://localhost:8000"); | |
/** | |
* set the region to local | |
* | |
* avoid getting default regions on localhost: | |
* aws_key_us-east-1.db | |
* | |
* you will see: | |
* aws_key_local.db | |
*/ | |
client.setSignerRegionOverride("local"); | |
return new DynamoDB(client); | |
} | |
public static void main(String[] args) { | |
Dynamo myDynamo = new Dynamo(); | |
DynamoDB dynamoClient = myDynamo.getDynamoDBClient(); | |
myDynamo.createTable(dynamoClient); | |
myDynamo.listTables(dynamoClient); | |
myDynamo.loadData(dynamoClient); | |
myDynamo.queryTable(dynamoClient); | |
myDynamo.scanTable(dynamoClient); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment