简述网站建设基本步骤,游戏推广代理,wordpress 文字颜色,南昌做网站开发的公司哪家好布隆过滤器是一种空间效率极高的概率数据结构#xff0c;用于测试一个元素是否是集合的成员。如果布隆过滤器返回 False#xff0c;则元素绝对不在集合中。如果返回 True#xff0c;则元素可能在集合中#xff0c;但也可能是一个误报。布隆过滤器利用了多个不同的哈希函数对…布隆过滤器是一种空间效率极高的概率数据结构用于测试一个元素是否是集合的成员。如果布隆过滤器返回 False则元素绝对不在集合中。如果返回 True则元素可能在集合中但也可能是一个误报。布隆过滤器利用了多个不同的哈希函数对元素进行哈希并将结果的位置在一个位数组上设置为 1。
1. redis5.0以上版本的布隆过滤器实现RedisBloom
RedisBloom 是 Redis 的一个模块提供了 Bloom Filter、Cuckoo Filter、Count-Min Sketch 和 Top-K 这些数据结构。这些数据结构特别适用于处理海量数据的场景因为它们是空间有效和计算高效的。
1.1 安装 RedisBloom:
确保你已经安装了 Redis。然后你可以从 RedisBloom 的 GitHub 存储库下载并构建它。
git clone --recursive https://github.com/RedisBloom/RedisBloom.git
cd RedisBloom
make这将生成一个名为 redisbloom.so 的共享对象文件。
1.2 运行 Redis 与 RedisBloom 模块:
redis-server --loadmodule ./redisbloom.so1.3 使用 Bloom Filter:
接下来你可以使用 redis-cli 或任何 Redis 客户端库与 Redis 交互。
例如使用 redis-cli
redis-cli添加元素到 Bloom Filter:
127.0.0.1:6379 BF.ADD myfilter item1
(integer) 1检查元素是否存在:
127.0.0.1:6379 BF.EXISTS myfilter item1
(integer) 1
127.0.0.1:6379 BF.EXISTS myfilter item2
(integer) 01.4 Python 示例:
要在 Python 中使用 RedisBloom你需要安装 redis-py 和 redisbloom 客户端库。
pip install redis redisbloom然后你可以这样使用
import redis
from redisbloom.client import Clientclient Client(hostlocalhost, port6379)
client.bfCreate(myfilter, errorRate0.01, capacity10000)
client.bfAdd(myfilter, item1)
print(client.bfExists(myfilter, item1)) # True
print(client.bfExists(myfilter, item2)) # False2. redis5.0以下版本的布隆过滤器实现手写实现
2.1 安装必要的库:
pip install redis bitarray2.2 布隆过滤器实现:
from datetime import datetime
import redis
from hashlib import md5class SimpleHash(object):def __init__(self, cap, seed):self.cap capself.seed seeddef hash(self, value):ret 0for i in range(len(value)):ret self.seed * ret ord(value[i])return (self.cap - 1) retclass BloomFilter(object):def __init__(self, host10.9.50.171, port6379, db1, passwordasmd888, keybloomfilter)::param host: the host of Redis:param port: the port of Redis:param db: witch db in Redis:param blockNum: one blockNum for about 90,000,000; if you have more strings for filtering, increase it.:param key: the keys name in Redisself.server redis.Redis(hosthost, portport, dbdb, passwordpassword, decode_responsesTrue)self.bit_size 1 25 # Redis的String类型最大容量为512M现使用4M误报率为(九/一百万)self.seeds [5, 7, 11, 13, 31, 37, 61]# self.seeds [5, 7, 11, 13, 31]self.key keyself.hashfunc []for seed in self.seeds:self.hashfunc.append(SimpleHash(self.bit_size, seed))def isContains(self, str_input):# 判断是否存在:param str_input::return:if not str_input:return Falsem5 md5()m5.update(str_input.encode())str_input m5.hexdigest()ret Truefor f in self.hashfunc:loc f.hash(str_input)bit_name self.server.getbit(self.key, loc)ret ret bit_namereturn retdef insert(self, str_input):添加新的值:param str_input::return:m5 md5()m5.update(str_input.encode())str_input m5.hexdigest()for f in self.hashfunc:loc f.hash(str_input)self.server.setbit(self.key, loc, 1)if __name__ __main__: 第一次运行时会显示 not exists!之后再运行会显示 exists! bf BloomFilter()old datetime.now()name www.baidusasdfasf.asdd4564asdif bf.isContains(name): # 判断字符串是否存在print(exists!)else:print(not exists!)bf.insert(name)new datetime.now()print(new - old) 文章转载自: http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.dbcw.cn.gov.cn.dbcw.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.prgnp.cn.gov.cn.prgnp.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.wphfl.cn.gov.cn.wphfl.cn http://www.morning.kgltb.cn.gov.cn.kgltb.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.jzlfq.cn.gov.cn.jzlfq.cn http://www.morning.wqrdx.cn.gov.cn.wqrdx.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.jntdf.cn.gov.cn.jntdf.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.pbksb.cn.gov.cn.pbksb.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.kkysz.cn.gov.cn.kkysz.cn http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.xmyrn.cn.gov.cn.xmyrn.cn http://www.morning.mlffg.cn.gov.cn.mlffg.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.zbmcz.cn.gov.cn.zbmcz.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.rnsjp.cn.gov.cn.rnsjp.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.wcft.cn.gov.cn.wcft.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.sxhdzyw.com.gov.cn.sxhdzyw.com http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.ztjhz.cn.gov.cn.ztjhz.cn http://www.morning.bpmdx.cn.gov.cn.bpmdx.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.lxwjx.cn.gov.cn.lxwjx.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.hsflq.cn.gov.cn.hsflq.cn http://www.morning.qszyd.cn.gov.cn.qszyd.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.plqhb.cn.gov.cn.plqhb.cn http://www.morning.dmnqh.cn.gov.cn.dmnqh.cn http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.nknt.cn.gov.cn.nknt.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.cpmwg.cn.gov.cn.cpmwg.cn http://www.morning.rqsnl.cn.gov.cn.rqsnl.cn http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.tkfnp.cn.gov.cn.tkfnp.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.wqbhx.cn.gov.cn.wqbhx.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.tyhfz.cn.gov.cn.tyhfz.cn http://www.morning.mkccd.cn.gov.cn.mkccd.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.qkqhr.cn.gov.cn.qkqhr.cn http://www.morning.ttshf.cn.gov.cn.ttshf.cn