[Redis] Jedis를 활용한 Redis Dummy Data 넣기
이글은 Java(Spring) 개발자에게만 유용할 수 있습니다.
Redis에서 나같이 더미데이터를 넣어서 성능테스트를 해볼사람이 있을 수 있다..
Redis는 mass insert라는 api를 제공해서 데이터를 빠르게 넣을 수 있다
근데이게 자기네는 쉬운 api라고 소개하는데 대충 모양이
"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n"
이런모양이다.
실제로도 그렇게 어려워보이진 않았지만 굳이 저걸 쓰고싶진 않았다..
자세한 정보는 아래에서 확인할 수 있다.
https://redis.io/topics/mass-insert
Redis Mass Insertion – Redis
*Redis Mass Insertion Sometimes Redis instances need to be loaded with a big amount of preexisting or user generated data in a short amount of time, so that millions of keys will be created as fast as possible. This is called a mass insertion, and the goal
redis.io
그래서 찾아낸 방법은 Jedis Client를 사용하는 방법이다.
Jedis Client는 pipeline api를 제공해주는데 사용방법은 아래와 같다
try (Jedis jedis = new Jedis(redisHost, redisPort)) {
Pipeline pipeline = jedis.pipelined();
for (int i = 0; i < 7000000; i++) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name", "p" + i);
hashMap.put("score", String.valueOf(i * (int)(Math.random() * 100) + 1));
pipeline.zadd(KEY, Double.parseDouble(hashMap.get("score")), hashMap.get("name"));
pipeline.hset(KEY + ":" + hashMap.get("name"), hashMap);
}
pipeline.sync();
}
이방법을 사용하면 약 700만개의 데이터가 1~2분 내외로 들어간다.
진짜 너무좋다 ..
메모리 생각 안하고 데이터를 꾸겨넣다보면 메모리가 금방 터지니 주의해야한다.
https://stackoverflow.com/questions/30728409/how-to-do-mass-insertion-in-redis-using-java
How to do Mass insertion in Redis using JAVA?
Hi I need to do multiple insertions of the form SADD key value I have the key value pair and needed to know how to perform mass insertions using JAVA . I have written a file in the Redis Protoco...
stackoverflow.com
ps
lettuce도 비슷한 api가 있다!
https://github.com/lettuce-io/lettuce-core/wiki/Pipelining-and-command-flushing
lettuce-io/lettuce-core
Advanced Java Redis client for thread-safe sync, async, and reactive usage. Supports Cluster, Sentinel, Pipelining, and codecs. - lettuce-io/lettuce-core
github.com
lettuce는 비동기라그런지 뭔가 복잡하다
단발로 끝낼꺼면 jedis를 쓰자