Three special data types for Redis introduction

geospatial geographic location

Location of friends, nearby people, taxi distance calculation

Redis's Geo is in redis3 Version 2 was launched. This function can calculate the geographical location information, the distance between the two places and the people within a few miles.

You can query some test data: http://www.jsons.cn/lngcodeinfo/0706D99C19A781A3

 

There are only six commands

 GEOADD

 http://www.redis.cn/commands/geoadd.html

geoadd adds the specified geospatial location (latitude, longitude, name) to the specified key
Rules cannot be directly added at two levels. We generally import Huixia's urban data directly through java programs at one time
The effective longitude is from - 180 degrees to 180 degrees
The effective latitude is from -85.05112878 degrees to 85.05112878 degrees
When the coordinate position exceeds the above specified range, the command will return an error.

Parameter key value ()

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 123.42 41.79 liaoning
(integer) 1
127.0.0.1:6379> GEOADD china:city 121.61 38.91 dalian 114.05 22.52 shenzhen
(integer) 2
127.0.0.1:6379> GEOADD china:city 120.12 30.25 hangzhou 104.06 30.65 chengdu
(integer) 2

GEOPOS

http://www.redis.cn/commands/geopos.html

127.0.0.1:6379> GEOPOS china:city beijing    # Gets the longitude and latitude of the specified city
1) 1) "116.39999896287918091"
   2) "39.90000009167092543"
127.0.0.1:6379> GEOPOS china:city dalian hangzhou
1) 1) "121.61000221967697144"
   2) "38.90999884014095045"
2) 1) "120.11999756097793579"
   2) "30.24999979792261939"

GEODIST

http://www.redis.cn/commands/geodist.html

Return the positions (longitude and latitude) of all the given positioning elements from the key.

Returns the distance between two given positions.

If one of the two locations does not exist, the command returns a null value.

The parameter unit of the specified unit must be one of the following units:

  • m , is expressed in meters.
  • km is expressed in kilometers.
  • mi = in miles.
  • ft is in feet.
127.0.0.1:6379> GEODIST china:city beijing dalian km    # View the straight-line distance from Beijing to Dalian in km
"461.0231"
127.0.0.1:6379> GEODIST china:city hangzhou dalian km
"972.7919"

GEOREDIUS

http://www.redis.cn/commands/georadius.html

Take the given longitude and latitude as the center, and return all position elements whose distance from the center does not exceed the given maximum distance among the position elements contained in the key.

The range can use one of the following units:

  • m is in meters.
  • km is expressed in kilometers.
  • mi = in miles.
  • ft is in feet.
127.0.0.1:6379> GEORADIUS china:city 110 30 1500 km    #With the longitude and latitude of 110 and 30 as the center, look for cities within a radius of 1500km
1) "chengdu"
2) "shenzhen"
3) "hangzhou"
4) "shanghai"
5) "beijing"
6) "dalian"
127.0.0.1:6379> GEORADIUS china:city 110 30 1500 km withdist    # Displays the straight-line distance to the center
1) 1) "chengdu"
   2) "574.7802"
2) 1) "shenzhen"
   2) "924.6408"
3) 1) "hangzhou"
   2) "973.6527"
4) 1) "shanghai"
   2) "1105.9098"
5) 1) "beijing"
   2) "1245.2858"
6) 1) "dalian"
   2) "1452.2961"
127.0.0.1:6379> GEORADIUS china:city 110 30 1500 km withcoord    # Shows the longitude and latitude of the city
1) 1) "chengdu"
   2) 1) "104.05999749898910522"
      2) "30.6499990746355806"
2) 1) "shenzhen"
   2) 1) "114.04999762773513794"
      2) "22.5200000879503861"
3) 1) "hangzhou"
   2) 1) "120.11999756097793579"
      2) "30.24999979792261939"
4) 1) "shanghai"
   2) 1) "121.47000163793563843"
      2) "31.22999903975783553"
5) 1) "beijing"
   2) 1) "116.39999896287918091"
      2) "39.90000009167092543"
6) 1) "dalian"
   2) 1) "121.61000221967697144"
      2) "38.90999884014095045"
127.0.0.1:6379> GEORADIUS china:city 110 30 1500 km withdist withcoord count 1 # Filter out the specified results
1) 1) "chengdu"
   2) "574.7802"
   3) 1) "104.05999749898910522"
      2) "30.6499990746355806"
