aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

Why doesn't DynamoDBMapper.save() append to arrays?

Open doapp-jeremy opened this issue 13 years ago • 10 comments

I have an array I want to append to in my dynamo table, currently I can't do this with DynamoDBMapper.save(). I first have to read the row out of dynamo, append to the array in memory, and then save it. I would like to be able to just save my object, and if the row exists, append to the array if my config is UPDATE, and overwrite if my config is CLOBBER.

Currently, with using an UpdateItemRequest, I can do this in one call.

Is this something that will be possible with DynamoDBMapper in the future.

doapp-jeremy avatar May 10 '13 16:05 doapp-jeremy

Hi jeremy,

Sorry for the late reply. The feature you are asking is indeed very useful when working with set type attributes. We will definitely consider adding this feature in the future. However, for the sake of backward compatibility, we would rather not change the behavior of UPDATE, since it might break other customers' code if they are trying to overwrite a set attribute using the default configuration. What we will probably end up doing is to add a new SaveBehavior option (something like APPEND_SET), and the mapper will be notified to force an "ADD" action on any set type attribute in the modeled object. We will keep you updated once this feature is available.

Thanks. -Shuo

hanshuo-aws avatar Aug 14 '13 18:08 hanshuo-aws

Shuo, thanks for the info. Are you able to give a general timeframe on when this would be implemented?

doapp-jeremy avatar Aug 14 '13 18:08 doapp-jeremy

I am afraid I cannot provide any ETA on this. But I have already added this into our backlog, and will keep you updated on our progress.

hanshuo-aws avatar Aug 14 '13 21:08 hanshuo-aws

Hi @doapp-jeremy, We are happy to let you know, we have include APPEND_SET support in the latest version of the SDK. Let us know how it works for you!

wmatveyenko avatar Sep 12 '13 20:09 wmatveyenko

@wmatveyenko for me it's not working it's behaving as if it's an update, can you suggest any solution for it

singireddyv avatar Apr 27 '16 23:04 singireddyv

@singireddyv Can you give me the following information.

  1. Which version of Java SDK are you using
  2. Can you provide sample code you are using so that I will have an idea on what you are trying to do and how you are doing it.

Thank you.

varunnvs92 avatar Apr 28 '16 00:04 varunnvs92

I am using SDK 1.7

DynamoDBMapperConfig config = new DynamoDBMapperConfig(DynamoDBMapperConfig.SaveBehavior.APPEND_SET); for(Details detail:detailsList){ System.out.println(config); mapper.save(detail,config);}

@DynamoDBTable(tableName = "Details") public class Details extends PersistentEntity {

private String hubDate;    
private String email;    
private Set<String> product;

@DynamoDBHashKey(attributeName = "hubDate")
public String getHubDate() {
    return hubDate;
}


public void setHubDate(String hubDate) {
    this.hubDate = hubDate;
}

@DynamoDBRangeKey(attributeName = "email")
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@DynamoDBAttribute(attributeName = "product")
@DynamoDBMarshalling(marshallerClass = SetDynamoMarshalling.class)
public Set<String> getProduct() {
    if(product==null){
        return new HashSet<String>();}
    return product;
}

public void setProduct(Set<String> product) {
    this.product = product;
}

}

below is my marshelling class public class SetDynamoMarshalling implements DynamoDBMarshaller<Set> {

private static final Logger log = LoggerFactory.getLogger(SetDynamoMarshalling.class);

@Override
public String marshall(Set set) {
    return set.toString();
}

@Override
public Set<String> unmarshall(Class<Set<String>> aClass, String s) {
    String[] products = s.split(",");
    Set<String> productSet = new HashSet<String>();
    for(String product:products ){
        productSet.add(product);
    }
    return productSet;
}

}

singireddyv avatar Apr 28 '16 03:04 singireddyv

I tried to use batchSave(listDetails,config) but it's giving me exception as ArryaList must be annotated with DyanmoDBTable, so moved to saving each item at a time

singireddyv avatar Apr 28 '16 03:04 singireddyv

APPEND_SET only works for things that are stored as sets in DynamoDB. You're explicitly marshaling the Java set into a single String for storage in DynamoDB.

fernomac avatar Apr 28 '16 05:04 fernomac

Thanks removed marshaling and it helped, but if I tried to save is as batchSave(detailList,config) giving exception as com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Class class java.util.ArrayList must be annotated with interface com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable, any help regarding this is helpful. thanks in advance.

singireddyv avatar Apr 28 '16 08:04 singireddyv