一、概述

wiki上的解释:

universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems.

128位的UUID也并非没有重复的可能,理论证明这个重复的概率很小近乎为零以至于可以忽略。

UUID规范的表示格式通常用32个16进制字符表示(也就是"0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /"a" / "b" / "c" / "d" / "e" / "f"这16个字符)。

这32个字符通常被连字符“-”分成5个组(简记为8-4-4-4-12格式),如下:

8a2986cc-256d-470b-bc3d-027113f76553

5个组所表示的含义如下表所示(图表来自wiki):

UUID record layout
Name Length (bytes) Length (hex digits) Contents
time_low 4 8 integer giving the low 32 bits of the time
time_mid 2 4 integer giving the middle 16 bits of the time
time_hi_and_version 2 4 4-bit "version" in the most significant bits, followed by the high 12 bits of the time
clock_seq_hi_and_res clock_seq_low 2 4 1-3 bit "variant" in the most significant bits, followed by the 13-15 bit clock sequence
node 6 12 the 48-bit node id

其中version代表了UUID按照何种规则产生。UUID有四种版本:

1、Time-based UUID

2、DCE security UUID

3、Name-based UUID

4、Randomly generated UUID

Version 1 UUIDs are generated from a time and a node id (usually the MAC address); version 2 UUIDs are generated from an identifier (usually a group or user id), time, and a node id; versions 3 and 5 produce deterministic UUIDs generated by hashing a namespace identifier and name; and version 4 UUIDs are generated using a random or pseudo-random number.

variant(变体,或者更通俗的理解就是类型,type)代表了UUID的位布局。UUID也有四种变体,除了variant 位的不同,variant 1和variant 2的区别在于存储和传输时,variant 1用网络字节序(大端模式),variant 2用本地字节序(小端模式)。

二、Java中UUID实现

java中提供了UUID类。在UUID类的内部,将128位分两部分存储:最低有效位(64位)leastSigBits和最高有效位(64位)mostSigBits

其文档中对这两部分的位布局做了简单说明,但是这里好像与wiki上面的有点出入,按照wiki的说法,这里应该是variant 1而不是variant 2,并且在randomUUID()中重置variant位时也是重置了2个bit位:10xx,总之这里关注重点即可。

 * <p>The layout of a variant 2 (Leach-Salz) UUID is as follows:
*
* The most significant long consists of the following unsigned fields:
* <pre>
* 0xFFFFFFFF00000000 time_low
* 0x00000000FFFF0000 time_mid
* 0x000000000000F000 version
* 0x0000000000000FFF time_hi
* </pre>
* The least significant long consists of the following unsigned fields:
* <pre>
* 0xC000000000000000 variant
* 0x3FFF000000000000 clock_seq
* 0x0000FFFFFFFFFFFF node
* </pre>

UUID通过静态工厂方法产生伪随机的UUID,生成UUID实例的步骤大致如下:

1、产生长度为16的伪随机字节数组

2、重置version位和variant位,4bit的version位重置为0100,2bit的variant位重置为10xx。

3、调用私有构造器UUID(byte[] data),将步骤2中的随机字节数组的前8字节给mostSigBits,后8字节给leastSigBits,至此,一个UUID实例产生。

    public static UUID randomUUID() {
SecureRandom ng = numberGenerator;
if (ng == null) {
numberGenerator = ng = new SecureRandom();
} byte[] randomBytes = new byte[16];
ng.nextBytes(randomBytes);
randomBytes[6] &= 0x0f; /* clear version */
randomBytes[6] |= 0x40; /* set to version 4 */
randomBytes[8] &= 0x3f; /* clear variant */
randomBytes[8] |= 0x80; /* set to IETF variant */
return new UUID(randomBytes);
}
private UUID(byte[] data) {
long msb = 0;
long lsb = 0;
assert data.length == 16;
for (int i=0; i<8; i++)
msb = (msb << 8) | (data[i] & 0xff);
for (int i=8; i<16; i++)
lsb = (lsb << 8) | (data[i] & 0xff);
this.mostSigBits = msb;
this.leastSigBits = lsb;
}

用法如下:

            UUID uuid = UUID.randomUUID();
System.out.println(uuid.toString());//f2fbeb3c-07e0-41e2-8dd2-1a49e77d6d67
System.out.println(Long.toHexString(uuid.getMostSignificantBits()));//f2fbeb3c07e041e2
System.out.println(Long.toHexString(uuid.getLeastSignificantBits()));//8dd21a49e77d6d67

也可以产生version 3的UUID,以传入的字节数组name为参数,通过MD5算法生成长度为16的字节数组,之后的处理过程与上面的类似。

    public static UUID nameUUIDFromBytes(byte[] name) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("MD5 not supported");
}
byte[] md5Bytes = md.digest(name);
md5Bytes[6] &= 0x0f; /* clear version */
md5Bytes[6] |= 0x30; /* set to version 3 */
md5Bytes[8] &= 0x3f; /* clear variant */
md5Bytes[8] |= 0x80; /* set to IETF variant */
return new UUID(md5Bytes);
}

