美文网首页
RedisBloom 模块布隆过滤器的导出与导入

RedisBloom 模块布隆过滤器的导出与导入

作者: AlphaHinex | 来源:发表于2025-11-08 18:51 被阅读0次

原文地址:https://alphahinex.github.io/2025/11/09/redis-bloom-filter-dump-and-load/


description: "介绍 RedisBloom 模块中用于导出和导入布隆过滤器的命令 BF.SCANDUMP 和 BF.LOADCHUNK,并提供一个 Python 脚本示例,用于实现布隆过滤器的数据迁移。"
date: 2025.11.09 10:26
categories:
- Redis
- Python
tags: [Redis, Data Structures, Python]
keywords: Redis, RedisBloom, Bloom Filter, BF.SCANDUMP, BF.LOADCHUNK, Data Migration, Python


RedisBloom 模块为 Redis 提供了 Bloom Filter 数据结构,除了基本的创建、添加、查询等操作外,还提供了用来导出单个过滤器的 BF.SCANDUMP 命令,和导入命令 BF.LOADCHUNK

官方文档 Example

Syntax

BF.SCANDUMP key iterator

iterator 参数初始值为 0,表示从头开始导出,命令返回两个元素的数组:

  1. 下一个迭代器值,如果再次为 0 则表示导出完成
  2. 导出的数据块
BF.LOADCHUNK key iterator data

Examples

redis> BF.RESERVE bf 0.1 10
OK
redis> BF.ADD bf item1
1) (integer) 1
redis> BF.SCANDUMP bf 0
1) (integer) 1
2) "\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x02\x00\x00\x00\b\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x9a\x99\x99\x99\x99\x99\xa9?J\xf7\xd4\x9e\xde\xf0\x18@\x05\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00"
redis> BF.SCANDUMP bf 1
1) (integer) 9
2) "\x01\b\x00\x80\x00\x04 \x00"
redis> BF.SCANDUMP bf 9
1) (integer) 0
2) ""
redis> DEL bf
(integer) 1
redis> BF.LOADCHUNK bf 1 "\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x05\x00\x00\x00\x02\x00\x00\x00\b\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x9a\x99\x99\x99\x99\x99\xa9?J\xf7\xd4\x9e\xde\xf0\x18@\x05\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00"
OK
redis> BF.LOADCHUNK bf 9 "\x01\b\x00\x80\x00\x04 \x00"
OK
redis> BF.EXISTS bf item1
(integer) 1
chunks = []
iter = 0
while True:
    iter, data = BF.SCANDUMP(key, iter)
    if iter == 0:
        break
    else:
        chunks.append([iter, data])

# Load it back
for chunk in chunks:
    iter, data = chunk
    BF.LOADCHUNK(key, iter, data)

可用于布隆过滤器数据迁移的 Python 脚本

Conda Env

$ conda create -n redisbloom python=3.12
$ conda activate redisbloom
$ pip install redis[hiredis]
$ pip list
Package    Version
---------- -------
hiredis    3.3.0
pip        25.2
redis      7.0.1
setuptools 80.9.0
wheel      0.45.1

备份脚本

redis_bloom_backup.py

import redis
import pickle
import argparse

def backup(key):
    r = redis.Redis(host='localhost', port=6379)

    chunks = []
    iter = 0
    while True:
        iter, data = r.bf().scandump(key, iter)
        if iter == 0:
            break
        else:
            chunks.append([iter, data])
    
    with open(f"dump_{key}.pkl", 'wb') as file:
        pickle.dump(chunks, file)
        
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="SCANDUMP bloom filter to a file.")
    parser.add_argument("key", type=str, help="Key of bloom filter")

    args = parser.parse_args()

    backup(args.key)
$ python redis_bloom_backup.py "test:restore"
$ $ ls -alh dump_test:restore.pkl
-rw-rw-r-- 1 root root 2.1G 11月  8 15:51 dump_test:restore.pkl

即使未向过滤器中插入数据,创建好指定容量的过滤器后,导出时,数据文件也是 BF.INFO 的 Size 大小。

恢复脚本

redis_bloom_restore.py

import redis
import pickle
import argparse

def restore(dump_file, key):
    r = redis.Redis(host='localhost', port=6379)
    
    with open(dump_file, 'rb') as file:
        chunks = pickle.load(file)
    
    for chunk in chunks:
        iter, data = chunk
        r.bf().loadchunk(key, iter, data)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="LOADCHUNK bloom filter from a file.")
    parser.add_argument("dump_file", type=str, help="Dump file of bloom filter")
    parser.add_argument("key", type=str, help="Key of bloom filter")

    args = parser.parse_args()

    restore(args.dump_file, args.key)
$ python redis_bloom_restore.py dump_test:restore.pkl tmpbf
$ redis-cli bf.info tmpbf
 1) Capacity
 2) (integer) 1000000000
 3) Size
 4) (integer) 2157872304
 5) Number of filters
 6) (integer) 1
 7) Number of items inserted
 8) (integer) 999973866
 9) Expansion rate
10) (integer) 2

导入时,不能提前创建过滤器,否则会报错,直接向不存在的 key 中导入即可。

相关文章

  • Guava - 布隆过滤器的使用

    布隆过滤器简单介绍 布隆过滤器介绍 maven引入 布隆过滤器的使用 参考及拓展 Guava的布隆过滤器 布隆过滤...

  • 模块 & promise

    一、模块(导入与导出) 1、export (1)导出可以导出多个命名模块导出时同时要声明 (2)导入通过解构导入模...

  • kata05:布隆过滤器

    这次kata的内容:实现一个布隆过滤器 布隆过滤器 (Bloom Filter) 什么是布隆过滤器呢?简单来说, ...

  • redis插件安装-bloom模块

    布隆过滤器 Redis 官方提供的布隆过滤器到了 Redis 4.0 提供了插件功能之后才正式登场。布隆过滤器作为...

  • SpringBoot2.x—使用Redis的bitmap实现布隆

    1. 布隆过滤器 1.1 布隆过滤器设计思想 布隆过滤器(Bloom Filter,下文简称BF)是专门用来检测集...

  • 模块化

    1、exports与require 模块导出 模块导入

  • Redis-001、安装布隆过滤器

    一、在Redis上安装布隆过滤器 二、Redis的布隆过滤器使用

  • JavaGuide知识点整理——布隆过滤器

    什么是布隆过滤器? 首先我们需要谅解布隆过滤器的概念。布隆过滤器是一个叫做Bloom的老哥1970年提出的。我们可...

  • 面试题 延伸 之 布隆去重的原理及实现

    什么情况下需要布隆过滤器? 布隆过滤器原理 布隆过滤器(Bloom Filter)的核心实现是一个超大的位数组和几...

  • 布隆过滤器

    参考:布隆过滤器Hash 和 Bloom Filter 概念 布隆过滤器(Bloom Filter)是 1970 ...

网友评论

      本文标题:RedisBloom 模块布隆过滤器的导出与导入

      本文链接:https://www.haomeiwen.com/subject/vhwkfstx.html