我正在尝试在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);
因为它显示已弃用。
答案 0 :(得分:2)
您滥用AttributeDefinition方法链接。您错误地创建了一个属性定义,其名称会不断更改(从&#34; ServiceID&#34;到&#34; ServiceName&#34;到&#34; CreatedDateUTC&#34;等等,最后到#34; IsDeleted&#34;)。
您需要提供一个数组或属性定义集合。例如:
attributeDefinitions.add(
new AttributeDefinition()
.withAttributeName("ServiceID")
.withAttributeType(ScalarAttributeType.N),
new AttributeDefinition()
.withAttributeName("ServiceName")
.withAttributeType(ScalarAttributeType.S),
...
);
答案 1 :(得分:0)
您只创建了一个AttributeDefinition
。您的方法链接只生成一个AttributeDefinition
,而不是它们的列表。您还只需为AttributeDefinition
表格或GSI的属性创建KeySchema
。
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("ServiceID")
.withAttributeType(ScalarAttributeType.N));
对于弃用问题,请查看Tables
的{{3}}:
已弃用。
使用Javadoc。
TablesUtils.waitUntilActive(dynamoDBClient, serviceRef);
答案 2 :(得分:0)
还必须提到AttributeDefinitions应该只包含表缩放/描述所必需的列(KeySchema和GlobalSecondaryIndexes)。因为DynamoDB是无架构的。
如果您尝试创建一个表的属性多于索引所需的属性,则会收到类似以下错误:Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException: One or more parameter values were invalid: Some AttributeDefinitions are not used. AttributeDefinitions: [Precipitation, Temperature, Rank, ID, Date, Location], keys used: [ID, Date, Location]
我是带着以下情况来到这里的:
属性定义:
attributeDefinitions.add(new AttributeDefinition("ID", "S"));
attributeDefinitions.add(new AttributeDefinition("Location", "S"));
attributeDefinitions.add(new AttributeDefinition("Date", "S"));
attributeDefinitions.add(new AttributeDefinition("Precipitation", "N"));
attributeDefinitions.add(new AttributeDefinition("Temperature", "N"));
attributeDefinitions.add(new AttributeDefinition("Rank", "N"));
关键架构:
tableKeySchema.add(new KeySchemaElement("ID", KeyType.HASH))
全球二级指数:
GlobalSecondaryIndex locationDateIndex = new GlobalSecondaryIndex()
.withIndexName("Location_Date-Index")
.withProjection(new
Projection().withProjectionType(ProjectionType.ALL))
.withProvisionedThroughput(new ProvisionedThroughput(10l, 1l))
.withKeySchema(new KeySchemaElement("Location", KeyType.HASH))
.withKeySchema(new KeySchemaElement("Date", KeyType.RANGE));
创建表请求:
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(tableName)
.withProvisionedThroughput(new ProvisionedThroughput(5L, 1L))
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema)
.withGlobalSecondaryIndexes(locationDateIndex);
以及上面的错误:
Exception in thread "main" com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException:
One or more parameter values were invalid: Some AttributeDefinitions are not used.
AttributeDefinitions: [Precipitation, Temperature, Rank, ID, Date, Location],
keys used: [ID, Date, Location] (Service: AmazonDynamoDBv2; Status Code: 400;
Error Code: ValidationException; Request ID: ...)
减少属性定义列表解决了我的问题:
attributeDefinitions.add(new AttributeDefinition("ID", "S"));
attributeDefinitions.add(new AttributeDefinition("Location", "S"));
attributeDefinitions.add(new AttributeDefinition("Date", "S"));
PS:我在这里回答是因为此页面与我在Google中的请求有关。