Created
April 30, 2021 04:32
-
-
Save harishsg99/d3c0215bf3ab60436dc3e6c4ba3a7572 to your computer and use it in GitHub Desktop.
This script allows me to upload csv into firestore
This file contains 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
import csv | |
import firebase_admin | |
import google.cloud | |
from firebase_admin import credentials, firestore | |
cred = credentials.Certificate("./ServiceAccountKey.json") | |
app = firebase_admin.initialize_app(cred) | |
store = firestore.client() | |
file_path = "CSV_FILE_PATH" | |
collection_name = "COLLECTION_TO_ADD_TO" | |
def batch_data(iterable, n=1): | |
l = len(iterable) | |
for ndx in range(0, l, n): | |
yield iterable[ndx:min(ndx + n, l)] | |
data = [] | |
headers = [] | |
with open(file_path) as csv_file: | |
csv_reader = csv.reader(csv_file, delimiter=',') | |
line_count = 0 | |
for row in csv_reader: | |
if line_count == 0: | |
for header in row: | |
headers.append(header) | |
line_count += 1 | |
else: | |
obj = {} | |
for idx, item in enumerate(row): | |
obj[headers[idx]] = item | |
data.append(obj) | |
line_count += 1 | |
print(f'Processed {line_count} lines.') | |
for batched_data in batch_data(data, 499): | |
batch = store.batch() | |
for data_item in batched_data: | |
doc_ref = store.collection(collection_name).document() | |
batch.set(doc_ref, data_item) | |
batch.commit() | |
print('Done') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment