本文共 5728 字,大约阅读时间需要 19 分钟。
关于redis GEO介绍命令可以参考官方文档:
主要命令: GEOADD 添加地理位置信息 GEODIST 查询位置距离 GEOHASH 查询位置GEOHASH编码 GEOPOS 查询位置的坐标 GEORADIUS 查询某点的附近点 GEORADIUSBYMEMBER 查询某位置距离的附近点 如果要演示redis GEO功能我们需要下载Unstable版本进行编译安装,可以在官网下载:或到github去下载最新Unstable版本代码编译:。 关于安装请参考: 下面我们来演示一下命令: 1.添加位置信息 [plain] view plain copy print? 192.168.1.90:6379> 192.168.1.90:6379> GEOADD Guangdong-cities 113.2099647 23.593675 Qingyuan (integer) 1 192.168.1.90:6379> GEOADD Guangdong-cities 113.2278442 23.1255978 Guangzhou 113.106308 23.0088312 Foshan 113.7943267 22.9761989 Dongguan 114.0538788 22.5551603 Shenzhen (integer) 4 192.168.1.90:6379> 2.查询位置距离 [plain] view plain copy print? 192.168.1.90:6379> GEODIST Guangdong-cities Qingyuan Guangzhou km192.168.1.90:6379>
3.查询位置GEOHASH编码 [plain] view plain copy print? 192.168.1.90:6379> GEOHASH Guangdong-cities Qingyuan Guangzhou 1) “ws0w0phgp70” 2) “ws0e89curg0” 192.168.1.90:6379> 4.查询位置的坐标 [plain] view plain copy print? 192.168.1.90:6379> GEOPOS Guangdong-cities Qingyuan Guangzhou Foshan 1) 1) “113.20996731519699” 2) “23.593675019671288” 2) 1) “113.22784155607224” 2) “23.125598202060807” 3) 1) “113.10631066560745” 2) “23.008831202413539” 192.168.1.90:6379> 5.查询某点的附近点 [plain] view plain copy print? 192.168.1.90:6379> GEORADIUS Guangdong-cities 113.2278442 23.1255978 50 km asc 1) “Guangzhou” 2) “Foshan” 192.168.1.90:6379> 6.查询某位置距离的附近点 [plain] view plain copy print? 192.168.1.90:6379> GEORADIUSBYMEMBER Guangdong-cities Guangzhou 50 km asc 1) “Guangzhou” 2) “Foshan” 192.168.1.90:6379> 使用redis Java客户端jedis实例; 由于jedis并没有实现redis GEO的命令,测试使用lua脚本来实现: [java] view plain copy print? package test;import java.util.List;
import redis.clients.jedis.Jedis;
public class Test {
private static Jedis jedis = null; static{ jedis = new Jedis(“192.168.1.90”, 6379); }public static void main(String[] args) throws Exception { System.out.println(geoADD("beijing", 116.312014, 39.963019,"haidian")); System.out.println(geoADD("beijing", 116.227501, 39.90858,"shijingshan")); System.out.println(geoADD("beijing", 116.297641, 39.861631,"fengtai")); System.out.println(geoADD("beijing", 116.428146, 39.9316,"dongcheng")); System.out.println(geoADD("beijing", 116.375829, 39.920091,"xicheng")); System.out.println(geoADD("beijing", 116.110793, 39.943992,"mentougou")); System.out.println(geoADD("beijing", 116.480464, 39.95948,"caoyang")); System.out.println(geoADD("beijing", 116.663862, 39.916107,"tongzhou")); System.out.println(geoADD("beijing", 116.349383, 39.729911,"daxing")); System.out.println(geoADD("beijing", 116.157361, 39.748109,"fangshan")); System.out.println(geoADD("beijing", 116.662137, 40.134017,"sunyi")); System.out.println(geoADD("beijing", 116.2367, 40.224862,"changping")); System.out.println(geoADD("beijing", 117.141617, 40.14196,"pinggu")); System.out.println(geoADD("beijing", 116.64144, 40.316466,"huairou")); System.out.println(geoHash("beijing","haidian")); System.out.println(geoDist("beijing","haidian","shijingshan","km")); System.out.println(geoPos("beijing","haidian")); System.out.println(geoRadius("beijing",116.421822, 39.906809,10,"km",false)); System.out.println(geoRadiusByMember("beijing","dongcheng",10,"km",true)); } /** * 添加geo * @param key * @param longitude * @param latitude * @param name 位置名称 * @return */ public static Long geoADD(String key,double longitude,double latitude,String dName){ return (Long)jedis.eval("return redis.call('GEOADD',KEYS[1],KEYS[2],KEYS[3],KEYS[4])", 4,key,String.valueOf(longitude),String.valueOf(latitude),dName); } /** * 查询2位置距离 * @param key * @param d1 * @param d2 * @param unit * @return */ public static Double geoDist(String key,String d1,String d2,String unit){ return Double.valueOf((String)jedis.eval("return redis.call('GEODIST',KEYS[1],KEYS[2],KEYS[3],KEYS[4])",4, key,d1,d2,unit)); } /** * 查询位置的geohash * @param key * @param dName * @return */ public static String geoHash(String key,String dName){ Object data = jedis.eval("return redis.call('GEOHASH',KEYS[1],KEYS[2])", 2, key,dName); ListresultList = (List )data; if(resultList!=null&&resultList.size() > 0){ return resultList.get(0); } return null; } /** * 查询位置坐标 * @param key * @param dName * @return */ public static List geoPos(String key,String dName){ Object data = jedis.eval("return redis.call('GEOPOS',KEYS[1],KEYS[2])", 2, key,dName); List
> resultList = (List
>)data; if(resultList!=null&&resultList.size() > 0){ return resultList.get(0); } return null; } /** * 查询附近坐标地址 * @param key * @param longitude * @param latitude * @param unit * @param asc * @return */ public static List geoRadius(String key,double longitude,double latitude,int radius,String unit,boolean asc){ Object data = jedis.eval("return redis.call('GEORADIUS',KEYS[1],KEYS[2],KEYS[3],KEYS[4],KEYS[5],KEYS[6])", 6, key,String.valueOf(longitude), String.valueOf(latitude),String.valueOf(radius),unit,asc?"ASC":"DESC"); return (List )data; } /** * 根据位置查询附近点 * @param key * @param dName * @param unit * @param asc * @return */ public static List geoRadiusByMember(String key,String dName,int radius,String unit,boolean asc){ Object data = jedis.eval("return redis.call('GEORADIUSBYMEMBER',KEYS[1],KEYS[2],KEYS[3],KEYS[4],KEYS[5])", 5, key,dName,String.valueOf(radius),unit,asc?"ASC":"DESC"); return (List )data; }
}
增加jar包依赖: [html] view plain copy print? redis.clients jedis 2.7.3 参考文章: 1.redis官网lua脚本 2.spring+springdata redis入门教程实现java端调用redis lua脚本片段和lua脚本文件 3.利用redis + lua解决抢红包高并发的问题原文地址:
其他介绍:转载地址:http://fvmpa.baihongyu.com/