How to import CSV or JSON to firebase cloud firestore

前端 未结 11 996
星月不相逢
星月不相逢 2020-11-29 16:19

Is there a way to import CSV or JSON to firebase cloud firestore like in firebase realtime database?

相关标签:
11条回答
  • 2020-11-29 16:53

    This workaround in Python may help some people. First convert json or csv to dataframe using Pandas, then convert dataframe to dictionary and upload dictionary to firestore.

    import firebase_admin as fb
    from firebase_admin import firestore
    import pandas as pd
    
    cred = fb.credentials.Certificate('my_credentials_certificate.json')
    default_app = fb.initialize_app(cred)
    db = firestore.client()
    
    df = pd.read_csv('data.csv')
    dict = df.to_dict(orient='records')
    
    my_doc_ref = db.collection('my_collection').document('my_document')
    my_doc_ref.set(dict)
    

    There could be similar workarounds in javascript and other languages using libraries similar to Pandas.

    0 讨论(0)
  • 2020-11-29 16:54

    General Solution

    I've found many takes on a script allowing to upload a JSON but none of them allowed sub-collections. My script above handles any level of nesting and sub-collections. It also handles the case where a document has its own data and sub-collections. This is based on the assumption that collection is array/object of objects (including an empty object or array).

    To run the script make sure you have npm and node installed. Then run your code as node <name of the file>. Note, there is no need to deploy it as a cloud funciton.

    const admin = require('../functions/node_modules/firebase-admin');
    const serviceAccount = require("./service-key.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://<your-database-name>.firebaseio.com"
    });
    
    const data = require("./fakedb.json");
    
    /**
     * Data is a collection if
     *  - it has a odd depth
     *  - contains only objects or contains no objects.
     */
    function isCollection(data, path, depth) {
      if (
        typeof data != 'object' ||
        data == null ||
        data.length === 0 ||
        isEmpty(data)
      ) {
        return false;
      }
    
      for (const key in data) {
        if (typeof data[key] != 'object' || data[key] == null) {
          // If there is at least one non-object item in the data then it cannot be collection.
          return false;
        }
      }
    
      return true;
    }
    
    // Checks if object is empty.
    function isEmpty(obj) {
      for(const key in obj) {
        if(obj.hasOwnProperty(key)) {
          return false;
        }
      }
      return true;
    }
    
    async function upload(data, path) {
      return await admin.firestore()
        .doc(path.join('/'))
        .set(data)
        .then(() => console.log(`Document ${path.join('/')} uploaded.`))
        .catch(() => console.error(`Could not write document ${path.join('/')}.`));
    }
    
    /**
     *
     */
    async function resolve(data, path = []) {
      if (path.length > 0 && path.length % 2 == 0) {
        // Document's length of path is always even, however, one of keys can actually be a collection.
    
        // Copy an object.
        const documentData = Object.assign({}, data);
    
        for (const key in data) {
          // Resolve each collection and remove it from document data.
          if (isCollection(data[key], [...path, key])) {
            // Remove a collection from the document data.
            delete documentData[key];
            // Resolve a colleciton.
            resolve(data[key], [...path, key]);
          }
        }
    
        // If document is empty then it means it only consisted of collections.
        if (!isEmpty(documentData)) {
          // Upload a document free of collections.
          await upload(documentData, path);
        }
      } else {
        // Collection's length of is always odd.
        for (const key in data) {
          // Resolve each collection.
          await resolve(data[key], [...path, key]);
        }
      }
    }
    
    resolve(data);
    
    0 讨论(0)
  • 2020-11-29 16:54

    No as of now, you can't.. firestore structures data into a different format that is, using collections and each collection has a series of documents which then are stored in JSON format.. in future they might make a tool to convert JSON in to firestore.. for reference check this out

    :https://cloud.google.com/firestore/docs/concepts/structure-data

    ****EDIT :****

    You can automate the process up to some extent, that is, write a mock software which only pushes the fields of your CSV or JSON data into your Cloud Firestore DB. I migrated my whole database my making just a simple app which retrieved my DB and pushed it on Firestore

    0 讨论(0)
  • 2020-11-29 16:57

    I used the General Solution provided by Maciej Caputa. Thank you (:

    Here are a few hints. Assuming that you have an Ionic Firebase application installed with the required Firebase node modules in the functions folder inside that solution. This is a standard Ionic Firebase install. I created an import folder to hold the script and data at the same level.

    Folder Hierarchy

    myIonicApp
        functions
            node_modules
                firebase-admin
    ImportFolder
        script.js
        FirebaseIonicTest-a1b2c3d4e5.json
        fileToImport.json
    

    Script Parameters

    const admin = require('../myIonicApp/functions/node_modules/firebase-admin'); //path to firebase-admin module
    const serviceAccount = require("./FirebaseTest-xxxxxxxxxx.json"); //service account key file
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://fir-test-xxxxxx.firebaseio.com" //Your domain from the hosting tab
    });
    

    Creating the Service Account Key File

    • In the Firebase console for your project, next to the Project Overwiew item, click on the gear icon and select Users and permissions
    • At the bottom of the screen, click Advanced permission settings

    • This opens another tab for the Google Cloud Platform Console
    • On the left select the Service Accounts item
    • Create a Service Account for an existing Service Account

    I simply added a key to the App Engine default service account

    The Create key function will offer to download the key to a JSON file

    JSON Data Structure

    To use the script provided, the data structure must be as follows:

    {
      "myCollection" : {
        "UniqueKey1" : {
          "field1" : "foo",
          "field2" : "bar"
        },{
        "UniqueKey2" : {
          "field1" : "fog",
          "field2" : "buzz"
        }...
    }
    
    0 讨论(0)
  • 2020-11-29 17:00

    https://gist.github.com/JoeRoddy/1c706b77ca676bfc0983880f6e9aa8c8

    This should work for an object of objects (generally how old firebase json is set up). You can add that code to an app that's already configured with Firestore.

    Just make sure you have it pointing to the correct JSON file.

    Good luck!

    0 讨论(0)
  • 2020-11-29 17:01

    This is a small modification that copies the 'id' of the document, if exists, to its path. Otherwise it will use the "for"'s index.

      ...
      ...
      } else {
        // Collection's length of is always odd.
        for (const key in data) {
          // Resolve each collection.
          if (data[key]['id']!==undefined)
            await resolve(data[key], [...path,data[key]['id']])
          else 
            await resolve(data[key], [...path, key]);
        }
      }
    }
    
    resolve(data);
    
    0 讨论(0)
提交回复
热议问题