redis特殊資料型別Geo(地理位置)

2021-10-25 15:53:22 字數 3460 閱讀 8285

常用命令

命令說明

geoadd

新增地理位置的座標

geopos

獲取地理位置的座標

geodist

計算兩個位置之間的距離

georadius

根據使用者給定的經緯度座標來獲取指定範圍內的地理位置集合

georadiusbymember

根據儲存在位置集合裡面的某個地點獲取指定範圍內的地理位置集合

geohash

返回乙個或多個位置物件的 geohash 值

示例

127.0.0.1:6379> geoadd city 116.397128 39.916527 beijing

(integer) 1

127.0.0.1:6379> geoadd city 108.93425 34.23053 xian

(integer) 1

127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing

(integer) 1

127.0.0.1:6379> geoadd city 113.27324 23.15792 guangzhou

(integer) 1

127.0.0.1:6379> geoadd city 121.48941 31.40527 shanghai

(integer) 1

127.0.0.1:6379> geoadd city 113.88308 22.55329 shenzhen

(integer) 1

127.0.0.1:6379> geopos city beijing

1) 1) "116.39712899923324585"

2) "39.91652647362980844"

127.0.0.1:6379> geopos city xian nanjing

1) 1) "108.93425256013870239"

2) "34.23053097599082406"

2) 1) "118.89209836721420288"

2) "31.32750976275760735"

127.0.0.1:6379> geodist city beijing nanjing km

"981.4711"

127.0.0.1:6379> geodist city beijing guangzhou km

"1887.0194"

127.0.0.1:6379> georadius city 100 30 1000 km

1) "xian"

127.0.0.1:6379> georadius city 100 30 2000 km

1) "xian"

2) "shenzhen"

3) "guangzhou"

4) "nanjing"

5) "beijing"

127.0.0.1:6379> georadius city 100 30 2000 km withcoord

1) 1) "xian"

2) 1) "108.93425256013870239"

2) "34.23053097599082406"

2) 1) "shenzhen"

2) 1) "113.88307839632034302"

2) "22.55329111565713873"

3) 1) "guangzhou"

2) 1) "113.27324062585830688"

2) "23.1579209662846921"

4) 1) "nanjing"

2) 1) "118.89209836721420288"

2) "31.32750976275760735"

5) 1) "beijing"

2) 1) "116.39712899923324585"

2) "39.91652647362980844"

127.0.0.1:6379> georadius city 100 30 2000 km withdist

1) 1) "xian"

2) "963.6929"

2) 1) "shenzhen"

2) "1611.4036"

3) 1) "guangzhou"

2) "1522.2270"

4) 1) "nanjing"

2) "1811.2832"

5) 1) "beijing"

2) "1852.5507"

127.0.0.1:6379> georadius city 100 30 2000 km withdist count 2

1) 1) "xian"

2) "963.6929"

2) 1) "guangzhou"

2) "1522.2270"

127.0.0.1:6379> georadiusbymember city nanjing 1000 km

1) "nanjing"

2) "shanghai"

3) "beijing"

4) "xian"

127.0.0.1:6379> georadiusbymember city nanjing 1000 km withdist count 3

1) 1) "nanjing"

2) "0.0000"

2) 1) "shanghai"

2) "246.8158"

3) 1) "beijing"

2) "981.4711"

127.0.0.1:6379> geohash city beijing

1) "wx4g0dtf9e0"

127.0.0.1:6379> geohash city guangzhou shenzhen

1) "ws0e9xg09v0"

2) "ws0br3xnkn0"

127.0.0.1:6379> geohash city guangzhou beijing

1) "ws0e9xg09v0"

2) "wx4g0dtf9e0

## geo的底層原理是zset,可以用zset命令來進行操作

127.0.0.1:6379> zrange city 0 -1

1) "xian"

2) "shenzhen"

3) "guangzhou"

4) "nanjing"

5) "shanghai"

6) "beijing"

127.0.0.1:6379> zrem city beijing

(integer) 1

127.0.0.1:6379> zrange city 0 -1

1) "xian"

2) "shenzhen"

3) "guangzhou"

4) "nanjing"

5) "shanghai"

redis特殊型別 GEO

redis目前已經到了3.2版本,3.2版本裡面新增的乙個功能就是對geo 地理位置 的支援 定位 附近的人 搖一搖等。127.0.0.1 6379 geoadd city 116.397128 39.916527 北京 116.23128 40.22077 上海 121.48941 31.4052...

redis特殊資料型別 Hyperloglog

獲取集合中的不重複元素 基數 不重複的元素 a 1,2,3,4,5,6,5 b 1,2,3,4,5 基數的的個數 6 redis 2.8.9版本就更新出了hyperloglog資料型別,基數統計的演算法,優點 占用的記憶體是固定的,2 64不同元素的計數,只需要12k的記憶體!從記憶體角度,hype...

特殊資料型別

1 結構型別 類似於c 中的struct 例如 patient.name xy patient.billing 130.12 patient.test 78 23 34 34 234 234 433 567 patient patient name xy billing 130.1200 test ...