溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-06-02 13:52:19 來(lái)源:億速云 閱讀:204 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫(kù)

本篇內(nèi)容介紹了“Redis如何實(shí)現(xiàn)數(shù)據(jù)的交集、并集和補(bǔ)集”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

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

場(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ù)量

“Redis如何實(shí)現(xiàn)數(shù)據(jù)的交集、并集和補(bǔ)集”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI