Migrating Google Firebase Firestore Database to Azure Cosmos DB
Published Sep 28 2023 01:05 AM 5,716 Views
Iron Contributor

kevin_comba_0-1695861024203.png

 

Introduction

 

In today's rapidly evolving digital landscape, data is the lifeblood of any application or service. Developers and businesses often find themselves needing to adapt and scale their databases to meet changing requirements. One common scenario is the migration from Firebase Firestore, a popular NoSQL database, to Azure Cosmos DB, Microsoft's globally distributed, multi-model database service. This migration can be driven by various factors, such as performance optimization, scalability needs, compliance requirements, or a shift in the cloud ecosystem.

 

In this comprehensive guide, we will take you on a journey through the process of migrating your data from Firebase Firestore to Azure Cosmos DB. We will begin by exploring the fundamental differences and structures of these two databases. Understanding the intricacies of both Firestore and Cosmos DB is crucial for a successful migration, as it will enable you to make informed decisions and minimize potential pitfalls along the way.

 

What does the blog entail?

 

  • Brief overview of what is Firebase Firestore and Azure cosmos db.
  • Configure the Firebase Firestore database for export.
  • Export the Firebase Firestore data to a JSON file.
  • Transform/modify Firebase Firestore JSON object.
  • Provision Azure Cosmos DB.
  • Use the Azure Cosmos DB Data Migration Tool to import the JSON file into Azure Cosmos DB.

What is Firebase Firestore?

 

Firestore Overview

 

Firebase Firestore is a NoSQL, document-oriented database that is part of the Firebase platform, a cloud-based mobile and web application development platform by Google. Firestore is designed for real-time data synchronization, scalability, and seamless integration with other Firebase services.


Firestore Data Model

 

Firestore stores data in collections, which are analogous to tables in traditional relational databases. Each collection contains a set of documents, and documents are the individual data units in Firestore. Documents are represented in JSON format, making it easy to work with structured data.

Firestore's data model includes collections, documents, and fields:

  • Collections: Collections are containers for documents and are often used to group related data. Collections are dynamic, meaning you can create them on the fly as you add documents.
  • Documents: Documents are individual records within collections. Each document has a unique identifier and contains a set of key-value pairs.
  • Fields: Fields are the key-value pairs within a document. Firestore supports various data types, including strings, numbers, Booleans, arrays, and nested objects.

Firestore's flexible data model and real-time capabilities make it a popular choice for building modern web and mobile applications.

What is Azure Cosmos DB?

 

Azure Cosmos DB Overview

 

Azure Cosmos DB is a fully managed NoSQL and relational database for modern app development. It is a globally distributed, multi-model database service provided by Microsoft Azure. It is designed for high availability, low latency, and seamless scaling across multiple regions. Cosmos DB supports multiple data models, including document, key-value, graph, column-family, and table, making it suitable for a wide range of applications.


Azure Cosmos DB APIs

 

Azure Cosmos DB offers multiple database APIs, which include NoSQL, MongoDB, PostgreSQL Cassandra, Gremlin, and Table. These APIs allow your applications to treat Azure Cosmos DB as if it were various other database technologies, without the overhead of management, and scaling approaches. Azure Cosmos DB helps you to use the ecosystems, tools, and skills you already have for data modelling and querying with its various APIs.

kevin_comba_0-1695851976990.png

 

For this blog, we are going to focus on API FOR NOSQL since it’s the most compatible API with our scenario.

 

Cosmos DB Data Model

 

Azure Cosmos DB's data model is rooted in its support for multiple data models, but for the purpose of this migration, we will focus on the document data model, which is most like Firestore.

  • Databases: In Cosmos DB, databases are top-level containers for your data. Each database can contain multiple collections.
  • Collections: Collections are containers for documents, similar to Firestore collections. Collections in Cosmos DB can have different partition keys, enabling efficient distribution of data.
  • Documents: Documents in Cosmos DB are similar to Firestore documents. They are JSON objects with unique identifiers and are stored within collections.
  • Partitioning: Cosmos DB uses a partitioning mechanism to distribute data across physical partitions for scalability. Choosing the right partition key is crucial for optimal performance.
  • Cosmos DB's multi-region replication and global distribution capabilities make it a powerful choice for applications with worldwide user bases.

 

Our Project Structure

 

Firebase Firestore Database

 

We are going to focus on a task management system database. This is the database structure we are going to use.

 

kevin_comba_2-1695852994826.png

 

 

 

 

 

We have two collections tasksCollection and usersCollections. Each has three documents with ids like task1-id, task2-id, task3-id, user1-id, user2-id, user3-id.

kevin_comba_1-1695852950708.png

 

 

 

 

Each document has various fields comprised of key and value pairs.

 

Screenshot 2023-09-28 025810.png

 

Requirements to get started.

 

 

Configure the Firebase Firestore database for export.

 

  • Assuming you have logged into the Firebase Firestore database, and you have populated the data above on your database. You will have the below database structure. TasksCollection with 3 documents and usersCollection with 3 documents.

kevin_comba_79-1695856398239.png

 

 

kevin_comba_80-1695856431597.png

 

  • Click project overview, select project settings.

kevin_comba_81-1695856485082.png

 

 

  • Click service accounts and then new private key. 

kevin_comba_82-1695856534912.png

 

  • Clicking that button will generate this JSON file. The name may differ but what matters is the content of that file. It contains credentials which should be kept secret. We are going to use them to access our database.

