从Firestore导出json

miz*_*z-k 24 firebase google-cloud-firestore

由于我们可以在Firebase RTDB控制台下载json文件,有没有办法导出Firestore集合/文档数据的json文件?

我的主要目标之一是比较更新文档之前/之后的数据.

Dan*_*ath 20

没有,你需要提出自己的过程,例如查询集合和循环所有内容.

更新

截至2018年8月7日,我们确实有一个托管导出系统,允许您将数据转储到GCS存储桶中.虽然这不是JSON,但它的格式与Cloud Datastore使用的格式相同,因此BigQuery了解它.这意味着您可以将其导入BigQuery.

  • 导出文件的格式是什么?如何查看内容? (7认同)
  • @ dan-mcgrath您能否指向有关子集合的任何信息,以及如何能够在BigQuery中一次有效地查询这些信息? (2认同)

Dal*_*yen 14

我刚为Firestore写了一个备份和恢复.你可以尝试我的GitHub.

https://github.com/dalenguyen/firestore-import-export

或者只使用NPM包中的备份和还原:

https://github.com/dalenguyen/firestore-backup-restore

谢谢,

  • 惊人的tysm (2认同)

Gun*_*ner 11

谷歌让它变得比它需要的更难,所以社区找到了一个解决方法。如果你已经npm安装,你可以这样做:

出口

npx -p node-firestore-import-export firestore-export -a credentials.json -b backup.json
Run Code Online (Sandbox Code Playgroud)

进口

npx -p node-firestore-import-export firestore-import -a credentials.json -b backup.json
Run Code Online (Sandbox Code Playgroud)

来源

  • 这是获取 json 格式的数据并截至该日期有效的最简单的方法。您需要获取 GCP 服务帐户凭据,如 https://cloud.google.com/docs/authentication/Production 所示<br/>更多文档:https://www.npmjs.com/package/firestore-export -进口 (3认同)
  • @sh_ark 是的,它计入您项目的读/写 (2认同)

Hez*_*Ziv 8

有一个用于 firestore 导出/导入的 npm

要导出的项目 转到 -> 项目设置 -> 服务帐户 -> 生成新的私钥 -> 将其另存为exportedDB.json

要导入的项目 转到 -> 项目设置 -> 服务帐户 -> 生成新的私钥 -> 将其另存为 ImportedDB.json

从保存文件的文件夹运行这两个命令

导出: npx -p node-firestore-import-export firestore-export -a导出的DB.json -b backup.json

导入: npx -p node-firestore-import-export firestore-import -a importDB.json -b backup.json


jlo*_*sli 7

我编写了一个遍历数据库集合/文档并将所有内容导出到单个json文件中的工具。此外,它还将导入相同的结构(有助于克隆/移动Firestore数据库)。由于有几个同事使用该代码,所以我认为我将其发布为NPM软件包。随时尝试并提供一些反馈。

https://www.npmjs.com/package/node-firestore-import-export


Rob*_*oli 7

如果有人想要使用Python 23的解决方案。

编辑:请注意,这不会备份规则

https://github.com/RobinManoli/python-firebase-admin-firestore-backup上分叉

首先安装和设置 Firebase Admin Python SDK:https ://firebase.google.com/docs/admin/setup

然后在你的python环境中安装它:

pip install firebase-admin
Run Code Online (Sandbox Code Playgroud)

安装 Firestore 模块:

pip install google-cloud-core
pip install google-cloud-firestore
Run Code Online (Sandbox Code Playgroud)

(来自ImportError: Failed to import the Cloud Firestore library for Python

Python代码

# -*- coding: UTF-8 -*-

import firebase_admin
from firebase_admin import credentials, firestore
import json

cred = credentials.Certificate('xxxxx-adminsdk-xxxxx-xxxxxxx.json') # from firebase project settings
default_app = firebase_admin.initialize_app(cred, {
    'databaseURL' : 'https://xxxxx.firebaseio.com'
})

db = firebase_admin.firestore.client()

# add your collections manually
collection_names = ['myFirstCollection', 'mySecondCollection']
collections = dict()
dict4json = dict()
n_documents = 0

for collection in collection_names:
    collections[collection] = db.collection(collection).get()
    dict4json[collection] = {}
    for document in collections[collection]:
        docdict = document.to_dict()
        dict4json[collection][document.id] = docdict
        n_documents += 1

jsonfromdict = json.dumps(dict4json)

path_filename = "/mypath/databases/firestore.json"
print "Downloaded %d collections, %d documents and now writing %d json characters to %s" % ( len(collection_names), n_documents, len(jsonfromdict), path_filename )
with open(path_filename, 'w') as the_file:
    the_file.write(jsonfromdict)
Run Code Online (Sandbox Code Playgroud)