Collectively register data in Firestore using csv file in Python

Overview

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

data structure

csv file format

The delimiter is ",". Occasionally there are csv files that do not follow the rules, so please be careful about that.

testimage.jpg

DB configuration

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

program

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

in conclusion

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

Collectively register data in Firestore using csv file in Python
Get Youtube data in Python using Youtube Data API
Csv in python
[Python] Read a csv file with a large data size using a generator
Create a GIF file using Pillow in Python
Read table data in PDF file with Python
I tried reading a CSV file using Python
Transpose CSV file in Python Part 2: Performance measurement
Create a MIDI file in Python using pretty_midi
Speed evaluation of CSV file output in Python
File operations in Python
File processing in Python
Data analysis using Python 0
Data cleaning using Python
File operations in Python
Read Python csv file
Create a data collection bot in Python using Selenium
Full-width and half-width processing of CSV data in Python
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
Get LEAD data using Marketo's REST API in Python
Create your first GDSII file in Python using gdspy
Process csv data with python (count processing using pandas)
Handle Ambient data in Python
Download the file in Python
Display UTM-30LX data in Python
Data analysis using python pandas
Translate using googletrans in Python
Download csv file with python
Using Python mode in Processing
[Python] Open the csv file in the folder specified by pandas
Inflating text data by retranslation using google translate in Python
Output Excel data in separate writing using Python3 + xlrd + mecab
Solve the Japanese problem when using the CSV module in Python.
Graph time series data in Python using pandas and matplotlib
Transpose CSV files in Python Part 1
[Python] Loading csv files using pandas
GUI programming in Python using Appjar
Precautions when using pit in Python
Data acquisition using python googlemap api
[Python] Write to csv file with Python
Get data from Quandl in Python
Save the binary file in Python
Linebot creation & file sharing in Python
Output to csv file with Python
Handle NetCDF format data in Python
Try using LevelDB in Python (plyvel)
Create a binary file in Python
Parsing CSV format data using SQL
Try PLC register access in Python
Using global variables in python functions
Python CSV file reading and writing
Hashing data in R and Python
[Python] How to store a csv file as one-dimensional array data
Let's see using input in python
Infinite product in Python (using functools)
Extract the targz file using python
Edit videos in Python using MoviePy
ORC, Parquet file operations in Python
Handwriting recognition using KNN in Python
Try using Leap Motion in Python
Depth-first search using stack in Python