首页 文章

Firebase数据到Google BigQuery

提问于
浏览
4

Firebase提供private backups on Google Cloud Storage . 其中一个特色用例是"Ingestion into Analytics Products":

Private Backups provides a perfect pipeline into cloud analytics products such as Google’s BigQuery. Cloud Analytics products often prefer to ingest data through Cloud Storage buckets rather than directly from the application.

我在Firebase中有很多数据(导出到 Cloud 存储桶时超过1GB),并且如Firebase产品中所述,我想将这些数据放入Big Query中 .

但是,是否真的可以编写适合Firebase原始数据的表模式?我们以Firebase文档中的dinosaur-facts数据库为例 . JSON看起来像这样:

{
  "dinosaurs" : {
    "bruhathkayosaurus" : {
      "appeared" : -70000000,
      "height" : 25
    },
    "lambeosaurus" : {
      "appeared" : -76000000,
      "height" : 2.1
    }
  },
  "scores" : {
    "bruhathkayosaurus" : 55,
    "lambeosaurus" : 21
  }
}

要列出所有恐龙,我想唯一的方法是在bigQuery模式中使用RECORD字段 . 但是,BigQuery中的RECORDS通常对应于导入的JSON中的数组 . Firebase中没有数组,只是一个以恐龙名字为关键名称的对象 .

所以像这样的BigQuery表模式不起作用:

[
    {
        "name": "dinosaurs",
        "type": "RECORD",
        "mode": "REQUIRED",
        "fields": [
            {
                "name": "dinosaur",
                "type": "RECORD",
                "mode": "REPEATED",
                "fields": [
                    {
                        "name": "appeared",
                        "type": "INTEGER"
                    },
                    {
                        "name": "height",
                        "type": "INTEGER"
                    },
                    {
                        "name": "length",
                        "type": "INTEGER"
                    },
                    {
                        "name": "order",
                        "type": "STRING"
                    },
                    {
                        "name": "vanished",
                        "type": "INTEGER"
                    },
                    {
                        "name": "weight",
                        "type": "INTEGER"
                    }
                ]
            },
            {
                "name": "scores",
                "type": "RECORD",
                "mode": "REPEATED",
                "fields": [
                    {
                        "name": "dinosaur",
                        "type": "INTEGER"
                    }
                ]
            }
        ]
    }
]

是否可以编写适合Firebase原始数据的表模式?或者我们应该首先准备数据以使其与BigQuery兼容?

3 回答

  • 2

    由于上面的数据只是JSON,因此您应该能够使用Firebase . 但是,我认为准备数据会更容易 after the backup .

    您提到Firebase数据中没有数组 . Firebase does support arrays, but they have to meet a certain criteria.

    // we send this
    ['a', 'b', 'c', 'd', 'e']
    // Firebase stores this
    {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
    // since the keys are numeric and sequential,
    // if we query the data, we get this
    ['a', 'b', 'c', 'd', 'e']
    

    即使它看起来像Firebase数据库中的对象,它也会在查询时作为数组返回 .

    因此,在Firebase数据库中创建架构是可行的,但它可能会为您的应用程序带来大量开销 .

  • 3

    事实上,Big Query仅支持换行符分隔的JSON或JSONL:https://cloud.google.com/bigquery/preparing-data-for-bigquery

    http://jsonlines.org/

    JSON Lines是一种方便的格式,用于存储可以一次处理一条记录的结构化数据 .

    要准备Firebase数据以便在Big Query中导入,我们只需要:

    • 从Firebase获取JSON(或在私有备份的情况下从 Cloud 存储桶获取)

    • 解析它以获取JS对象

    • 遍历每条记录,字符串化数据并添加行分隔符

    var dataForBigQuery = '';
        for (var i in dinosaurs) {
          dataForBigQuery+= JSON.stringify(dinosaurs[i]) + '\n';
        }
    
    • 将这些数据保存在新文件中 . 然后它就可以导入BigQuery了 .
  • 2

    在写这篇03/2017时,我可以确认Firebase实时数据库和BigQuery之间没有真正的集成 . 只有Firebase Analytics才能轻松导入BigQuery . 所有这些都没有在Firebase上明确解释......

    我们最终编写了自己的解决方案,但你可以查看这个有大约400颗星的Github repo,所以我假设有一些人发现它很有用......

相关问题