Open In App

Bloom Filters in Java Using Redis

Bloom filters are the in-memory data structures used in applications to test whether a key belongs to a particular set or not, it’s like a Redis cache but where the Redis cache stores the entire data as it is rather than any optimization, but in Bloom filters the bitset or bitmap implementation where it provides a good memory optimization and find operation is very fast here each bit represents the presence of a particular element where each bit is hashed by different hash functions.

Bloom filters are much faster and more efficient for some use cases like membership testing i.e. particular elements belong to a set or not in the memory, compared to normal Redis caching.



Steps for Starting the Redis Stack to Start the Redis Bloom Server Using Docker

Step 1: Adding the dependency on pom.xml after starting the Redis bloom server:

<dependency>
<groupId>com.redislabs</groupId>
<artifactId>jrebloom</artifactId>
<version>2.1.0</version>
</dependency>

Step 2: Initializing Bloom Filter Client in Java



Client clientVariable = new Client(hostAddress, port, timeOut , poolSize );

The parameters that can be given while initializing:

Basic operations performed on Redis Bloom

Operations

Syntax

Parameters

Creating a Redis Bloom filter

clientVariable.createFilter( name, initCapacity, errorRate)

  • name: The name of the Bloom Filter we want to Keep
  • initCapacity: The size of the Bloom Filter that we want to keep
  • errorRate : It is the expected rate or probability at which it gives incorrect result for the existence of the element in the set

Checking the info the Bloom filter

clientVariable.info(filterName)

  • filterName: The name of the Bloom Filter

Adding the keys into the Filter

clientVarible.add(filterName, key)

  • filterName: The name of the Bloom Filter
  • key : The element that we want to add into that particular filter

Checking if it exists in the filter

clientVarible.exists(filterName, key)

  • filterName: The name of the Bloom Filter
  • key : The element that we want to check if exists in that particular filter

Deleting the bloomFilter

clientVariable.delete(filterName)

  • filterName: The name of the Bloom Filter

Program of Bloom Filters in Java using Redis

Below is the implementation for Creating and Checking info Redis Bloom filter:

Example 1:




package org.geeksforgeeks;
  
import io.rebloom.client.Client;
import java.util.Map;
  
public class Main {
    public static void main(String args[]) 
    {
  
        Client bloomFilerClient = new Client("localhost", 6379,2000,20);
        bloomFilerClient.createFilter("premium_users",200000000,0.01);
        bloomFilerClient.createFilter("non_premium_users",500000000,0.01);
        Map<String, Object> mp_premium = bloomFilerClient.info("premium_users");
        Map<String, Object> mp_non_premium = bloomFilerClient.info("non_premium_users");
        System.out.println("Premium Users Filter Info : " + mp_premium.toString());
        System.out.println("Non Premium Users Filter Info : " + mp_non_premium.toString());
  
    }
}

Output:

Below in the output we can see the Redis Bloom Filter info has created.

Example 2:

Below is the implementation for Adding and Checking keys into the Filter:




package org.geeksforgeeks;
  
import io.rebloom.client.Client;
  
public class Main {
    
    public static void main(String[] args) {
  
        Client bloomFilerClient = new Client("localhost", 6379,2000,20);
        bloomFilerClient.add("premium_users","gfg1@gmail.com");
        bloomFilerClient.add("non_premium_users","gfg2@gmail.com");
        
        if(bloomFilerClient.exists("premium_users","gfg1@gmail.com")) {
            System.out.println("gfg1@gmail.com is a premium user");
        }
        
        if(bloomFilerClient.exists("non_premium_users","gfg2@gmail.com")) {
            System.out.println("gfg2@gmail.com is not a premium user");
        }
  
    }
}

Output :

Below in the output we can see the keys added into the filter.

Example 3:

Below is the implementation of Deleting the bloomFilter:




package org.geeksforgeeks;
  
import io.rebloom.client.Client;
  
public class Main {
    public static void main(String[] args) {
  
        Client bloomFilerClient = new Client("localhost", 6379,2000,20);
        bloomFilerClient.delete("premium_users");
        if(!bloomFilerClient.exists("premium_users","gfg1@gmail.com")) {
            System.out.println("gfg1@gmail.com is no longer a premium user");
        }
    }
}

Output :

Below in the output we can see the bloom filter has deleted.

Limitations of Bloom Filter:


Article Tags :