Think of it as having done the following: Check items differ depending on the contract status, so it's easy.
--Enable Firestore --Issuing API key (can be done with firebase) --firebase is in native mode
The delimiter is ",". Occasionally there are csv files that do not follow the rules, so please be careful about that.
This time it is a simple configuration. I think of unique as a primary key, and there is no duplication.
DB configuration
unique ---------- item1
| | a : 1
| | b : 2
| ∟ c : 3
|
|--------- item1
| | a : 4
| | b : 5
| ∟ c : 6
|
∟ --------- item1
| a : 7
| b : 8
∟ c : 9
I am doing the following.
--Data reading --firestore variable initialization --Register one by one in the firestore
sample.py
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import csv
#Data reading
def read_data(file_name):
csv_data = None
with open(file_name) as r:
reader =csv.reader(r)
csv_data = [row for row in reader]#Stored in a variable as a two-dimensional array
print('read end')
return csv_data
#firebase initialization process
def firebase_init(api_key):
api_key = credentials.Certificate(api_key)
#It seems that the previous initialization state remains, so first delete the previous initialization state
if(len(firebase_admin._apps)):
firebase_admin.delete_app(firebase_admin.get_app())
firebase_admin.initialize_app(api_key)
print("init end")
#Main processing / data registration
def exe(data):
db = firestore.client()#firestore client variable
header = data[0]#Get header
doc_base = db.collection(header[0])#Set unique place
print("proc start")
for i in range(1, len(data)):#Since the data is from the second line, from 1
doc_ref = doc_base.document(data[i][0])# a,b,Set at c
buf = {}#buffer
for j in range(1, len(data[i])):
buf[header[j]] = data[i][j]
doc_ref.set(buf) #Save to firestore
print("poc end")
def main():
file_name = "csv filename"
api_key = r'api_key'
#File reading
data = read_data(file_name)
#firebase initialization
firebase_init(api_key)
#Main processing
exe(data)
if __name__=="__main__":
main()
This time it is a batch registration in a clean state, but I think that it is better to collectively acquire data from the firestore on the way, check the difference and update to the firestore. Since the number of api calls is reduced, isn't the charge reduced? I haven't made a trial calculation.
Recommended Posts