Why doesn't DynamoDBMapper.save() append to arrays?
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.
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
Shuo, thanks for the info. Are you able to give a general timeframe on when this would be implemented?
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.
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 for me it's not working it's behaving as if it's an update, can you suggest any solution for it
@singireddyv Can you give me the following information.
- Which version of Java SDK are you using
- 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.
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;
}
}
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
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.
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.