redis之set应用 实现对账(集合比较)功能

redis使用之bitmap

Redis实现布隆过滤器

《》

现状:每日在进行系统之间的订单对账时,往往是这样的操作流程; 
1.从外部系统拉取数据存入本地数据库; 
2.查询本地订单数据集合localSet; 
3.查询外部系统订单数据集合outerSet; 
4.以本地localSet为基准,对照outerSet,进行遍历,将数据不一致(金额、状态等),或者localSet存在而outerSet不存在的数据,放入新集合localDiffSet; 
5.以外部outerSet为基准,对照localSet,进行遍历,将数据(金额、状态等)不一致,或者outerSet存在而localSet不存在的数据,放入新集合outerDiffSet; 
6.将localDiffSet与outerDiffSet的数据,存入差异账表 

一、JVM的list求交集、并集和差集(去重)示例

package com.dxz.base;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ListTest {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<>();
        stringList.add("a");
        stringList.add("b");
        stringList.add("c");
        stringList.add("i");
        stringList.add("j");
        stringList.add("a");

        // 一、求交集
        // 方法1:直接通过retainAll直接过滤
        List<String> stringList1 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
        stringList1.retainAll(stringList);
        System.out.println("交集1: " + stringList1);

        // 方法2:通过过滤掉存在于stringList的数据
        List<String> stringList1_2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
        List<String> strings = stringList1_2.stream().filter(item -> stringList.contains(item)).collect(Collectors.toList());
        System.out.println("交集2:" + strings);

        // 二、并集
        // 有重并集
        List<String> stringList2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
        stringList2.addAll(stringList);
        System.out.println("并集: " + stringList2);

        // 无重并集
        List<String> stringList2_2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
        List<String> stringList_1 = new ArrayList<>(Arrays.asList("a,b,c,i,j,a".split(",")));
        stringList2_2.removeAll(stringList_1);
        stringList_1.addAll(stringList2_2);

        System.out.println("无重并集: " + stringList_1);

        // 三、求差集
        // 方法1:直接使用removeAll()方法
        List<String> stringList3 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
        stringList3.removeAll(stringList);
        System.out.println("差集1: " + stringList3);

        // 方法2:通过过滤掉不存在于stringList的数据,然后和本数组进行交集处理
        List<String> stringList3_2 = new ArrayList<>(Arrays.asList("a,b,c,d,e,f,g,h".split(",")));
        stringList3_2.retainAll(stringList3_2.stream().filter(item -> !stringList.contains(item)).collect(Collectors.toList()));
        System.out.println("差集2:" + stringList3_2);
    }
}

问题: 
当比对数据无限多,数据全部在JVM中比对,对服务器的影响就比较大,执行效率也低下;

二、redis解决方案

步骤一:外部系统数据拉取入库 
步骤二:从数据库查询需要比对的数据,本地数据(localSet),外部系统数据(outerSet)

//---查询订单信息,组成字符串
SELECT CONCAT(order_no,','outer_order_no,',',trans_amount,',',status) FROM `order_info` where create_time BETWEEN '2015-12-01 00:00:00' and  '2015-12-31 23:59:59'; 

为什么组合成字符串,而不是object对象;因为我们届时要比对的信息就是上述字段,如果字符串一致,那么就说明订单信息一致,而不用再去一一比对对象的属性; 
步骤三:将localSet与outerSet分别存入redis

//相关函数:redis.clients.jedis.JedisCluster.sadd(String key, String... member)
redisClusterUtils.sadd("{account}:localSet", "GM002215120800002,0.01,3","GM002215120800003,0.01,3");
redisClusterUtils.sadd("{account}:outerSet", "CZ001215120800010,0.01,3","CZ001215120800013,0.01,1");

注意点:这里的key,必须要用{}形式,来指定,使我们要比对的集合都处于同一slot,不然在稍后比对时会出现异常:

