* Initialize multiple contains hash The array of classes in each class hash Functions are different */ public MyBloomFilter() { // Initialize multiple different Hash functions for (int i = 0; i < SEEDS.length; i++) { func[i] = new SimpleHash(DEFAULT_SIZE, SEEDS[i]); } } /** * Add element to digit group */ public void add(Object value) { for (SimpleHash f : func) { bits.set(f.hash(value), true); } } /** * Determines whether the specified element exists in the bit array */ public Boolean contains(Object value) { Boolean ret = true; for (SimpleHash f : func) { ret = ret && bits.get(f.hash(value)); } return ret; } /** * Static inner class. For hash operation! */ public static class SimpleHash { private int cap; private int seed; public SimpleHash(int cap, int seed) { this.cap = cap; this.seed = seed; } /** * Calculate hash value */ public int hash(Object value) { int h; return (value == null) ? 0 : Math.abs(seed * (cap - 1) & ((h = value.hashCode()) ^ (h >>> 16))); } }
}
Test: ```java String value1 = "https://javaguide.cn/"; String value2 = "https://github.com/Snailclimb"; MyBloomFilter filter = new MyBloomFilter(); System.out.println(filter.contains(value1)); System.out.println(filter.contains(value2)); filter.add(value1); filter.add(value2); System.out.println(filter.contains(value1)); System.out.println(filter.contains(value2));
Output:
false false true true
Test:
Integer value1 = 13423; Integer value2 = 22131; MyBloomFilter filter = new MyBloomFilter(); System.out.println(filter.contains(value1)); System.out.println(filter.contains(value2)); filter.add(value1); filter.add(value2); System.out.println(filter.contains(value1)); System.out.println(filter.contains(value2));
Output:
false false true true
5, Use the bloom filter in Google's open source Guava
The purpose of our own implementation is to make ourselves understand the principle of Bloom filter. The implementation of Bloom filter in Guava is quite authoritative, so we don't need to manually implement a bloom filter in the actual project.
First, we need to introduce Guava's dependency into the project:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency>
The actual use is as follows:
We have created a bloom filter that can store up to 1500 integers, and we can tolerate a false positive probability of 0.01%
// Create a bloom filter object BloomFilter<Integer> filter = BloomFilter.create( Funnels.integerFunnel(), 1500, 0.01); // Determine whether the specified element exists System.out.println(filter.mightContain(1)); System.out.println(filter.mightContain(2)); // Add element to bloom filter filter.put(1); filter.put(2); System.out.println(filter.mightContain(1)); System.out.println(filter.mightContain(2));
In our example, when the mightcontainer () method returns true, we can 99% determine that the element is in the filter. When the filter returns false, we can 100% determine that the element does not exist in the filter.
The implementation of the bloom filter provided by Guava is still very good (you can see its source implementation for details), but it has a major defect that it can only be used on a single machine (in addition, capacity expansion is not easy). Now the Internet is generally distributed. In order to solve this problem, we need to use the bloom filter in Redis.
6, Bloom filter in Redis
1. Introduction
Redis v4. After 0, there is the Module function. Redis Modules allows redis to use external modules to expand its functions. Bloom filter is one of the modules.
In addition, the official website recommends a redisbloom as the Module of redisbloom filter. The address is: https://github.com/RedisBloom/RedisBloom . Others include:
- Redis Lua scaling bloom filter (implemented by Lua script): https://github.com/erikdubbelboer/redis-lua-scaling-bloom-filter
- Pyrebloom (quick Redis bloom filter in Python): https://github.com/seomoz/pyreBloom
- ...
RedisBloom provides client support in multiple languages, including Python, Java, JavaScript and PHP.
2. Install using Docker
The specific operations are as follows:
➜ ~ docker run -p 6379:6379 --name redis-redisbloom redislabs/rebloom:latest ➜ ~ docker exec -it redis-redisbloom bash root@21396d02c252:/data# redis-cli 127.0.0.1:6379>
3. List of common commands
Note: key: the name of the bloom filter, item: the added element.
- BF.ADD: adds an element to the bloom filter, which is created if it does not already exist. Format: BF ADD {key} {item} .
- BF.MADD: add one or more elements to the bloom filter and create a filter that does not yet exist. The operation mode of this command is BF Add is the same, except that it allows multiple inputs and returns multiple values. Format: BF MADD {key} {item} [item ...] .
- BF.EXISTS: determines whether the element exists in the bloom filter. Format: BF EXISTS {key} {item}.
- BF.MEXISTS: determines whether one or more elements exist in the bloom filter. Format: BF MEXISTS {key} {item} [item ...].
In addition, BF The reserve command needs to be introduced separately:
The format of this command is as follows:
BF.RESERVE {key} {error_rate} {capacity} [EXPANSION expansion] .
last
Share some systematic interview questions. You can brush them to prepare for the interview and raise your salary.
After you like it, Poke here and get it for free!
Technical points corresponding to these interview questions:
- JVM
- MySQL
- Mybatis
- MongoDB
- Redis
- Spring
- Spring boot
- Spring cloud
- Kafka
- RabbitMQ
- Nginx
- ...
The main categories are:
- Java Foundation
- Data structure and algorithm
- Concurrent programming
- database
- Design pattern
- Microservices
- Message Oriented Middleware
mg-WwHY9iL1-1628570168764)]
[external chain picture transferring... (img-hIi1iXME-1628570168765)]
[external chain picture transferring... (img-XB3qLBCf-1628570168766)]
[external chain picture transferring... (img-nWzxMbWp-1628570168767)]
[external chain picture transferring... (img-5SWs2mrm-1628570168768)]