In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use the GEO geographic location module in Redis, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
GEO is a new geolocation module added by Redis after version 3.2, which means that Redis can be used to implement nearby location functions. [related recommendation: Redis video tutorial]
Calculate with database
The general method is to limit the number of elements through a rectangular region, and then calculate the full distance of the elements in the region and then sort them. In this way, the amount of calculation can be reduced obviously.
Select id from positions where x0Murr
< x < x0+r and y0-r < y < y0+r 以上SQL为了增加性能,需要在经纬度坐标加上双向复合索引。 但是数据库查询性能毕竟有限,如果在高并发场合,这可能并不是一个很好的方案。 GEO算法 业界比较通用的地理位置距离排序算法是 GeoHash 算法,Redis 也使用 GeoHash 算法。 GeoHash 算法将 二维的经纬度数据映射到一维的整数,这样所有的元素都将在挂载到一条线上,距离靠近的二维坐标映射到一维后的点之间距离也会很接近。当我们想要计算「附近的人时」,首先将目标位置映射到这条线上,然后在这个一维的线上获取附近的点就行了。 算法实现,它将整个地球看成一个 二维平面,然后划分成了一系列正方形的方格,就好比围棋棋盘。所有的地图元素坐标都将放置于唯一的方格中。方格越小,坐标越精确。然后对这些方格进行整数编码,越是靠近的方格编码越是接近。 编码之后,每个地图元素的坐标都将变成一个整数,通过这个整数可以还原出元素的坐标,整数越长,还原出来的坐标值的损失程度就越小。 GeoHash 算法会继续对这个整数做一次 base32 编码 (0-9,a-z 去掉 a,i,l,o 四个字母) 变成一个字符串。 在 Redis 里面,经纬度使用 52 位的整数进行编码,放进了 zset 里面,zset 的 value 是元素的 key ,score 是 GeoHash 的 52 位整数值。 在使用 Redis 进行 Geo 查询时,我们要时刻想到它的内部结构实际上只是一个 zset(skiplist)。通过 zset 的 score 排序就可以得到坐标附近的其它元素 (实际情况要复杂一些,不过这样理解足够了),通过将 score 还原成坐标值就可以得到元素的原始坐标。 Redis GEO指令1. Add geoadd
Geoadd key longitude latitude member [longitude latitude member...] 127.0.0.1 gugong 6379 > geoadd beijing 116.403856 39.924043 gugong (integer) 1127.0.0.1 tiantan 6379 > geoadd beijing 116.343620 39.947633 dongwuyuan (integer) 1127.0.0.1 tiantan 6379 > geoadd beijing 116.328643 39.900272 xizhan 116.415324 39.931231 meishuguan 116.416852 39.887607 tiantan (integer) 3
Just delete the zrem with zset
two。 Distance geodist
Geodist key member1 member2 [unit] 127.0.0.1 geodist beijing gugong xizhan km 6379 > geodist beijing gugong dongwuyuan # default unit m "5768.5737" 127.0.0.1 geodist beijing gugong xizhan km 6379 > geodist beijing xizhan xizhan "0.0000"
Distance can be in m, km, ml, and ft, representing meters, kilometers, miles, and rulers, respectively.
3. Location geopos
Geopos key member [member...] 127.0.0.1 geopos beijing gugong1) 1) "116.4038559794426" 2) "39.92404192 186725" 127.0.0.1 geopos beijing tiantan xizhan1) 1) "116.41685396432877" 2) "39.887607839922914" 2) "116.3286414724698" 2) "39.900271306834973"
4. Hash value geohash
Geohash key member [member...] 127.0.0.1 geohash beijing gugong1) "wx4g0gfwqk0"
Latitude and longitude string coding is base32 coding, and latitude and longitude can be found directly through http://geohash.org/wx4g0gfwqk0.
5. Nearby location georadiusbymember
1. Query a maximum of 3 elements within 20 km of the ireader by distance, and it will not exclude itself (inverted use desc)
127.0.0.1 georadiusbymember company ireader 6379 > 20 km count 3 asc1) "ireader" 2) "juejin" 3) "meituan"
2. Three optional parameters withcoord withdist withhash are used to carry additional parameters. Withdist is very useful. It can be used to display the distance.
Georadiusbymember key member radius m | km | ft | mi [WITHCOORD] [WITHHASH] [COUNT count] [ASC | DES] 127.0.0.1 georadiusbymember beijing gugong 5 km withcoord withdist withhash count 3 asc1) 1) "gugong" 2) "0.0000" 3) (integer) 4069885568932443 4) 1) "116.4038559794426" 2) "39.92404192 186725" 2) "meishuguan" 2) "1.2634" 3) (integer) 4069885710390435 4) 1) ) "116.41532510519028" 2) "39.93123039107514" 3) 1) "tiantan" 2) "4.2014" 3) (integer) 4069885398502557 4) 1) "116.41685396432877" 2) "39.887607839922914"
3. Query the nearby elements according to the coordinate values
Georadius key longitude latitude radius m | km | ft | mi [WITHCOORD] [WITHHASH] [ASC | DES] 127.0.0.0.16379 > georadius beijing 116.383882 39.922061 5 km withcoord withdist withhash count 3 asc1) 1) "gugong" 2) "1.7180" 3) (integer) 4069885568932443 4) 1) "116.4038559794426" 2) "meishuguan" 2) "2.8693" 3) (integer) ) 4069885710390435 4) 1) "116.41532510519028" 2) "39.93123039107514" 3) "dongwuyuan" 2) "4.4588" 3) (integer) 4069879836419688 4) 1) "116.34361892938614" 2) "39.9476325169722" precautions
In practice, there may be millions of pieces of data, and we know that Redis Geo will put them all in a single zset collection. In the cluster environment of Redis, collections may migrate from one node to another. If the data of a single key is too large, it will have a great impact on the migration of the cluster. In the cluster environment, the amount of data corresponding to a single key should not exceed 1m, otherwise it will lead to stutter in the migration of the cluster and affect the normal operation of online services.
Therefore, it is recommended that Geo's data be deployed using a separate Redis instance instead of a clustered environment.
If the amount of data exceeds 100 million or more, it is necessary to split the Geo data by country, by province, by city, and even by district in mega-cities with population. This significantly reduces the size of a single zset collection.
Thank you for reading this article carefully. I hope the article "how to use the GEO Geographic location Module in Redis" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.