Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use DynamoDBAutoGeneratedKey to give me an auto generated key?

I need to use DynamoDBAutoGeneratedKey from the AWS SDK to give me a random key(of type String) that I can then use to do something. I can't find any examples online of doing this, and while it seems like it should be relatively straightforward I am really struggling to get this working. Can anyone link me to an example of this being used?

like image 582
Mark Keane Avatar asked Jun 19 '16 21:06

Mark Keane


People also ask

Does DynamoDB auto generate ID?

DynamoDB does not support auto-incrementing IDs for items. They can be a performance bottleneck at scale, and most workloads are better off with UUIDs anyway. However, there are use cases where auto-incrementing integers are required.

What is DynamoDBDocument?

DynamoDBDocument. Indicates that a class can be serialized as an Amazon DynamoDB document. For example, suppose that you wanted to map a JSON document to a DynamoDB attribute of type Map ( M ). The following code example defines an item containing a nested attribute (Pictures) of type Map.

What is DynamoDBMapper?

The DynamoDBMapper class is the entry point to Amazon DynamoDB. It provides access to a DynamoDB endpoint and enables you to access your data in various tables. It also enables you to perform various create, read, update, and delete (CRUD) operations on items, and run queries and scans against tables.

What is DynamoDBIndexHashKey?

Annotation Type DynamoDBIndexHashKeyAnnotation for marking a property in a class as the attribute to be used as the hash key for one or more global secondary indexes on a DynamoDB table. Applied to the getter method or the class field for the index hash key property.


2 Answers

Found easy answer.

String uniqueID = UUID.randomUUID().toString();

Screw using DynamoDBAutoGeneratedKey, sounds like a headache.

like image 95
Mark Keane Avatar answered Sep 24 '22 11:09

Mark Keane


@DynamoDBTable( tableName = "Details")
public class Details
{
    @DynamoDBGeneratedUuid( DynamoDBAutoGenerateStrategy.CREATE )
    private UUID id;
    ....
    @DynamoDBHashKey(attributeName = "id")
    @DynamoDBAutoGeneratedKey
    public UUID getId()
    {
        return id; 
    }
    // also you need to add the setter otherwise you will get an exception
    public void setId(UUID id)
    {
        this.id = id;
    }
...
like image 28
Claudia L Avatar answered Sep 24 '22 11:09

Claudia L