导出 Firestore 数据为 JSON 文件

Google 的 Firestore Console 无法将 Firestore 数据导出为 CSV/JSON 等格式的文件,经过调研,需要先将 Firestore 数据导出到 GCS,然后再导入 BigQuery 进行格式化,将格式化后的文件导出到 GCS,最后下载到本地。

也可以通过调用 SDK 的方式写代码进行导出。

导出步骤

以下命令都在 Google Cloud Shell 中执行。

创建一个 GCS Bucket 存放导出的数据:

1
gcloud storage buckets create gs://warner/firestore-export

创建一个 BigQuery 数据集:

1
bq mk --dataset warner-firestore-export

导出 Firestore 数据到 GCS Bucket:

1
gcloud firestore export gs://warner/firestore-export/<collection_id> --collection-ids=<collection_id>

导入 BigQuery 数据集中:

1
2
# warner_firestore_export.<collection_id> 意思为 <数据集名称>.<表名称>,此处以 collection_id 作为表名
bq load --replace --source_format=DATASTORE_BACKUP warner_firestore_export.<collection_id> gs://warner/firestore-export/<collection_id>/all_namespaces/kind_<collection_id>/all_namespaces_kind_<collection_id>.export_metadata

格式化为 JSON 并导出到 GCS Bucket 中:

1
bq extract --destination_format NEWLINE_DELIMITED_JSON 'warner_firestore_export.<collection_id>' gs://warner/firestore-export/<collection_id>.json

随后就可以在 UI 中进行下载,如果 JSON 文件较多,也可以通过命令批量拷贝,随后下载整个目录即可:

1
2
3
4
5
6
7
mkdir firestore_json_files

gsutil -m cp \
"gs://warner/firestore-export/aaa.json" \
"gs://warner/firestore-export/bbb.json" \
"gs://warner/firestore-export/ccc.json" \
./firestore_json_files
Author

Warner Chen

Posted on

2026-01-04

Updated on

2026-01-04

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.