推荐学习:redis视频教程
场景说明今天我们来模拟一个这样的场景,我们在本地有多个文本文件,每个文件里面存了很多的32位的字符串作为用户的唯一标识,每个用户存做一行,假如我们每天都有非常大规模的用户,这样我们可能在工作中就存在需要对这些用户进行交集、并集或补集等处理,最简单的方式是通过java中的集合来进行运算即可,比如通过hashset来进行相应的一些运算,但是这样的运算存在一个局限性,那就是我们一般在jvm运行过程中初始的内存是有限的,这样如果全部在jvm内存中进行计算的话,很容易出现内存空间不足导致的oom异常,那么我们今天来介绍一种拓展性更强的方式来进行这样的一些交并补的运算:通过redis来实现数据的交集、并集、补集
环境说明redis版本: redis 6.0.6
jedis版本: 4.2.2
工具类hutool版本: 5.8.0.m3
pom文件:
<dependencies> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>4.2.2</version> </dependency> <dependency> <groupid>cn.hutool</groupid> <artifactid>hutool-all</artifactid> <version>5.8.0.m3</version> </dependency></dependencies>
交并补计算初始化常量public class rediscalculateutils { static string onefilestring = /users/tmp/test-1.txt; static string twofilestring = /users/tmp/test-2.txt; static string difffilestring = /users/tmp/diff-test.txt; static string interfilestring = /users/tmp/inter-test.txt; static string unionfilestring = /users/tmp/union-test.txt; static string onefilecachekey = onefile; static string twofilecachekey = twofile; static string difffilecachekey = difffile; static string interfilecachekey = interfile; static string unionfilecachekey = unionfile; }
初始化数据到指定文件/*** 初始化数据并写入文件中*/public static void writefile() { file onefile = new file(onefilestring); list<string> fs = new arraylist<>(10000); for (int i = 10000; i < 15000; i++) { string s = secureutil.md5(string.valueof(i)); fs.add(s); } fileutil.writeutf8lines(fs, onefile); file twofile = new file(twofilestring); fs.clear(); for (int i = 12000; i < 20000; i++) { string s = secureutil.md5(string.valueof(i)); fs.add(s); } fileutil.writeutf8lines(fs, twofile); }
指定文件写入redis/*** 读取文件数据并写入redis*/public static void writecache() { try(jedis jedis = new jedis(127.0.0.1, 6379)) { pipeline p = jedis.pipelined(); list<string> onefilestringlist = fileutil.readlines(onefilestring, utf-8); for (string s : onefilestringlist) { p.sadd(onefilecachekey, s); } p.sync(); list<string> twofilestringlist = fileutil.readlines(twofilestring, utf-8); for (string s : twofilestringlist) { p.sadd(twofilecachekey, s); } p.sync(); } catch (exception e) { throw new runtimeexception(e); }}
差集的计算 /** * onekey对应的set 与 twokey对应的set 的差集 并写入 threekey * @param onekey 差集前面的集合key * @param twokey 差集后面的集合key * @param threekey 差集结果的集合key */ public static void diff(string onekey, string twokey, string threekey) { try(jedis jedis = new jedis(127.0.0.1, 6379)) { long result = jedis.sdiffstore(threekey, onekey, twokey); system.out.println(onekey 与 twokey 的差集的个数: + result); } catch (exception e) { throw new runtimeexception(e); } }
差集计算结果写入到指定文件 /** * 将计算的差集数据写入到指定文件 */ public static void writedifftofile() { file difffile = new file(difffilestring); try(jedis jedis = new jedis(127.0.0.1, 6379)) { set<string> result = jedis.smembers(difffilecachekey); fileutil.writeutf8lines(result, difffile); } catch (exception e) { throw new runtimeexception(e); } }
交集的计算/** * * @param cachekeyarray 交集集合key * @param destinationkey 交集集合结果key */ public static void inter(string[] cachekeyarray, string destinationkey) { try(jedis jedis = new jedis(127.0.0.1, 6379)) { long result = jedis.sinterstore(destinationkey, cachekeyarray); system.out.println(cachekeyarray 的交集的个数: + result); } catch (exception e) { throw new runtimeexception(e); } }
交集计算结果写入指定文件 /** * 将计算的交集数据写入到指定文件 */ public static void writeintertofile() { file interfile = new file(interfilestring); try(jedis jedis = new jedis(127.0.0.1, 6379)) { set<string> result = jedis.smembers(interfilecachekey); fileutil.writeutf8lines(result, interfile); } catch (exception e) { throw new runtimeexception(e); } }
并集的计算 /** * 计算多个key的并集并写入到新的key * @param cachekeyarray 求并集的key * @param destinationkey 并集结果写入的key */ public static void union(string[] cachekeyarray, string destinationkey) { try(jedis jedis = new jedis(127.0.0.1, 6379)) { long result = jedis.sunionstore(destinationkey, cachekeyarray); system.out.println(cachekeyarray 的并集的个数: + result); } catch (exception e) { throw new runtimeexception(e); } }
并集计算结果写入到指定文件 /** * 将计算的并集数据写入到指定文件 */ public static void writeuniontofile() { file unionfile = new file(unionfilestring); try(jedis jedis = new jedis(127.0.0.1, 6379)) { set<string> result = jedis.smembers(unionfilecachekey); fileutil.writeutf8lines(result, unionfile); } catch (exception e) { throw new runtimeexception(e); } }
redis命令说明sdiffstore destination key [key …]举例说明:
key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}sdiff key1 key2 key3 = {b,d}
sdiffstore 命令的作用和sdiff类似,不同的是它将结果保存到 destination 集合,而把结果集返回给客户端。
如果 destination 集合已经存在,则将其覆盖。
返回值
结果集中成员数量sinterstore destination key [key …]举例说明:
key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}sinter key1 key2 key3 = {c}
sinterstore 命令与 sinter 命令类似,不同的是它并不是直接返回结果集,而是将结果保存在 destination 集合中。
如果 destination 集合存在, 则会被覆盖。
返回值
结果集中成员数量sunionstore destination key [key …]举例说明:
key1 = {a,b,c,d}key2 = {c}key3 = {a,c,e}sunion key1 key2 key3 = {a,b,c,d,e}
sunionstore 命令的功能类似于 sunion,不同的是不反回结果集,而是存储在 destination 中。
如果 destination 已经存在,则被覆盖。
返回值
结果集中的成员数量推荐学习:redis视频教程
以上就是实例详解redis实现数据的交集、并集和补集的详细内容。
