首页 文章

从Firestore导出json

提问于
浏览
9

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

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

5 回答

  • 12

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

    更新

    截至2018年8月7日,我们确实有一个允许您将数据转储到GCS存储桶的managed export system . 虽然这不是JSON,但它的格式与Cloud Datastore使用的格式相同,因此BigQuery了解它 . 这意味着你可以import it into BigQuery .

  • 2

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

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

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

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

    谢谢,

  • 7

    Firestore还处于早期开发阶段,请查看docs on backups以获取有关Firestore的任何信息 .

    我发现这个npm包,node-firestore-backup,简单实用 .

    请注意, --accountCredentials path/to/credentials/file.json 指的是服务帐户密钥json文件,您可以按照https://developers.google.com/identity/protocols/application-default-credentials中的说明获取该文件 .

    转到API控制台凭据页面 . 从项目下拉列表中,选择您的项目 . 在“凭据”页面上,选择“创建凭据”下拉列表,然后选择“服务帐户密钥” . 从“服务帐户”下拉列表中,选择现有服务帐户或创建新帐户 . 对于Key type,选择JSON键选项,然后选择Create . 该文件会自动下载到您的计算机 . 将刚刚下载的* .json文件放在您选择的目录中 . 此目录必须是私有的(您不能让任何人访问此目录),但可以访问您的Web服务器代码 .

  • 2

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

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

  • 0

    如果有人想要使用 Python 2 的解决方案 .

    叉上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
    

    安装Firestore模块:

    pip install google-cloud-core
    pip install google-cloud-firestore
    

    (来自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)
    

相关问题