kevin_comba_83-1695856576931.png

 

 

Rename the Json file to appConfig.json and open that folder in VS Code. Then add this command on the terminal ` npx -p node-firestore-import-export firestore-export -a appConfig.json -b firebaseExportedDBFile.json`. This will use appConfig.json data to export our database to firebaseExportedDBFile.json.

 

Export the Firebase Firestore data to a JSON file.

 

kevin_comba_7-1695853313578.png

 

  • If you get this prompt, just accept.

kevin_comba_84-1695856670155.png

  • Wait till all collections are exported in the file.

kevin_comba_10-1695853585364.png

 

  • After opening the file, we have all our data into two collections.

kevin_comba_9-1695853517409.png

Transform/modify Firebase Firestore JSON object.

  • We need to make the ids to be part of Json object and make the collections elements be an array of objects.

kevin_comba_8-1695853474072.png

  • We need to transform our collections i.e. task collection into an array of objects.

kevin_comba_13-1695853708483.png

  • Clone this repo, using 
    git clone https://github.com/kelcho-spense/Firebase_to_Cosmos_DB_JSON_Object_Parser.git​

kevin_comba_14-1695853751759.png

 

Move firebaseExportedDBFile.json inside the folder we cloned. Make sure the file matches the filename we are importing. The function below uses loops and node.js fs module to remove and restructure our and export all the collections inside the original firebaseExportedDBFile.json into collection files.

 

kevin_comba_15-1695853787072.png

 

Run 

 

cd Firebase_to_Cosmos_DB_JSON_Object_Parser

 

on the terminal to change directory, then run 

 

yarn or npm install

 

 to install the node packages. Then to start the node.js local server

 

yarn start or npm start

 

kevin_comba_19-1695853905811.png

  • This will produce two collections.

kevin_comba_20-1695853949238.png

 

 

 

kevin_comba_0-1695858340638.png

 

 

Provision Azure Cosmos DB.

 

Search for Azure Cosmos DB and select the first option.

kevin_comba_22-1695854300462.png

 

  • Click create.

kevin_comba_25-1695854405973.png

 

 

 

  • Click create Azure Cosmos DB for NoSQL.

kevin_comba_26-1695854436306.png

  • Create a new resource group and account name, the click next.

kevin_comba_39-1695854622990.png

 

 

 

  • Leave this as default or enable these options for geo back up.

kevin_comba_40-1695854657557.png

 

  • Select all networks and TLS 1.0 (this will work with the current tool we will use to import data to cosmosdb)

kevin_comba_41-1695854774033.png

 

  • Let’s go with the defaults or select your appropriate options for periodic back up policy.

kevin_comba_42-1695854829362.png

 

  • Select service-management key.

kevin_comba_43-1695854857238.png

 

 

  • Click create to provision our services.

kevin_comba_44-1695854889857.png

 

 

  • Go to resource.

kevin_comba_45-1695854921313.png

 

  • Click data explorer and create new database.

kevin_comba_46-1695854953396.png

 

 

  • Provide database id and click okey.

kevin_comba_47-1695854988170.png

 

 

  • Hover the three dots next to our DB name and click on new container.

kevin_comba_48-1695855016134.png

 

 

  • Provide container id as our collection name and partition key for the tasksCollection.

kevin_comba_51-1695855118655.png

 

 

  • Do the same for the usersCollection.

kevin_comba_52-1695855165703.png

 

 

Use the Azure Cosmos DB Data Migration Tool to import the JSON file into Azure Cosmos DB.

 

  • Visit this link, download the zip file and extract the files.

kevin_comba_78-1695856164174.png

 

  • After extracting run the dtui.exe file. Using this tool am going to import my two collection files.

kevin_comba_77-1695856128902.png

 

  • Click add files, select tasksCollection.json. Click next.

kevin_comba_76-1695856097558.png

 

  • Back to the portal, click keys and click the eye icon on primary connection string then copy this secret string.

kevin_comba_75-1695856065660.png

 

 

  • Back to Azure Cosmos DB data migration tool, paste the primary connection string in the connection string. Add Database=taskManagementDB and click verify to test coonection. If connection is okey add the collectionName and the partition key which you want to map our JSON data. Click next.

kevin_comba_74-1695856036542.png

 

  • Click next.

kevin_comba_73-1695856012312.png

 

  • Confirm everything is okey and click next.

kevin_comba_72-1695855986154.png

 

  • When successful you should see the number of transferred documents.

kevin_comba_71-1695855955262.png

 

 

  • Let’s confirm this in the portal. Go to data explorer, click TasksCollection, items then id and you will see the details of each document(JSON object).

kevin_comba_70-1695855927135.png

 

  • To see all data, click on new SQL Query icon then click execute query button. All the records(documents) will be visible.

kevin_comba_69-1695855887285.png

 

 

  • Repeat the above process with usersCollection.

kevin_comba_68-1695855851457.png

 

  • And there you have it, we have successfully imported both collections from firebase.

kevin_comba_67-1695855351472.png

 

NB: you can as well import the original JSON file we exported from Firestore DB, but I just wanted us to simulate the file structure in firebase.

kevin_comba_66-1695855318545.png

 

 

Check these resources for more information.

 

 

 

 

 

Co-Authors
Version history
Last update:
‎Sep 27 2023 11:26 PM
Updated by: