这里我们使用的是DynamoDB的低级接口,文章末尾有相应要下载的文件地址,废话不多说,直接上代码..
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.*;
import com.amazonaws.services.dynamodbv2.document.spec.*;
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.*;
public class DynamoDBDemo {
public static AmazonDynamoDB client = getAmazonDynamoDBClient();
public static DynamoDB dynamoDB = new DynamoDB(client);
public static Table table = dynamoDB.getTable("Movies");
public static AmazonDynamoDB getAmazonDynamoDBClient() {
return AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
.build();
}
public void createTable(String tableName) {
try {
Table table = dynamoDB.createTable(tableName,
Arrays.asList(new KeySchemaElement("year", KeyType.HASH),
new KeySchemaElement("title", KeyType.RANGE)),
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 (InterruptedException e) {
System.err.println("Unable to create table: ");
System.err.println(e.getMessage());
e.printStackTrace();
}
}
public void deleteTable() {
try {
table.delete();
table.waitForDelete();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void loadData() {
try {
JsonParser parser = new JsonFactory().createParser(new File("D:\\ProjectData\\idea-workspace\\dynamodbcurd\\src\\main\\java\\com\\merlin\\dynamodbcurd\\demo\\data\\moviedata.json"));
JsonNode rootNode = new ObjectMapper().readTree(parser);
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();
table.putItem(new Item().withPrimaryKey("year", year, "title", title).withJSON("info", currentNode.path("info").toString()));
System.out.println("PutItem succeeded: " + year + " " + title);
parser.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void putItems() {
int year = 2015;
String title = "The Big New Movie";
final Map<String, Object> infoMap = new HashMap<>();
infoMap.put("plot", "Nothing happens at all.");
infoMap.put("rating", 0);
PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("year", year, "title", title).withMap("info", infoMap));
System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());
System.out.println("PutItem succeeded:\n" + outcome.getItem());
}
public void getItems() {
int year = 2015;
String title = "The Big New Movie";
GetItemSpec spec = new GetItemSpec().withPrimaryKey("year", year, "title", title);
Item outcome = table.getItem(spec);
System.out.println("GetItem succeeded: " + outcome);
}
public void updateItem() {
int year = 2015;
String title = "The Big New Movie";
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year", year, "title", title)
.withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a")
.withValueMap(new ValueMap().withNumber(":r", 5.5)
.withString(":p", "Everything happens all at once.")
.withList(":a", Arrays.asList("larry", "Moe", "Curly")))
.withReturnValues(ReturnValue.UPDATED_NEW);
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
}
public void updateItemByConditions() {
int year = 2015;
String title = "The Big New Movie";
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey(new PrimaryKey("year", year, "title", title))
.withUpdateExpression("remove info.actors[0]")
.withConditionExpression("size(info.actors)>:num")
.withValueMap(new ValueMap().withNumber(":num", 2))
.withReturnValues(ReturnValue.UPDATED_NEW);
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
System.out.println("UpdateItem succeeded:\n" + outcome.getItem().toJSONPretty());
}
public void deleteItem() {
int year = 2015;
String title = "The Big New Movie";
DeleteItemSpec deleteItemSpec = new DeleteItemSpec()
.withPrimaryKey(new PrimaryKey("year", year, "title", title))
.withConditionExpression("info.rating <= :val")
.withValueMap(new ValueMap().withNumber(":val", 10.0));
table.deleteItem(deleteItemSpec);
System.out.println("DeleteItem succeeded");
}
public void queryItem() {
HashMap<String, String> nameMap = new HashMap<>();
nameMap.put("#yr", "year");
HashMap<String, Object> valueMap = new HashMap<>();
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;
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"));
}
valueMap.put(":yyyy", 1992);
valueMap.put(":letter1", "A");
valueMap.put(":letter2", "L");
querySpec.withProjectionExpression("#yr,title,info.genres,info.actors[0]")
.withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2")
.withNameMap(nameMap)
.withValueMap(valueMap);
System.out.println("Movies from 1992 - titles A-L,with genres and lead actor");
items = table.query(querySpec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getNumber("year") + ": " + item.getString("title") + " " + item.getMap("info"));
}
}
public void scanItem() {
HashMap<String, String> nameMap = new HashMap<>();
nameMap.put("#yr", "year");
HashMap<String, Object> valueMap = new HashMap<>();
valueMap.put(":start_yr", 1950);
valueMap.put(":end_yr", 1959);
ScanSpec scanSpec = new ScanSpec()
.withProjectionExpression("#yr,title,info.rating")
.withFilterExpression("#yr between :start_yr and :end_yr")
.withNameMap(nameMap)
.withValueMap(valueMap);
ItemCollection<ScanOutcome> items = table.scan(scanSpec);
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
Item item = iter.next();
System.out.println(item.toString());
}
}
public static void main(String[] args) {
DynamoDBDemo dynamoDBDemo = new DynamoDBDemo();
dynamoDBDemo.queryItem();
}
}
样本数据moviedata.json下载地址