遍历 redis 数据库

在redis数据库里存储了一些商品分类和对应商品个数,将其取出存进文件里。

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import redis.clients.jedis.Jedis;

public class readRedis {
    public static void main(String args[]) throws IOException {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        readallRediskeys(jedis);
    }
    
    public static void readallRediskeys(Jedis jedis) throws IOException {
        Set s = jedis.keys("*");
        Iterator it = s.iterator();
        String FILEPATH = "/home/tqhy/yl/";
        FileWriter writer = new FileWriter(FILEPATH + "category3.txt", true);        
        while(it.hasNext()) {
            String key = (String) it.next();
            String value = jedis.get(key);
            System.out.println(key + ": " + value);
            writer.write(key + ": " + value);
            writer.write(System.getProperty("line.separator"));
        }
        writer.close();
    }
}

参考链接:

 https://java-er.com/blog/redis-jedis-java/

转载于:https://www.cnblogs.com/lely/p/10315536.html

你可能感兴趣的:(遍历 redis 数据库)