头图

[Redis series] redis learning five, learn more about three special data types of redis

Three special data types of redis

  • Geospatial
  • Hyperloglog cardinality statistics
  • Bitmap bitmap scene

Geospatial

Geospatial was launched in version 3.2 of redis

You can see the usage in detail on the official documentation:

https://www.redis.net.cn/order/3685.html

Geospatial can be used in the following scenarios:

  • People nearby
  • Calculate distance by taxi
  • friend location
  • and a series of scenes related to positioning

Geospatial has only six commands

GEOADD

Add geolocation

  • Valid longitudes are from -180 degrees to 180 degrees.
  • Valid latitudes are from -85.05112878 degrees to 85.05112878 degrees.

The command will return an error when the coordinate position exceeds the above specified range.

  • GEOADD key [NX|XX] [CH] longitude latitude member [longitude latitude member ...]

Add latitude and longitude, city name

127.0.0.1:6379> GEOADD city 113.087559 28.251818 changsha
(integer) 1
127.0.0.1:6379> GEOADD city 114.064552 22.548457 shenzhen
(integer) 1
127.0.0.1:6379> GEOADD city 104.087045 30.666416 chengdu
(integer) 1
127.0.0.1:6379> GEOADD city 118.802422 32.064653 nanjing
(integer) 1
127.0.0.1:6379> GEOADD city 106.558434 29.568996 chongqing
(integer) 1
127.0.0.1:6379> GEOADD city 121.463615 31.195908 shanghai
(integer) 1
127.0.0.1:6379> GEOADD city 117.208093 39.091103 tianjin
(integer) 1

GEOPOS

  • GEOPOS key member [member ...]

Get the latitude and longitude information of the specified city

127.0.0.1:6379> GEOPOS city changsha
1) 1) "113.08755666017532349"
   2) "28.25181827470789386"
127.0.0.1:6379> GEOPOS city tianjin
1) 1) "117.20809489488601685"
   2) "39.0911021322545551"

GEODIST

  • GEODIST key member1 member2 [m|km|ft|mi]

There are 4 units as follows:

m : meters

km: kilometers

ft: feet

mi: miles

Get the distance between two cities

127.0.0.1:6379> GEODIST city changsha tianjin
"1264101.6876"
127.0.0.1:6379> GEODIST city changsha tianjin km
"1264.1017"
127.0.0.1:6379> GEODIST city changsha shenzhen km
"641.9034"

GEOHASH

  • GEOHASH key member [member ...]

Returns one or more elements represented by GEOHASH, returns an 11-character Geohash string

127.0.0.1:6379> GEOHASH city changsha
1) "wt02tr5fg00"
127.0.0.1:6379> GEOHASH city changsha beijing
1) "wt02tr5fg00"
2) (nil)
127.0.0.1:6379> GEOHASH city changsha beijing tianjin chongqing
1) "wt02tr5fg00"
2) (nil)
3) "wwgq7hk64t0"
4) "wm7b0yc7zk0"

GEORADIUS

  • GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOR

Specify the latitude and longitude as the origin, specify the radius, and query the cities within the specified range, these cities are all in our own collection

127.0.0.1:6379> GEORADIUS city 110 30 500 m
(empty array)
127.0.0.1:6379> GEORADIUS city 110 30 500 km
1) "chongqing"
2) "changsha"
127.0.0.1:6379> GEORADIUS city 110 30 1000 km
1) "chongqing"
2) "chengdu"
3) "shenzhen"
4) "changsha"
5) "nanjing"
127.0.0.1:6379> GEORADIUS city 110 30 700 km withcoord
1) 1) "chongqing"
   2) 1) "106.55843228101730347"
      2) "29.56899626404301529"
2) 1) "chengdu"
   2) 1) "104.08704489469528198"
      2) "30.6664164635846177"
3) 1) "changsha"
   2) 1) "113.08755666017532349"
      2) "28.25181827470789386"
127.0.0.1:6379> GEORADIUS city 110 30 700 km withdist
1) 1) "chongqing"
   2) "335.6530"
2) 1) "chengdu"
   2) "572.3911"
3) 1) "changsha"
   2) "357.4711"
127.0.0.1:6379> GEORADIUS city 110 30 700 km withhash
1) 1) "chongqing"
   2) (integer) 4026059435699931
