How to connect to Redis in Java?
How to connect to Redis in Java?
Connecting to Redis from a Java application is relatively straightforward and can be achieved using the Jedis library. Here's a step-by-step guide on how to do it:
Step 1: Add the Jedis Library
To use Jedis, you need to add the library as a dependency in your project. You can do this by adding the following line to your pom.xml file (if you're using Maven) or your build.gradle file (if you're using Gradle):
redis
jedis
2.9.1
dependencies {
implementation 'redis: jedis: 2.9.1'
}
Step 2: Create a Jedis Connection
Once you've added the library, you can create a Jedis connection to your Redis instance using the following code:
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisConnection {
public static void main(String[] args) {
// Set up the connection settings
String host = "localhost";
int port = 6379;
String password = null; // If your Redis instance requires a password, set it here
// Create the Jedis pool configuration
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(100); // Set the maximum number of connections to your Redis instance
config.setMaxIdle(10); // Set the maximum idle time for connections
// Create the Jedis pool
JedisPool pool = new JedisPool(config, host, port, password);
// Get a connection from the pool
Jedis jedis = null;
try {
jedis = pool.getResource();
} catch (Exception e) {
System.out.println("Failed to get a connection to Redis: " + e.getMessage());
return;
}
// Use the connection to interact with your Redis instance
// Close the connection when you're done
if (jedis != null) {
jedis.close();
}
}
}
Step 3: Use the Jedis Connection
Once you have a connection to your Redis instance, you can use it to perform various operations, such as setting and getting values from keys. Here's an example of how to do that:
public class RedisExample {
public static void main(String[] args) {
// Set up the Jedis connection (same as above)
// Set a value for a key
jedis.set("mykey", "Hello, Redis!");
// Get the value for the same key
String value = jedis.get("mykey");
System.out.println("Value: " + value);
// Close the connection when you're done
}
}
That's it! With these steps, you should be able to connect to your Redis instance from a Java application using Jedis.
Java redis client example
Here is a basic Java Redis client example:
First, you need to add the following dependencies to your pom.xml
file (if you're using Maven) or your build.gradle
file (if you're using Gradle):
Maven:
org.redisson
redission-client
3.15.0
redis.clients
jedis
2.9.1
Gradle:
dependencies {
implementation 'org.redisson:redission-client:3.15.0'
implementation 'redis.clients:jedis:2.9.1'
}
Now, let's create a simple Java class that connects to Redis and performs some basic operations:
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisClientExample {
public static void main(String[] args) {
// Create a Jedis pool with 100 connections to Redis
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(100);
config.setMaxWaitMillis(5000);
JedisPool pool = new JedisPool(config, "localhost", 6379, true);
try {
// Get a connection from the pool
Jedis jedis = pool.getResource();
// Set a key-value pair in Redis
jedis.set("name", "John");
// Get the value of the key we just set
String name = jedis.get("name");
System.out.println("Name: " + name);
// Remove the key from Redis
jedis.del("name");
// Close the connection
pool.returnResource(jedis);
} finally {
if (pool != null) {
pool.close();
}
}
}
}
In this example, we're using the Jedis
class to connect to Redis and perform some basic operations like setting a key-value pair, getting the value of that key, and deleting the key. The JedisPool
is used to manage connections to Redis.
This code will establish a connection to Redis, set a key-value pair with the key "name" and the value "John", get the value of that key, print it out, then delete the key, close the connection, and finally return the resource back to the pool.
Remember that you can customize this example by modifying the JedisPoolConfig
object to suit your needs. For example, you might want to set a different maximum number of active connections or a longer timeout for getting a connection from the pool.
Please note that the above code is an example and should be used in production environment after proper testing.