一个或多个参数值无效dynamoDB+;JAVA
我正在尝试在DynamoDB中创建一个表。我可以使用DynamoDB控制台,但我更喜欢通过Java来实现。但是下面的代码给出了异常,我找不到原因 请检查以下代码一个或多个参数值无效dynamoDB+;JAVA,java,amazon-dynamodb,Java,Amazon Dynamodb,我正在尝试在DynamoDB中创建一个表。我可以使用DynamoDB控制台,但我更喜欢通过Java来实现。但是下面的代码给出了异常,我找不到原因 请检查以下代码 String serviceRef = "ServiceRef"; ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement() .wi
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();
}
还有在空间中使用什么
Tables.awaitTableToBecomeActive(dynamoDBClient, serviceRef);
因为它显示的是弃用的。您误用了AttributeDefinition方法链接。您错误地创建了一个属性定义,其名称一直在更改(从“ServiceID”到“ServiceName”再到“CreatedDateUTC”等,最后到“IsDeleted”) 您需要提供属性定义的数组或集合。例如:
attributeDefinitions.add(
new AttributeDefinition()
.withAttributeName("ServiceID")
.withAttributeType(ScalarAttributeType.N),
new AttributeDefinition()
.withAttributeName("ServiceName")
.withAttributeType(ScalarAttributeType.S),
...
);
您只创建了一个
属性定义
。您的方法链接只生成一个属性定义
,而不是它们的列表。您还只需要为表或GSI的KeySchema
属性创建AttributeDefinition
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("ServiceID")
.withAttributeType(ScalarAttributeType.N));
有关弃用问题,请查看表的列表
:
已弃用。
使用
还有必要提及的是,AttributeDefinition应该只包含表贴花/描述(KeySchema和GlobalSecondarinIndex)所需的列。因为DynamoDB是无模式的 如果您试图创建一个属性多于索引所需属性的表,则会出现如下错误:
线程“main”com.amazonaws.services.dynamodbv2.model.amazondynamodexception中出现异常:一个或多个参数值无效:未使用某些AttributeDefinition。属性定义:[降水量、温度、等级、ID、日期、位置],使用的键:[ID、日期、位置]
我是带着下面的案子来的:
属性定义:
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中的请求相关。为了测试table active,javadoc建议您使用TableUtils.waitUntilActive(AmazonDynamoDB,String)。非常感谢,我犯了一个非常基本的错误。在相应地更改代码后,现在我可以创建我的表了。谢谢您的回复。我现在可以创建代码了。但从那以后,我有了另一个问题。如果我创建一个空表会发生什么?它会变得活跃吗?目前我得到的例外是。。。。线程“main”com.amazonaws.AmazonClientException中的异常:com.amazonaws.services.dynamodbv2.util.TableUtils.waitunticative(TableUtils.java:150)com.amazonaws.services.dynamodbv2.util.TableUtils.waitunticative(TableUtils.java:117)com.rit.randemmiddleware.controller.TestMain上的TableServiceRef从未激活(TestMain.java:47)@GourabBanerjee您是在使用DynamoDB Local,还是在运行一个端点?不是。我使用凭据直接连接到Amazon SQS。是的,我使用US_WEST_2作为区域端点。感谢您花时间回答这个问题,这帮助我解决了我的问题。
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"));