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 已经存在,则被覆盖。
- 返回值
结果集中的成员数量
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanffgei
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24