"I want to use FireBase's Cloud Firestore with Python for the time being!" While following the [Official document] of FireBase (https://firebase.google.com/docs/firestore/quickstart?authuser=0), write the procedure when trying the basic "data input / output". I will.
A "project" is a "group" for using services such as Firestore, Cloud Functions, Authentication, and Hosting. Projects created with FireBase are also common to GCP.
Now let's create a project.
First, go to the FireBase Console (https://console.firebase.google.com). Click "Create Project" in the middle. Enter a name for your project and click Continue.
Step 2/3 will explain "Google Analytics (for Firebase projects)", so you can enable it or not. (Turn it off this time.)
Finally, click "Create Project" to start the creation process automatically.
After a short while, the message "New project is ready" will be displayed as shown below. Click "Continue".
Then, the screen will change to the console.
This completes the project creation.
In Cloud Firestore, "Document" to write "Data", There is a "collection" (like a folder) for keeping documents together. A "database" is a "box" that holds a collection.
.... it's difficult. If you're not sure, take a look at other people's very easy-to-understand articles!
Let's create a database first.
Click Database from the menu on the left. Then click "Create Database".
A dialog will appear, so select "Start in test mode" and then "Next". Leave the location selection and click Finish.
After provisioning and security settings are complete, the screen below automatically appears.
This completes the database creation.
Now let's get ready to stream the data.
Click "Start Collection" in the middle. A dialog will appear, so enter an arbitrary character string (collection name) in "Collection ID".
The "Document ID" can be any character string, and if you click "Automatic ID", it will be assigned appropriately. You can set the "field", but if you enter the data, it will be generated without permission.
This will create a collection and documents all at once, so you're ready to populate your data.
Finally, generate and download the API key for playing with FireBase from Python.
Click the gear mark on the upper left → "Project settings".
Then select "Service Account".
As it is, click "Generate New Private Key" and then click "Generate Key". The Json file will be downloaded, so keep it in a suitable location.
From here, I will leave FireBase and start working on the Python side. The code written here is taken from the Official Document and partially modified.
We already have a library for playing with FireBase from Python.
pip install firebase-admin
To install the FireBase library.
Initialize your Cloud Firestore SDK instance.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
cred = credentials.Certificate('Write the private key path.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
With this alone, the SDK is initialized and data can be input and output.
Now let's add the data to the database created in the previous step. Use the sample from the Official Documentation (https://firebase.google.com/docs/firestore/quickstart) as is. Please add the code below below the initialization code above!
doc_ref = db.collection(u'users').document(u'alovelace')
doc_ref.set({
u'first': u'Ada',
u'last': u'Lovelace',
u'born': 1815
})
A little commentary
db.collection (u'users')
... Which collection in the database to specify.document (u'alovelace')
... Which document in the previously specified collection to specify ʻU'first': u'Ada',` ... Add the value "Ada" to the "first" field
Let's do it!
If executed correctly, you should see something like this on the FireBase Console side.
Now, let's read the added data. Here, the character string is output as it is in the Python output window.
Under the initialization code
users_ref = db.collection(u'users')
docs = users_ref.stream()
for doc in docs:
print(u'{} => {}'.format(doc.id, doc.to_dict()))
If all goes well, you should see something like this:
Finally, let's delete the data.
I think the structure before deleting is as follows
To delete only the added field
city_ref = db.collection(u'users').document(u'alovelace')
city_ref.update({
u'born': firestore.DELETE_FIELD
})
This code removes the "born" field.
To delete the entire document
db.collection(u'users').document(u'alovelace').delete()
To delete the collection as well
def delete_collection(coll_ref, batch_size):
docs = coll_ref.limit(batch_size).get()
deleted = 0
for doc in docs:
print(u'Deleting doc {} => {}'.format(doc.id, doc.to_dict()))
doc.reference.delete()
deleted = deleted + 1
if deleted >= batch_size:
return delete_collection(coll_ref, batch_size)
(When you delete a collection, you need to get all the documents and delete them.)
This is the end of the basic operation.
I think it was a difficult article to read, but thank you for reading this far. Have a good FireBase life!
Recommended Posts