04Redis--three special data types

content

geospatial

(1) geoadd adds address location

(2) geodist distance between two people

(3) geopos gets the current positioning: it must be a coordinate value

(4) georadius takes the given latitude and longitude as the center, and finds the element of a certain radius

(5) georadiusbymember finds other elements around the specified element

(6) geohash returns the geohash representation of one or more position elements 

View all element information in the map

Remove the element pointed to in the map

Hyperloglog cardinality statistics

(1) Create pfadd

(2) Merge pferage

(3) View pfcount

bitmap (bit storage) two states

(1) Setting value

(2) View a specific value

(3) Statistical operation, count the number of days of punching


geospatial

Friends positioning, nearby people, taxi distance technology, this function can calculate geographic location information, the distance between two places, and people within a radius of a few miles!

Redis' Geo was launched in reids 3.2, and
some test data can be queried: latitude and longitude query - coordinate picking system

(1) geoadd adds address location

Rules : Two levels cannot be added directly. We generally download city data and import it directly through the java program!

参数 key 值 (经度、纬度、名称)
127.0.0.1:6379> geoadd china:city 116 39 beijing
(integer) 1
127.0.0.1:6379> geoadd china:city 121 31 shanghai
(integer) 1
127.0.0.1:6379> geoadd china:city 120 30 hangzhou
(integer) 1
127.0.0.1:6379> geoadd china:city 113 22 shenzhen
(integer) 1
127.0.0.1:6379> geoadd china:city 117 39 tianjin
(integer) 1
127.0.0.1:6379> geoadd china:city 117 21212 tianjin   # 错误添加数值 超过范围
(error) ERR invalid longitude,latitude pair 117.000000,21212.000000

(2) geodist distance between two people

Units: m, km, mi miles, ft feet

127.0.0.1:6379> geodist china:city beijing shanghai km # 查看北京到上海的直线距离
"999.2077"
127.0.0.1:6379> geodist china:city shenzhen tianjin km # 查看深圳到天津的直线距离
"1928.7377"

(3) geopos gets the current positioning: it must be a coordinate value

127.0.0.1:6379> geopos china:city shenzhen  # 获取制定城市的精度和维度
1) 1) "112.99999862909317017"
   2) "21.99999950739083232"
127.0.0.1:6379> geopos china:city beijing
1) 1) "116.00000113248825073"
   2) "38.99999918434559731"

(4) georadius takes the given latitude and longitude as the center, and finds the element of a certain radius

127.0.0.1:6379> zrange china:city 0 -1   # 获取全部的地图中元素
1) "shenzhen"
2) "hangzhou"
3) "shanghai"
4) "beijing"
5) "tianjin"
127.0.0.1:6379> GEORADIUS china:city 110 30 2000 km # 查看 以 100 30 为中心 2000 为半径 周围内的城市
1) "shenzhen"
2) "hangzhou"
3) "shanghai"
4) "beijing"
5) "tianjin"
127.0.0.1:6379> GEORADIUS china:city 110 30 1000 km # 查看 以 100 30 为中心 1000 为半径 周围内的城市
1) "shenzhen"
2) "hangzhou"
127.0.0.1:6379> georadius china:city 110 30 1000 km withdist # 查看 以 100 30 为中心 1000 为半径 周围内的城市 并且显示 到中间的距离位置
1) 1) "shenzhen"
   2) "938.8574"
2) 1) "hangzhou"
   2) "962.9418"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord # 显示他人的定位信息
1) 1) "shenzhen"
   2) 1) "112.99999862909317017"
      2) "21.99999950739083232"
2) 1) "hangzhou"
   2) 1) "120.00000089406967163"
      2) "30.00000024997701331"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord count 1 # 筛选出指定的结果
1) 1) "shenzhen"
   2) 1) "112.99999862909317017"
      2) "21.99999950739083232"
127.0.0.1:6379> georadius china:city 110 30 1000 km withcoord count 2
1) 1) "shenzhen"
   2) 1) "112.99999862909317017"
      2) "21.99999950739083232"
2) 1) "hangzhou"
   2) 1) "120.00000089406967163"
      2) "30.00000024997701331"

(5) georadiusbymember finds other elements around the specified element

127.0.0.1:6379> GEORADIUSBYMEMBER china:city shenzhen 1000 km
1) "shenzhen"
127.0.0.1:6379> GEORADIUSBYMEMBER china:city hangzhou 1000 km
1) "hangzhou"
2) "shanghai"

(6) geohash returns the geohash representation of one or more position elements 

The command will return 11 strings Geohash strings

127.0.0.1:6379> geohash china:city hangzhou beijing
1) "wtm6dtm6dt0"
2) "wwfmzesx7y0"

 The underlying implementation principle of GEO is actually Zset! We can use the Zset command to operate geo

View all element information in the map

Remove the element pointed to in the map

127.0.0.1:6379> zrange china:city 0 -1  # 查看地图中所有的元素信息
1) "shenzhen"
2) "hangzhou"
3) "shanghai"
4) "beijing"
5) "tianjin"
127.0.0.1:6379> zrem china:city beijing  # 移除地图中指的元素
(integer) 1
127.0.0.1:6379> zrem china:city shenzhen 
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "hangzhou"
2) "shanghai"
3) "tianjin"

Hyperloglog cardinality statistics

what is cardinality

a{1, 3, 5, 7, 8, 7}
b{1, 2, 5, 7, 8}
cardinality (non-repeating elements) = 5, the error is acceptable

Redis hyperloglog cardinality statistics algorithm
Advantages: The memory occupation is fixed, 2^64 different elements count, only 12kb of memory is required, if you want to compare the uv of the hyperloglog preferred
web page from the memory point of view (one person visits a website many times, but still counts In the
traditional way, the set protects the user's id, and its elements are not repeated, and then the number of elements in the set can be counted as a standard to judge
this method. If a large number of user ids are maintained, it will be troublesome. Our purpose is to count, instead of saving
the user id; 0.81% error rate , acceptable

(1) Create pfadd

(2) Merge pferage

(3) View pfcount

127.0.0.1:6379> pfadd mykey a s d f g h z x c v # 创建第一组元素 mykey
(integer) 1
127.0.0.1:6379> pfcount mykey # 统计 mykey 元素的基数数量
(integer) 10
127.0.0.1:6379> pfadd mykey2 q w e r t y z x c v # 创建第二组元素 mykey2
(integer) 1
127.0.0.1:6379> pfcount mykey2
(integer) 10
127.0.0.1:6379> pfmerge mykey3 mykey mykey2 # 合并两组 mykey mykey2 -> mykey3 并集
OK
127.0.0.1:6379> pfcount mykey3 # 查看并集的数量
(integer) 15

If fault-tolerant statistics are allowed, hyperloglog can be used
if set or other data types are not allowed

bitmap (bit storage) two states

bit storage


Statistics of user information, active, inactive, login, for login, check-in, 365 check-in! Both states can use bitmaps
bitmaps bitmaps , data structures are all recorded by operating binary bits, there are only two states of 0
and 1, 365=365 bit 1 byte=8 bit 46 bytes or so!

Test
Record Monday to Sunday punching
Monday: 1, Tuesday: 1, Wednesday: 0 Thursday 0 Friday 1 Saturday 1 Sunday 1

(1) Setting value

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

(2) View a specific value

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

(3) Statistical operation, count the number of days of punching

127.0.0.1:6379> bitcount sing  # 统计这周打卡的记录,查看是否有全勤
(integer) 5

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324120304&siteId=291194637
Recommended