Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Dataflow: FirestoreIO for Python SDK

Hello people!

I have documents stored in Firestore and was wondering if there is a native FirestoreIO in Apache Beam for Python SDK. On the internet I could only find that there is a FirestoreIO but for Java. Maybe I can accomplish my task using multi language data pipelines.

I read you in the comments.

PS: I've already asked Duet AI. This is its response. (Warning: a little hallucination ahead.)

Prompt: I'm using Apache Beam to create my data pipelines. I want to create a PCollection from a Document in Firestore using Python SDK. Give me a step by step tutorial on how to accomplish this task

Response:

import apache_beam as beam
import firestore

# Create a FirestoreIO source object.
source = beam.io.FirestoreIO(collection='my-collection', document='my-document')

# Create a PCollection object by calling the `read()` method on the `FirestoreIO` source object.
pcoll = source.read()

# Use the `PCollection` object to perform data processing operations.
pcoll | 'Filter' >> beam.Map(lambda x: x if x['field1'] == 'value1' else None) | 'Write to BigQuery' >> beam.io.WriteToBigQuery(table='my-table')

--
Best regards
David Regalado
Web | Linkedin | Cloudskillsboost

0 3 661
3 REPLIES 3

Hi @davidregalado25 ,

Currently, Beam's Python SDK does not include a native FirestoreIO connector. This feature is currently only available in the Java SDK. However,  there are several alternative methods you can explore to work with Firestore data in your Python Beam pipelines:

Multi-language Pipelines:

An intriguing option is to use Java for the FirestoreIO part of your pipeline and then integrate it with your Python pipeline using the ExternalTransform interface. While this approach is somewhat more complex, it allows you to leverage the FirestoreIO connector available in the Java SDK, bringing the best of both worlds into your project.

Third-party Libraries:

You might find it useful to explore third-party libraries that offer Firestore integration for Beam in Python. A notable example is the python-beam-firestore library, which provides functionalities for reading from and writing to Firestore. It's important to consider the compatibility of these libraries with the latest versions of Apache Beam and Firestore, as well as their overall stability and support.

Custom Code:

If you're up for a challenge and wish for more tailored control, developing your own custom connector using the Python SDK for Apache Beam is a viable path. This approach demands a robust understanding of both Apache Beam and Firestore APIs but offers unparalleled customization and flexibility.

Alternative Approaches:

  • Another practical solution is to utilize the Firestore REST API or the Python Firestore client library directly within your Beam pipeline. This method may require some additional data transformation steps and considerations for performance and scalability, but it can be an effective workaround.

As for the code snippet provided by Duet AI, which references beam.io.FirestoreIO in the Python SDK, is not accurate.

 

Multi-language Pipelines:

Where can I follow a step-by-step guide on this?

Third-party Libraries:

I'll definitely check out thepython-beam-firestore library.

Custom Code:

This is appealing to me. I'll try it later. But first I need to deliver the project I'm working on quickly.

Alternative Approaches:

I'll try this if there's something wrong with the third-party library you just recommended.

Thank you for your kindly response.

--
Best regards
David Regalado
Web | Linkedin | Cloudskillsboost

Hi @davidregalado25 ,

Here's a detailed roadmap to guide you through setting up and using a multi-language Beam pipeline, integrating FirestoreIO in Java with your Python code:

  1. Environment Setup:

    • Install the necessary SDKs:
      • Java SDK for Apache Beam, including the FirestoreIO connector.
      • Python SDK for Apache Beam.
    • Choose an appropriate mechanism for multi-language support in Beam. While Apache Spark and gRPC are valid for general inter-process communication, Apache Beam's multi-language pipelines typically use the ExternalTransform class for this purpose.
  2. Defining the Pipeline:

    • Start by setting up your Python Beam pipeline.
    • Create a Beam ExternalTransform object in Python. This object will reference the Java code that implements the FirestoreIO logic.
    • Configure the ExternalTransform with the necessary parameters, such as the Firestore collection path and any desired processing steps.
    • Incorporate the ExternalTransform within your Python pipeline to apply Firestore operations to your PCollection.
  3. Java Code for FirestoreIO:

    • Develop a Java class that implements the DoFn interface for data processing.
    • Utilize the FirestoreIO connector within this class to read from and interact with the specified Firestore collection.
    • Process and transform the data as required within your Java DoFn logic.
    • Expose this Java DoFn as an ExternalTransform using the Beam Java SDK, making it accessible from the Python pipeline.
  4. Running the Pipeline:

    • Execute your Python Beam pipeline as usual.
    • The ExternalTransform will trigger the Java DoFn, facilitating interaction with Firestore and processing the data accordingly.
    • The processed data is then seamlessly returned to your Python pipeline for any further downstream operations.

Here are some additional resources for setting up and using multi-language pipelines with Apache Beam and Firestore:

Official Beam Documentation: