最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Redis實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集的示例

 更新時(shí)間:2022年08月10日 11:52:23   作者:有夢(mèng)想的攻城獅  
本文主要介紹了Redis實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

場(chǎng)景說(shuō)明

今天我們來(lái)模擬一個(gè)這樣的場(chǎng)景,我們?cè)诒镜赜卸鄠€(gè)文本文件,每個(gè)文件里面存了很多的32位的字符串作為用戶的唯一標(biāo)識(shí),每個(gè)用戶存做一行,假如我們每天都有非常大規(guī)模的用戶,這樣我們可能在工作中就存在需要對(duì)這些用戶進(jìn)行交集、并集或補(bǔ)集等處理,最簡(jiǎn)單的方式是通過(guò)Java中的集合來(lái)進(jìn)行運(yùn)算即可,比如通過(guò)HashSet來(lái)進(jìn)行相應(yīng)的一些運(yùn)算,但是這樣的運(yùn)算存在一個(gè)局限性,那就是我們一般在JVM運(yùn)行過(guò)程中初始的內(nèi)存是有限的,這樣如果全部在JVM內(nèi)存中進(jìn)行計(jì)算的話,很容易出現(xiàn)內(nèi)存空間不足導(dǎo)致的OOM異常,那么我們今天來(lái)介紹一種拓展性更強(qiáng)的方式來(lái)進(jìn)行這樣的一些交并補(bǔ)的運(yùn)算:通過(guò)Redis來(lái)實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集

環(huán)境說(shuō)明

  • 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>

交并補(bǔ)計(jì)算

初始化常量

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";
}

初始化數(shù)據(jù)到指定文件

/**
* 初始化數(shù)據(jù)并寫入文件中
*/
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

/**
* 讀取文件數(shù)據(jù)并寫入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);
? ? }
}

差集的計(jì)算

? ? /**
? ? ?* oneKey對(duì)應(yīng)的Set 與 twoKey對(duì)應(yīng)的Set 的差集 并寫入 threeKey
? ? ?* @param oneKey 差集前面的集合Key
? ? ?* @param twoKey 差集后面的集合Key
? ? ?* @param threeKey 差集結(jié)果的集合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 的差集的個(gè)數(shù):" + result);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? }
? ? }

差集計(jì)算結(jié)果寫入到指定文件

? ? /**
? ? ?* 將計(jì)算的差集數(shù)據(jù)寫入到指定文件
? ? ?*/
? ? 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);
? ? ? ? }
? ? }

交集的計(jì)算

/**
? ? ?*
? ? ?* @param cacheKeyArray 交集集合Key
? ? ?* @param destinationKey 交集集合結(jié)果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 的交集的個(gè)數(shù):" + result);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? }
? ? }

交集計(jì)算結(jié)果寫入指定文件

    /**
     * 將計(jì)算的交集數(shù)據(jù)寫入到指定文件
     */
    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);
        }
    }

并集的計(jì)算

? ? /**
? ? ?* 計(jì)算多個(gè)Key的并集并寫入到新的Key
? ? ?* @param cacheKeyArray 求并集的Key
? ? ?* @param destinationKey 并集結(jié)果寫入的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 的并集的個(gè)數(shù):" + result);
? ? ? ? ?} catch (Exception e) {
? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ?}
? ? ?}

并集計(jì)算結(jié)果寫入到指定文件

    /**
     * 將計(jì)算的并集數(shù)據(jù)寫入到指定文件
     */
    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命令說(shuō)明

SDIFFSTORE destination key [key …]

舉例說(shuō)明:

key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SDIFF key1 key2 key3 = {b,d}

SDIFFSTORE 命令的作用和SDIFF類似,不同的是它將結(jié)果保存到 destination 集合,而把結(jié)果集返回給客戶端。

如果 destination 集合已經(jīng)存在,則將其覆蓋。

返回值

  • 結(jié)果集中成員數(shù)量

SINTERSTORE destination key [key …]

舉例說(shuō)明:

key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SINTER key1 key2 key3 = {c}

SINTERSTORE 命令與 SINTER 命令類似,不同的是它并不是直接返回結(jié)果集,而是將結(jié)果保存在 destination 集合中。

如果 destination 集合存在, 則會(huì)被覆蓋。

返回值

  • 結(jié)果集中成員數(shù)量

SUNIONSTORE destination key [key …]

舉例說(shuō)明:

key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SUNION key1 key2 key3 = {a,b,c,d,e}

SUNIONSTORE 命令的功能類似于 SUNION,不同的是不反回結(jié)果集,而是存儲(chǔ)在 destination 中。

如果 destination 已經(jīng)存在,則被覆蓋。

返回值

  • 結(jié)果集中的成員數(shù)量

參考資料: https://www.redis.com.cn/set.html

