“This is the 13th day of my participation in the August Text Challenge.

1. HyperLogLog base statistics

What is the cardinal number?

For example, a dataset {1, 3, 5, 7, 5, 7, 8} would have a cardinality of {1, 3, 5, 7, 8} and a cardinality (not repeating elements) of 5. Cardinality estimation is to quickly calculate the cardinality within an acceptable range of error.

Redis HyperLogLog

Redis added the HyperLogLog structure in version 2.8.9.

Redis HyperLogLog is used to do cardinality statistics algorithm. The advantage of HyperLogLog is that when the number or volume of input elements is very, very large, the space required to calculate the cardinality is always fixed and small.

In Redis, each HyperLogLog key costs only 12 KB of memory to calculate the bases of close to 2^64 different elements. This is in stark contrast to collections where the more elements you have, the more memory you consume when calculating cardinality.

However, because HyperLogLog only calculates the cardinality from the input elements and does not store the input elements themselves, HyperLogLog cannot return the individual elements of the input, as collections do.

Common commands:

The serial number Commands and Description
1 [PFADD key Element [Element…] Adds the specified element to HyperLogLog.
2 [PFCOUNT key [key…] Returns the cardinality estimate for the given HyperLogLog.
3 [PFMERGE destkey sourcekey [sourcekey…] Merges multiple HyperLogLog into one HyperLogLog

Example:

# add the specified element to HyperLogLog 127.0.0.1:6379> pfadd key1 a b c d e f g h (integer) 1 Pfcount key1 (integer) 8 127.0.0.1:6379> pfadd key2 e f g h x w y x z (integer) 1 127.0.0.1:6379> pfcount key2 (integer) 127.0.0.1:6379> pfmerge key3 key1 key2 OK 127.0.0.1:6379> pfcount key3 (integer) 12Copy the code

2. Detailed explanation of Bitmap scenarios

A bitmap is a set of zero or one bits that represent the value or state of an element. The value of a bit, either 0 or 1; This means that a bit can store up to 2 bits of information.

Redis has added setbit,getbit,bitcount and other bitmap-related commands since version 2.2.0. New commands, but no new data types are added, because commands such as setbit are merely extensions of set.

Syntax:

 setbit key offset value
Copy the code

Sets or clears bits at the specified offset for the string value stored by key, or if none exists. If it already exists, clear the old one and set a new one.

The value returned is the value of this bit before setbit. Value can only be 0 or 1. Offset starts at 0, even if the in-situ map has only 10 bits, and offset can be 1000

 getbit key offset
Copy the code

Gets the bit at the specified offset for the string value stored by key.

 bitcount key [start end]
Copy the code

Getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap getbitmap

Scenario 1: Count the number of times you punch in a week

0-6 indicates Monday to weekend, where 1 indicates clocked and 0 indicates unclocked

# = = = = = = = = = = = = = = = = = = = = = = = = record the clock within the week = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > setbit sign 0 1 (integer) 0 127.0.0.1:6379 > Setbit sign 1 1 (integer) 0 127.0.0.1:6379> setbit sign 2 0 (integer) 0 127.0.0.1:6379> setbit sign 3 0 (integer) 0 127.0.0.1:6379> setbit sign 4 1 (integer) 0 127.0.0.1:6379> setbit sign 5 1 (integer) 0 127.0.0.1:6379> setbit sign 6 0 (integer) 0 # = = = = = = = = = = = = = = = = = = = = = = = = check the specific day clock of = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > getbit sign 2 0 (integer) # = = = = = = = = = = = = = = = = = = = = = = = = statistical clock within one week = = = = = = = = = = = = = = = = = = = = = = = = 127.0.0.1:6379 > bitcount sign (integer) 4Copy the code

Scenario 2: Collecting statistics on active users

Use the time as the Key, and then the user ID is offset, set to 1 if active that day

127.0.0.1:6379> setbit 20210812 1 1 (integer) 0 127.0.0.1:6379> setbit 20210812 20 (integer) 0 127.0.0.1:6379> setbit 20210812 3 0 (integer) 0 # Check whether the specified user is active on August 12, 2021 127.0.0.1:6379> getbit 20210812 2 (integer) 0 127.0.0.1:6379> bitcount 20210812 (integer) 1Copy the code

Using Bitmaps is a space-saving and efficient way to do this

\