Harnessing the power of Google dataflow: Processing data from diverse sources

Google DataFlow @ Freshers.in

Google Dataflow, a robust data processing service that can seamlessly process data from different sources. In this article, we delve into the flexibility of Google Dataflow and illustrate its capabilities through practical examples.

Diverse Data Sources and Google Dataflow

Google Dataflow is designed to integrate with various data sources seamlessly. From traditional databases to streaming services and cloud-based storage solutions, Dataflow provides connectors and integrations that simplify the data processing journey. Some popular data sources include:

Google Cloud Storage (GCS): An object storage service perfect for a range of applications from storing backups to serving website content.

BigQuery: Google’s fully-managed, petabyte-scale, and cost-effective analytics warehouse.

Pub/Sub: A real-time messaging service that allows you to send and receive messages between independent applications.

Relational databases: Through JDBC connectors, Dataflow can access traditional databases.

Third-party sources: With open-source connectors, Dataflow can pull in data from places like Apache Kafka or Cassandra.

Example: Integrating Data from GCS and Pub/Sub into BigQuery

This scenario involves reading streaming data from Pub/Sub, combining it with batch data in GCS, and then writing the aggregated results into BigQuery.


Create a Pub/Sub topic and subscription.

Upload your batch data to a GCS bucket.

Create a destination table in BigQuery for aggregated results.

Deploying the Dataflow Pipeline:

from apache_beam import Pipeline, io, options
from apache_beam.options.pipeline_options import PipelineOptions
pipeline_options = PipelineOptions()
with Pipeline(options=pipeline_options) as pipeline:
    # Read from Pub/Sub
    streaming_data = (
        | 'Read from PubSub' >> io.ReadFromPubSub(subscription='your-subscription-name')
    # Read from GCS
    batch_data = (
        | 'Read from GCS' >> io.ReadFromText('gs://your-gcs-bucket-name/your-data-file.txt')
    # Let's assume some transformations (e.g., combining, aggregating) occur here...
    processed_data = (
        | 'Combine with Batch' >> YourCombineFunction(batch_data)
    # Write to BigQuery
    processed_data | 'Write to BigQuery' >> io.WriteToBigQuery(

YourCombineFunction is a placeholder for a Beam transform function you would write to combine or aggregate the data as needed.

The purpose of YourCombineFunction is to take two datasets, one from Pub/Sub (streaming data) and the other from GCS (batch data), and merge or aggregate them in some manner. Here’s a simple example:

Let’s say our streaming data from Pub/Sub are user clicks, and our batch data from GCS is user profile information. Our goal might be to enrich the click data with user profile details.

First, we’d parse the incoming data:

The Pub/Sub messages might be JSON formatted user clicks: {“user_id”: “85554525”, “clicked_url”: “https://www.freshers.in”}
The GCS data might be CSV lines of user profiles: “85554525,Sachin,Rohan,sachin@freshers.in”
Given the above, here’s how we might implement YourCombineFunction:

import apache_beam as beam

class ParsePubSub(beam.DoFn):
    def process(self, element):
        import json
        return [json.loads(element)]

class ParseGCS(beam.DoFn):
    def process(self, element):
        parts = element.split(',')
        user_id, first_name, last_name, email = parts
        return [{
            "user_id": user_id,
            "first_name": first_name,
            "last_name": last_name,
            "email": email

class EnrichClicksWithUserProfile(beam.DoFn):
    def __init__(self, user_profiles):
        self.user_profiles = user_profiles

    def process(self, click):
        user_profile = self.user_profiles.get(click["user_id"], {})
        click["first_name"] = user_profile.get("first_name")
        click["last_name"] = user_profile.get("last_name")
        click["email"] = user_profile.get("email")
        return [click]

def run():
    with beam.Pipeline() as pipeline:
        user_profiles = (
            | "Read from GCS" >> beam.io.ReadFromText('gs://your-gcs-bucket-name/user-profiles.txt')
            | "Parse GCS Data" >> beam.ParDo(ParseGCS())
            | "Convert to Dict" >> beam.Map(lambda x: (x["user_id"], x))
        enriched_clicks = (
            | "Read from PubSub" >> beam.io.ReadFromPubSub(subscription='your-subscription-name')
            | "Parse PubSub Data" >> beam.ParDo(ParsePubSub())
            | "Enrich Click Data" >> beam.ParDo(EnrichClicksWithUserProfile(beam.pvalue.AsDict(user_profiles)))
        enriched_clicks | 'Write to BigQuery' >> ...

In this example:

We’ve used DoFn (Do Function) to define our transformations.
The ParsePubSub function parses the Pub/Sub messages into a Python dictionary.
The ParseGCS function parses the CSV lines from GCS into a dictionary.
The EnrichClicksWithUserProfile function takes each click event and enriches it with user profile information.
We use the AsDict method to convert the user profiles into a dictionary, which allows for efficient lookups by user_id.
This is a basic example, and there are more optimal ways to manage larger datasets, like using side inputs or joins, but this provides a basic idea of how you might combine streaming and batch data in Beam.

Author: user

Leave a Reply