【发布时间】:2018-11-29 01:12:05
【问题描述】:
我正在尝试在 DynamoDB 中创建一个表。我可以使用 DynamoDB 控制台,但我更喜欢通过 Java 来完成。但是下面的代码给出了异常,我找不到原因..
请检查以下代码..
String serviceRef = "ServiceRef";
ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
keySchema.add(new KeySchemaElement()
.withAttributeName("ServiceID")
.withKeyType(KeyType.HASH));
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("ServiceID")
.withAttributeType(ScalarAttributeType.N)
.withAttributeName("ServiceName")
.withAttributeType(ScalarAttributeType.S)
.withAttributeName("CreatedDateUTC")
.withAttributeType(ScalarAttributeType.N)
.withAttributeName("UpdatedDateUTC")
.withAttributeType(ScalarAttributeType.N)
.withAttributeName("IsDeleted")
.withAttributeType(ScalarAttributeType.B));
CreateTableRequest request = new CreateTableRequest()
.withTableName(serviceRef)
.withKeySchema(keySchema)
.withAttributeDefinitions(attributeDefinitions)
.withProvisionedThroughput(
new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(1L));
System.out.println("Issuing create table request for: " + serviceRef);
dynamoDBClient.createTable(request);
System.out.println("Waiting for table Name: " + serviceRef + " to be created...");
try {
Tables.awaitTableToBecomeActive(dynamoDBClient, serviceRef);
} catch (InterruptedException e) {
e.printStackTrace();
}
这段代码有什么问题? 它给出的错误是 -
Exception in thread "main" com.amazonaws.AmazonServiceException: One or more parameter values were invalid: Some index key attributes are not defined in AttributeDefinitions. Keys: [ServiceID], AttributeDefinitions: [IsDeleted] (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: SA8P5UUVH37T8P1R7F6VVPP357VV4KQNSO5AEMVJF66Q9ASUAAJG)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1219)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:803)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:505)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:317)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:1803)
at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.createTable(AmazonDynamoDBClient.java:832)
at com.rit.randemmiddleware.controller.TestMain.main(TestMain.java:38)
还有在空间中使用什么
Tables.awaitTableToBecomeActive(dynamoDBClient, serviceRef);
因为它显示已弃用。
【问题讨论】:
-
要测试表是否处于活动状态,javadoc 建议您使用 TableUtils.waitUntilActive(AmazonDynamoDB, String)。
标签: java amazon-dynamodb