Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UUIDs for DynamoDB?

Is it possible to get DynamoDB to automatically generate unique IDs when adding new items to a table?

I noticed the Java API mentions @DynamoDBAutoGeneratedKey so I'm assuming there's a way to get this working with PHP as well.

If so, does the application code generate these IDs or is it done on the DynamoDB side?

like image 561
Adam Biggs Avatar asked Jan 24 '12 04:01

Adam Biggs


People also ask

Is UUID DynamoDB partition key good?

Choosing Keys For example, unique Partition keys based on UUID's is ideal. Avoid having a large number of duplicate Partition key items (with different Sort key values). A common use case for me is to use DynamoDB for transaction logs or states in a microservice.

Can DynamoDB generate unique ID?

Because DynamoDB already guarantees that the pk attribute is unique, you need a mechanism to ensure that the userName and email attributes are also unique. Every time that you insert a new item into the table, you also must insert the other two items. This guarantees uniqueness for the userName and email attributes.

How do I create a unique constraint in DynamoDB?

To define a unique constraint for the username too, just add items with the type “username”. Also, by defining the rarer of the two parts of the key as the partition, you'll end up with more partitions, which is a best practice when working with DynamoDB.

Does DynamoDB enforce primary key?

Short answer: No. DynamoDB is a key:value store. It is very good at quickly retrieving/saving Items because it does a couple of compromise. This is a constraint you have to handle yourself.


1 Answers

Good question - while conceptually possible, this seems not currently available as a DynamoDB API level feature, insofar neither CreateTable nor PutItem refer to such a functionality.

The @DynamoDBAutoGeneratedKey notation you have noticed is a Java annotation, i.e. syntactic sugar offered by the Java SDK indeed:

An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code.

As such @DynamoDBAutoGeneratedKey is one of the Amazon DynamoDB Annotations offered as part of the Object Persistence Model within the Java SDK's high-level API (see Using the Object Persistence Model with Amazon DynamoDB):

Marks a hash key or range key property as being auto-generated. The Object Persistence Model will generate a random UUID when saving these attributes. Only String properties can be marked as auto-generated keys.

like image 187
Steffen Opel Avatar answered Sep 26 '22 23:09

Steffen Opel