All Projects → GUAIK-ORG → go-snowflake

GUAIK-ORG / go-snowflake

Licence: Apache-2.0 license
go-snowflake

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to go-snowflake

sno
Compact, sortable and fast unique IDs with embedded metadata.
Stars: ✭ 77 (-23.76%)
Mutual labels:  snowflake
vue-snowf
Snowfall component for Vue.js, let it snow on your page! ❄ demo: https://fuxy526.github.io/snowf/
Stars: ✭ 38 (-62.38%)
Mutual labels:  snowflake
java-sdk
一些常用的java sdk和工具类(日期工具类,分布式锁,redis缓存,二叉树,反射工具类,线程池,对称/非对称/分段加解密,json序列化,http工具,雪花算法,字符串相似度,集合操作工具,xml解析,重试Retry工具类,Jvm监控等)
Stars: ✭ 26 (-74.26%)
Mutual labels:  snowflake
DBTestCompare
Application to compare results of two SQL queries
Stars: ✭ 15 (-85.15%)
Mutual labels:  snowflake
laravel-snowflake
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
Stars: ✭ 94 (-6.93%)
Mutual labels:  snowflake
starlake
Starlake is a Spark Based On Premise and Cloud ELT/ETL Framework for Batch & Stream Processing
Stars: ✭ 16 (-84.16%)
Mutual labels:  snowflake
DiscordLookup
DiscordLookup | Get more out of Discord with Discord Lookup! Snowflake Decoder, Guild List with Stats, Invite Info and more...
Stars: ✭ 86 (-14.85%)
Mutual labels:  snowflake
versatile-data-kit
Versatile Data Kit (VDK) is an open source framework that enables anybody with basic SQL or Python knowledge to create their own data pipelines.
Stars: ✭ 144 (+42.57%)
Mutual labels:  snowflake
sunflake
Zero dependency, lightweight, snowflake generator
Stars: ✭ 17 (-83.17%)
Mutual labels:  snowflake
simple-ddl-parser
Simple DDL Parser to parse SQL (HQL, TSQL, AWS Redshift, BigQuery, Snowflake and other dialects) ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc. & table properties, types, domains, etc.
Stars: ✭ 76 (-24.75%)
Mutual labels:  snowflake
snowflake
a language
Stars: ✭ 16 (-84.16%)
Mutual labels:  snowflake
go-snowflake
❄ An Lock Free ID Generator for Golang based on Snowflake Algorithm (Twitter announced).
Stars: ✭ 206 (+103.96%)
Mutual labels:  snowflake
sqltools-snowflake-driver
A Snowflake driver for the SQLTools VSCode extension.
Stars: ✭ 28 (-72.28%)
Mutual labels:  snowflake
Excelerator
This is an Excel Addin for Windows that reads and writes data to Snowflake
Stars: ✭ 53 (-47.52%)
Mutual labels:  snowflake
distributed-id
基于netty4+twitter-snowFlake分布式Id生成之服务实现
Stars: ✭ 18 (-82.18%)
Mutual labels:  snowflake
onionfruit
OnionFruit™ Connect - Tor access client with country selection, bridge configuration, pluggable transports and experimental DNS support
Stars: ✭ 150 (+48.51%)
Mutual labels:  snowflake
pdo snowflake
PHP PDO driver for snowflake
Stars: ✭ 36 (-64.36%)
Mutual labels:  snowflake
piccolo
Netty4长连接网关
Stars: ✭ 19 (-81.19%)
Mutual labels:  snowflake
dbt-ml-preprocessing
A SQL port of python's scikit-learn preprocessing module, provided as cross-database dbt macros.
Stars: ✭ 128 (+26.73%)
Mutual labels:  snowflake
dremio-snowflake
Snowflake Connector for Dremio using the ARP SDK.
Stars: ✭ 14 (-86.14%)
Mutual labels:  snowflake

❄️ GO-Snowflake

Build Status

Snowflake简介

在单机系统中我们会使用自增id作为数据的唯一id,自增id在数据库中有利于排序和索引,但是在分布式系统中如果还是利用数据库的自增id会引起冲突,自增id非常容易被爬虫爬取数据。在分布式系统中有使用uuid作为数据唯一id的,但是uuid是一串随机字符串,所以它无法被排序。