到此這篇關(guān)于Redis實(shí)現(xiàn)數(shù)據(jù)的交集、并集、補(bǔ)集的示例的文章就介紹到這了,更多相關(guān)Redis數(shù)據(jù)交集、并集、補(bǔ)集內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • window環(huán)境redis通過(guò)AOF恢復(fù)數(shù)據(jù)的方法

    window環(huán)境redis通過(guò)AOF恢復(fù)數(shù)據(jù)的方法

    這篇文章主要介紹了window環(huán)境redis通過(guò)AOF恢復(fù)數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Redission實(shí)現(xiàn)分布式鎖lock()和tryLock()方法的區(qū)別小結(jié)

    Redission實(shí)現(xiàn)分布式鎖lock()和tryLock()方法的區(qū)別小結(jié)

    Redisson是一種基于Redis的分布式鎖框架,提供了lock()和tryLock()兩種獲取鎖的方法,本文主要介紹了Redission實(shí)現(xiàn)分布式鎖lock()和tryLock()方法的區(qū)別小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • 淺談redis在項(xiàng)目中的應(yīng)用

    淺談redis在項(xiàng)目中的應(yīng)用

    下面小編就為大家?guī)?lái)一篇淺談redis在項(xiàng)目中的應(yīng)用。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • RedisDesktopManager無(wú)法遠(yuǎn)程連接Redis的完美解決方法

    RedisDesktopManager無(wú)法遠(yuǎn)程連接Redis的完美解決方法

    下載RedisDesktopManager客戶端,輸入服務(wù)器IP地址,端口(缺省值:6379);點(diǎn)擊Test Connection按鈕測(cè)試連接,連接失敗,怎么回事呢?下面小編給大家?guī)?lái)了RedisDesktopManager無(wú)法遠(yuǎn)程連接Redis的完美解決方法,一起看看吧
    2018-03-03
  • Redis主從/哨兵機(jī)制原理分析

    Redis主從/哨兵機(jī)制原理分析

    本文介紹了Redis的主從復(fù)制和哨兵機(jī)制,主從復(fù)制實(shí)現(xiàn)了數(shù)據(jù)的熱備份和負(fù)載均衡,而哨兵機(jī)制可以監(jiān)控Redis集群,實(shí)現(xiàn)自動(dòng)故障轉(zhuǎn)移,哨兵機(jī)制通過(guò)監(jiān)控、下線、選舉和故障轉(zhuǎn)移等步驟,確保Redis集群的高可用性
    2025-01-01
  • 解決redis-cli報(bào)錯(cuò)Could not connect to Redis at 127.0.0.1:6379: Connection refused

    解決redis-cli報(bào)錯(cuò)Could not connect to Redis&

    這篇文章主要介紹了解決redis-cli報(bào)錯(cuò)Could not connect to Redis at 127.0.0.1:6379: Connection refused,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 基于redis實(shí)現(xiàn)的點(diǎn)贊功能設(shè)計(jì)思路詳解

    基于redis實(shí)現(xiàn)的點(diǎn)贊功能設(shè)計(jì)思路詳解

    點(diǎn)贊是我們現(xiàn)在經(jīng)常見(jiàn)到的一個(gè)效果,如朋友圈、微博都有點(diǎn)贊的效果,下面這篇文章主要跟大家分享了基于redis實(shí)現(xiàn)的點(diǎn)贊功能設(shè)計(jì)思路的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家實(shí)現(xiàn)點(diǎn)贊功能具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • hiredis從安裝到項(xiàng)目實(shí)戰(zhàn)操作

    hiredis從安裝到項(xiàng)目實(shí)戰(zhàn)操作

    這篇文章主要介紹了hiredis從安裝到項(xiàng)目實(shí)戰(zhàn)操作,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Redis 實(shí)現(xiàn)“附近的人”功能

    Redis 實(shí)現(xiàn)“附近的人”功能

    Redis基于geohash和有序集合提供了地理位置相關(guān)功能。這篇文章主要介紹了Redis 實(shí)現(xiàn)“附近的人”功能,需要的朋友可以參考下
    2019-11-11
  • Redis遍歷所有key的兩個(gè)命令(KEYS 和 SCAN)

    Redis遍歷所有key的兩個(gè)命令(KEYS 和 SCAN)

    這篇文章主要介紹了Redis遍歷所有key的兩個(gè)命令(KEYS 和 SCAN),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04

最新評(píng)論

孝昌县| 弥勒县| 六枝特区| 五大连池市| 中江县| 吉木萨尔县| 金阳县| 美姑县| 元谋县| 巨鹿县| 中西区| 马关县| 崇礼县| 黑山县| 巴中市| SHOW| 漳平市| 乐至县| 桑植县| 策勒县| 高邑县| 长海县| 文成县| 孟村| 二手房| 清徐县| 吉林市| 绥棱县| 称多县| 贡觉县| 屏南县| 启东市| 普兰店市| 尼勒克县| 运城市| 怀柔区| 盐津县| 永吉县| 蒙山县| 临泉县| 沛县|