2) 1) "chengdu"
   2) (integer) 4026137831798506
3) 1) "changsha"
   2) (integer) 4050903792284309

GEORADIUSBYMEMBER

  • GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count [ANY]] [ASC|DESC] [STORE key] [STOREDIST key]

Find cities around the specified element

127.0.0.1:6379> GEORADIUSBYMEMBER city tianjin 1000 km
1) "nanjing"
2) "tianjin"
3) "shanghai"
127.0.0.1:6379> GEORADIUSBYMEMBER city tianjin 500 km
1) "tianjin"
127.0.0.1:6379> GEORADIUSBYMEMBER city tianjin 5000 km
1) "chongqing"
2) "chengdu"
3) "shenzhen"
4) "changsha"
5) "shanghai"
6) "nanjing"
7) "tianjin"

The underlying principle of Geospatial is 1620a5382ba0e4 implemented by using ordered set. We can use the commands of Zset ordered set to operate Geo. We use Zset commands to experience a wave

127.0.0.1:6379> ZRANGE city 0 -1
1) "chongqing"
2) "chengdu"
3) "shenzhen"
4) "changsha"
5) "shanghai"
6) "nanjing"
7) "tianjin"
127.0.0.1:6379> ZCARD city
(integer) 7

Hyperloglog cardinality statistics

What is the radix of ?

A base is a number that does not repeat, for example:

A = {1,2,3,4,5}

B = {2,3,4,5,6}

Then the cardinality of the union of A and B is 6

Hyperloglog is a data structure that has been available since redis 2.8.9

Redis hyperloglog cardinality statistics is also an algorithm

For example, there are such application scenarios:

  • Statistics on the number of web page visitors, the same user visits the website multiple times, it is only counted as 1

    The traditional way is to use the set collection method to save the id, count the number of ids in the set, to calculate the number of people

    There is no problem with this method, but if the amount of data is large, it will be very troublesome, because it is useless for us to take the id, we only need to count

Advantage

​ The memory space occupied by Hyperloglog is fixed, 2^16 power, only needs to occupy 12 KB of memory. From the perspective of memory, Hyperloglog is the first choice.

  • PFADD key element [element ...]

Add one or more elements to Hyperloglog

  • PFMERGE destkey sourcekey [sourcekey ...]

Take the union of multiple Hyperloglogs to get a result Hyperloglog, the data in it will not be repeated

127.0.0.1:6379> PFADD myhash  1 2 3 4 5 6 7 8
(integer) 1
127.0.0.1:6379> pfadd myhash2 3 4 5 6 7 8 9 0 88 99
(integer) 1
127.0.0.1:6379> PFMERGE res myhash myhash2
OK
127.0.0.1:6379> PFCOUNT res
(integer) 12
127.0.0.1:6379> PFCOUNT myhash
(integer) 8
127.0.0.1:6379> PFCOUNT myhash2
(integer) 10

Bitmaps Bitmap scene

Bitmaps bitmap, bit storage

Generally used for statistical user information, active, inactive,

log in, don't log in

Punching, not punching, etc., two states

Bitmaps Bitmap is also a data structure, which is recorded by operating binary. has only two states of 0 and 1.

For example, let's take an example to record the mood of each day of the week, 1 is happy, 0 is depression

  • SETBIT key offset value

set bit value

  • GETBIT key offset

Get the value of a bit

127.0.0.1:6379> SETBIT week 0 1
(integer) 0
127.0.0.1:6379> SETBIT week 1  1
(integer) 0
127.0.0.1:6379> SETBIT week 2 1
(integer) 0
127.0.0.1:6379> SETBIT week 3 0
(integer) 0
127.0.0.1:6379> SETBIT week 4 0
(integer) 0
127.0.0.1:6379> SETBIT week 5 1
(integer) 0
127.0.0.1:6379> SETBIT week 6 1
(integer) 0
127.0.0.1:6379> GETBIT week 6
(integer) 1
127.0.0.1:6379> GETBIT week 5
(integer) 1

Welcome to like, follow, favorite

Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality

Okay, here it is this time

Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.

I am little devil boy Nezha , welcome to like, follow and favorite, see you next time~


阿兵云原生
192 声望37 粉丝