sequence
This paper mainly studies the use of Redis GEO
Relevant command
geoadd
Time order log N.
Geoadd cityGeo 116.405285 39.904989"Beijing"Geoadd cityGeo 121.472644 31.231706"Shanghai"
Copy the code
- Add latitude and longitude information
geopos
Time order log N.
Geopos cityGeo Beijing 1) 1)"116.40528291463851929"
2) "39.9049884229125027"
Copy the code
- Query the latitude and longitude of a specified key. Multiple keys can be specified and returned in batches
geodist
Time order log N.
127.0.0.1:6379> Geodist cityGeo"1067597.9668"127.0.0.1:6379> Geodist cityGeo Beijing Shanghai km"1067.5980"
Copy the code
- Returns the distance between two places. You can specify units such as meters m, kilometers km, miles mi, or feet ft
georadius
The time complexity is O(N+log(M)), where N is the number of elements within the specified radius and M is the number to be returned
Georadius cityGeo 116.405285 39.904989 100 km WITHDIST WITHCOORD ASC COUNT 5Copy the code
- Returns an element whose radius does not exceed the specified distance, based on the given latitude and longitude
- You can specify WITHDIST to return the distance, WITHCOORD to return latitude and longitude, and WITHHASH to return the geohash value
- You can specify ASC or DESC to sort by distance
- You can specify COUNT to limit the number of records returned
georadiusbymember
The time complexity is O(log(N)+M), where N is the number of elements within the specified radius and M is the number to be returned
Georadiusbymember cityGeo Beijing 100 km WITHDIST WITHCOORD ASC COUNT 5Copy the code
- Queries the location within the specified radius based on the specified location
- You can specify WITHDIST to return the distance, WITHCOORD to return latitude and longitude, and WITHHASH to return the geohash value
- You can specify ASC or DESC to sort by distance
- You can specify COUNT to limit the number of records returned
geohash
The time to find a location is order log of N.
127.0.0.1:6379> GeoHash cityGeo Beijing 1)"wx4g0b7xrt0"
Copy the code
- The geohash value is returned
RedisTemplate GEO use example
@Test
public void testAdd(){Long addedNum = redistemplate.opsforgeo ().add(cityGeoKey,new Point(116.405285,39.904989),"Beijing");
System.out.println(addedNum);
}
@Test
public void testGeoGet(){
List<Point> points = redisTemplate.opsForGeo().position(cityGeoKey,"Beijing"."Shanghai"."Shenzhen");
System.out.println(points);
}
@Test
public void testDist(){
Distance distance = redisTemplate.opsForGeo()
.distance(cityGeoKey,"Beijing"."Shanghai", RedisGeoCommands.DistanceUnit.KILOMETERS);
System.out.println(distance);
}
@Test
public void testNearByXY() {/ / longitude, latitude Circle Circle = new Circle (116.405285, 39.904989, the Metrics. KILOMETERS. GetMultiplier ()); RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5) ; GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo() .radius(cityGeoKey,circle,args); System.out.println(results); } @Test public voidtestNearByPlace(){ Distance distance = new Distance(5,Metrics.KILOMETERS); RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().includeCoordinates().sortAscending().limit(5) ; GeoResults<RedisGeoCommands.GeoLocation<String>> results = redisTemplate.opsForGeo() .radius(cityGeoKey,"Beijing",distance,args);
System.out.println(results);
}
@Test
public void testGeoHash(){
List<String> results = redisTemplate.opsForGeo()
.hash(cityGeoKey,"Beijing"."Shanghai"."Shenzhen");
System.out.println(results);
}
Copy the code
- The operations of geoadd, geopOS, geodist, Georadius, Georadiusbymember and geohash using RedisTemplate are respectively shown above
- The x axis is longitude longitude and the y axis is latitude latitude
summary
Redis provides rich operations for GEO, and RedisTemplate also encapsulates corresponding apis, making it very convenient to use.
doc
- geo
- geoadd
- geopos
- geodist
- georadius
- georadiusbymember
- geohash
- Mongo’s Geo query