Home  >  Article  >  Database  >  In-depth understanding of special data types in Redis: cardinality statistics, bitmaps, geographical location

In-depth understanding of special data types in Redis: cardinality statistics, bitmaps, geographical location

青灯夜游
青灯夜游forward
2021-12-22 09:59:402442browse

This article will take you through the three special data types in Redis (cardinality statistics, bitmaps, geographical location), I hope it will be helpful to you!

In-depth understanding of special data types in Redis: cardinality statistics, bitmaps, geographical location

In addition to the 5 basic data types, Redis also has three special data types, namely HyperLogLogs (cardinality statistics), Bitmaps (bitmap) and geospatial (geographic location). [Related recommendations: Redis video tutorial]

HyperLogLogs (cardinality statistics)

Redis version 2.8.9 has updated the Hyperloglog data structure!

  • What is a base number?

For example, A = {1, 2, 3, 4, 5}, B = {3, 5, 6, 7, 9}; Then the cardinality (non-repeating elements) = 1, 2, 4, 6, 7, 9; (Fault tolerance is allowed, that is, a certain error can be accepted)

  • HyperLogLogs What problems are cardinality statistics used to solve?

This structure can be very memory-saving to count various counts, such as the number of registered IPs, the number of daily visited IPs, the real-time UV of the page, the number of online users, and the number of common friends. wait.

  • #What are its advantages?

For a large website, for example, there are 1 million IPs every day. Roughly calculating that one IP consumes 15 bytes, then 1 million IPs is 15M. Each key in HyperLogLog occupies 12K of content in Redis, and the theoretical storage is approximately close to 2^64 values. No matter what the stored content is, it is an algorithm based on cardinality estimation, which can only estimate the cardinality more accurately. Use a small amount of fixed memory to store and identify unique elements in a collection. Moreover, the base of this estimate is not necessarily accurate. It is an approximation with a standard error of 0.81% (for business scenarios that can accept a certain error tolerance, such as IP number statistics, UV, etc., this can be ignored).

  • Related commands use

127.0.0.1:6379> pfadd key1 a b c d e f g h i	# 创建第一组元素
(integer) 1
127.0.0.1:6379> pfcount key1					# 统计元素的基数数量
(integer) 9
127.0.0.1:6379> pfadd key2 c j k l m e g a		# 创建第二组元素
(integer) 1
127.0.0.1:6379> pfcount key2
(integer) 8
127.0.0.1:6379> pfmerge key3 key1 key2			# 合并两组:key1 key2 -> key3 并集
OK
127.0.0.1:6379> pfcount key3
(integer) 13

Bitmap (bit storage)

Bitmap is a bitmap data structure, which operates on binary bits for recording, and has only two states: 0 and 1.

  • What problem is it used to solve?

For example: Statistics of user information, active, inactive! Login, not logged in! Check in, don’t check in! Bitmaps can be used in both states!

How much memory is needed to store one year's check-in status? 365 days = 365 bit 1 byte = 8bit 46 bytes or so!

  • Related commands are used

Use bitmap to record the check-in from Monday to Sunday! Monday: 1 Tuesday: 0 Wednesday: 0 Thursday: 1...

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 1
(integer) 0
127.0.0.1:6379> setbit sign 4 0
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer) 0
127.0.0.1:6379> setbit sign 6 1
(integer) 0

Check whether there is a check-in on a certain day!

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

Statistical operation, count the number of days clocked in!

127.0.0.1:6379> bitcount sign # 统计这周的打卡记录,就可以看到是否有全勤!
(integer) 3

geospatial (geographic location)

Redis’ Geo was launched in Redis 3.2 version! This function can calculate geographical location information: the distance between two places, the radius People from miles away.

geoadd

Add geolocation

127.0.0.1:6379> geoadd china:city 118.76 32.04 manjing 112.55 37.86 taiyuan 123.43 41.80 shenyang
(integer) 3
127.0.0.1:6379> geoadd china:city 144.05 22.52 shengzhen 120.16 30.24 hangzhou 108.96 34.26 xian
(integer) 3

