Redis_HyperLogLog

Redis Hyperloglog

In Redis 2.8.9 release adds HyperLogLog structure.

Redis HyperLogLog base is used to make statistical algorithms HyperLogLog advantage is that, when the number of input elements or very, very large volume of space required for the calculation base is always fixed, and is very small.

In Redis inside each key HyperLogLog takes only 12 KB of memory, you can calculate the closest base 2 ^ 64 different elements. When this calculation and the base, the more the cost of memory elements of the set, the more contrast.

However, because HyperLogLog will be calculated based on the input element base, but does not store the input element itself, so HyperLogLog not like a collection that returns each element inputs.

1. What is the base?

Such as data set {1, 3, 5, 7, 5, 7, 8}, the cardinality of the set of the data set {1, 3, 5, 7, 8}, the base (not repeat elements) 5. Cardinality estimate error is within the acceptable range, fast calculation base.

2. Examples

redis 127.0.0.1:6379> PFADD runoobkey "redis"

1) (integer) 1

redis 127.0.0.1:6379> PFADD runoobkey "mongodb"

1) (integer) 1

redis 127.0.0.1:6379> PFADD runoobkey "mysql"

1) (integer) 1

redis 127.0.0.1:6379> PFCOUNT runoobkey

(integer) 3

3.Pfadd

Integer, if there is at least one element is added returns 1, otherwise it returns 0.

redis 127.0.0.1:6379> PFADD mykey a b c d e f g h i j
(integer) 1
redis 127.0.0.1:6379> PFCOUNT mykey
(integer) 10

4.Pfcount

An integer, the value is returned to the given group of HyperLogLog, if a plurality of base and HyperLogLog valuation is returned.

redis 127.0.0.1:6379> PFADD hll foo bar zap
(integer) 1
redis 127.0.0.1:6379> PFADD hll zap zap zap
(integer) 0
redis 127.0.0.1:6379> PFADD hll foo bar
(integer) 0
redis 127.0.0.1:6379> PFCOUNT hll
(integer) 3
redis 127.0.0.1:6379> PFADD some-other-hll 1 2 3
(integer) 1
redis 127.0.0.1:6379> PFCOUNT hll some-other-hll
(integer) 6
redis> 

5.PFMERGE

Redis PFMERGE command to merge a plurality HyperLogLog HyperLogLog, HyperLogLog cardinality estimates are combined by any given HyperLogLog be calculated and set.

redis> PFADD hll1 foo bar zap a
(integer) 1
redis> PFADD hll2 a b c foo
(integer) 1
redis> PFMERGE hll3 hll1 hll2
"OK"
redis> PFCOUNT hll3
(integer) 6
redis> 
Published 165 original articles · won praise 12 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/104631108