【问题标题】:Firestore - How to aggregate data by time (in this example by hour)?Firestore - 如何按时间聚合数据(在此示例中按小时)?
【发布时间】:2020-01-26 09:50:37
【问题描述】:

我想在唯一访问者图表上生成如下所示的实时图表(有点像谷歌分析)。 经过一番调查,我注意到 Firestore 不像 SQL 那样支持 GROUP BY。

在我的示例数据下面,第 1 和第 3 是在同一小时内但相同的 user_id = 1。所以它应该算作 1 而不是 2。所以,访问网站的人数“笔.com”在 10:00 时为 1。

我如何为网站计数唯一访问时间序列聚合这些数据,以我按小时分组 user_id 做的这个例子为例?有没有办法做到这一点,因为我看到谷歌分析可以做到这一点,即使在几秒钟内 (https://i.imgur.com/JfkcZqr.png)!

以下是示例数据,我想汇总数据以显示如下图。我绘制的图表就是基于这个样本数据。

跟踪

  (10:00 hour)    

1. document(aaa123) <----------- inside 10:00 hour same place "pen.com"
user_id = 1
site_unique_identifier = "pen.com"
timestamp = 10/09/2019 10:11

2. document(xyz567)
user_id = 2
site_unique_identifier = "hex.com"
timestamp = 10/09/2019 10:15

3. document(qqq123) <----------- inside 10:00 hour same place "pen.com"
user_id = 1
site_unique_identifier = "pen.com"
timestamp = 10/09/2019 10:18

4. document(xyz567)
user_id = 3
site_unique_identifier = "hex.com"
timestamp = 10/09/2019 10:19

5. document(fff567)
user_id = 4
site_unique_identifier = "hex.com"
timestamp = 10/09/2019 10:19

  (11:00 hour)    

6. document(xxx123)
user_id = 1
site_unique_identifier = "hex.com"
timestamp = 10/09/2019 11:21

7. document(zzz123)
user_id = 2
site_unique_identifier = "hex.com"
timestamp = 10/09/2019 11:22

8. document(ggg123)
user_id = 2
site_unique_identifier = "pen.com"
timestamp = 10/09/2019 11:24

根据上述示例数据,下面是按小时访问各个网站的唯一身份访问者的时间序列图。 (观察网站“pen.com”的 10:00 小时访问计数是 1 而不是 2)

为图表生成信息的查询输出(基于相同的数据):

10:00
- pen.com - 1
- hex.com - 3
11:00
- pen.com - 1
- hex.com - 2

你可以看到我们正在按 user_id、site_unique_identifier、小时分组

【问题讨论】:

    标签: firebase firebase-realtime-database group-by google-cloud-firestore google-cloud-functions


    【解决方案1】:

    Google Analytics(分析)可能使用 BigQuery 而不是 Firestore。

    Firestore 是一个 NoSQL 数据库,由于它的结构,您不能使用 group by 和类似的增加复杂性的运算符,因为这不是本应做的 (you can check this doc for further understanding at section 3.2 Data Model)。

    对于您在问题中描述的用例,我宁愿选择 SQL 数据库,例如 Cloud SQL,因为它是非常结构化的数据。

    如果您仍想为此使用 Firestore,也许您可​​以尝试使用 FireSQL ,但请注意存在限制,并且您想要的也可能无法实现。

    【讨论】:

    • 我已经放弃了这个想法,直到有一个真正的解决方案,时间序列似乎有点太难了,我相信他们必须认真研究的东西。我正在迁移回 SQL。
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2018-02-28
    • 2014-03-09
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多