用法如下:

            UUID uuid = UUID.nameUUIDFromBytes("2018".getBytes());
System.out.println(uuid.toString());//84ddfb34-126f-33a4-8ee3-8d7044e87276

三、MySQL中的UUID

MySQL中提供了UUID()函数来获取UUID,必要的情况下可以方便地以此充当表的主键。此外,UUID_SHORT()函数返回一个64位的无符号数字。

四、参考资料

1、https://en.wikipedia.org/wiki/Universally_unique_identifier

2、https://tools.ietf.org/html/rfc4122#section-4.1

3、https://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_uuid

4、JDK1.6 src

												

UUID简记的更多相关文章

  1. RangePartitioner 实现简记

    摘要: 1.背景 2.rangeBounds 上边界数组源码走读 3.RangePartitioner的sketch 源码走读 4.determineBounds 源码走读 5.关于RangePart ...

  2. 使用C#代码生成一个随机的UUID

    在日常开发中常见于生成主键的ID,比较规范好用,详细代码如下(写注释是个好习惯): using System;using System.Collections.Generic;using System ...

  3. JAVA UUID 生成

    UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成UUID的API.UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址.纳秒级时间.芯 ...

  4. UUID库

    If you cannot afford to use Boost, then there is a very minimal library that I implemented which sim ...

  5. Solr4.0 如何配置使用UUID自动生成id值

    原文链接http://blog.csdn.net/keepthinking_/article/details/8501058#comments 最近学习了Lucene,随便也学习了Solr,Solr规 ...

  6. 解决svn uuid变更问题

    简介: 今天在snv根目录下重新定位上传的url,更改后出现如下错误 .可以看到,原来Repository创建者的uuid是前者,而现在我操作的是后者的uuid.因此,目前的操作办法是 使用相关命令更 ...

  7. python使用uuid库生成唯一id

    概述: UUID是128位的全局唯一标识符,通常由32字节的字符串表示. 它可以保证时间和空间的唯一性,也称为GUID,全称为: UUID -- Universally Unique IDentifi ...

  8. hibernate UUID问题

    前言:hibernate对于字符串类型主键支持UUID主键生成策略,(号称是世界上唯一的字符串) 运行环境:运行环境:hibernate5.2,mysql5.6 一,使用hibernate给Strin ...

  9. VC++ 产生GUID或UUID

    GUID 和 UUID 是一样的,表示全球唯一标识码. 下面是Windows系统中,产生GUID的一种方法(Windows API) char* GUID_Generator() { ] = {}; ...

随机推荐

  1. 【Java基础】【26网络编程】

    26.01_网络编程(网络编程概述)(了解) A:计算机网络 是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下, ...

  2. mtools-你可能没用过的mongodb神器

    前言 接触 mongodb 已经有一段时间了,从一开始的不了解,到现在已慢慢适应这个nosql领域的佼佼者,还是经历了不少波折. 在进行数据库选型的时候,许多人总是喜欢拿 mongodb和mysql. ...

  3. 磊哥测评之数据库SaaS篇:腾讯云控制台、DMC和小程序

    本文由云+社区发表 作者:腾讯云数据库 随着云计算和数据库技术的发展,数据库正在变得越来越强大.数据库的性能如处理速度.对高并发的支持在节节攀升,同时分布式.实时的数据分析.兼容主流数据库等强大的性能 ...

  4. -1-3 java集合框架基础 java集合体系结构 Collection 常用java集合框架 如何选择集合 迭代器 泛型 通配符概念 Properties 集合 迭代器

    集合又称之为容器存储对象的一种方式 •数组虽然也可以存储对象,但长度是固定的:显然需要可变长度的容器 集合和数组的区别?                 A:长度区别                  ...

  5. 痞子衡嵌入式:ARM Cortex-M文件那些事(0)- 文件关联

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式开发里的文件关联. 本篇是文件系列第一篇,本系列文章会逐一介绍ARM Cortex-M开发过程中(以IAR集成开发环境为例,其他开发 ...

  6. Django 系列博客(九)

    Django 系列博客(九) 前言 本篇博客介绍 Django 模板的导入与继承以及导入导入静态文件的几种方式. 模板导入 模板导入 语法:``{% include '模板名称' %} 如下: < ...

  7. jquery/js知识点收藏

    1]关于页面跳转 "window.location.href"."location.href"是本页面跳转 "parent.location.href ...

  8. Html5 localStorage 缓存

    1.客户端页面临时存贮数据变化多段,cookie ,session, data-xxx , hidden input 这些司空见惯不废话,我们采用 localStorage 特点:1.数据不会删除,除 ...

  9. Spring webflux

    Spring-webflux Spring 5.0 Spring-webflux 是一个全新的非堵塞的函数式 Reactive Web 框架,可以用来构建异步的.非堵塞的.事件驱动的服务. sprin ...

  10. 【20190328】CSS-transform-origin(变形原点)解析

    因为搜遍网上也没有一篇文章把transform-origin讲得很清楚的,所以自己总结了一下 transform-origin是变形原点,也就是该元素围绕着那个点变形或旋转,该属性只有在设置了tran ...