这时就要用到bitmap的聚合运算了,命令BITOP, 支持AND(与)、OR(或), XOR(异或) and NOT(非)运算,除了NOT后面跟一个bitmap外,其他3种聚合运算后面都可以跟多个bitmap...,命令如下: BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyN BITOP OR destkey srckey1 srckey2 srckey3...... srckeyN BITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyN BITOP NOT destkey srckey 为了让demo简单一些...,我这里给出一个查看2天内没有迟到的员工,代码如下: /** * 命令:BITOP * 复杂度:O(N) * 整个月全勤的员工数量,这里用2天代表整个月 * @param key1 第一天 *...比如我们对一个10亿的用户进行日活计算,占用的空间只有120M: 10亿/8/1024/1024=120M 官网链接: https://redis.io/commands/bitop
and key3 key1 key2 (integer) 1 127.0.0.1:6379> get key3 " " 4.2 对一个或者多个key进行逻辑或运算,并将结果保存到key4中 bitop...or destkey key [key...] key1二进制值为0110 0000 key2二进制值为0010 0000 "`" 二进制值为0110 0000 127.0.0.1:6379> bitop...or key4 key1 key2 (integer) 1 127.0.0.1:6379> get key4 "`" 4.3 对一个或者多个key进行逻辑异或运算,并将结果保存到key5中 bitop...xor key5 key1 key2 (integer) 1 127.0.0.1:6379> get key5 "@" 4.4 对给定key进行逻辑非运算,并将结果保存到key6中 bitop not...destkey key key1二进制值为0110 0000 "\x9f"对应二进制值为1001 1111 127.0.0.1:6379> bitop not key6 key1 (integer)
6379> setbit bitmap 8 1 (integer) 0 127.0.0.1:6379> bitcount bitmap (integer) 14 127.0.0.1:6379> 4. bitop...127.0.0.1:6379> set hello good OK 127.0.0.1:6379> set world good OK 127.0.0.1:6379> bitop and hello_world...hello world (integer) 4 127.0.0.1:6379> get hello_world "good" 127.0.0.1:6379> bitop or hello_world...hello world (integer) 4 127.0.0.1:6379> get hello_world "good" 127.0.0.1:6379> bitop not hello_world...hello (integer) 4 127.0.0.1:6379> get hello_world "\x98\x90\x90\x9b" 127.0.0.1:6379> bitop xor hello_world
bitop and|or|not|xor destkey key [key…] (对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 destkey 上。)...BITOP OR destkey key[key …]:对一个或多个 key 求逻辑或,并将结果保存到 destkey 。...BITOP XOR destkey key[key …]:对一个或多个 key 求逻辑异或,并将结果保存到 destkey 。...BITOP NOT destkey key :对给定 key 求逻辑非,并将结果保存到 destkey 。 除了 NOT 操作之外,其他操作都可以接受一个或多个 key 作为输入。..."\x00\x00\x00" 处理不同长度的字符串: 当 BITOP 处理不同长度的字符串时,较短的那个字符串所缺少的部分会被看作 0 。
unique:users:2019-04-28 (integer) 4 127.0.0.1:6379> bitcount unique:users:2019-04-28 1 10 (integer) 3 bitop...30 5 1 (integer) 0 127.0.0.1:6379> setbit unique:users:2019-04-30 8 1 (integer) 0 127.0.0.1:6379> bitop...:2019-04-30 (integer) 3 127.0.0.1:6379> bitcount user:and:2019-04-28-30 (integer) 0 127.0.0.1:6379> bitop...users:2019-04-30 (integer) 3 127.0.0.1:6379> bitcount user:or:2019-04-28-30 (integer) 8 127.0.0.1:6379> bitop...not user:not:2019-04-28 unique:users:2019-04-28 (integer) 3 127.0.0.1:6379> bitop xor user:xor:2019-
了解前面的文章有助于更好的理解本文: ---- 1.Linux上安装Redis 2.Redis中的五种数据类型简介 3.Redis字符串(STRING)介绍 ---- BIT相关的命令是指BITCOUNT/BITFIELD/BITOP...BITOP BITOP可以对一个或者多个二进制位串执行并(AND)、或(OR)、异或(XOR)以及非(NOT)运算,如下:a对应的ASCII码转为二进制是01100001,c对应的二进制位串是01100011...对这两个二进制位串分别执行AND\OR\XOR的结果如下: 127.0.0.1:6379> set k1 a OK 127.0.0.1:6379> set k2 c OK 127.0.0.1:6379> BITOP...:6379> get k3 "c" 127.0.0.1:6379> BITOP xor k3 k1 k2 (integer) 1 127.0.0.1:6379> get k3 "\x02" 另外,BITOP...也可以执行NOT运算,但是注意参数个数,如下: 127.0.0.1:6379> BITOP not k3 k4 (integer) 1 这里会对k4的二进制位串取反,将取反结果交给k3.
OVERFLOW WRAP|SAT|FAIL] summary: Perform arbitrary bitfield integer operations on strings since: 3.2.0 BITOP...= bitmapStatsService.bitPos(USER_MONTH_SIGN, true); log.info("首签{}号", pos); } // 不用bitOp...也能实现统计(bitCount,感觉还要简单一些,至于那个更合适有待进一步思考) @RequestMapping("/bitOp") public void bitOp() {...operation destkey key [key ...] # opration 可以是 and、OR、NOT、XOR # 7天连续登录 bitop and uid:sign:r uid:sign...:1 uid:sign:2 uid:sign:3... # 7天内有登录过 bitop or uid:sign:r uid:sign:1 uid:sign:2 uid:sign:3...
BITOP operation destkey key [key ...]...,operation 可以是 AND 、 OR 、 NOT 、 XOR 这四种操作中的任意一种:● BITOP AND destkey key [key ...]...● BITOP OR destkey key [key ...] ,对一个或多个 key 求逻辑或,并将结果保存到 destkey 。...● BITOP XOR destkey key [key ...] ,对一个或多个 key 求逻辑异或,并将结果保存到 destkey 。...● BITOP NOT destkey key ,对给定 key 求逻辑非,并将结果保存到 destkey 。
命令 说明 getbit 获取二级制中对应偏移量的值 setbit 设置对应二进制位的值 bitcount 统计二进制中位中为1的个数 bitop 对二进制数据做位元操作,与,或,非,异或操作 bitpos...BitOp命令 对一个或多个保存二进制位的字符串 key 进行位元操作,并将结果保存到 destkey 上。...BITOP 命令支持 AND(与) 、 OR(或) 、 NOT(非) 、 XOR(异或)运算。...比如a 01100001和c 01100011进行相关运算 127.0.0.1:6379> get k1 "a" 127.0.0.1:6379> get k2 "c" 127.0.0.1:6379> bitop...127.0.0.1:6379> bitop not k6 k1 (integer) 1 127.0.0.1:6379> get k6 "\x9e" BitPos命令 返回字符串里面第一个被设置为1或者
或者 1 [start] [end] 开始结束范围 # BITPOS key bit [start] [end] > BITPOS test 1 0 (integer) 1 进行位元操作,并保存结果(BITOP...) ITOP AND destkey key [key …],对一个或多个key求逻辑并,并将结果保存到destkey BITOP OR destkey key [key …],对一个或多个key求逻辑或...,并将结果保存到destkey BITOP XOR destkey key [key …],对一个或多个key求逻辑异或,并将结果保存到destkey BITOP NOT destkey key ,对给定...key求逻辑非,并将结果保存到destkey # 对每bit进行运算 # BITOP operation destkey key [key ...] > set key1 aa OK > set key2...bb OK > bitop and dest key1 key2 (integer) 2 > get dest "``" > bitop or dest key1 key2 (integer) 2 >
的偏移量 (以我们刚刚的签到记录为例子) BITPOS WEEK:CHECK:USERID:1 1 (返回0) BITPOS WEEK:CHECK:USERID:1 0 (返回3) BITTOP BITOP...这个命令就是对多个字符串进行操作(AND 逻辑与、OR逻辑或、XOR求异或 或者 NOT 逻辑非) set a "\xff" set b "\x00" bitop AND andkey a b (andkey...:"\x00") bitop OR orkey a b (orkey:"\xff") bitop XOR xorkey a b (xorkey:"\xff") bitop not notkey a
key key offset value 用于修改指定key对应的值,中对应offset的bit 3、 bitcount key [start end] 用于统计字符串被设置为1的bit数 4、bitop...or 获取一周内访问过的用户数量 3、连续三天访问的用户数量 bitop and 4、三天内没有访问的用户数量 bitop not 5、统计在线人数 设置在线key:“online:active”,当用户登录时...说明:BITOP 命令支持 AND 、 OR 、 NOT 、 XOR 这四种操作中的任意一种参数 //日期对应的活跃用户 $data = array( '2020-01-10' => array...PHP_EOL; $redis->bitOp('AND', 'stat1', 'stat_2020-01-10', 'stat_2020-01-11', 'stat_2020-01-14') ....PHP_EOL; $redis->bitOp('AND', 'stat2', 'stat_2020-01-10', 'stat_2020-01-11') .
举个,如果一个用户群中有两个用户: 3和7,即[3,7],用bitmap表示那就是:00010001 最后利用redis提供的bitOp命令: bitOp AND \ bitOp XOR \ bitOp...而且可以预想到对于两个较大的bitmap key执行bitOp也是非常消耗CPU的,应该尽量避免在存储型的redis实例中做这种十分消耗CPU的计算操作。
key [start end]示例#获取mykey内值为 1 的个数BITCOUNT mykey# 获取指定范围内值为 1 的个数,start 和 end 以字节为单位BITCOUNT mykey 0 1BITOP...在多个键(包含字符串值)之间执行按位运算并将结果存储在目标键中语法:#AND 与运算 OR 或运算 |# XOR 异或 ^#NOT 取反 ~BITOP ...destkey key [key ...]BITOP 命令支持四种按位运算:AND、OR、XOR 和 NOT,因此调用该命令的有效形式为:BITOP AND destkey srckey1 srckey2...srckeyNBITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyNBITOP NOT destkey srckey操作的结果始终存储在 destkey 中示例:BITOP...登录setbit login:20240117 2 1用户4登录setbit login:20240117 4 1用户6登录setbit login:20240117 6 1统计连续两天活跃的用户总数:bitop
.20180906 102400000 0 setbit login.20180905 201400000 1 GETBIT 时间复杂度为O(1) getbit login.20180905 201400000 BITOP...时间复杂度为O(N) bitop or login.9m.week1or login.20180905 login.20180906 getbit login.9m.week1or 201400000...对于bitset的操作要注意,各个操作的时间复杂度,如果是getbit、setbit则都是O(1),bitop、bitcount、bitpos等都是O(N),在N比较大的时候要注意,可能是潜在的慢查询...doc setbit getbit bitop bitpos bitcount Efficient analytics with Redis bitmaps Be Careful With your Redis
注意返回bit位,不是在某个字节段中的bit位,而是整个key完整bit长度里的bit位 bitcount bitcount key start end 字节位start 和end中1出现的次数统计 bitop...语法 :bitop 操作 目标key key [key ...] eg 进行k1 k2的按位与 bitop and xxkey k1 k2 bitop or xxkey k1 k2 Hash
统计一周内的签到数据 bitop - BITOP operation destkey key [key ...]...destkey 上 AND : 对一个或多个 key 求逻辑并OR : 对一个或多个 key 求逻辑或XOR : 对一个或多个 key 求逻辑异或NOT : 对给定 key 求逻辑非 $redis->bitop...$redis->bitCount('threeAnd'); $redis->bitop('OR', 'threeOr', 'login:20190311', 'login:20190312', 'login...$redis->bitCount('threeOr'); $redis->bitop('AND', 'monthActivities'', $redis->keys('login:201903*')
本文假设读者对于位图这个数据结构,有基本的认识 目录 介绍 简单使用 相关命令 Redis 客户端示例 Java 代码示例 python 代码示例 进阶使用 实现原理 GETBIT SETBIT BITCOUNT BITOP...BITOP AND destkey srckey1 srckey2 srckey3 ... srckeyN BITOP OR destkey srckey1 srckey2 srckey3 ... srckeyN...BITOP XOR destkey srckey1 srckey2 srckey3 ... srckeyN BITOP NOT destkey srckey 其中 destkey 是结果存储的 key...实现原理 本节简单介绍下GETBIT,SETBIT,BITCOUNT, BITOP等几个命令的实现原理。...BITOP Redis 是基于 C 语言的,C 语言支持对字节进行与,或,异或,非操作,因此 BITOP 操作就是调用 C 语言的对应逻辑实现的。
(1)设置关键字的指定offset的值为 0 或 1 > setbit key 100 1 (2)bit运算 例如 key1 -> 0101 key2 -> 0011 and 运算 > bitop...and ret key1 key2 就是对 key1 key2 各位进行 and 运算后赋值给 ret,结果为 0001 or 运算 > bitop or ret key1 key2 就是对...1 (2)统计今天登录的用户数 > bitcount userlogin:20160118 (3)统计3天内都登录过的用户 “都登录过”是要取得bit值都为1的,通过 and 计算获取 > bitop
setbit test 2 0 3)bitop operation destkey key1 [key2 ...]...operation 可以是 AND、OR、NOT、XOR 4)bitop operation destkey key1 [key2 ...]...,key不能写多个 setbit lower 7 0 setbit lower 2 1 构造一个 0010 0000 任何字母只要和他取或(or) 关系,则变成小写字母 set test Q bitop...or result test lower get result的结果为q 同理,一个小写字母转换为大写字母的也很容易,只需要提前构造1101 1111(upper) bitop and result
领取专属 10元无门槛券
手把手带您无忧上云