Last active
March 12, 2020 00:22
-
-
Save hiranya911/7cb8408bc24cfda39473b09244bc906c to your computer and use it in GitHub Desktop.
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
from itertools import islice | |
import firebase_admin | |
from firebase_admin import credentials | |
from firebase_admin import db | |
from firebase_admin import firestore | |
def _split(data, size=500): | |
"""Splits a dictionary into a sequence of smaller dictionaries.""" | |
it = iter(data) | |
for i in range(0, len(data), size): | |
yield {k:data[k] for k in islice(it, size)} | |
def transform_rooms(rooms, members): | |
for room_id in rooms: | |
member_data = members.get(room_id) | |
if member_data: | |
rooms[room_id]['members'] = member_data | |
return rooms | |
def transform_messages(messages): | |
unwrapped = {} | |
for room_id, messages_dict in messages.items(): | |
for message_id, message in messages_dict.items(): | |
key = (room_id, message_id) | |
unwrapped[key] = message | |
return unwrapped | |
def load_rooms(client, rooms): | |
rooms_collection = client.collection('rooms') | |
for rooms_batch in _split(rooms): | |
batch_op = client.batch() | |
for room_id, room_data in rooms_batch.items(): | |
doc = rooms_collection.document(room_id) | |
batch_op.set(doc, room_data) | |
batch_op.commit() | |
def load_messages(client, messages): | |
rooms_collection = client.collection('rooms') | |
for messages_batch in _split(messages): | |
batch_op = client.batch() | |
for ((room_id, msg_id), msg_data) in messages_batch.items(): | |
doc = rooms_collection.document(room_id).collection('messages').document(msg_id) | |
batch_op.set(doc, msg_data) | |
batch_op.commit() | |
if __name__ == '__main__': | |
cred = credentials.Certificate('path/to/service_account.json') | |
firebase_admin.initialize_app(cred, options={ | |
'databaseURL': 'https://my-project-id.firebaseio.com' | |
}) | |
# Extract | |
rooms = db.reference('rooms').get() | |
members = db.reference('members').get() | |
messages = db.reference('messages').get() | |
# Transform | |
rooms = transform_rooms(rooms, members) | |
messages = transform_messages(messages) | |
# Load | |
client = firestore.client() | |
load_rooms(client, rooms) | |
load_messages(client, messages) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if we want to load a json data file to cloud firestore or RTDB in Firebase?