Redis05_ three special data types


1. geospatial location

Note: The underlying implementation principle of GEO is Zset, and you can use Zset commands to operate GEO

Application scenarios: location of friends, nearby people, calculation of taxi distance

geoadd adds the specified geographic location (longitude, latitude, name) to the specified key

127.0.0.1:6379> geoadd china:city 116.40 39.90 Beijing
(integer) 1
127.0.0.1:6379> geoadd china:city 121.47 31.23 Shanghai
(integer) 1
127.0.0.1:6379> geoadd china:city 106.50 29.53 Chongqin
(integer) 1
127.0.0.1:6379> geoadd china:city 114.08 22.54 Shenzhen
(integer) 1
127.0.0.1:6379> geoadd china:city 120.15 30.28 Hangzhou
(integer) 1
127.0.0.1:6379> geoadd china:city 108.94 34.26 Xian
(integer) 1

Geopos gets the longitude and latitude of the specified location.
Geodist gets the distance between two locations. The default unit is meters. Units: m, km, mi (miles), ft (feet).
Georadius takes the specified longitude and latitude as the center to find the location within the specified radius. withcoord parameter indicates the position of latitude and longitude, withdist parameter indicates the position of the distance parameter count represents the number of query results
georadiusbymember to specify a location for the center, find the location within a specified radius
geohash the specified two-dimensional position of latitude and longitude to a string of one-dimensional

127.0.0.1:6379> geopos china:city Beijing
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"

127.0.0.1:6379> geopos china:city Beijing Shanghai
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"
2) 1) "121.47000163793563843"
   2) "31.22999903975783553"

127.0.0.1:6379> geodist china:city Beijing Shanghai
"1067378.7564"
127.0.0.1:6379> geodist china:city Beijing Shanghai km
"1067.3788"

127.0.0.1:6379> georadius china:city 110.00 30.00 500 km
1) "Chongqin"
2) "Xian"
127.0.0.1:6379> georadius china:city 110.00 30.00 500 km withcoord withdist count 1
1) 1) "Chongqin"
   2) "341.9374"
   3) 1) "106.49999767541885376"
      2) "29.52999957900659211"

127.0.0.1:6379> georadiusbymember china:city Shanghai 400 km
1) "Hangzhou"
2) "Shanghai"

127.0.0.1:6379> geohash china:city Beijing
1) "wx4fbxxfke0"

Use Zset command to operate GEO

127.0.0.1:6379> zrange china:city 0 -1
1) "Chongqin"
2) "Xian"
3) "Shenzhen"
4) "Hangzhou"
5) "Shanghai"
6) "Beijing"

127.0.0.1:6379> zrem china:city Beijing
(integer) 1

2. Hyperloglog base statistics

What is the base?

A {1,3,5,7,8,7}
B {1,3,5,7,8}
cardinality (not repeated elements 1,3,5,7,8) = 5

Hyperloglog is an algorithm for base statistics

Application scenario: The
traditional way of UV of the website (a person visits a website multiple times, only counts as 1 person) , set (unordered and non-repeated collection) saves the user id, counts the number of elements in the set, and saves a large number of users id, compare
the advantages of Hyperloglog that occupies space : the memory occupied is fixed and extremely small.
Note: Hyperloglog has an error rate of about 0.81%

pfadd adds elements to the specified Hyperloglog data structure in the
pfcount statistics Hyperloglog base estimate
pfmerge merges Hyperloglog

127.0.0.1:6379> pfadd mykey a b c d e f g h i j
(integer) 1
127.0.0.1:6379> pfcount mykey
(integer) 10
127.0.0.1:6379> pfadd mykey2 i j z x c v b n m
(integer) 1
127.0.0.1:6379> pfcount mykey2
(integer) 9
127.0.0.1:6379> pfmerge mykey3 mykey mykey2
OK
127.0.0.1:6379> pfcount mykey3
(integer) 15

3. Bitmap

Application scenarios: statistics of user information, login status, check-in records

Record by operating binary bits, non-zero or one-state

Example, record the check-in situation of seven days a week: 1 means check-in, 0 means no check-in

127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 0
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 1
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer) 0
127.0.0.1:6379> setbit sign 6 0
(integer) 0

Check whether the card is clocked in a certain day:

127.0.0.1:6379> getbit sign 4
(integer) 1

Count the number of check-in days in a week:

127.0.0.1:6379> bitcount sign
(integer) 3

Guess you like

Origin blog.csdn.net/BLU_111/article/details/108293010
Recommended