简体   繁体   English

使用 RedisGears 对接收 pubsub 消息进行计算

[英]Use RedisGears to perform calculations on receiving pubsub messages

In my scenario I'm publishing messages to Redis, these messages contain GPS coordinates (latitude/longitude pairs).在我的场景中,我将消息发布到 Redis,这些消息包含 GPS 坐标(纬度/经度对)。

Example:例子:

redis-cli -p 16379 PUBLISH gps_positions "{'lat': 50.5243584, 'lon': 12.3616320}"
redis-cli -p 16379 PUBLISH gps_positions "{'lat': 50.5063360, 'lon': 12.3377472}"

A client subscribes to these messages:客户端订阅这些消息:

redis-cli -p 16379 PSUBSCRIBE gps_positions

Now I'd like to calculate the bearing angle between the current and the previous GPS coordinate in the moment they are published to the Redis pubsub channel.现在我想计算当前前一个GPS 坐标在发布到 Redis pubsub 频道时的方位角。 The calculation should happen directly within Redis, transparent to the client.计算应该直接在 Redis 中进行,对客户端透明。 Since there is a huge amount of published messages I'd like to avoid saving any data within Redis - I only need the previous GPS coordinate for each new published coordinate.由于有大量已发布的消息,我想避免在 Redis 中保存任何数据 - 对于每个新发布的坐标,我只需要以前的 GPS 坐标。

In the end the client should receive the GPS positions including the bearing angle without having to do any calculations on its own:最后,客户端应该收到包括方位角在内的 GPS 位置,而无需自己进行任何计算:

"{'lat': 50.5243584, 'lon': 12.3616320, 'bearing': $BEARING}"

The algorithm to calculate the bearing does not matter for this question, but it looks like this:计算轴承的算法对于这个问题无关紧要,但它看起来像这样:

import pyproj

def get_bearing(lat1, lon1, lat2, lon2):
    geodesic = pyproj.Geod(ellps='WGS84')
    fwd_azimuth, back_azimuth, distance = geodesic.inv(lat1, lon1, lat2, lon2)
    return fwd_azimuth, back_azimuth, distance

lat1, lon1 = 50.5243584, 12.3616320
lat2, lon2 = 50.5063360, 12.3377472

bearing = get_bearing(lat1, lon1, lat2, lon2)[0]

Is RedisGears able to listen and alter published messages before the clients receives these? RedisGears 是否能够在客户端收到这些消息之前侦听和更改已发布的消息? Or can I use RedisGears to publish a message to a different channel containing the calculated bearing angle?或者我可以使用 RedisGears 将消息发布到包含计算出的方位角的不同通道吗?

I'm not sure you can listen for PubSub events.我不确定您是否可以收听 PubSub 事件。 However you could write to a stream that is only consumed by RedisGears and then either PUBLISH the modified event or maybe use Streams so you also get acknowledgement of these messages from consumers.但是,您可以写入仅由 RedisGears 使用的流,然后发布修改后的事件,或者使用 Streams,这样您也可以从消费者那里获得这些消息的确认。

I wrote a demo that has this approach: https://github.com/hnsk/redis-streams-log-demo/blob/master/gears_functions.py我写了一个具有这种方法的演示: https : //github.com/hnsk/redis-streams-log-demo/blob/master/gears_functions.py

My test generator writes to a stream called "test", which is consumed by Gears with trimStream=True (default) so the messages get trimmed from the stream on consumption.我的测试生成器写入一个名为“test”的流,该流由 Gears 使用 trimStream=True(默认)使用,因此消息在消费时从流中被修剪。

Then it takes the field "log_level" from the event and uses that as the key for a new stream and writes (XADD) the same data in it and that is then consumed by the actual consumers.然后它从事件中获取字段“log_level”并将其用作新流的键并在其中写入(XADD)相同的数据,然后由实际使用者使用。

In the example it also writes the messages to a hash for RediSearch and a sorted set to count the events for each log_level.在示例中,它还将消息写入 RediSearch 的散列和排序集以计算每个 log_level 的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM