Redis特殊数据类型geospatial详解

    科技2024-07-26  11

    5.1 geospatial 数据类型详解

    5.1.1 简介

    记录地理位置的数据类型

    实际场景:

    微信定位 ,附近的人,打车距离计算

    自3.2.0起可用。

    **时间复杂度:**添加的每一项的O(log(N)),其中N是排序集中的元素数。

    将指定的地理空间项(纬度,经度,名称)添加到指定键。数据以排序集的形式存储在密钥中,该方式使得以后可以使用GEORADIUS或GEORADIUSBYMEMBER命令通过半径查询来检索项目。

    该命令采用标准格式x,y的参数,因此必须在纬度之前指定经度。可以分度的坐标有限制:极点附近的区域无法分度。由EPSG:900913 / EPSG:3785 / OSGEO:41001指定的确切限制如下:

    有效经度为-180至180度。有效纬度为-85.05112878至85.05112878度。

    当用户尝试索引超出指定范围的坐标时,该命令将报告错误。

    redis官方对于geo的介绍,传送门

    所以说,我还需要一个GPS的经纬度, 传送门


    5.1.2 基本命令介绍

    Related commands

    GEOADDGEODISTGEOHASHGEOPOSGEORADIUSGEORADIUSBYMEMBER

    5.1.3 geoadd 添加经纬度命令详解

    geoadd key longitude latitude member [longitude latitude member …]

    注意事项: 地理位置两级无法直接输入经度纬度 ,一般我们通过Java程序批量读取地理位置信息插入

    ##添加城市数据 127.0.0.1:6379> geoadd chian:city 116.405285 39.904989 beijing 108.948024 34.263161 shanxi (integer) 2 ##添加城市数据 127.0.0.1:6379> geoadd chian:city 87.617733 43.792818 xinjiang (integer) 1 ##添加城市数据 127.0.0.1:6379> geoadd chian:city 115.892151 28.676493 jiangxi (integer) 1 127.0.0.1:6379>

    5.1.4 geopos 获取经纬度命令详解

    geopos key member [member …]

    127.0.0.1:6379> geopos chian:city shanxi 1) 1) "108.94802302122116089" 2) "34.2631604414749944" 127.0.0.1:6379> geopos chian:city shanxi beijing 1) 1) "108.94802302122116089" 2) "34.2631604414749944" 2) 1) "116.40528291463851929" 2) "39.9049884229125027" 127.0.0.1:6379>

    5.1.5 geodist 距离计算命令详解

    geodist key member1 member2 [m|km|ft|mi] 单位有米,千米,英里,英尺,默认以米作为单位

    127.0.0.1:6379> keys * 1) "chian:city" 127.0.0.1:6379> geopos chian:city beijing shanxi 1) 1) "116.40528291463851929" 2) "39.9049884229125027" 2) 1) "108.94802302122116089" 2) "34.2631604414749944" 127.0.0.1:6379> geodist chian:city beijing shanxi "911278.5437" 127.0.0.1:6379> geodist chian:city beijing shanxi m "911278.5437" 127.0.0.1:6379> geodist chian:city beijing shanxi km "911.2785" 127.0.0.1:6379> geodist chian:city beijing shanxi mi "566.2436" 127.0.0.1:6379> geodist chian:city beijing shanxi ft "2989759.0015" 127.0.0.1:6379>

    5.1.6 实际场景实现附近的人相关命令

    第一步: 要想获得附近的人的信息,首先是要获取附近的人的相关地理位置信息,手机上就有一个定位功能,

    把获取到的定位信息存入一个集合,是以半径的机制查询,获取附近人的信息,一般由APP来收集

    相关命令: 传送门

    GEORADIUS

    GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE ke


    案例演示:

    ##查询所有的key 127.0.0.1:6379> keys * 1) "chian:city" ##获取该Key下的所有元素 127.0.0.1:6379> geopos chian:city beijing shanxi shanghai 1) 1) "116.40528291463851929" 2) "39.9049884229125027" 2) 1) "108.94802302122116089" 2) "34.2631604414749944" 3) (nil) ##以116.39这个经纬度为中心,半径1000km查询 ##一般以哪个精度为中心,这个精度都是用户自己, 127.0.0.1:6379> georadius chian:city 116 39 1000 km 1) "beijing" 2) "shanxi" ##还可以后面携带withcoord,查询完成后获得经纬度信息 127.0.0.1:6379> georadius chian:city 116 39 1000 km withcoord 1) 1) "beijing" 2) 1) "116.40528291463851929" 2) "39.9049884229125027" 2) 1) "shanxi" 2) 1) "108.94802302122116089" 2) "34.2631604414749944" ##还可以后面携带withdist,查询完成后获得直线距离 127.0.0.1:6379> georadius chian:city 116 39 1000 km withdist 1) 1) "beijing" 2) "106.5063" 2) 1) "shanxi" 2) "820.3684" ##还可以后面携带conut参数,指定获取附近的几个 127.0.0.1:6379> georadius chian:city 116 39 1000 km count 1 1) "beijing"

    前面我们的都是以经纬度坐标来找附近的人,还可以通过城市来找附近的人

    georadiusbymember key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key]

    127.0.0.1:6379> georadiusbymember chian:city beijing 1000 km 1) "beijing" 2) "shanxi" 127.0.0.1:6379>

    5.1.7 geohash 命令详解

    geohash key member [member …] 查询结果,以hash值返回

    127.0.0.1:6379> keys * 1) "chian:city" 127.0.0.1:6379> geopos chian:city beijing shanxi shanghai jingxi 1) 1) "116.40528291463851929" 2) "39.9049884229125027" 2) 1) "108.94802302122116089" 2) "34.2631604414749944" 3) (nil) 4) (nil) 127.0.0.1:6379> geohash chian:city beijing shanxi 1) "wx4g0b7xrt0" 2) "wqj6zjmt2u0" 127.0.0.1:6379>

    5.1.8 原理

    geo底层就是一个zset集合,我能可以使用zset命令使用geo

    场景展示

    比如现在我不想查询附近的人了! 要删除自己的位置信息,而geo命令中是没有移除这个命令的,但是他的底层是zset,我们就可以使用zset来操作geo,…很多功能

    127.0.0.1:6379> zrange chian:city 0 -1 1) "xinjiang" 2) "shanxi" 3) "jiangxi" 4) "beijing" ------------------------------------------------------------------------------------------------------------ 127.0.0.1:6379> zrange chian:city 0 -1 withscores 1) "xinjiang" 2) "3846741531194771" 3) "shanxi" 4) "4040115616141630" 5) "jiangxi" 6) "4051506205099900" 7) "beijing" 8) "4069885370671010" ------------------------------------------------------------------------------------------------------------ 127.0.0.1:6379> zrem chian:city beijing (integer) 1 ------------------------------------------------------------------------------------------------------------ 127.0.0.1:6379> zrange chian:city 0 -1 withscores 1) "xinjiang" 2) "3846741531194771" 3) "shanxi" 4) "4040115616141630" 5) "jiangxi" 6) "4051506205099900" 127.0.0.1:6379>
    Processed: 0.015, SQL: 8