未加星标

Build geospatial apps with Redis

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Build geospatial apps with Redis

For an increasing number of applications, tracking location is essential. A social application might connect users based on location. A hospitality or travel application might use the user’s location to point out interesting sights or provide custom itineraries. A sensor application might store and analyze data that is both geospatial and time series, to trigger an action like detecting patterns, outliers, and anomalies.

Further, as geospatial technology matures, location-based applications are evolving from mainly mapping applications into sophisticated, cutting-edge programs that process and analyze millions of data points from mobile users, sensor networks, IoT devices, and other sources. The world is in constant motion, and our apps are beginning to catch on.

Location data presents an interesting challenge for the developer because querying it or performing calculations of position and distance have to consider longitude (x), latitude (y), and sometimes even elevation (z). The multidimensional nature of location data requires optimized mechanisms to process it -- treating it as merely integers is highly inefficient. If the database, whether an RDBMS or a NoSQL store, lacks the capabilities for handling geospatial data, application programmers have to do the extra work of preprocessing the data, or they have to build in logic that treats the data as geospatial.

Processing geospatial data is also a real-time, big data challenge. Applications that use and manage geospatial data must serve, at minimal latency, a high number of requests for location (“Where are you?”), updates to location (“I am here”), and searches for data by location (“Who or what is nearby?”).

Simple reads (fetch location) and writes (update location) are challenging at scale. Searching further compounds the challenge. The key to satisfying the above requirements is maintaining effective indexes for the data. An effective index is one that can facilitate speedy searches and isn’t expensive to maintain (in terms of memory and compute power).

The characteristics and performance of Redis make it an excellent fit for location-based applications. All that was missing was native support for geolocation data. Starting with version 3.2, however, Redis comes with geospatial indexing built in. Developers of applications that rely on geospatial data can now look to Redis to store, process, and analyze it -- with all of the speed and simplicity they’ve come to expect from Redis in other applications.

Brief intro to Redis

Redis is an in-memory data structure store that's commonly used as a database, a cache, and a message broker. Data structures in Redis are like Lego building blocks, helping developers achieve specific functionality with minimal complexity. Redis also minimizes network overhead and latency because operations are executed extremely efficiently in memory, right next to where the data is stored.

Redis data structures include Hashes, Sets, Sorted Sets, Lists, Strings, Bitmaps, and HyperLogLogs. These are highly optimized, each providing specialized commands that help you execute complex functionality with very little code. These data structures make Redis extremely powerful and allow Redis-based applications to handle extreme volumes of operations at very low latency.

Sorted Sets are particularly significant. Unique to Redis, they add an ordered view to members, sorted by scores. Sorted Sets are tremendously advantageous for processing data like bids, ranks, user points, and time stamps -- allowing analysis to be performed a couple of orders of magnitude faster compared to ordinary key/value or NoSQL stores.

Geospatial indexing is implemented in Redis using Sorted Sets as the underlying data structure, but with on-the-fly encoding and decoding of location data and new APIs. This means that location-specific indexing, searching, and sorting can all be offloaded to Redis, with very few lines of code and very little effort, using built-in commands like GEOADD , GEODIST , GEORADIUS , and GEORADIUSBYMEMBER .

When you combine this geospatial support with other Redis capabilities, some interesting functionality becomes extremely simple to implement. For example, by melding the new Geo Sets and PubSub, it is nearly trivial to set up a real-time tracking system in which every update to a member’s position is sent to all interested parties (think of a running or biking group where you want to track group members locations in real time).

The Geo Set

The Geo Set is the basis for working with geospatial data in Redis -- it is a data structure that is specialized for managing geospatial indices. Each Geo Set is made up of one or more members, with each member consisting of a unique identifier and a longitude/latitude pair. Similar to all of the data structures in Redis, Geo Sets are manipulated and queried using a subset of simple-to-use and at the same time highly optimal commands.

Internally, Geo Sets are implemented with a Sorted Set. Sorted Sets exhibit a good space-time balance by consuming a linear amount of RAM while providing logarithmic computing complexity for most operations.

Creating and adding to the index

The Redis command for adding members to a geospatial index is called GEOADD . This command is used both for creating new sets and for adding members. The following example, illustrated from the command line and the Node Redis client, demonstrates its use.

Redis command example:

GEOADD locations 10.9971645 45.4435245 Romeo

Node Redis example:

redis.geoadd(‘locations’, ‘10.9971645’, ‘45.4435245’, ‘Romeo’);

The above tells Redis to use a Geo Set called locations for storing the coordinates of the member named Romeo. In case the locations data structure doesn’t exist, it will first be created by Redis. The new member will be added to the index if and only if it does not exist in the set.

It is also possible to add multiple members to the index with a single call to GEOADD . By batching multiple operations in a single command, this form of invocation reduces the load on the database and the network.

Redis command example:

GEOADD locations 10.9971645 45.4435245 Mercutio 10.9962165 45.4419226 Juliet

Node Redis example:

redis.geoadd(‘locations’, ‘10.9971645’, ‘45.4435245’, ‘Mercutio’, ‘10.9962165’, ’45.4419226’, ‘Juliet’);

Updating the index

After a member and its coordinates have been recorded in the index, Redis allows you to update that member’s location. Updating members in a Geo Set is done by calling the same command used for adding them, namely GEOADD . When called with existing members, GEOADD simply updates the spatial data that is associated with each member with the new values. Therefore, once Romeo exits the house to begin his evening stroll, his updated location can be recorded with the following.

Redis command example:

GEOADD locations 10.999216 45.4432923 Romeo

Node Redis example:

redis.geoadd(‘locations’, ‘10.999216’, ‘45.4432923’, ‘Romeo’);

Removing members from the index After having been added to the index, members may need to be deleted from it at a later time. To facilitate the deletion of members from the Geo Set, Redis provides the ZREM command. To delete a member (or members) from the set, ZREM

本文数据库(综合)相关术语:系统安全软件

主题: RedisSQL
分页:12
转载请注明
本文标题:Build geospatial apps with Redis
本站链接:http://www.codesec.net/view/480269.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(47)