Home > Net >  Update String column in DynamoDB
Update String column in DynamoDB

Time:02-11

I'm new to DynamoDB and was following tutorial to implement basic CRUD operations.

I have the below code

public static void main(String ... args) throws Exception {
    
    //1. create client
    client = AmazonDynamoDBClientBuilder.standard()
            .withRegion(Regions.US_EAST_1)
            .build();

    //2. Create table
    String jobID="8aee43e5c44212040529fe11000117cdd0cb77eb";
    PutItemSpec putItemSpec = new PutItemSpec();
    putItemSpec.withItem(new Item().withPrimaryKey("jobid", jobID ).
            withString("type", "first").withBoolean("cancel", false));
    PutItemOutcome putItemOutcome = table.putItem(putItemSpec);
    System.out.println(putItemOutcome);

    //3. Read
    GetItemSpec spec = new GetItemSpec().withPrimaryKey("jobid", jobID);
    Item item = table.getItem(spec);
    System.out.println(item);

    //4.Update boolean
    UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("jobid", jobID)
            .withUpdateExpression("set cancel=:s")
            .withValueMap(new ValueMap().withBoolean(":s",true));
    UpdateItemOutcome updateItemOutcome = table.updateItem(updateItemSpec);

    //4. read updated bool
    item = table.getItem(spec);
    System.out.println(item);

    //5. update String breaks
    UpdateItemSpec updateItemSpec2 = new UpdateItemSpec().withPrimaryKey("jobid", jobID)
            .withUpdateExpression("set type=:s")
            .withValueMap(new ValueMap().withString(":s","updated"));
    UpdateItemOutcome updateItemOutcome2 = table.updateItem(updateItemSpec2);

    //4. read updated bool
    item = table.getItem(spec);
    System.out.println(item);
}}

In this code, Create works fine and getItem fetches the data. When I updated the cancel boolean column it works fine and returns the updated Item. However, when I try to update the String column type to different value it throws the following exception.

Exception in thread "main" com.amazonaws.AmazonServiceException: Unable to unmarshall exception response with the unmarshallers provided (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 4RHDOGACM1MOADU4N8RUPFMUJBVV4KQNSO5AEMVJF66Q9ASUAAJG; Proxy: null)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleErrorResponse(AmazonHttpClient.java:1862)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleServiceErrorResponse(AmazonHttpClient.java:1415)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1384)

The AWS-SDK-Version is 1.12.153 and JDK Version is 17.0.1. Also, when I add new string column type2 as part of the update and update it with another call it works. But, String column added as part of create is not getting updated.

Please let me know what I'm missing?

Thanks!

CodePudding user response:

The word type is one of many DynamoDB reserved words.

You cannot use a reserved word in an expression. Instead you need to use an alias and then provide DynamoDB with a map from the alias to the real name. For example you might indicate an alias by set #t=:s and then provide a map from #t to type.

Your code will look something like this:

UpdateItemSpec updateItemSpec2 =
    new UpdateItemSpec()
        .withPrimaryKey("jobid", jobID)
        .withUpdateExpression("set #t=:s")
        .withValueMap(new ValueMap().withString(":s","updated"))
        .withNameMap(new NameMap().with("#t","type"));
  • Related