English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java with Redis

Redis Advanced Tutorial

Installation Before starting to use Redis in Java, We need to make sure that the redis service and Java redis driver have been installed, and that Java can be used normally on your machine. Java installation and configuration can be referred to in our Java Development Environment Configuration

Include the driver package in your classpath. 2jedis9This site provides.0 jar version download:-2jedis9.

.0.jar

import redis.clients.jedis.Jedis;
 
Connect to the redis service
    public static void main(String[] args) {
        //Connect to the local Redis server
        Jedis jedis = new Jedis("localhost");
        // public class RedisJava {
        // If the Redis service is set with a password, the following line is required, otherwise it is not needed123456 
        System.out.println("Connection successful");
        //jedis.auth("
        Check if the service is running+System.out.println("Service is running: ")
    }
}

jedis.ping());

Connection successful
Compile the above Java program and make sure the path to the driver package is correct.

Service is running: PONG

import redis.clients.jedis.Jedis;
 
Redis Java String (String) Example
    public static void main(String[] args) {
        //Connect to the local Redis server
        Jedis jedis = new Jedis("localhost");
        System.out.println("Connection successful");
        //public class RedisStringJava {
        Set redis string data3jedis.set("oldtoolbag.com");
        // Get stored data and output
        System.out.println("The string stored in redis is: ")+ jedis.get("w3codeboxkey");
    }
}

Compile the above program.

Connection successful
The string stored in redis is: www.oldtoolbag.com

Redis Java List (List) Example

import java.util.List;
import redis.clients.jedis.Jedis;
 
public class RedisListJava {
    public static void main(String[] args) {
        //Connect to the local Redis server
        Jedis jedis = new Jedis("localhost");
        System.out.println("Connection successful");
        //Store data into the list
        jedis.lpush("site-list, "w3codebox");
        jedis.lpush("site-list, "Google");
        jedis.lpush("site-list, "Taobao");
        // Get stored data and output
        List<String> list = jedis.lrange("site-list, 0 ,2)
        for (int i = 0; i < list.size(); i++) {}}
            System.out.println("List item is: "+list.get(i));
        }
    }
}

Compile the above program.

Connection successful
List item is: Taobao
List item is: Google
List item is: w3codebox

Redis Java Keys Example

import java.util.Iterator;
import java.util.Set;
import redis.clients.jedis.Jedis;
 
public class RedisKeyJava {
    public static void main(String[] args) {
        //Connect to the local Redis server
        Jedis jedis = new Jedis("localhost");
        System.out.println("Connection successful");
 
        // Get data and output
        Set<String> keys = jedis.keys("* 
        Iterator<String> it = keys.iterator() ;   
        while(it.hasNext()){   
            String key = it.next();   
            System.out.println(key);   
        }
    }
}

Compile the above program.

Connection successful
w3codeboxkey
site-list