Rules

Two levels cannot be added directly. We usually download city data (you can query GEO at this website: http://www.jsons.cn/lngcode)!

  • Valid longitudes range from -180 degrees to 180 degrees.
  • Valid latitudes range from -85.05112878 degrees to 85.05112878 degrees.
# 当坐标位置超出上述指定范围时,该命令将会返回一个错误。
127.0.0.1:6379> geoadd china:city 39.90 116.40 beijin
(error) ERR invalid longitude,latitude pair 39.900000,116.400000

geopos

Get the longitude and latitude of the specified member

127.0.0.1:6379> geopos china:city taiyuan manjing
1) 1) "112.54999905824661255"
   1) "37.86000073876942196"
2) 1) "118.75999957323074341"
   1) "32.03999960287850968"

Get the current positioning, which must be a coordinate value!

geodist

If it does not exist, return empty

Units are as follows

  • m
  • km
  • mi miles
  • ft feet
127.0.0.1:6379> geodist china:city taiyuan shenyang m
"1026439.1070"
127.0.0.1:6379> geodist china:city taiyuan shenyang km
"1026.4391"

georadius

##Nearby people==> Get the address, location, and radius of all nearby people

Get the specified number of people

127.0.0.1:6379> georadius china:city 110 30 1000 km			以 100,30 这个坐标为中心, 寻找半径为1000km的城市
1) "xian"
2) "hangzhou"
3) "manjing"
4) "taiyuan"
127.0.0.1:6379> georadius china:city 110 30 500 km
1) "xian"
127.0.0.1:6379> georadius china:city 110 30 500 km withdist
1) 1) "xian"
   2) "483.8340"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord withdist count 2
1) 1) "xian"
   2) "483.8340"
   3) 1) "108.96000176668167114"
      2) "34.25999964418929977"
2) 1) "manjing"
   2) "864.9816"
   3) 1) "118.75999957323074341"
      2) "32.03999960287850968"

Parameter key longitude latitude radius unit [longitude and latitude of displayed results] [distance of displayed results] [number of displayed results]

georadiusbymember

Display other members within a certain radius of the specified member

127.0.0.1:6379> georadiusbymember china:city taiyuan 1000 km
1) "manjing"
2) "taiyuan"
3) "xian"
127.0.0.1:6379> georadiusbymember china:city taiyuan 1000 km withcoord withdist count 2
1) 1) "taiyuan"
   2) "0.0000"
   3) 1) "112.54999905824661255"
      2) "37.86000073876942196"
2) 1) "xian"
   2) "514.2264"
   3) 1) "108.96000176668167114"
      2) "34.25999964418929977"
The parameters are the same as georadius

geohash(less used)

This command returns an 11-character hash string

127.0.0.1:6379> geohash china:city taiyuan shenyang
1) "ww8p3hhqmp0"
2) "wxrvb9qyxk0"
Convert the two-dimensional longitude and latitude Convert to a one-dimensional string. If the two strings are closer, the closer the distance is

Underlying

geo底层的实现原理实际上就是Zset, 我们可以通过Zset命令来操作geo

127.0.0.1:6379> type china:city
zset

查看全部元素 删除指定的元素

127.0.0.1:6379> zrange china:city 0 -1 withscores
 1) "xian"
 2) "4040115445396757"
 3) "hangzhou"
 4) "4054133997236782"
 5) "manjing"
 6) "4066006694128997"
 7) "taiyuan"
 8) "4068216047500484"
 9) "shenyang"
1)  "4072519231994779"
2)  "shengzhen"
3)  "4154606886655324"
127.0.0.1:6379> zrem china:city manjing
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "xian"
2) "hangzhou"
3) "taiyuan"
4) "shenyang"
5) "shengzhen"

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of In-depth understanding of special data types in Redis: cardinality statistics, bitmaps, geographical location. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete