在上一篇文章中我们讲了五大基本类型,他们是最常用的,而且百分之九十的程序员对于Redis只限于了解String这种最常用的。但是我个人认为,既然Redis官方提供了其他的数据类型,肯定是有相应的考量的,在某些特殊的业务场景中,这些特殊的类型还是能够给我们多一种解决思路!那么,这个学习就是值得的!!!接下来我们就来讲一讲三大特殊类型。
Geospatial: 地理位置
城市经纬度查询:
注意点1:两极无法直接添加,我们一般会下载城市数据,直接通过java程序一次性导入!
注意点2:有效的经度从-180度到180度。
注意点3:有效的纬度从-85.05112878度到85.05112878度。
注意点4:m 为米。km 为千米。mi 为英里。ft 为英尺。
①geoadd(添加)、geopos(查看)、geodist(计算距离)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| 127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing 197.30794 31.79322
(error) ERR syntax error. Try GEOADD key [x1] [y1] [name1] [x2] [y2] [name2] ...
127.0.0.1:6379> geoadd city 118.8921 31.32751 nanjing 117.30794 31.79322 hefei 102.82147 24.88554 kunming 91.13775 29.65262 lasa 116.23128 40.22077 beijing 106.54041 29.40268 chongqing (integer) 6 127.0.0.1:6379> ZRANGE city 0 -1
1) "lasa" 2) "kunming" 3) "chongqing" 4) "hefei" 5) "nanjing" 6) "beijing" 127.0.0.1:6379> geopos city nanjing 1) 1) "118.89209836721420288" 2) "31.32750976275760735" 127.0.0.1:6379> geopos city nanjing beijing 1) 1) "118.89209836721420288" 2) "31.32750976275760735" 2) 1) "116.23128265142440796" 2) "40.22076905438526495" 127.0.0.1:6379> geodist city nanjing beijing "1017743.1413" 127.0.0.1:6379> geodist city nanjing beijing km "1017.7431" 127.0.0.1:6379> geodist city nanjing beijing mi "632.3978" 127.0.0.1:6379> geodist city nanjing beijing ft "3339052.3010"
|
②georadius(查询附近位置)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| 127.0.0.1:6379> ZRANGE city 0 -1 1) "lasa" 2) "kunming" 3) "chongqing" 4) "hefei" 5) "nanjing" 6) "beijing"
127.0.0.1:6379> georadius city 120 38 1000 km 1) "beijing" 2) "hefei" 3) "nanjing" 127.0.0.1:6379> georadius city 120 38 400 km (empty array) 127.0.0.1:6379> georadius city 120 38 550 km 1) "beijing"
127.0.0.1:6379> georadius city 120 38 1000 km withcoord 1) 1) "beijing" 2) 1) "116.23128265142440796" 2) "40.22076905438526495" 2) 1) "hefei" 2) 1) "117.30793744325637817" 2) "31.79321915080526395" 3) 1) "nanjing" 2) 1) "118.89209836721420288" 2) "31.32750976275760735"
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist 1) 1) "beijing" 2) "408.3496" 3) 1) "116.23128265142440796" 2) "40.22076905438526495" 2) 1) "hefei" 2) "732.6371" 3) 1) "117.30793744325637817" 2) "31.79321915080526395" 3) 1) "nanjing" 2) "749.0265" 3) 1) "118.89209836721420288" 2) "31.32750976275760735"
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist withhash 1) 1) "beijing" 2) "408.3496" 3) (integer) 4069896088584598 4) 1) "116.23128265142440796" 2) "40.22076905438526495" 2) 1) "hefei" 2) "732.6371" 3) (integer) 4052763834193093 4) 1) "117.30793744325637817" 2) "31.79321915080526395" 3) 1) "nanjing" 2) "749.0265" 3) (integer) 4054278565840695 4) 1) "118.89209836721420288" 2) "31.32750976275760735"
127.0.0.1:6379> georadius city 120 38 1000 km withcoord withdist withhash count 2 1) 1) "beijing" 2) "408.3496" 3) (integer) 4069896088584598 4) 1) "116.23128265142440796" 2) "40.22076905438526495" 2) 1) "hefei" 2) "732.6371" 3) (integer) 4052763834193093 4) 1) "117.30793744325637817" 2) "31.79321915080526395"
|
③ georadiusbymember (查找指定元素指定范围内的元素)、geohash (返回经纬度的hash值)、zrange、zrem(使用zset命令操作geo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 127.0.0.1:6379> georadiusbymember city nanjing 500 km 1) "hefei" 2) "nanjing"
127.0.0.1:6379> georadiusbymember city chongqing 1500 km 1) "lasa" 2) "kunming" 3) "chongqing" 4) "hefei" 5) "nanjing" 6) "beijing"
127.0.0.1:6379> geohash city beijing nanjing 1) "wx4sucvncn0" 2) "wtsd1qyxfx0"
127.0.0.1:6379> ZRANGE city 0 -1 1) "lasa" 2) "kunming" 3) "chongqing" 4) "hefei" 5) "nanjing" 6) "beijing"
127.0.0.1:6379> ZREM city lasa (integer) 1
127.0.0.1:6379> ZRANGE city 0 -1 1) "kunming" 2) "chongqing" 3) "hefei" 4) "nanjing" 5) "beijing"
|
④总结:实际需求中,我们可以用来查询附近的人、计算两人之间的距离等。当然,那些所需的经纬度我们肯定要结合java代码来一次导入,手动查询和录入太过于浪费时间!
Hyperloglog: 基数
首先得明白什么是基数?
再数学层面上可以说是:两个数据集中不重复的元素~
但是再Redis中,可能会有一定的误差性。 官方给出的误差率是0.81%。
Hyperloglog的优点: 占用的内存是固定的,2^64个元素,相当于只需要12kb的内存即可。效率极高!
①pfadd(添加数据集)、pfcount(统计数据集)、pfmegre(合并数据集-自动去重)
1 2 3 4 5 6 7 8 9 10 11 12 13
| 127.0.0.1:6379> pfadd dataList 1 2 3 4 5 6 7 (integer) 1 127.0.0.1:6379> pfcount dataList (integer) 7 127.0.0.1:6379> pfadd dataList1 4 5 6 7 8 9 10 (integer) 1 127.0.0.1:6379> pfcount dataList1 (integer) 7
127.0.0.1:6379> pfmerge newdata dataList dataList1 OK 127.0.0.1:6379> pfcount newdata (integer) 10
|
②总结:如果在实际业务中,允许一定的误差值,我们可以使用基数统计来计算~效率非常高!比如:网站的访问量,就可以利用Hyperloglog来进行计算统计!
Bitmap: 位存储
Bitmap 位图,数据结构! 都是操作二进制位来进行记录,就只有0 和 1 两个状态!
①setbit(添加)、getset(获取)、bitcount(统计)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 127.0.0.1:6379> setbit login 1 1 (integer) 0 127.0.0.1:6379> setbit login 2 1 (integer) 0 127.0.0.1:6379> setbit login 3 1 (integer) 0 127.0.0.1:6379> setbit login 4 0 (integer) 0 127.0.0.1:6379> setbit login 5 0 (integer) 0 127.0.0.1:6379> setbit login 6 1 (integer) 0 127.0.0.1:6379> setbit login 7 0 (integer) 0 127.0.0.1:6379> getbit login 1 (integer) 1 127.0.0.1:6379> getbit login 4 (integer) 0 127.0.0.1:6379> bitcount login (integer) 4
|
②总结:实际需求中,可能需要我们统计用户的登陆信息,员工的打卡信息等等。只要是事务的只有两个状态的,我们都可以用Bitmap来进行操作!!!