【问题标题】:Fast Way of Writing Data to Firestore from BigQuery从 BigQuery 将数据写入 Firestore 的快速方法
【发布时间】:2021-04-07 14:44:55
【问题描述】:

我需要每天从 BigQuery 将大约 1000 万条数据加载到 Firestore。哪种方法最快?

具有并行单个写入的云功能是一个选项(根据以下链接),但在这种情况下,并行化 bigquery 表将是一个挑战。

What is the fastest way to write a lot of documents to Firestore?

Dataflow是否在这种场景下工作,通过Dataflow读写数据?

【问题讨论】:

    标签: google-cloud-firestore google-cloud-dataflow


    【解决方案1】:

    Dataflow 在这种情况下有效。它让您可以并行化从 BigQuery 读取数据并将其写入 Firestore 的方式。

    将 Firestore 接收器添加到 Beam 的工作正在进行中。它应该可用于 Beam 2.31.0 中的 Java SDK:请参阅 https://github.com/apache/beam/pull/14261

    与此同时,您也许可以自己动手:在 Python 中会是这样:

    (p 
     | ReadFromBigQuery(...)
     | GroupIntoBatches(50)  # Batches of 50-500 elements will help with throughput
     | ParDo(WriteToFirestoreDoFn())
    

    你写自己的WriteToFirestoreDoFn 来做这样的事情:

    class WriteToFirestoreDoFn(DoFn):
      def __init__(self, firestore_info):
        self.client = None
        self.firestore_info = firestore_info
      
      def process(self, batch):
        if not self.client:
          self.client = firestore.Client(self.firestore_info)
        self.client.write_data(batch)
    

    这是一个小小的伪代码,但它应该可以帮助您开始使用您想要的东西。

    【讨论】:

    • 太好了,现在有什么性能数据可以分享吗?什么是最好的批量大小?根据 firestore doc,每秒最多可以对数据库进行 10k 次写入
    • 我不确定,因为我不太了解 Firestore。我以前使用过 500 条记录插入,但可能会有所不同。我建议你测试一下。我想 150 条记录可能是合理的,并且可以让您的转换达到高并行度
    猜你喜欢
    • 2018-04-30
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    相关资源
    最近更新 更多