redis的hyperLoglog的应用-网站的数据统计(Uv数、IP数等)

HyperLogLog并不是一种新的数据结构(实际类型为字符串类型),而是一种基数算法,通过HyperLogLog可以利用极小的内存空间完成独立总数的统计。

一、场景概述

HyperLogLog可以使用在一些数据统计业务(不考虑单条数据,只统计独立总数),可以容忍一定的误差。Redis官方给出的误差率是0.81%。

比如:统计访问网站的IP,Uv数,在线用户数等

二、命令

HyperLogLog提供了3个命令:pfadd、pfcount、pfmerge

1.添加-pfadd

pfadd key element [element1 element2]

2.统计数量-pfcount

pfcount key

3.合并-pfmerge

pfmerge destkey key1 key2
127.0.0.1:6379> pfadd test:20190122:uid uid1 uid2
(integer) 1
127.0.0.1:6379> pfcount test:20190122:uid
(integer) 2
127.0.0.1:6379> pfadd test:20190121 uid1 uid3 uid4
(integer) 1
127.0.0.1:6379> pfcount test:20190121
(integer) 3
127.0.0.1:6379> pfmerge test:pf:merge test:20190122:uid test:20190121
OK
127.0.0.1:6379> pfcount test:fp:merge
(integer) 0
127.0.0.1:6379> pfcount test:pf:merge
(integer) 4

三、特点

HyperLogLog占用内存空间小

猜你喜欢

转载自blog.csdn.net/weixin_33781606/article/details/87491342