UUIDs for DynamoDB?

て烟熏妆下的殇ゞ 提交于 2019-12-17 22:38:01

问题


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?


回答1:


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.




回答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;
}



回答3:


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




回答4:


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?




回答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());



回答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.



来源:https://stackoverflow.com/questions/8982148/uuids-for-dynamodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!