5
votes

I am trying to map by custom defined class to a DynamoDB table using DynamoDB Mapper annotations:

public class MyClass {
  String string1;
  List<String> stringList;
  Boolean flag;
  Map<String, String> map;
}

I know DDB mapper supports limited data type. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.DataTypes.html

But how can I map this class ? Any help on how can I map the list and map.

1

1 Answers

7
votes

The DynamoDB mapper will automatically interpret and assign the correct DynamoDB data type based on the Java type.

If you want to assign the DDB data type specifically, you can use DynamoDBTyped annotation.

Please refer the boolean attribute in the below example.

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.DynamoDBAttributeType;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTyped;

@DynamoDBTable(tableName = "yourTableName")
public class MyClass {

    String string1;
    List<String> stringList;
    Boolean flag;
    Map<String, String> map;

    @DynamoDBHashKey(attributeName = "string1")
    public String getString1() {
        return string1;
    }

    public void setString1(String string1) {
        this.string1 = string1;
    }

    @DynamoDBAttribute(attributeName = "stringList")
    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    @DynamoDBTyped(DynamoDBAttributeType.BOOL)
    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    @DynamoDBAttribute(attributeName = "map")
    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

}