No way to dispatch this command to Redis Cluster because keys have different slots.

步骤四:进行2个集合的比对,得出交集union,将交集放入key”{account}:union”中

redisClusterUtils.sinterstore("{account}:union", "{account}:localSet", "{account}:outerSet");

步骤五:localSet和outerSet分别与交集进行比较,得出差集{account}:localDiff、{account}:outerDiff

redisClusterUtils.sdiffstore("{account}:localDiff", "{account}:localSet", "{account}:union");
redisClusterUtils.sdiffstore("{account}:outerDiff", "{account}:localSet", "{account}:union");

步骤六:将差集的数据存入数据库差异账表

//--获取差集的每个成员
redisClusterUtils.smembers("{account}:localDiff");
redisClusterUtils.smembers("{account}:outerDiff");

------------------------------------------------------------

 补充一下api:

    private SetOperations<String, String> setOpe;

    @SuppressWarnings("rawtypes")
    public RedisManage(StringRedisTemplate template) {
        this.setOpe = template.opsForSet();
    }
//----------------------------------------------
    /**
     * 往set中添加元素
     * @param setKey
     * @param sets
     * @return
     */
    public Long sadd(int ttl, String setKey, String... sets) {
        Long count = setOpe.add(setKey, sets);
        stringRedisTemplate.expire(setKey, ttl, TimeUnit.SECONDS);
        return count;
    }
    /**
     * 求两个set集合的交集
     * @param setKey
     * @param otherSetKey
     * @return
     */
    public Set<String> sinter(String setKey, String otherSetKey) {
        return setOpe.intersect(setKey, otherSetKey);
    }
    
    /**
     * 求两个set集合的交集,并将交集保存到指定的set集合
     * @param setKey
     * @param otherSetKey
     * @param destSetKey
     * @return
     */
    public Long sinterstore(int ttl, String setKey, String otherSetKey, String destSetKey) {
        Long count = setOpe.intersectAndStore(setKey, otherSetKey, destSetKey);
        stringRedisTemplate.expire(destSetKey, ttl, TimeUnit.SECONDS);
        return count;
    }
    
    /**
     * 求两个set集合的差集
     * @param setKey
     * @param otherSetKey
     * @return
     */
    public Set<String> sdiff(String setKey, String otherSetKey) {
        return setOpe.difference(setKey, otherSetKey);
    }
    
    /**
     * 求两个set集合的差集,并将差集保存到指定的set集合
     * @param setKey
     * @param otherSetKey
     * @param destSetKey
     * @return
     */
    public Long sdiffstore(int ttl, String setKey, String otherSetKey, String destSetKey) {
        Long count = setOpe.differenceAndStore(setKey, otherSetKey, destSetKey);
        stringRedisTemplate.expire(destSetKey, ttl, TimeUnit.SECONDS);
        return count;
    }
    
    /**
     * 求两个set集合的合集,并将差集保存到指定的set集合
     * @param ttl
     * @param setKey
     * @param otherSetKey
     * @param destSetKey
     * @return
     */
    public Long unionAndStore(int ttl, String setKey, String otherSetKey, String destSetKey) {
        Long count = setOpe.unionAndStore(setKey, otherSetKey, destSetKey);
        stringRedisTemplate.expire(destSetKey, ttl, TimeUnit.SECONDS);
        return count;
    }
    
    /**
     * set的size
     * @param setKey
     * @return
     */
    public Long ssize(String setKey) {
        return setOpe.size(setKey);
    }
    
    /**
     * 返回set的所有集合
     * @param setKey
     * @return
     */
    public Set<String> smembers(String setKey) {
        return setOpe.members(setKey);
    }

 

 

关于redis集合(Set)操作的的相关命令,Redis集合命令相关资料

转载:https://blog.csdn.net/qq_33144861/article/details/79467888

posted on 2018-06-20 14:39  duanxz  阅读(5714)  评论(5编辑  收藏  举报