Twitter设计了Snowflake算法为分布式系统生成ID,Snowflake的id是int64类型,它通过datacenterId和workerId来标识分布式系统,下面看下它的组成:

1bit 41bit 5bit 5bit 12bit
符号位(保留字段) 时间戳(当前时间-纪元时间) 数据中心id 机器id 自增序列

算法简介

在使用Snowflake生成id时,首先会计算时间戳timestamp(当前时间 - 纪元时间),如果timestamp数据超过41bit则异常。同样需要判断datacenterId和workerId不能超过5bit(0-31),在处理自增序列时,如果发现自增序列超过12bit时需要等待,因为当前毫秒下12bit的自增序列被用尽,需要进入下一毫秒后自增序列继续从0开始递增。


🚀 快速开始

🕹 克隆 & 运行

git clone https://github.com/GUAIK-ORG/go-snowflake.git

go run main.go

💾 安装 & 导入

go get github.com/GUAIK-ORG/go-snowflake

// 在项目中导入模块
import "github.com/GUAIK-ORG/go-snowflake/snowflake"

⚠️注意事项

  • 在多实例(多个snowflake对象)的并发环境下,请确保每个实例(datacenterid,workerid)的唯一性,否则生成的ID可能冲突。

📊 测试

本机测试:

参数 配置
OS MacBook Pro (13-inch, Late 2016, Four Thunderbolt 3 Ports)
CPU 2.9 GHz 双核Intel Core i5
RAM 8 GB 2133 MHz LPDDR3

测试代码

func TestLoad() {
    var wg sync.WaitGroup
    s, err := snowflake.NewSnowflake(int64(0), int64(0))
    if err != nil {
        glog.Error(err)
        return
    }
    var check sync.Map
    t1 := time.Now()
    for i := 0; i < 200000; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            val := s.NextVal()
            if _, ok := check.Load(val); ok {
                // id冲突检查
                glog.Error(fmt.Errorf("error#unique: val:%v", val))
                return
            }
            check.Store(val, 0)
            if val == 0 {
                glog.Error(fmt.Errorf("error"))
                return
            }
        }()
    }
    wg.Wait()
    elapsed := time.Since(t1)
    glog.Infof("generate 20k ids elapsed: %v", elapsed)
}

运行结果

load

🗂 使用说明

创建Snowflake对象

// NewSnowflake(datacenterid, workerid int64) (*Snowflake, error)
// 参数1 (int64): 数据中心ID (可用范围:0-31)
// 参数2 (int64): 机器ID    (可用范围:0-31)
// 返回1 (*Snowflake): Snowflake对象 | nil
// 返回2 (error): 错误码
s, err := snowflake.NewSnowflake(int64(0), int64(0))
if err != nil {
    glog.Error(err)
    return
}

生成唯一ID

s, err := snowflake.NewSnowflake(int64(0), int64(0))
// ......
// (s *Snowflake) NextVal() int64
// 返回1 (int64): 唯一ID
id := s.NextVal()
// ......

通过ID获取数据中心ID与机器ID

// ......
// GetDeviceID(sid int64) (datacenterid, workerid int64)
// 参数1 (int64): 唯一ID
// 返回1 (int64): 数据中心ID
// 返回2 (int64): 机器ID
datacenterid, workerid := snowflake.GetDeviceID(id))

通过ID获取时间戳(创建ID时的时间戳 - epoch)

// ......
// GetTimestamp(sid int64) (timestamp int64)
// 参数1 (int64): 唯一ID
// 返回1 (int64): 从epoch开始计算的时间戳
t := snowflake.GetTimestamp(id)

通过ID获取生成ID时的时间戳

// ......
// GetGenTimestamp(sid int64) (timestamp int64)
// 参数1 (int64): 唯一ID
// 返回1 (int64): 唯一ID生成时的时间戳
t := snowflake.GetGenTimestamp(id)

通过ID获取生成ID时的时间(精确到:秒)

// ......
// GetGenTime(sid int64)
// 参数1 (int64): 唯一ID
// 返回1 (string): 唯一ID生成时的时间
tStr := snowflake.GetGenTime(id)

查看时间戳字段使用占比(41bit能存储的范围:从epoch开始往后69年)

// ......
// GetTimestampStatus() (state float64)
// 返回1 (float64): 时间戳字段使用占比(范围 0.0 - 1.0)
status := snowflake.GetTimestampStatus()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].