firebase - 定期将大数据 (json) 导入 Firebase

标签 firebase architecture firebase-realtime-database google-cloud-datastore google-cloud-functions

我们的情况是,我们必须定期更新 firebase 中的大量数据(大约 5 个 Mio 记录)。目前我们有一些大小约为 1 GB 的 json 文件。

由于现有的第三方解决方案( herehere )存在一些可靠性问题(每个对象导入对象;或需要开放连接)并且与谷歌云平台生态系统完全脱节。我想知道现在是否有“官方”使用方式,即新的谷歌云功能?或者与应用引擎/谷歌云存储/谷歌云数据存储的组合。

我真的不喜欢处理身份验证 - 云功能似乎处理得很好,但我认为该功能会超时(?)

使用新的 firebase 工具,如何:

  • 有长期运行的云函数来获取/插入数据吗? (有意义吗?)
  • 从谷歌云平台内部的某个地方获取 json 文件?
  • 首先将大数据放入 google-cloud-datastore 是否有意义(即存储在 firebase 中太贵了)或者 firebase 实时数据库是否可以可靠地作为大数据存储。
  • 最佳答案

    我终于发布了答案,因为它与 2017 年新的 Google Cloud Platform 工具保持一致。

    新推出的 Google Cloud Functions 的运行时间有限,约为 9 分钟 ( 540 seconds )。但是,云函数能够像这样从云存储创建 node.js 读取流( @googlecloud/storage on npm )

    var gcs = require('@google-cloud/storage')({
    // You don't need extra authentication when running the function
    // online in the same project
      projectId: 'grape-spaceship-123',
      keyFilename: '/path/to/keyfile.json'
    });
    
    // Reference an existing bucket. 
    var bucket = gcs.bucket('json-upload-bucket');
    
    var remoteReadStream = bucket.file('superlarge.json').createReadStream();
    

    即使它是远程流,它也是非常高效的。在测试中,我能够在 4 分钟内解析大于 3 GB 的 json,进行简单的 json 转换。

    当我们现在使用 node.js 流时,任何 JSONStream 库都可以有效地动态转换数据 ( JSONStream on npm ),异步处理数据,就像具有事件流 ( event-stream on npm ) 的大型数组一样。

    es = require('event-stream')
    
    remoteReadStream.pipe(JSONStream.parse('objects.*'))
      .pipe(es.map(function (data, callback(err, data)) {
        console.error(data)
        // Insert Data into Firebase.
        callback(null, data) // ! Return data if you want to make further transformations.
      }))
    

    在管道末端的回调中仅返回 null 以防止内存泄漏阻塞整个函数。

    如果您执行需要更长运行时间的更重的转换,请使用 firebase 中的“作业数据库”来跟踪您所在的位置,并且只执行 100.000 次转换并再次调用该函数,或者设置一个附加函数来监听插入一个“forimport db”,它最终将原始 jsons 对象记录异步转换为您的目标格式和生产系统。拆分导入和计算。

    此外,您可以在 nodejs 应用引擎中运行云函数代码。但不一定反过来。

    关于firebase - 定期将大数据 (json) 导入 Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44398442/

    相关文章:

    具有多个持久层的 PHP DataMapper

    java - Android 使用带有空格的键访问 Firebase

    xcode - 无法构建 Objective-C 模块 'Firebase' 和 'FirebaseAnalytics/FirebaseAnalytics.h' 文件未找到

    javascript - 在 npm 上使用 firebase 进行部署 --prefix $RESOURCE_DIR 运行 lint 花费的时间太长

    web-applications - 为移动和 Web 应用程序设计通用服务器端

    c# - 具有相同泛型方法的抽象类和接口(interface)

    java - 如何仅在 Firebase 数据库发生更改时调用通知?

    swift - 将 Firebase Remote Config 与 Swift 结合使用时,从 'FIRRemoteConfigValue!' 转换为不相关类型 'String' 始终失败

    swift - Firestore 对 Order By 的查询无效?

    ios - 如何更新 Firebase-Database 树中的最新条目?