50

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?

Madura Pradeep
  • 2,378
  • 1
  • 30
  • 34
Adam Biggs
  • 3,527
  • 4
  • 23
  • 27

7 Answers7

36

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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Steffen Opel
  • 63,899
  • 11
  • 192
  • 211
  • 2
    Thanks I'm not familiar with Java so your answer was really helpful. So @DynamoDBAutoGeneratedKey is simply getting the Java API to generate a UUID, not Dynamo itself right? It would be really great if Dynamo had a UUID attribute type which would generate the IDs on their side and return them when adding new items. – Adam Biggs Jan 24 '12 at 17:56
  • 1
    @Adam: Indeed, `@DynamoDBAutoGeneratedKey` is simply a custom annotation provided by the Java SDK, which yields code being generated at compile time and in turn executed within the Java application to generate the UUID at runtime - this has nothing to do with DynamoDB as such. And I agree, a native DynamoDB UUID datatype would be quite convenient - let's hope the AWS team will expand the DynamoDB functionality over time, they are well known for responding to customer demand and innovate accordingly! – Steffen Opel Jan 24 '12 at 18:51
5

While working with dynamodb in javascript with nodejs. I use the npm module uuid to genrate unique key.

Ex:

id=uuid.v1();

refer :uuid npm

Simran
  • 2,364
  • 1
  • 12
  • 19
2

By using schema based AWS dynamodb data mapper library in Node.js, Hash key (id) will be generated automatically. Auto generated ids are based on uuid v4.

For more details, have a look on following aws package.

Data Mapper with annotation

Data Mapper package for Javascript

Sample snipet

@table('my_table')
class MyDomainClass {
    @autoGeneratedHashKey()
    id: string;

    @rangeKey({defaultProvider: () => new Date()})
    createdAt: Date;
}
Ashok JayaPrakash
  • 2,115
  • 1
  • 16
  • 22
1

The client can create a (for all intents and purposes) unique ID either by picking a long random id (DynamoDB supports 128-bit integers, for example), or by picking an ID which contains the client's IP address, CPU number, and current time - or something along these lines. The UUID standard even includes a standard way to do this (and you have libraries in various languages to create such UUIDs on the client side), but you don't really need to use a standard. And interesting question is how do you plan to find these items if they have random keys. Or are you planning to use a secondary index?

Nadav Har'El
  • 11,785
  • 1
  • 24
  • 45
1

The 2022 answer is here: https://dev.to/prabusah_53/aws-lambda-in-built-uuid-382f

External libraries are no longer needed.

Gullbyrd
  • 1,545
  • 1
  • 16
  • 14
-5

Here is another good method taken from mkyong

http://www.mkyong.com/java/how-to-get-current-timestamps-in-java/

I adjusted his method to get the milliseconds instead of the actual date

java.util.Date date= new java.util.Date();
System.out.println(new Timestamp(date.getTime()).getTime());
josliber
  • 43,891
  • 12
  • 98
  • 133
-6

The approach I'm taking is to use the current timestamp for the hash-key (or the range-key, if using a range-key too). Store the timestamp as an integer, representing the number of milliseconds since the start of the "UNIX epoch" (in the UTC timezone). Many date/time libraries can produce this number for you.

This has the advantage that if you want to have a "creation time" field in your table, your UUID already stores this information. Just call another method in your date/time library to convert the timestamp to a readable format.

(Be sure to handle the exception which will occur if a second item is created in the same table with the same millisecond timestamp; just fall back and retry the operation in that case, with a slightly later, current timestamp.)

For example:

User table

hash-key only: userID (timestamp of the creation of this user).

WidgetAttributes table

hash-key plus range-key.
hash-key: userID (use the userID from the User table of the user to whom the widget belongs). range-key: attribID (use the timestamp of the creation of this widget-attribute).

Now you can run "query" operations on the WidgetAttributes table to get all widget-attributes for a certain user; by using "greater-than-zero" as the query-parameter for the range-key.

Community
  • 1
  • 1
FreeAgent
  • 77
  • 2
  • 1
  • 9
    timestamp is not guaranteed to be unique, especially if multiple clients are running in parallel. – Jason Feb 08 '16 at 21:40
  • 5
    @shle2821 It'd be better to just generate a UUID for the id and store the timestamp in a separate field than this suggestion. – Lo-Tan May 04 '16 at 04:17