通过 BigQuery API 向 BigQuery 添加新数据的最佳方式是什么?

What is the best way to add new data to BigQuery through BigQuery API?

我使用 Django 作为我的后端框架来将我的 Web 应用程序与 BigQuery 连接起来。我的做法是在 views.py 中使用 BigQuery API 从 BQ 获取数据。到目前为止,根据我的研究,我发现了两种可以从我的 Django 添加数据到 BQ 的方法:

  1. 使用 insert_rows_json() 方法,我只需要 JSON 格式的数据,它会将数据附加到 BQ。
  2. 使用 to_gbq() 方法,我需要将数据放在 Pandas DataFrame 中,我可以包含参数 if_exists="replace" 来更新 BQ 上的现有表。

目前,对于添加新数据,我会使用方法1,对于更新和删除等其他操作,我会使用方法2[=35] =].

我的问题:如果我对所有操作都使用方法2会更好,还是我应该坚持使用方法1用于添加新数据和方法2用于其他操作?

或者也许 是否有任何其他方法可以更有效地让 Web 应用程序 运行 甚至更快?

引自此doc:

For new projects, we recommend using the BigQuery Storage Write API instead of the tabledata.insertAll method. The Storage Write API has lower pricing and more robust features, including exactly-once delivery semantics. The tabledata.insertAll method is still fully supported.

  • 您可以尝试 BigQuery Storage Write API 而不是传统的 insert_rows_json() 方法将数据流式传输到 BigQuery。它具有更低的价格和更强大的功能,包括 exactly-once 交付语义。如果您仍然需要使用遗留流 insert_rows_json() 方法,则可以使用它。 Google Cloud 仍然完全支持它。

  • 使用 insert_rows_json() 方法将数据流式传输到 BigQuery,因为这是推荐的方法并由 Google Cloud 维护。

  • 在流式插入后立即执行更新和删除查询时,您还可以通过 BigQuery client libraries. But, there are some limitations 在 BigQuery 中使用 DML 查询更新和删除 table 数据。

Rows that were written to a table recently by using streaming (the tabledata.insertall method or the Storage Write API) cannot be modified with UPDATE, DELETE, or MERGE statements. The recent writes are those that occur within the last 30 minutes. All other rows in the table remain modifiable by using UPDATE, DELETE, or MERGE statements. The streamed data can take up to 90 minutes to become available for copy operations.

  • 如果你还想使用to_gbq()方法来更新和删除table,你可以使用它。参考 here 你可以找到 pandas-gbqgoogle-cloud-bigquery 库之间的区别。