kirti
kirti

Reputation: 679

Working with DynamoDB's PutItem ConditionExpression in Java (avoid replacement of attributes)

I have a dataset that does not contain any unique key (primary key). Only when all the rows are combined it is unique. However, I have given the first attribute (Class type) in the key to create table in dynamoDB as below:

    CreateTableRequest createTableRequest = new CreateTableRequest()
        .withTableName(tableName)
        .withKeySchema(newKeySchemaElement()
        .withAttributeName("Class type")
        .withKeyType(KeyType.HASH))
.withAttributeDefinitions(newAttributeDefinition().withAttributeName("Class type").withAttributeType(ScalarAttributeType.S))
            ...
            ...;

With this dataset, PutItem does not work because it replaces one attribute with other since the "Class type" is not unique. I want all rows to be uploaded to dynamoDB. My putItem is shown below:

Map<String, AttributeValue> item = newItem(col[0], col[1], col[2], col[3], titanic_col[4]);
PutItemRequest putItemRequest = new PutItemRequest(tableName, item).withConditionExpression("Class type = NULL"); 
// No error in the above line. But getting exception.
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);

Exception that I am getting on Condition Expression line:

Caught an AmazonServiceException, which means your request made it to AWS, but was rejected with an error response for some reason.
Error Message:    Invalid ConditionExpression: Syntax error; token: "type", near: "Class type =" (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID:  J51GM61PM4AE2HPD0DVMQ6TNU7VV4KQNSO5AEMVJF66Q9ASUAAJG)
HTTP Status Code: 400
AWS Error Code:   ValidationException
Error Type:       Client

I want to know whether the syntax is correct. If not, please provide an correct syntax to resolve the problem (avoiding replacement of attributes).

Upvotes: 2

Views: 4839

Answers (1)

Mingliang Liu
Mingliang Liu

Reputation: 5787

  1. Yes there is syntax error. There is a space between the two words of "Class type". The parser don't know this unless you tell it. To let the parse know the "Class type" is a single attribute, please see expression attribute name
  2. You need a unique ID, even a uuid will work

Upvotes: 1

Related Questions