Giter Site home page Giter Site logo

kirauid's Introduction

kiraUID

基于SnowFlake算法的可定制UID生成器

食用方式

默认配置

long workId = 1;
UidGenerator g = DefaultIdGenerator.createDefaultGenerator();
for (int i = 0; i < 5; i++) {
    System.out.println(g.nextId(workId));
}

输出

6739574785627394048

6739574785631588352

6739574785631588353

6739574785631588354

6739574785631588355

自定义配置

BitsConfig c = new BitsConfig();
c.addPart(BitsProperties.TIME); // 时间戳
c.addPart(BitsProperties.CUSTOM, 5); // 自定义参数 1
c.addPart(BitsProperties.CUSTOM, 5); // 自定义参数 2
c.addPart(BitsProperties.SEQUENCE); // 自增序列
c.setTwepoch(1420041600000L); // 设置起始时间(可以不设置)

BitsAllocator a = new BitsAllocator(c);
UidGenerator g = new DefaultIdGenerator(a);

long d1 = 1;
long d2 = 2;
for (int i = 0; i < 5; i++) {
    System.out.println(g.nextId(d1, d2));
}

输出

783490216609587200

783490216609587201

783490216609587202

783490216609587203

783490216609587204

解析ID

注意:解析一个ID必须使用生成该ID的generator,否则解析出的数据有误

long[] datas = generator.parseId(783490216609587200L);
System.out.println(Arrays.toString(datas));

输出

[186798624184, 1, 2, 0]

kirauid's People

Contributors

kamijoucen avatar

Stargazers

 avatar luyiese avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.