【问题标题】:how to fetch firebase data?如何获取firebase数据?
【发布时间】:2021-03-20 02:01:55
【问题描述】:

我是 python 和 firebase 的新手,我正在尝试扁平化我的 firebase 数据库。

我有一个这种格式的数据库

每只猫都有成千上万的数据。我想要的只是获取猫的名字并将它们放入一个数组中。例如,我希望输出为 ['cat1','cat2'....]

我正在使用本教程 http://ozgur.github.io/python-firebase/

from firebase import firebase
firebase = firebase.FirebaseApplication('https://your_storage.firebaseio.com', None)
result = firebase.get('/Data', None)

上述代码的问题是它会尝试获取 Data 下的所有数据。我怎样才能只获取“猫”?

【问题讨论】:

    标签: python firebase firebase-realtime-database


    【解决方案1】:

    如果您想将猫中的值作为列获取,请尝试使用pyrebase,在cmd / anaconda prompt 使用pip install pyrebase(如果您没有设置PIP 或Python,则稍后会首选)环境路径。安装后:

    import pyrebase
    
    config {"apiKey": yourapikey
    "authDomain": yourapidomain
    "databaseURL": yourdatabaseurl,  
    "storageBucket": yourstoragebucket,  
    "serviceAccount": yourserviceaccount
    }
    

    注意:您可以在 Firebase 的控制台中找到以上所有信息: https://console.firebase.google.com/project/ >>> 你的项目 >>> 点击图标“”,标签为“将 firebase 添加到你的网络应用程序”

    回到代码...

    制作一个简洁的定义,以便您可以将其存储到 py 文件中:

    def connect_firebase():
    # add a way to encrypt those, I'm a starter myself and don't know how
    username: "usernameyoucreatedatfirebase"
    password: "passwordforaboveuser"
    
        firebase = pyrebase.initialize_app(config)
        auth = firebase.auth() 
    
        #authenticate a user > descobrir como não deixar hardcoded
        user = auth.sign_in_with_email_and_password(username, password)
    
        #user['idToken']
        # At pyrebase's git the author said the token expires every 1 hour, so it's needed to refresh it
        user = auth.refresh(user['refreshToken'])
    
        #set database
        db = firebase.database()
    
        return db
    

    好的,现在将它保存到一个整洁的.py 文件中

    接下来,在您的新笔记本或主 .py 中,您将导入 这个我们将调用的新 .py 文件 auth.py 从现在开始...

    from auth import *
    
    # add do a variable
    db = connect_firebase()
    
    #and now the hard/ easy part that took me a while to figure out:
    # notice the value inside the .child, it should be the parent name with all the cats keys
    values = db.child('cats').get()
    
    # adding all to a dataframe you'll need to use the .val()  
    data = pd.DataFrame(values.val())
    

    就是这样,print(data.head()) 检查值/列是否在预期的位置。

    【讨论】:

      【解决方案2】:

      Firebase 实时数据库为 one big JSON tree

      当您在数据库中的某个位置获取数据时,您还会检索 它的所有子节点。

      最佳做法是denormalize your data,为同一数据创建多个位置(节点):

      很多时候,您可以通过使用查询检索数据来对数据进行非规范化处理。 数据子集

      在您的情况下,您可以创建一个名为“类别”的第二个节点,您可以在其中“仅”列出类别名称。

      /cat1
          /...
      /cat2
          /...
      /cat3
          /...
      /cat4
          /...
      /categories
         /cat1
         /cat2
         /cat3
         /cat4
      

      在这种情况下,您可以使用update() method 同时写入多个位置。

      【讨论】:

      • 这正是我想要做的。我有数百个类别,所以我想以编程方式检索它们并为它们创建一个类别节点
      【解决方案3】:

      我正在探索pyrebase 文档。据此,我们可能只从某个路径中提取键。

      要仅返回特定路径的键,请使用 shallow() 方法。

      all_user_ids = db.child("users").shallow().get()
      

      在你的情况下,它会是这样的:

      firebase = pyrebase.initialize_app(config)
      db = firebase.database()
      
      allCats = db.child("data").shallow().get()
      

      如果没有帮助,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多