127.0.0.1:6379> GEORADIUS china:city 110 30 1500 km withdist withcoord count 2
1) 1) "chengdu"
   2) "574.7802"
   3) 1) "104.05999749898910522"
      2) "30.6499990746355806"
2) 1) "shenzhen"
   2) "924.6408"
   3) 1) "114.04999762773513794"
      2) "22.5200000879503861"

GEORADIUSBYMEMBER

http://www.redis.cn/commands/georadiusbymember.html 

127.0.0.1:6379> GEORADIUSBYMEMBER china:city beijing 1500 km    # Displays the city within the specified city radius
1) "hangzhou"
2) "shanghai"
3) "beijing"
4) "dalian"
5) "liaoning"
127.0.0.1:6379> GEORADIUSBYMEMBER china:city beijing 500 km
1) "beijing"
2) "dalian"

GEOHASH

http://www.redis.cn/commands/geohash.html 

Returns the of one or more location elements Geohash Indicates.

# Convert the two-dimensional latitude and longitude into one-dimensional string. The closer the two strings are, the closer the distance is
127.0.0.1:6379> GEOHASH china:city beijing dalian
1) "wx4fbxxfke0"
2) "wwymr73enm0"

The underlying implementation of geo is actually zset. We can use the zset command to operate geo

127.0.0.1:6379> ZRANGE china:city 0 -1    # View all elements
1) "chengdu"
2) "shenzhen"
3) "hangzhou"
4) "shanghai"
5) "beijing"
6) "dalian"
7) "liaoning"
127.0.0.1:6379> ZREM china:city liaoning    # Removes the specified element
(integer) 1
127.0.0.1:6379> ZRANGE china:city 0 -1
1) "chengdu"
2) "shenzhen"
3) "hangzhou"
4) "shanghai"
5) "beijing"
6) "dalian"

Hyperloglog

brief introduction

Redis version 2.8.9 updates the Hyperloglog data structure

Redis Hyperloglog cardinality statistics algorithm

Advantages: the occupied memory is fixed. 2 ^ 64 different cardinality elements only need to waste 12kb of memory. If you want to compare from a memory perspective, Hyperloglog is preferred

UV of web page (a person visits a website many times, but still counts as a person)

In the way of pain, set saves the user's id, and the number of elements in set can be counted as the standard judgment

If you save a large number of user IDs in this way, it will be more troublesome. Our purpose is to count, not save user IDs

0.81% error rate, statistical UV tasks, negligible

Test use

127.0.0.1:6379> PFADD mykey a b c d e f g h i j    # Create the first set of elements mykey
(integer) 1
127.0.0.1:6379> PFCOUNT mykey    # Count the cardinality quantity in mykey
(integer) 10
127.0.0.1:6379> PFADD mykey2 i j z x c v b n m     # Create a second set of elements mykey2
(integer) 1
127.0.0.1:6379> PFCOUNT mykey2
(integer) 9
127.0.0.1:6379> PFMERGE mykey3 mykey mykey2    # Merge two groups of MyKey mykey2 = > mykey3 Union
OK
127.0.0.1:6379> PFCOUNT mykey3    # Look at the number of union sets
(integer) 15

If fault tolerance is allowed, Hyperloglog must be used

If fault tolerance is not allowed, you can use set or your own data type

Bitmaps

Bit storage

Count user information, active, inactive, logged in, not logged in, punch in and 365 punch out.

Bitmaps can be used for both statuses

Bitmaps, bitmaps, data structures. Both operate binary bits to record, that is, there are two states: 0 and 1

365 days = 365bit # 1 byte = 8bit # about 46 bytes

Use bitmap to record clock outs from Monday to Sunday

Monday: 1 Tuesday: 0 Wednesday: 0

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 there is a clock out on a certain day

127.0.0.1:6379> getbit sign 3
(integer) 1
127.0.0.1:6379> getbit sign 6
(integer) 0

Statistics operation: count the number of days clocked in

127.0.0.1:6379> BITCOUNT sign    # Count the number of clocks in this week
(integer) 3

Keywords: Redis

Added by binujayaraj on Sun, 16 Jan 2022 22:48:36 +0200