redis entry to the proficiency (VII): redis advanced data types Comments (BitMaps, HyperLogLog, GEO)

High five basic data types and data types, data structures is not new. Advanced data types are often used to solve a number of business scenarios.

(A) BitMaps

(1.1) BitMaps Overview

In the scenario, there are only two data attributes, such as whether a student, whether party members and so on, for these data, the most economical way is to use a bit of memory to record to see if a student, for example, 1 for students , 0 represents not a student. Then 1,000,110 on behalf of seven people in three students, this is the storage needs of BitMaps.

Bitmaps may operate a string of bits, we can imagine Bitmaps are a string of binary digits, each location stores only 0 and 1. Bitmaps subscripted offset (offset)

(1.2) BitMaps operation

Get the value of the bit corresponding to the specified key offset

getbit key offset

Provided corresponding to the specified key bit offset value, value can only be 1 or 0

setbit key offset value

 

Key for the specified bit cross, and, NOT, XOR operation and save the result to destKey

bitop and destKey key1 key2....  //交
bitop or destKey key1 key2....   //并
bitop not destKey key1 key2....  //非
bitop xor destKey key1 key2....  //异或

Specifies the key statistics of the number 1

bitcount key start end

(1.3) BitMaps scenarios

We suppose a company to count all registered members of their daily / weekly / landing site situation, you can use BitMaps year. Wherein the number of daily landing member following statistical methods : establishing a key value of the current date BITMAPS, when the login id for the members 5, 4 to set the offset (index starts from 0), the offset BITMAPS number 4 is set to 1, and so on. The number of members of statistics daily landing on the use of statistics to bitcount command.

Weekly statistical landing membership is as follows: For the weekly statistical landing membership, seven days a week just to use the data and (or) operation can be calculated. One day a member as long as the value is equal to 1, and then after the operation his value is equal to 1, indicating that this week he landed. Then bitcount statistics can be.

(二)HyperLogLog

HyperLogLog base is used to make statistics, so-called statistical base, refers to the number of digits in the string of numbers do not overlap, as is {1,2,1,2,3} of cardinality 3.

adding data:

pfadd key element1 element2...

Statistical data

pfcount key1 key2...

Merge Data

pfmerge destkey sourcekey1 sourcekey2...

 

HyperLogLog use of:

First HyperLogLog can only record data.

As the core of cardinality estimation algorithm with 0.81% in the number of large errors .

Small footprint, each hyperLogLog occupies only 12k of memory.

pfadd command is not a one-time allocation of data 12k, will gradually increase with the increase of base memory.

After pfmerge command to merge the storage space for the 12k, the amount of data no matter how much before.

(C) GEO

GEO is advanced data types redis concerning the calculation of the location, such as near a friend micro letter will show your friends from a distance, which is an application of GEO.

Add coordinates of the point:

geoadd key longitude latitude member [longitude latitude member...]

Obtaining coordinate points

geopos key member

Calculated coordinate distance

geodist key member1 member2 [unit] unit表示单位,默认m,可以设置 km, ft, mi

For simplicity I have horizontal and vertical coordinates expressed in simple numbers

 

The coordinate data in the required range (withcoord display coordinates, withdist display the distance, withhash display hash value, count count to take range)

georadius key longitude latitude radius m|km|ft|mi [withcoord] [withdist] [withhash] [count count]

Determining the range of the data in accordance with the

georadiusbymember key member radius m|km|ft|mi  [withcoord] [withdist] [withhash] [count count]

Obtaining the hash value of the coordinate point corresponding to the specified

geohash key member1 member2...

Published 55 original articles · won praise 614 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_41973594/article/details/103940765