Home  >  Article  >  Database  >  An in-depth analysis of the three special data types in redis

An in-depth analysis of the three special data types in redis

青灯夜游
青灯夜游forward
2021-08-24 10:57:102273browse

This article will take you through the three special data types in redis. Interested friends can learn about it~

An in-depth analysis of the three special data types in redis

Three special types of redis Data type

  • Geospatial Geographic location

  • Hyperloglog Cardinality statistics

  • ##Bitmap bit Picture scene

[Related recommendations:

Redis video tutorial]

Geospatial Geographical location

redis Version 3.2 was launched Geospatial

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

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

An in-depth analysis of the three special data types in redis

Geospatial can be used in the following scenarios:

    People nearby
  • Taking a taxi to calculate distance
  • Friend positioning
  • Waiting for a series of scenarios related to positioning
Geospatial has only six commands

  • GEOADD
  • GEODIST
  • GEOHASH
  • GEOPOS
  • GEORADIUS
  • GEORADIUSBYMEMBER
##GEOADD

Add geolocation

Valid longitudes range from -180 degrees to 180 degrees.
  • Valid latitudes range from -85.05112878 degrees to 85.05112878 degrees.
  • When the coordinate position exceeds the above specified range, this command will return an error.

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 the following 4 units:

m : meter

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 11 characters Geohash characters 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 longitude and latitude 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 the cities located 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 implemented using Zset ordered collection

, we can use Zset ordered collection To operate Geo, we use the Zset command 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 Cardinality?

The base number is a non-repeating number, for example:

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

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

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

Introduction

Hyperloglog is redis 2.8.9 This kind of data structure is available at the beginning of version

redis hyperloglog cardinality statistics is also an algorithm

For example, there is such an application scenario:

    Number of visitors to the web page According to statistics, if the same user visits the website multiple times, it will only be counted as 1
  • The traditional way is to use a set to save the ID, and 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 get the id, we only need to count

  • Advantages

## Hyperloglog occupies a fixed memory space, 2^16 power, and only requires 12 KB of memory. From the perspective of memory, Hyperloglog is the first choice for technology selection.An in-depth analysis of the three special data types in redis

PFADD key element [element ...]

  • Add one or more elements to Hyperloglog
    • PFMERGE destkey sourcekey [sourcekey ...]

    将多个 Hyperloglog  取并集,得到一个结果 Hyperloglog ,里面的数据是不会重复的

    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 位图场景

    Bitmaps 位图,位存储

    一般用于,统计用户信息,活跃,不活跃,

    登录,不登录

    打卡,不打卡 等等,两种状态

    Bitmaps 位图,也是一种数据结构,是操作二进制的方式来进行记录的,只有 0  和 1 两种状态

    An in-depth analysis of the three special data types in redis

    例如我们举个例子,来记录着一周每天的心情,1 是开心,0 是沮丧

    • SETBIT key offset value

    设置 bit 位的值

    • GETBIT key offset

    获取 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

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

The above is the detailed content of An in-depth analysis of the three special data types in redis. 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