{{announcement.body}}
{{announcement.title}}

Redis-Based Bloom Filter for Java

DZone 's Guide to

Redis-Based Bloom Filter for Java

Learn how to use Bloom filters in Java and Redis with the Redis Java client Redisson.

· Java Zone ·
Free Resource

Learn how to use Bloom filters in Java and Redis with the Redis Java client Redisson.

A Bloom filter is a probabilistic data structure that is used to efficiently test whether an element is present in a set. The use of Bloom filters can help reduce the number of expensive disk lookups for key-value pairs.

In the Java programming language, developers can use a variety of pre-built Bloom filter data structures, including the BloomFilter class in Google’s Guava core Java libraries.

You may also like: Redis for Java Developers: Tutorial and Code Examples

Redis is an open-source in-memory data structure store that can be used to implement a NoSQL database. However, Java is not compatible with Redis out of the box.

Java developers will have to use a Redis Java client in order to access features such as Bloom filters. In this article, we’ll discuss how to use Bloom filters in Java and Redis with the Redis Java client Redisson.

Bloom Filters in Redis and Java with Redisson

Redisson is an ultra-fast, lightweight Java client for Redis that provides many common Java objects and functionality, including Bloom filters.

The following example code demonstrates how to use Bloom filters in Redisson with the RBloomFilter interface:

Java




x
11


 
1
RBloomFilter<SomeObject> bloomFilter = redisson.getBloomFilter("sample");
2
// initialize Bloom filter with 
3
// expectedInsertions = 55000000
4
// falseProbability = 0.03
5
bloomFilter.tryInit(55000000L, 0.03);
6
 
          
7
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
8
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
9
 
          
10
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));
11
bloomFilter.count();



Bloom filters are a probabilistic data structure: they can definitively state that an element is not present in the set, but can only say that an element may be present in the set. The falseProbability parameter controls the probability of having a false positive with the given RBloomFilter.

The expectedInsertions parameter defines the expected number of insertions per element. An  RBloomFilter object may contain up to 2^32 bits.

Redisson also supports distributed Bloom filters in Redis via the RClusteredBloomFilter interface. The RClusteredBloomFilter is more memory-efficient, shrinking the memory used across all Redis nodes. An RClusteredBloomFilter object may contain up to 2^64 bits. Note that the  RClusteredBloomFilter is only available in Redisson's cluster mode.

The following example code demonstrates how to use the  RClusteredBloomFilter interface:

Java
xxxxxxxxxx
1
11
 
1
RClusteredBloomFilter<SomeObject> bloomFilter = redisson.getClusteredBloomFilter("sample");
2
// initialize Bloom filter with 
3
// expectedInsertions = 255000000
4
// falseProbability = 0.03
5
bloomFilter.tryInit(255000000L, 0.03);
6
 
          
7
bloomFilter.add(new SomeObject("field1Value", "field2Value"));
8
bloomFilter.add(new SomeObject("field5Value", "field8Value"));
9
 
          
10
bloomFilter.contains(new SomeObject("field1Value", "field8Value"));
11
 
          


And that's it!

Further Reading

Redis for Java Developers: Tutorial and Code Examples

Java Distributed Caching in Redis

Quickstart: How to Use Redis on Java

Topics:
redis ,java ,bloom filter

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}