iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringBoot+Redis如何实现布隆过滤器
  • 305
分享到

SpringBoot+Redis如何实现布隆过滤器

2023-06-29 13:06:14 305人浏览 泡泡鱼
摘要

小编给大家分享一下SpringBoot+Redis如何实现布隆过滤器,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!简述关于布隆过滤器的详细介绍,我在这里就不再赘述一遍了我们首先知道:BloomFilter使用长度为m bi

小编给大家分享一下SpringBoot+Redis如何实现布隆过滤器,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

简述

关于布隆过滤器的详细介绍,我在这里就不再赘述一遍了

我们首先知道:BloomFilter使用长度为m bit的字节数组,使用k个hash函数,增加一个元素: 通过k次hash将元素映射到字节数组中k个位置中,并设置对应位置的字节为1。查询元素是否存在: 将元素k次hash得到k个位置,如果对应k个位置的bit是1则认为存在,反之则认为不存在。

Guava 中已经有具体的实现,而在我们实际生产环境中,本地的存储往往无法满足我们实际的 需求。所以在这时候,就需要我们使用 Redis 了。

Redis 安装 Bloom Filter

git clone https://GitHub.com/RedisLabsModules/redisbloom.gitcd redisbloommake # 编译vi redis.conf## 增加配置loadmodule /usr/local/WEB/redis/RedisBloom-1.1.1/rebloom.so##redis 重启#关闭./redis-cli -h 127.0.0.1 -p 6379 shutdown#启动./redis-server ../redis.conf &

基本指令

#创建布隆过滤器,并设置一个期望的错误率和初始大小bf.reserve userid 0.01 100000#往过滤器中添加元素bf.add userid 'sbc@163.com'#判断指定key的value是否在bloomfilter里存在,存在:返回1,不存在:返回0bf.exists userid 'sbc@163.com'

结合 SpingBoot

搭建一个简单的 springboot 框架

方式一

配置

<?xml version="1.0" encoding="UTF-8"?><project xmlns="Http://Maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.bloom</groupId>    <artifactId>test-bloomfilter</artifactId>    <version>1.0-SNAPSHOT</version>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.8.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-lang3</artifactId>            <version>3.0.1</version>        </dependency>    </dependencies></project>

redis本身对布隆过滤器就有一个很好地实现,在 java 端,我们直接导入 redisson 的 jar包即可

<dependency>  <groupId>org.redisson</groupId>  <artifactId>redisson</artifactId>  <version>3.8.2</version></dependency>

将 Redisson实例 注入 Springioc 容器

@Configurationpublic class RedissonConfig {    @Value("${redisson.redis.address}")    private String address;    @Value("${redisson.redis.passWord}")    private String password;    @Bean    public Config redissionConfig() {        Config config = new Config();        SingleServerConfig singleServerConfig = config.useSingleServer();        singleServerConfig.setAddress(address);        if (StringUtils.isNotEmpty(password)) {            singleServerConfig.setPassword(password);        }        return config;    }    @Bean    public RedissonClient redissonClient() {        return Redisson.create(redissionConfig());    }}

配置文件

redisson.redis.address=redis://127.0.0.1:6379redisson.redis.password=

最后测试我们的布隆过滤器

