Cassandra: Generate a unique ID?

后端 未结 4 1337
难免孤独
难免孤独 2020-12-08 07:25

I\'m working on a distributed data base. I\'m trying to generate a unique ID that will serve as a column family primary key in cassandra.

I read some articl

相关标签:
4条回答
  • 2020-12-08 07:47

    You can use the TimeUUID type in Cassandra, which backs a Type 1 UUID. This uses the current time and the creator's MAC address and a sequence number. If the TimeUUID number is generated correctly this can be done with zero collisions (you can use the CQL now() method or insert your own, the java SDK's provide some thread-safe implementations). The main advantage of TimeUUIDs is that the IDs can be time ordered. See http://wiki.apache.org/cassandra/TimeBaseUUIDNotes for more info.

    However, the time ordering is unlikely to be useful for row primary keys, since the ordering is useless when using a hash partitioner, though possible using a clustering key. And also the complexity of generating a unique ID could be a source of bugs if you roll your own. Cassandra also supports Type 4 UUIDs by using the UUID type. These are just random bits. There is a collision probability, but the collision probability (assuming uncorrelated random number sources, which it will be if you generate in Java) is extremely low - if you created 1 billion a second for 100 years the probability of one collision is about 50%. (See http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates for more details.)

    0 讨论(0)
  • 2020-12-08 07:56

    As said by Richard you can use TimeUUID, and generating TimeUUID value is not a big deal. Just follow cassandra FAQ timeuuid.

    0 讨论(0)
  • 2020-12-08 07:59

    You should investigate using Twitter Snowflake. From the project readme:

    As we at Twitter move away from Mysql towards Cassandra, we've needed a new way to generate id numbers. There is no sequential id generation facility in Cassandra, nor should there be.

    Snowflake uses an intuitive algorithm that generates longs which are both time-ordered and unique. Since your database is distributed, this service should suit your needs well.

    0 讨论(0)
  • 2020-12-08 08:02

    You need to use cassandra function now() to generate timeuuid and use uuid() function to generate uuid type string.

    0 讨论(0)
提交回复
热议问题