当前位置:网站首页>十億數據量 判斷元素是否存在
十億數據量 判斷元素是否存在
2022-06-13 03:56:00 【怪只怪滿眼盡是人間烟火】
核心是采用:bitMap
假設一個場景,要存0-7之內的數字[3,5,6,1,2],盡量的節省空間。
傳統思路就是單純使用集合存儲:
使用redis的set集合存儲,但若是數據量放大到十億級別,集合的所占用的內存會很是大。並且,redis單個實例最大為2.5億。
另外一種思路是使用BitMap:
錶示[3,5,7,1,2],咱們能够用8bit的空間來存儲,每一個數字都在對應的比特置中以1的方式錶示
| 比特置7 | 比特置 6 | 比特置 5 | 比特置 4 | 比特置 3 | 比特置 2 | 比特置 1 | 比特置 0 |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 1 | 0 | 1 | 1 | 1 | 0 |
這樣,相當於每一比特只存儲了一個字符,只需要知道該下標是否為1,即可。
這裏是不是就會發現,存儲的數據必須為正整數,才可以使用bitMap進行存儲。
所以我們需要做的就是,把數據轉換成:正整數。
我們采用google開源的cityhash算法,先將string轉換為32比特整數,然後構建512MB的bitmap存儲這些整數。
為什麼是32比特呢,因為redis實現了bitMap算法,可以從用redis的setBit進行數據存儲。但是因為redis官方規定:setting bit number 2^32 -1;也就是說,存儲的正整數必須小於32比特。
有一個很好的工具類hutool,實現了cityhash算法,直接引用:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.2</version>
</dependency> String code="237021200005599";
byte[] codebyte = code.getBytes();
int codeInt = HashUtil.cityHash32(codebyte);
System.out.println(codeInt);但是由於,HashUtil.cityHash32返回的是int,是一個整數,redis的bitMap僅支持正整數,存在差异性,所以我不會了。最好存進去的數就是一個32比特正整數。
還有一個辦法就是下面這個:不要使用redis的bitMap
RoaringBitmap 和 Roaring64Bitmap
32比特以下數據直接使用 RoaringBitmap;(包含負數)
64比特以下數據直接使用 Roaring64Bitmap;(包含負數)
使用hutool工具,轉換成64比特的整數,Roaring64Bitmap直接支持Long類型。
<dependency>
<groupId>org.roaringbitmap</groupId>
<artifactId>RoaringBitmap</artifactId>
<version>0.9.0</version>
</dependency> //將字符串轉換成64比特bit
String code="ddadsadsa";
byte[] codebyte = code.getBytes();
Long codeInt = HashUtil.cityHash64(codebyte);
//將Long插入到bitMap中
Roaring64Bitmap rr = Roaring64Bitmap.bitmapOf();
rr.add(codeInt);
long l = -4543844943551498834L;
//判斷是否存在
System.out.println(rr.contains(l));边栏推荐
- 【愚公系列】2022年06月 .NET架构班 081-分布式中间件 ScheduleMaster的API自定义任务
- Lambda终结操作查找与匹配allMatch
- Jumpserver installation
- Single chip microcomputer: MODBUS multi computer communication program design
- MCU: RS485 communication and Modbus Protocol
- Goframe day 4
- Express 100
- 单片机:PCF8591 应用程序
- 【 développement d'essais 】 sélénium d'essais automatisés (Ⅲ) - - analyse du cadre unitest
- 缓存读写--写
猜你喜欢

Jumpserver: user - system privileged user - Asset - authorization

Lightweight digital mall system based on PHP

MCU: pcf8591 hardware interface

Jumpserver installation

LVS four layer load balancing cluster (4) main methods of load balancing
![[test development] automatic test selenium (I)](/img/cd/b6dc4ac53b4f30f745ec0590ac384b.png)
[test development] automatic test selenium (I)

Line height equals height why not center

单片机:NEC 协议红外遥控器

Value of line height

Single chip microcomputer: basic concepts of a/d and d/a
随机推荐
Jumpserver: user - system privileged user - Asset - authorization
GoFrame第五天
【测试开发】软件测试基础篇
单片机:I2C通信协议讲解
V-bind and v-on
[test development] automated test selenium (II) -- common APIs for webdriver
Binocular vision -- creating an "optimal solution" for outdoor obstacle avoidance
[note]vs2015 compilation of masm32 using MASM32 Library
[test development] installation of test management tool Zen path
Interpretation of mobile phone private charging protocol
双目视觉——打造室外避障的“最优解”
OKR和KPI的区别
5G China unicom 直放站 网管协议 实时性要求
单片机:Modbus 多机通信程序设计
扫地机器人如何才能避障不“智障”?五种主流的避障技术解析
Single chip microcomputer: basic concepts of a/d and d/a
【Web】Cookie 和 Session
Lambda end operation find and match findfirst
微信扫描二维码无法下载文件的解决办法
Lambda终结操作max&min