@SpringBootApplicationpublic class BloomApplication {    public static void main(String[] args) {        ConfigurableApplicationContext context = SpringApplication.run(BloomApplication.class, args);        RedissonClient redisson = context.getBean(RedissonClient.class);        RBloomFilter bf = redisson.getBloomFilter("test-bloom-filter");        bf.tryInit(100000L, 0.03);        Set<String> set = new HashSet<String>(1000);        List<String> list = new ArrayList<String>(1000);      //向布隆过滤器中填充数据,为了测试真实,我们记录了 1000 个 uuid,另外 9000个作为干扰数据        for (int i = 0; i < 10000; i++) {           String uuid = UUID.randomUUID().toString();          if(i<1000){            set.add(uuid);            list.add(uuid);          }                     bf.add(uuid);        }        int wrong = 0; // 布隆过滤器误判的次数        int right = 0;// 布隆过滤器正确次数        for (int i = 0; i < 10000; i++) {            String str = i % 10 == 0 ? list.get(i / 10) : UUID.randomUUID().toString();            if (bf.contains(str)) {                if (set.contains(str)) {                    right++;                } else {                    wrong++;                }            }        }        //right 为1000        System.out.println("right:" + right);        //因为误差率为3%,所以一万条数据wrong的值在30左右        System.out.println("wrong:" + wrong);          //过滤器剩余空间大小        System.out.println(bf.count());    }}

以上使我们使用 redisson 的使用方式,下面介绍一种比较原始的方式,使用lua脚本的方式

方式二

bf_add.lua

local bloomName = KEYS[1]local value = KEYS[2]local result = redis.call('BF.ADD',bloomName,value)return result

bf_exist.lua

local bloomName = KEYS[1]local value = KEYS[2] local result = redis.call('BF.EXISTS',bloomName,value)return result
@Servicepublic class RedisBloomFilterService {    @Autowired    private RedisTemplate redisTemplate;    //我们依旧用刚刚的那个过滤器    public static final String BLOOMFILTER_NAME = "test-bloom-filter";        public Boolean bloomAdd(String str) {        DefaultRedisScript<Boolean> LuaScript = new DefaultRedisScript<Boolean>();        LuaScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("bf_add.lua")));        LuaScript.setResultType(Boolean.class);        //封装传递脚本参数        List<String> params = new ArrayList<String>();        params.add(BLOOMFILTER_NAME);        params.add(str);        return (Boolean) redisTemplate.execute(LuaScript, params);    }        public Boolean bloomExist(String str) {        DefaultRedisScript<Boolean> LuaScript = new DefaultRedisScript<Boolean>();        LuaScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("bf_exist.lua")));        LuaScript.setResultType(Boolean.class);        //封装传递脚本参数        ArrayList<String> params = new ArrayList<String>();        params.add(BLOOMFILTER_NAME);        params.add(String.valueOf(str));        return (Boolean) redisTemplate.execute(LuaScript, params);    }}

最后我们还是用上面的启动器执行测试代码

@SpringBootApplicationpublic class BloomApplication {    public static void main(String[] args) {        ConfigurableApplicationContext context = SpringApplication.run(BloomApplication.class, args);        RedisBloomFilterService filterService = context.getBean(RedisBloomFilterService.class);        Set<String> set = new HashSet<String>(1000);        List<String> list = new ArrayList<String>(1000);        //向布隆过滤器中填充数据,为了测试真实,我们记录了 1000 个 uuid,另外 9000个作为干扰数据        for (int i = 0; i < 10000; i++) {            String uuid = UUID.randomUUID().toString();            if (i < 1000) {                set.add(uuid);                list.add(uuid);            }            filterService.bloomAdd(uuid);        }        int wrong = 0; // 布隆过滤器误判的次数        int right = 0;// 布隆过滤器正确次数        for (int i = 0; i < 10000; i++) {            String str = i % 10 == 0 ? list.get(i / 10) : UUID.randomUUID().toString();            if (filterService.bloomExist(str)) {                if (set.contains(str)) {                    right++;                } else {                    wrong++;                }            }        }        //right 为1000        System.out.println("right:" + right);        //因为误差率为3%,所以一万条数据wrong的值在30左右        System.out.println("wrong:" + wrong);    }}

相比而言,个人比较推荐第一种,实现的原理都是差不多,redis 官方已经为我封装好了执行脚本,和相关 api,用官方的会更好一点

看完了这篇文章,相信你对“SpringBoot+Redis如何实现布隆过滤器”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网精选频道,感谢各位的阅读!

--结束END--

本文标题: SpringBoot+Redis如何实现布隆过滤器

本文链接: https://www.lsjlt.com/news/324974.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot+Redis如何实现布隆过滤器
    小编给大家分享一下SpringBoot+Redis如何实现布隆过滤器,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!简述关于布隆过滤器的详细介绍,我在这里就不再赘述一遍了我们首先知道:BloomFilter使用长度为m bi...
    99+
    2023-06-29
  • Redis如何实现布隆过滤器
    小编给大家分享一下Redis如何实现布隆过滤器,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!布隆过滤器(Bloom Filter...
    99+
    2024-04-02
  • Redis 中布隆过滤器的实现
    Redis 中布隆过滤器的实现?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。什么是『布隆过滤器』布隆过滤器是一个神奇的数据结构,可以用来判断一...
    99+
    2024-04-02
  • SpringBoot+Redis实现布隆过滤器的示例代码
    目录简述Redis 安装 Bloom Filter基本指令结合 SpingBoot方式一方式二简述 关于布隆过滤器的详细介绍,我在这里就不再赘述一遍了 我们首先知道:BloomFil...
    99+
    2024-04-02
  • redis如何使用布隆过滤器
    布隆过滤器2个基本指令是bf.add和bf.exists,如果想要一次添加多个,就需要用到bf.madd 指令,同样如果需要一次查询多个元素是否存在,就需要用到bf.mexists 指令,基本使用如下:127.0.0.1:6379>&...
    99+
    2024-04-02
  • Redis中的布隆过滤器怎么实现
    这篇文章主要介绍“Redis中的布隆过滤器怎么实现”,在日常操作中,相信很多人在Redis中的布隆过滤器怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Redis中的布...
    99+
    2024-04-02
  • Redis BloomFilter布隆过滤器原理与实现
    目录Bloom Filter 概念Bloom Filter 原理缓存穿透Bloom Filter的缺点常见问题go语言实现Bloom Filter 概念 布隆过滤器(英语:Bloom...
    99+
    2024-04-02
  • Java的布隆过滤器如何实现
    今天小编给大家分享一下Java的布隆过滤器如何实现的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。BitMap现代计算机用二进...
    99+
    2023-06-29
  • Python实现布隆过滤器
    转载自:http://blog.csdn.net/demon24/article/details/8537665 http://blog.csdn.net/u013402746/article/details/28414901      ...
    99+
    2023-01-31
    过滤器 Python
  • 详解SpringBoot中如何使用布隆过滤器
    目录前言一、Guava 实现布隆过滤器二、Hutool 布隆过滤器三、Redission 布隆过滤器四、小结五、Guava 布隆过滤器结合 Redis 使用昨天写了一篇Redis布隆...
    99+
    2024-04-02
  • Redis怎么安装布隆过滤器
    Redis安装布隆过滤器的方法:打开终端命令行,依次输入以下命令进行安装。wget https://github.com/RedisLabsModules/rebloom/archive/v1.1.1.tar.gz #下载安装包tar zx...
    99+
    2024-04-02
  • Java怎么实现布隆过滤器
    这篇“Java怎么实现布隆过滤器”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Java怎么实现布隆过滤器”文章吧。什么是布隆...
    99+
    2023-07-05
  • Redis中布隆过滤器如何安装和配置
    这篇文章给大家分享的是有关Redis中布隆过滤器如何安装和配置的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、版本要求推荐版本6.x,最低4.x版本,可以通过如下命令查看版本:...
    99+
    2024-04-02
  • Redis中Bloomfilter布隆过滤器的学习
    目录1.概念2.guava实现2.1.依赖2.2.初始化布隆过滤器2.3.布隆过滤器2.4.添加元素或者判断是否存在3.Redisson实现3.1.依赖3.2.注入或测试1.概念 ​...
    99+
    2022-12-14
    Redis Bloom filter Redis布隆过滤器
  • Redis中Redisson布隆过滤器的学习
    目录简介使用Demo依赖测试代码简析初始化添加元素检索元素简介 本文基于Spring Boot 2.6.6、redisson 3.16.0简单分析Redisson布隆过滤器的使用。 ...
    99+
    2024-04-02
  • SpringBoot+Redis布隆过滤器防恶意流量击穿缓存
    目录什么是恶意流量穿透怎么防布隆过滤器的另一个用武场景给Redis安装Bloom Filter在Redis里使用Bloom Filter结合SpringBoot使用搭建spring ...
    99+
    2024-04-02
  • Redis中Bloom filter布隆过滤器的学习
    目录1.概念2.guava实现2.1.依赖2.2.初始化布隆过滤器2.3.布隆过滤器2.4.添加元素或者判断是否存在3.Redisson实现3.1.依赖3.2.注入或测试1.概念 ​ 布隆过滤器是一个高空间利用率的概率性...
    99+
    2022-12-14
    RedisBloomfilter Redis布隆过滤器
  • redis布隆过滤器的作用是什么
    Redis布隆过滤器是一种数据结构,用于快速判断一个元素是否存在于一个集合中。它可以高效地判断一个元素是否可能在集合中,但无法确保元...
    99+
    2024-04-09
    redis
  • 什么是布隆过滤器,它在Redis中如何使用
    本篇内容介绍了“什么是布隆过滤器,它在Redis中如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! ...
    99+
    2024-04-02
  • 布隆过滤器的Python实现(标准、计
    github:bloompy 布隆过滤器的Python3实现,包括标准、计数、标准扩容、计数扩容。更新自pybloom。 安装 pip install bloompy 使用 通过bloompy你可以使用四种布隆过滤器 标准布隆过滤器 ...
    99+
    2023-01-31
    过滤器 标准 Python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作