# Orchestrating MongoDB & BigQuery for ML Excellence with PyMongoArrow and BigQuery Pandas Libraries

In today's data-driven world, the ability to analyze and efficiently move data across different platforms is crucial. MongoDB Atlas and Google BigQuery are two powerful platforms frequently used for managing and analyzing data. While they excel in their respective domains, connecting and transferring data between them seamlessly can pose challenges. However, with the right tools and techniques, this integration becomes not only possible but also streamlined.

One effective way to establish a smooth pipeline between MongoDB Atlas and BigQuery is by leveraging PyMongoArrow and pandas-gbq, two powerful Python libraries that facilitate data transfer and manipulation. PyMongoArrow acts as a bridge between MongoDB and Arrow, a columnar in-memory analytics layer, enabling efficient data conversion. On the other hand, pandas-gbq is a Python client library for Google BigQuery, allowing easy interaction with BigQuery datasets.

 on data read from both Google BigQuery and MongoDB Atlas platforms without physically moving the data between these platforms. This will simplify the effort required by data engineers to move the data and offers a faster way for data scientists to build machine learning (ML) models.

Let's discuss each of the implementation advantages with examples.

### ETL data from MongoDB to BigQuery

Let’s consider a sample shipwreck dataset available on MongoDB Atlas for this use case.

Use the commands below to install the required libraries on the notebook environment of your choice. For easy and scalable setup, use BigQuery Jupyter notebooks or managed VertexAI workbench notebooks.

 for setting up your cluster, network access, and authentication. Load a sample dataset to your Atlas cluster. Get the Atlas connection string and replace the URI string below with your connection string. The below script is also available in the GitHub repository with steps to set up.

```python
#Read data from MongoDB
import certifi
import pprint 
import pymongo
import pymongoarrow 
from pymongo import MongoClient

client = MongoClient("URI ``sting``",tlsCAFile=certifi.where())

#Initialize database and collection
db = client.get_database("sample_geospatial")
col = db.get_collection("shipwrecks")

for doc in col.find({}): 
  pprint.pprint(doc)

from pymongoarrow.monkey import patch_all 
patch_all()

#Create Dataframe for data read from MongoDB
import pandas as pd 
df = col.find_pandas_all({})
```

Transform the data to the required format — e.g., transform and remove the unsupported data formats, like the MongoDB object ID, or convert the MongoDB object to JSON before writing it to BigQuery. Please refer to the documentation to learn more about data types supported by pandas-gbq and PyMongoArrow.

```python
#Transform the schema for required format. 
#e.g. the object id is not supported in dataframes can be removed or converted to string. 

del(df"_id"])
```

Once you have retrieved data from MongoDB Atlas and converted it into a suitable format using PyMongoArrow, you can proceed to transfer it to BigQuery using either the pandas-gbq or google-cloud-bigquery. In this article, we are using pandas-gbq. Refer to the [documentation for more details on the differences between pandas-gbq and google-cloud-bigquery libraries. Ensure you have a dataset in BigQuery to which you want to load the MongoDB data. You can create a new dataset or use an existing one.

```python
#Write the transformed data to BigQuery.

import pandas_gbq

pandas_gbq.to_gbq(df0:100], "gcp-project-name.bigquery-dataset-name.bigquery-table-name", project_id="gcp-project-name")
```

As you embark on building your pipeline, optimizing the data transfer process between MongoDB Atlas and BigQuery is essential for performance. A few points to consider:

1. Batch Dataframes in to chunks, especially when dealing with large datasets, to prevent memory issues.
1. Handle schema mapping and data type conversions properly to ensure compatibility between the source and destination databases.
1. With the right tools like Google colab, VertexAI Workbench etc, this pipeline can become a cornerstone of your data ecosystem, facilitating smooth and reliable data movement between MongoDB Atlas and Google BigQuery.

### Introduction to Google BigQuery DataFrames (bigframes)

Google bigframes is a Python API that provides a pandas-compatible DataFrame and machine learning capabilities powered by the BigQuery engine. It provides a familiar pandas interface for data manipulation and analysis. Once the data from MongoDB is written into BigQuery, the BigQuery DataFrames can unlock the user-friendly solution for analyzing petabytes of data with ease. The pandas DataFrame can be read directly into BigQuery DataFrames using the Python bigframes.pandas library. Install the bigframes library to use BigQuery DataFrames.

```
!pip install bigframes
```

Before reading the pandas DataFrames into BigQuery DataFrames, rename the columns as per Google's [schema guidelines. (Please note that at the time of publication, the feature may not be GA).

```python
import bigframes.pandas as bpd
bigframes.options.bigquery.project = "GCP project ID"

# df = 
bdf = bpd.read_pandas(df)
```

For more information on using Google Cloud Bigquery DataFrames, visit the Google Cloud documentation.

## Conclusion

Creating a robust pipeline between MongoDB Atlas and BigQuery using PyMongoArrow and pandas-gbq opens up a world of possibilities for efficient data movement and analysis. This integration allows for the seamless transfer of data, enabling organizations to leverage the strengths of both platforms for comprehensive data analytics and decision-making.

### Further reading

- Learn more about MongoDB PyMongoArrow libraries and how to use them.
- Read more about Google BigQuery DataFrames for pandas and ML.
- Load DataFrames to BigQuery with Google pandas-gbq.

  [1]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt2557413e5cba18f3/65c3cbd20872227d14497236/image1.png
  [2]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt7459376aeef23b01/65c3cbd2245ed9a8b190fd38/image2.png