How to make a UUID in DynamoDB?

2020-01-29 09:51发布

In my db scheme, I need a autoincrement primary key. How I can realize this feature?

PS For access to DynamoDB, I use dynode, module for Node.js.

9条回答
走好不送
2楼-- · 2020-01-29 10:31

If you're okay with gaps in your incrementing id, and you're okay with it only roughly corresponding to the order in which the rows were added, you can roll your own: Create a separate table called NextIdTable, with one primary key (numeric), call it Counter.

Each time you want to generate a new id, you would do the following:

  • Do a GetItem on NextIdTable to read the current value of Counter --> curValue
  • Do a PutItem on NextIdTable to set the value of Counter to curValue + 1. Make this a conditional PutItem so that it will fail if the value of Counter has changed.
  • If that conditional PutItem failed, it means someone else was doing this at the same time as you were. Start over.
  • If it succeeded, then curValue is your new unique ID.

Of course, if your process crashes before actually applying that ID anywhere, you'll "leak" it and have a gap in your sequence of IDs. And if you're doing this concurrently with some other process, one of you will get value 39 and one of you will get value 40, and there are no guarantees about which order they will actually be applied in your data table; the guy who got 40 might write it before the guy who got 39. But it does give you a rough ordering.

Parameters for a conditional PutItem in node.js are detailed here. http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html#!AWS/DynamoDB.html. If you had previously read a value of 38 from Counter, your conditional PutItem request might look like this.

var conditionalPutParams = {
    TableName: 'NextIdTable',
    Item: {
        Counter: {
            N: '39'
        }
    },
    Expected: {
        Counter: {
            AttributeValueList: [
                {
                    N: '38'
                }
            ],
            ComparisonOperator: 'EQ'
        }
    }
};
查看更多
放荡不羁爱自由
3楼-- · 2020-01-29 10:31

Another approach is to use a UUID generator for primary keys, as these are highly unlikely to clash.

IMO you are more likely to experience errors consolidating primary key counters across highly available DynamoDB tables than from clashes in generated UUIDs.

For example, in Node:

npm install uuid

var uuid = require('uuid');

// Generate a v1 (time-based) id
uuid.v1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'

// Generate a v4 (random) id
uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

Taken from SO answer.

查看更多
SAY GOODBYE
4楼-- · 2020-01-29 10:33

For those coding in Java, DynamoDBMapper can now generate unique UUIDs on your behalf.

DynamoDBAutoGeneratedKey

Marks a partition key or sort key property as being auto-generated. DynamoDBMapper will generate a random UUID when saving these attributes. Only String properties can be marked as auto-generated keys.

Use the DynamoDBAutoGeneratedKey annotation like this

@DynamoDBTable(tableName="AutoGeneratedKeysExample")
public class AutoGeneratedKeys { 
    private String id;

    @DynamoDBHashKey(attributeName = "Id")
    @DynamoDBAutoGeneratedKey
    public String getId() { return id; }
    public void setId(String id) { this.id = id; } 

As you can see in the example above, you can apply both the DynamoDBAutoGeneratedKey and DynamoDBHashKey annotation to the same attribute to generate a unique hash key.

查看更多
登录 后发表回答