Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java DynamoDBMapper. Mapping Attribute value 'M'

I'm just getting started on my first DynamoDB project and I've been trying to read the documentation as much as possible. I think the best possibility for my project is to use the High Level DynamoDbMapper in the SDK to allow CRUD operations.

In the DynamoDB Documentation there is a type of attribute value 'M' which can be seen here http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_AttributeValue.html

In the lower level java API, such as getItem, or getItemBatch, this value type translates to java.util.map<>.

But I can't seem to find any resources that say I can use the HigherLevel DBMapper to use this data type. Supported Data Types here. http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.DataTypes.html

I noticed at the bottom of the page it gives some insight to creating your own higher level mapping system. But I figured I would ask here first before I dove into the code for trying that. So I guess my question is... Is there anyway to use the DynamoDBMapper to work with a Java.Util.Map data type?

The only insight I could find on google was this weird github error where the user is seemingly doing exactly what I want to do. https://github.com/aws/aws-sdk-java/issues/520

Hope this makes sense. Ralph

like image 871
Ralph Emerson Avatar asked Dec 05 '25 05:12

Ralph Emerson


1 Answers

UPDATE: Noticed that you need map a Java.Util.Map property into DynamoDB map attribute, so everything below doesn't answer your question, sorry. In my case, a property of custom class is used, not a property of Java.Util.Map class.


Suppose your table has name "my_table", and suppose the following is a json dump of a record:

{
  uuid: "52f9d257-7998-4379-928b-9d41d70dd8a8",
  my_map: {
    field1: 123,
    field2: 456
  }  
}

(Here my_map is the desired field of 'M' type)

To use DynamoDBMapper for such table you would need to create two annotated classes, the first one for the table itself:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@DynamoDBTable(tableName="my_table")
public class MyClass {
    private String uuid;
    private MyMapClass myMap; 

    @DynamoDBHashKey(attributeName="uuid")
    public String getUuid() {
        return uuid;
    }   
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @DynamoDBAttribute(attributeName = "my_map")    
    public MyMapClass getMyMap() {
        return myMap;
    }
    public void setMyMap(MyMapClass myMap) {
        this.myMap = myMap;
    }       
}

and the second one -- for the my_map field:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;


@DynamoDBDocument
public class MyMapClass {
    private int field1;
    private int field2;

    @DynamoDBAttribute(attributeName = "field1")    
    public int getField1() {
        return field1;
    }
    public void setField1(int field1) {
        this.field1 = field1;
    }

    @DynamoDBAttribute(attributeName = "field2")    
    public int getField2() {
        return field2;
    }
    public void setField2(int field2) {
        this.field2 = field2;
    }
}

Here is a usage example:

...
DynamoDBMapper dbMapper = new DynamoDBMapper(dbClient);
...
MyClass item = new MyClass();
item.setUuid("52f9d257-7998-4379-928b-9d41d70dd8a8");
MyMapClass map = new MyMapClass();
map.setField1(123);
map.setField2(456);
item.setMyMap(map);
dbMapper.save(item);

Hope this helps!

like image 52
xtx Avatar answered Dec 07 '25 19:12

xtx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!