欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

場景說明

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

環(huán)境說明

  • 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ǔ)計算

初始化常量

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);
? ? }
}

差集的計算

? ? /**
? ? ?* oneKey對應(yīng)的Set 與 twoKey對應(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 的差集的個數(shù):" + result);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? }
? ? }

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

? ? /**
? ? ?* 將計算的差集數(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);
? ? ? ? }
? ? }

交集的計算

/**
? ? ?*
? ? ?* @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 的交集的個數(shù):" + result);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? throw new RuntimeException(e);
? ? ? ? }
? ? }

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

    /**
     * 將計算的交集數(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);
        }
    }

并集的計算

? ? /**
? ? ?* 計算多個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 的并集的個數(shù):" + result);
? ? ? ? ?} catch (Exception e) {
? ? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ? ?}
? ? ?}

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

    /**
     * 將計算的并集數(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命令說明

SDIFFSTORE destination key [key …]

舉例說明:

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 …]

舉例說明:

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

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

如果 destination 集合存在, 則會被覆蓋。

返回值

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

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,不同的是不反回結(jié)果集,而是存儲在 destination 中。

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

返回值

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

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

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

相關(guān)文章

  • 詳解Redis 緩存刪除機(jī)制(源碼解析)

    詳解Redis 緩存刪除機(jī)制(源碼解析)

    這篇文章主要介紹了Redis 緩存刪除機(jī)制(源碼解析),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Redis跳躍表添加元素的方法實現(xiàn)

    Redis跳躍表添加元素的方法實現(xiàn)

    本文主要介紹了Redis跳躍表添加元素的方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 關(guān)于Redis的讀寫一致問題

    關(guān)于Redis的讀寫一致問題

    在項目使用Redis過程中,當(dāng)數(shù)據(jù)更新時,我們要保證緩存和數(shù)據(jù)庫的一致性,否則會導(dǎo)致很多臟數(shù)據(jù)出現(xiàn),此時我們就要思考如何去進(jìn)行數(shù)據(jù)更新,本文就給大家講講關(guān)于redis的讀寫一致問題,需要的朋友可以參考下
    2023-08-08
  • Redis配置外網(wǎng)可訪問(redis遠(yuǎn)程連接不上)的方法

    Redis配置外網(wǎng)可訪問(redis遠(yuǎn)程連接不上)的方法

    默認(rèn)情況下,當(dāng)我們在部署了redis服務(wù)之后,redis本身默認(rèn)只允許本地訪問。Redis服務(wù)端只允許它所在服務(wù)器上的客戶端訪問,如果Redis服務(wù)端和Redis客戶端不在同一個機(jī)器上,就要進(jìn)行配置。
    2022-12-12
  • Centos7 Redis主從搭建配置的實現(xiàn)

    Centos7 Redis主從搭建配置的實現(xiàn)

    這篇文章主要介紹了Centos7 Redis主從搭建配置的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • redis如何取hash的值

    redis如何取hash的值

    這篇文章主要介紹了redis如何取hash的值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Redis中Scan命令的踩坑實錄

    Redis中Scan命令的踩坑實錄

    這篇文章主要給大家介紹了關(guān)于Redis中Scan命令踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Redis優(yōu)雅地實現(xiàn)延遲隊列的方法分享

    Redis優(yōu)雅地實現(xiàn)延遲隊列的方法分享

    Redisson是Redis服務(wù)器上的分布式可伸縮Java數(shù)據(jù)結(jié)構(gòu),這篇文中主要為大家介紹了Redisson實現(xiàn)的優(yōu)雅的延遲隊列的方法,需要的可以參考一下
    2023-02-02
  • Redis整合SpringBoot的RedisTemplate實現(xiàn)類(實例詳解)

    Redis整合SpringBoot的RedisTemplate實現(xiàn)類(實例詳解)

    這篇文章主要介紹了Redis整合SpringBoot的RedisTemplate實現(xiàn)類,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • 詳解redis集群的三種方式

    詳解redis集群的三種方式

    Redis三種集群方式分別是主從復(fù)制,哨兵模式,Cluster集群,這篇文章主要介紹了redis集群的三種方式,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07

最新評論