【问题标题】:Boil down events to time intervals将事件归结为时间间隔
【发布时间】:2018-02-23 15:45:49
【问题描述】:

场景: 我有一个记录事件的服务,例如在这个 CSV 示例中:

#TimeStamp, Name, ColorOfPullover
TimeStamp01, Peter, Green
TimeStamp02, Bob, Blue
TimeStamp03, Peter, Green
TimeStamp04, Peter, Red
TimeStamp05, Peter, Green

事件,例如彼得穿绿色会经常出现连续。

我有两个目标:

  1. 尽量减少数据
  2. 保留所有相关数据

相关的意思是:我需要知道,一个人在哪个时间跨度穿着什么颜色。例如:

#StartTime, EndTime, Name, ColorOfPullover
TimeStamp01, TimeStamp03, Peter, Green
TimeStamp02, TimeStamp02, Bob, Blue
TimeStamp03, TimeStamp03, Peter, Green
TimeStamp04, TimeStamp04, Peter, Red
TimeStamp05, TimeStamp05, Peter, Green

在这种格式中,我可以回答以下问题:Peter 在 TimeStamp02 时穿的是什么颜色的? (我可以有把握地假设每个人在两次记录的事件之间穿着相同的颜色以获取相同的颜色。)

主要问题: 我可以使用现有的技术来实现这一点吗? IE。我可以为它提供连续的事件流,它会提取和存储相关数据吗?


确切地说,我需要实现这样的算法(伪代码)。 CSV 示例的每一行都会调用OnNewEvent 方法。其中参数event 已经包含来自该行的数据作为成员变量。

def OnNewEvent(even)
    entry = Database.getLatestEntryFor(event.personName)
    if (entry.pulloverColor == event.pulloverColor)
        entry.setIntervalEndDate(event.date)
        Database.store(entry)
    else
        newEntry = new Entry
        newEntry.setIntervalStartDate(event.date)
        newEntry.setIntervalEndDate(event.date)
        newEntry.setPulloverColor(event.pulloverColor))
        newEntry.setName(event.personName)
        Database.createNewEntry(newEntry)
    end
end

【问题讨论】:

  • 应该可以用logstash来做,但问题是你必须为每一行做一个elasticsearch请求来检索最新的条目,这会使这个过程很慢。这就是为什么我不认为 logstash 是合适的工具。
  • 您的数据量是多少?当新事件发生时您需要多快做出反应?有些事件丢失了可以吗?
  • 对事件的反应可能很慢。例如。 1 天延迟是可以接受的。因此,每天一个 cron 作业可能是一种选择。事件可能不会丢失,这是关键任务。

标签: events logging mapreduce reducing


【解决方案1】:
This is typical scenario of any streaming architecture.  

There are multiple existing technologies which work in tandem  to get what you want. 


1.  NoSql Database (Hbase, Aerospike, Cassandra)
2.  streaming jobs Like Spark streaming(micro batch), Storm 
3.  Run mapreduce in micro batch to insert into NoSql Database.
4.  Kafka Distriuted queue

The end to end flow. 

Data -> streaming framework -> NoSql Database. 
OR 
Data -> Kafka -> streaming framework -> NoSql Database. 


IN NoSql database there are two ways to model your data. 
1. Key by "Name" and for every event for that given key, insert into Database.
   While fetching u get back all events corresponding to that key. 

2. Key by "name", every time a event for key is there, do a UPSERT into a existing blob(Object saved as binary), Inside the blob you maintain the time range and color seen.  

Code sample to read and write to Hbase and Aerospike  

Hbase:http://bytepadding.com/hbase/

Aerospike:http://bytepadding.com/aerospike/

【讨论】:

  • 两个链接都坏了
  • 对不起伙计,黑客们玩得很开心,刚刚修复了网站。随意浏览示例。如果您需要更多说明,请告诉我
【解决方案2】:

一种方法是使用 HiveMQ。 HiveMQ 是一种基于 MQTT 的消息队列技术。关于它的好处是您可以编写自定义插件来处理传入的消息。要获取一个人的最新事件条目,HiveMQ 插件中的哈希表可以正常工作。如果不同人的数量非常多,我会考虑使用像 Redis 这样的缓存来缓存每个人的最新事件。

您的服务将事件发布到 HiveMQ。 HiveMQ 插件处理传入事件,并更新您的数据库。

HiveMQ Plugin

Redis

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2021-02-16
    • 2014-10-03
    • 1970-01-01
    相关资源
    最近更新 更多