Last active
December 30, 2021 07:54
-
-
Save AgungPambudi/a604fa9f856fbe7b99f2e8f9044b7f2c to your computer and use it in GitHub Desktop.
data migration scripts that migrates data from excel files to an actual Firestore Collections database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install firebase-admin pandas xlrd==1.2.0 | |
import firebase_admin | |
from firebase_admin import credentials | |
from firebase_admin import firestore | |
from xlrd import open_workbook | |
credential_info = credentials.Certificate("path/to/service_account.json") | |
firebase_admin.initialize_app(credential_info) | |
client_database = firestore.client() | |
client_workbook = open_workbook("path/to/filename.xlsx") | |
values = [] | |
for sheet in client_workbook.sheets(): | |
for row in range(1, sheet.nrows): | |
column_names = sheet.row(0) | |
column_value = {} | |
for name, col in zip(column_names, range(sheet.ncols)): | |
value = sheet.cell(row, col).value | |
try: | |
value = str(value) | |
except: | |
pass | |
column_value[name.value] = value | |
client_database.collection("CollectionName").add(column_value) | |
values.append(column_value) | |
# using pandas | |
#---------------- | |
import firebase_admin | |
import pandas as pd | |
from firebase_admin import credentials, firestore | |
user_credential = credentials.Certificate("../credential.json") | |
default_app = firebase_admin.initialize_app(user_credential) | |
client_database = firestore.client(default_app) | |
database_collection = client_database.collection(u'table_name') | |
# df = pd.read_excel('../data/data.xlsx') | |
df = pd.read_csv(r'../data/data.csv') | |
for index, row in df.iterrows(): | |
print(row['Chapter']) | |
document_id = str(row['Chapter']) + "_" + str(row['Verse']) | |
data = { | |
'chapter': row['Chapter'], | |
'chapter_name': row['Chapter Name'], | |
'verse': row['Verse'], | |
'meaning': row['Meaning'], | |
'translation': row['Translation'] | |
} | |
database_collection.document(document_id).set(data) | |
print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment