Redis Java
Redis Java
Installation
Before we start using Redis in our Java programs, we need to make sure that we have Redis java
Driver and Java set up on the machine. You can check Java tutorial for Java installation on your
machine. Now, let us check how to set up Redis java driver.
You need to download the jar from the path Download jedis.jar. Make sure to download
latest release of it.
You need to include the jedis.jar into your classpath.
import redis.clients.jedis.Jedis;
public class RedisJava {
public static void main(String[] args) {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//check whether server is running or not
System.out.println("Server is running: "+jedis.ping());
}
}
Now, let's compile and run above program to test the connection to redis server. You can change
your path as per your requirement. We are assuming current version of jedis.jar is available in the
current path
$javac RedisJava.java
$java RedisJava
Connection to server sucessfully
Server is running: PONG
import redis.clients.jedis.Jedis;
public class RedisStringJava {
public static void main(String[] args) {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//set the data in redis string
jedis.set("tutorial-name", "Redis tutorial");
// Get the stored data and print it
System.out.println("Stored string in redis:: "+ jedis.get("tutorial-name"));
}
}
$javac RedisStringJava.java
$java RedisStringJava
Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis Java List Example
import redis.clients.jedis.Jedis;
public class RedisListJava {
public static void main(String[] args) {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//store data in redis list
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
// Get the stored data and print it
List<String> list = jedis.lrange("tutorial-list", 0 ,5);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
}
}
$javac RedisListJava.java
$java RedisListJava
Connection to server sucessfully
Stored string in redis:: Redis
Stored string in redis:: Mongodb
Stored string in redis:: Mysql
import redis.clients.jedis.Jedis;
public class RedisKeyJava {
public static void main(String[] args) {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//store data in redis list
// Get the stored data and print it
List<String> list = jedis.keys("*");
for(int i=0; i<list.size(); i++) {
System.out.println("List of stored keys:: "+list.get(i));
}
}
}
$javac RedisKeyJava.java
$java RedisKeyJava
Connection to server sucessfully
List of stored keys:: tutorial-name
List of stored keys:: tutorial-list