实例详解Redis实现数据的交集、并集和补集

 3326

本篇文章给大家带来了关于Redis的相关知识,其中主要介绍了关于实现数据的交集并集补集的相关问题,如果全部在JVM内存中进行计算的话,很容易出现内存空间不足导致的OOM异常,下面一起来看一下,希望对大家有帮助。


实例详解Redis实现数据的交集、并集和补集

场景说明

今天我们来模拟一个这样的场景,我们在本地有多个文本文件,每个文件里面存了很多的32位的字符串作为用户的唯一标识,每个用户存做一行,假如我们每天都有非常大规模的用户,这样我们可能在工作中就存在需要对这些用户进行交集、并集或补集等处理,最简单的方式是通过Java中的集合来进行运算即可,比如通过HashSet来进行相应的一些运算,但是这样的运算存在一个局限性,那就是我们一般在JVM运行过程中初始的内存是有限的,这样如果全部在JVM内存中进行计算的话,很容易出现内存空间不足导致的OOM异常,那么我们今天来介绍一种拓展性更强的方式来进行这样的一些交并补的运算:通过Redis来实现数据的交集、并集、补集

环境说明

Redis版本: Redis 6.0.6

Jedis版本: 4.2.2

工具类hutool版本: 5.8.0.M3

pom文件:

  1. <dependencies>
  2.     <dependency>
  3.         <groupId>redis.clients</groupId>
  4.         <artifactId>jedis</artifactId>
  5.         <version>4.2.2</version>
  6.     </dependency>
  7.     <dependency>
  8.         <groupId>cn.hutool</groupId>
  9.         <artifactId>hutool-all</artifactId>
  10.         <version>5.8.0.M3</version>
  11.     </dependency>
  12. </dependencies>


交并补计算

初始化常量

  1. public class RedisCalculateUtils {
  2.     static String oneFileString = "/Users/tmp/test-1.txt";
  3.     static String twoFileString = "/Users/tmp/test-2.txt";
  4.  
  5.     static String diffFileString = "/Users/tmp/diff-test.txt";
  6.  
  7.     static String interFileString = "/Users/tmp/inter-test.txt";
  8.  
  9.     static String unionFileString = "/Users/tmp/union-test.txt";
  10.  
  11.     static String oneFileCacheKey = "oneFile";
  12.  
  13.     static String twoFileCacheKey = "twoFile";
  14.  
  15.     static String diffFileCacheKey = "diffFile";
  16.  
  17.     static String interFileCacheKey = "interFile";
  18.  
  19.     static String unionFileCacheKey = "unionFile";
  20. }

初始化数据到指定文件

  1. /**
  2. * 初始化数据并写入文件中
  3. */
  4. public static void writeFile() {
  5.     File oneFile = new File(oneFileString);
  6.     List<String> fs = new ArrayList<>(10000);
  7.     for (int i = 10000; i < 15000; i++) {
  8.         String s = SecureUtil.md5(String.valueOf(i));
  9.         fs.add(s);
  10.     }
  11.     FileUtil.writeUtf8Lines(fs, oneFile);
  12.     File twoFile = new File(twoFileString);
  13.     fs.clear();
  14.     for (int i = 12000; i < 20000; i++) {
  15.         String s = SecureUtil.md5(String.valueOf(i));
  16.         fs.add(s);
  17.     }
  18.     FileUtil.writeUtf8Lines(fs, twoFile);
  19. }

指定文件写入Redis

  1. /**
  2. * 读取文件数据并写入Redis
  3. */
  4. public static void writeCache() {
  5.     try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  6.         Pipeline p = jedis.pipelined();
  7.         List<String> oneFileStringList = FileUtil.readLines(oneFileString, "UTF-8");
  8.  
  9.         for (String s : oneFileStringList) {
  10.             p.sadd(oneFileCacheKey, s);
  11.         }
  12.         p.sync();
  13.  
  14.         List<String> twoFileStringList = FileUtil.readLines(twoFileString, "UTF-8");
  15.  
  16.         for (String s : twoFileStringList) {
  17.             p.sadd(twoFileCacheKey, s);
  18.         }
  19.         p.sync();
  20.  
  21.     } catch (Exception e) {
  22.         throw new RuntimeException(e);
  23.     }
  24. }


