How to realize the function of querying nearby people in Redis
Redis how to achieve query nearby people function, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Import redis.clients.jedis.GeoCoordinate
Import redis.clients.jedis.GeoRadiusResponse
Import redis.clients.jedis.GeoUnit
Import redis.clients.jedis.Jedis
Import java.util.HashMap
Import java.util.List
Import java.util.Map
Public class GeoHashExample {
Public static void main (String [] args) {
Jedis jedis = new Jedis ("127.0.0.1", 6379)
Map map = new HashMap ()
/ / add Xiaoming's position
Map.put ("xiaoming", new GeoCoordinate (116.404269, 39.913164)
/ / add the location of Xiao Hong
Map.put ("xiaohong", new GeoCoordinate (116.36, 39.922461)
/ / add the location of Xiao Mei
Map.put ("xiaomei", new GeoCoordinate (116.499705, 39.874635)
/ / add a waiter
Map.put ("xiaoer", new GeoCoordinate (116.193275, 39.996348)
Jedis.geoadd ("person", map)
/ / query the straight line distance between Xiao Ming and Xiao Hong
System.out.println ("Xiao Ming and Xiao Hong are apart:" + jedis.geodist ("person", "xiaoming")
"xiaohong", GeoUnit.KM) + "KM")
/ / inquire about people who are 5 kilometers near Xiaoming.
List res = jedis.georadiusByMemberReadonly ("person", "xiaoming"
5, GeoUnit.KM)
For (int I = 1; I < res.size (); iTunes +) {
System.out.println ("people near Xiaoming:" + res.get (I) .getMemberByString ())
}
}
}
The results of the above procedures are as follows:
Distance between Xiao Ming and Xiao Hong: 3.9153 KM
People near Xiaoming: xiaohong
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.