👨🏼‍💻Migrate Firebase Realtime database to Firebase Firestore(Android / Kotlin)

Appcaesars
Huawei Developers
Published in
2 min readMar 22, 2022

Before you start:

Check the ‘Realtime database vs Firestore’ article link below and make sure about whether you should make migration or not.

https://firebase.google.com/docs/database/rtdb-vs-firestore

Introduction:

In this article , I will be talking about how I migrated my Firebase Realtime database to Firebase Firestore.

1) I exported my Realtime database:

Realtime export.
Realtime exported as opened with notepad.
Exported my json file to my app -> res -> raw(create raw directory if you don’t have one inside the res).
Deleted DB name for my scenario necessity (“EventTrackerFireBaseDb”)(This approach can be different for your use case).
Added Firebase Firestore sdk (I assume you already added Realtime database, so you only need to add Firestore).

2) Helper class for migrating Realtime database to Firestore:

This class can be modified according to your usecase(I had a ArrayList that needs to be changed to LinkedTreeMap)(Timber is a logging library you can use different logging methods for checking success of method).

3) Reading json file method:

4) Reading json file and passing to my helper class:

Important note: I used coroutines for creating asynchronous threads for reading json file and migrating Realtime database to Firestore, you can use other asynchronous methods!

Also I called this method on my application class and you only need to call once, after migration you can delete or comment this code block!

5) Output:

Realtime database

Migrated into ->

Firestore

6) Conclusion:

In this article I tried to give information about migrating Firebase Realtime database to Firebase Firestore. Since our database designs are different so the approaches and the codes can be different. So I hope this article give you some insights and ideas how can you do it. Happy coding :)

References:

--

--