差集的计算

  1. /**
  2.  * oneKey对应的Set 与 twoKey对应的Set 的差集 并写入 threeKey
  3.  * @param oneKey 差集前面的集合Key
  4.  * @param twoKey 差集后面的集合Key
  5.  * @param threeKey 差集结果的集合Key
  6.  */
  7. public static void diff(String oneKey, String twoKey, String threeKey) {
  8.     try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  9.         long result = jedis.sdiffstore(threeKey, oneKey, twoKey);
  10.         System.out.println("oneKey 与 twoKey 的差集的个数:" + result);
  11.     } catch (Exception e) {
  12.         throw new RuntimeException(e);
  13.     }
  14. }

差集计算结果写入到指定文件

  1. /**
  2.  * 将计算的差集数据写入到指定文件
  3.  */
  4. public static void writeDiffToFile() {
  5.     File diffFile = new File(diffFileString);
  6.     try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  7.         Set<String> result = jedis.smembers(diffFileCacheKey);
  8.         FileUtil.writeUtf8Lines(result, diffFile);
  9.     } catch (Exception e) {
  10.         throw new RuntimeException(e);
  11.     }
  12. }


交集的计算

  1. /**
  2.  *
  3.  * @param cacheKeyArray 交集集合Key
  4.  * @param destinationKey 交集集合结果Key
  5.  */
  6. public static void inter(String[] cacheKeyArray, String destinationKey) {
  7.     try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  8.         long result = jedis.sinterstore(destinationKey, cacheKeyArray);
  9.         System.out.println("cacheKeyArray 的交集的个数:" + result);
  10.     } catch (Exception e) {
  11.         throw new RuntimeException(e);
  12.     }
  13. }

交集计算结果写入指定文件

  1. /**
  2.  * 将计算的交集数据写入到指定文件
  3.  */
  4. public static void writeInterToFile() {
  5.     File interFile = new File(interFileString);
  6.     try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  7.         Set<String> result = jedis.smembers(interFileCacheKey);
  8.         FileUtil.writeUtf8Lines(result, interFile);
  9.     } catch (Exception e) {
  10.         throw new RuntimeException(e);
  11.     }
  12. }


并集的计算

  1. /**
  2.  * 计算多个Key的并集并写入到新的Key
  3.  * @param cacheKeyArray 求并集的Key
  4.  * @param destinationKey 并集结果写入的KEY
  5.  */
  6.  public static void union(String[] cacheKeyArray, String destinationKey) {
  7.      try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  8.          long result = jedis.sunionstore(destinationKey, cacheKeyArray);
  9.  
  10.          System.out.println("cacheKeyArray 的并集的个数:" + result);
  11.      } catch (Exception e) {
  12.          throw new RuntimeException(e);
  13.      }
  14. }

并集计算结果写入到指定文件

  1. /**
  2.  * 将计算的并集数据写入到指定文件
  3.  */
  4. public static void writeUnionToFile() {
  5.      File unionFile = new File(unionFileString);
  6.      try(Jedis jedis = new Jedis("127.0.0.1", 6379)) {
  7.          Set<String> result = jedis.smembers(unionFileCacheKey);
  8.          FileUtil.writeUtf8Lines(result, unionFile);
  9.      } catch (Exception e) {
  10.          throw new RuntimeException(e);
  11.      }
  12. }


Redis命令说明

SDIFFSTORE destination key [key …]

举例说明:

  1. key1 = {a,b,c,d}
  2. key2 = {c}
  3. key3 = {a,c,e}
  4. SDIFF key1 key2 key3 = {b,d}

SDIFFSTORE 命令的作用和SDIFF类似,不同的是它将结果保存到 destination 集合,而把结果集返回给客户端。

如果 destination 集合已经存在,则将其覆盖。

返回值

结果集中成员数量


SINTERSTORE destination key [key …]

举例说明:

  1. key1 = {a,b,c,d}
  2. key2 = {c}
  3. key3 = {a,c,e}
  4. SINTER key1 key2 key3 = {c}

SINTERSTORE 命令与 SINTER 命令类似,不同的是它并不是直接返回结果集,而是将结果保存在 destination 集合中。

如果 destination 集合存在, 则会被覆盖。

返回值

结果集中成员数量


SUNIONSTORE destination key [key …]

举例说明:

  1. key1 = {a,b,c,d}
  2. key2 = {c}
  3. key3 = {a,c,e}
  4. SUNION key1 key2 key3 = {a,b,c,d,e}

SUNIONSTORE 命令的功能类似于 SUNION,不同的是不反回结果集,而是存储在 destination 中。

如果 destination 已经存在,则被覆盖。

返回值

结果集中的成员数量


本文网址:https://www.zztuku.com/detail-12222.html
站长图库 - 实例详解Redis实现数据的交集、并集和补集
申明:本文转载于《CSDN》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