Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One or more parameter values were invalid dynamoDB + Java

I am trying to create a table in DynamoDB. I could use DynamoDB console but I prefer doing it via Java. But the following code is giving exception and I can't find why..

Please check the following code..

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();
}

What is wrong with this code? The error it is giving is -

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)

Also what to use in space of

Tables.awaitTableToBecomeActive(dynamoDBClient, serviceRef);

As it is showing deprecated.

like image 423
Gourab Banerjee Avatar asked Oct 23 '25 04:10

Gourab Banerjee


1 Answers

You're misusing AttributeDefinition method chaining. You've mistakenly created one attribute definition whose name you keep changing (from "ServiceID" to "ServiceName" to "CreatedDateUTC" etc., and finally to "IsDeleted").

You need to provide an array or collection of attribute definitions. For example:

attributeDefinitions.add(
    new AttributeDefinition()
        .withAttributeName("ServiceID")
        .withAttributeType(ScalarAttributeType.N),
    new AttributeDefinition()
        .withAttributeName("ServiceName")
        .withAttributeType(ScalarAttributeType.S),
    ...
);
like image 79
jarmod Avatar answered Oct 25 '25 18:10

jarmod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!