用户画像利用BitMap存储

目前项目上存储利用BitMap进行存储,比如用户id 为1001,对应的标签id A1001,

setbit 1001 A1001,1 

在生产中经常会出现一个用户对应多个标签因此用BitMap存储的话就很方便

 

package com.demo.cn.Redis;

import java.util.BitSet;

public class BitSetUtils {


    /**
     * 将BitSet对象转化为ByteArray
     * @param bitSet
     * @return
     */
    public static byte[] bitSet2ByteArray(BitSet bitSet) {
        byte[] bytes = new byte[bitSet.size() / 8];
        for (int i = 0; i < bitSet.size(); i++) {
            int index = i / 8;
            int offset = 7 - i % 8;
            bytes[index] |= (bitSet.get(i) ? 1 : 0) << offset;
        }
        return bytes;
    }

    /**
     *
     * @param bytes
     * @return
     */
    public static BitSet byteArray2BitSet(byte[] bytes) {
        BitSet bitSet = new BitSet(bytes.length * 8);
        int index = 0;
        for (int i = 0; i < bytes.length; i++) {
            for (int j = 7; j >= 0; j--) {
                bitSet.set(index++, (bytes[i] & (1 << j)) >> j == 1 ? true
                        : false);
            }
        }
        return bitSet;
    }
}

 

package com.demo.cn.Redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.ResourceBundle;

public class RedisUtil {

    private static ResourceBundle resourceBundle;

    static {
        resourceBundle = ResourceBundle.getBundle("redis_config");

        initPool();
    }
    private static volatile JedisPool jedisPool;
    public static Jedis getResource() {
        return jedisPool.getResource();
    }
    public static void returnResource(Jedis jedis) {
        jedisPool.returnResource(jedis);
    }

    public static void initPool() {
        if(jedisPool != null){
            return;
        }
        String host = resourceBundle.getString("redis.host");
        int port = Integer.parseInt(resourceBundle.getString("redis.port"));
        JedisPoolConfig config = config();
        jedisPool = new JedisPool(config,host,port);
    }

    private static JedisPoolConfig config() {
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    public static void activeUser(long userId, String dateKey) {
        Jedis jedis= RedisUtil.getResource();
        try{
            jedis.setbit(dateKey,userId,true);
        }finally {
            RedisUtil.returnResource(jedis);
        }
    }

    //该天用户总数
    public static long totalCount(String dateKey) {
        Jedis jedis= RedisUtil.getResource();
        try{
            return jedis.bitcount(dateKey);
        }finally {
            RedisUtil.returnResource(jedis);
        }
    }

    //该天登录所有的用户id
    public static List activeUserIds(String  dateKey) {
        Jedis jedis= RedisUtil.getResource();
        try{

            if(jedis.get(dateKey)==null){
                return null;
            }
            BitSet set= BitSetUtils.byteArray2BitSet(jedis.get(dateKey).getBytes());

            Listlist=new ArrayList();
            for (long i=0;i continueActiveUserCount(String... dateKeys) {
        Jedis jedis= RedisUtil.getResource();
        try{
            BitSet all = null;
            for (String key:dateKeys){
                if(jedis.get(key)==null){
                    continue;
                }
                BitSet set= BitSetUtils.byteArray2BitSet(jedis.get(key).getBytes());
                if(all==null){
                    all=set;
                }
                System.out.println(set.size());
                all.and(set);
            }
            Listlist=new ArrayList();
            for (long i=0;i list=activeUserIds("20180925");
        for(Long i:list){
            System.out.println("the userid :"+i);
            System.out.println(totalCount("20180925"));
        }


    }
}

Redis的存储结果

用户画像利用BitMap存储_第1张图片

你可能感兴趣的:(大数据)