我试图在本地计算机上开始使用DynamoDB来解决问题。
我已经安装了DynamoDB,还安装了AWS CLI。
为了在我的机器上启动数据库,我使用下一行:
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("FILE.txt", True)
file.WriteLine("line1")
file.WriteLine("line2")
file.Close()
我已使用这段代码创建了一个新的本地表(取自AWS网站):
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
在命令行中检查表是否使用下一行创建时:
public class MoviesCreateTable {
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-east-2"))
.build();
DynamoDB dynamoDB = new DynamoDB(client);
String tableName = "Movies";
try {
System.out.println("Attempting to create table; please wait...");
Table table = dynamoDB.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());
}
}
}
我收到下一个输出:
aws dynamodb list-tables --endpoint-url http://localhost:8000
,所以我认为表是正确创建的。
但是,当尝试使用下一个代码将项目添加到此表中时,我什么也没得到,代码运行没有错误,但未添加项目:
{ "TableNames": ["Movies"]}
我在做什么错?为什么没有将该项目添加到表格中?我该如何解决? 任何帮助将不胜感激,谢谢。
已编辑
该项目实际上已添加。刚刚检查了
public class MoviesItemOps01 {
public static void main(String[] args) throws Exception {
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-east-2"))
.build();
DynamoDB dynamoDB = new DynamoDB(client);
Table table = dynamoDB.getTable("Movies");
int year = 2015;
String title = "The Big New Movie";
final Map<String, Object> infoMap = new HashMap<String, Object>();
infoMap.put("plot", "Nothing happens at all.");
infoMap.put("rating", 0);
try {
System.out.println("Adding a new item...");
PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("year", year, "title", title).withMap("info", infoMap));
System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());
}
catch (Exception e) {
System.err.println("Unable to add item: " + year + " " + title);
System.err.println(e.getMessage());
}
}
}
但是,问题是为什么aws dynamodb scan --table-name Movies --endpoint-url http://localhost:8000
方法之后的outcome
参数为空?它不应该保存插入表中的项目吗?