what is Dataflow? how to connect pub/sub with Dataflow and BigQuery for real-time data processing
hi everyone!
as the title says, i will try to explain google cloud dataflow in this article. why? well, let’s just say i struggled quite a bit while figuring it out! so, i decided to document everything i learned to make it easier to understand (and maybe help a few of you out there too) 😄
introduction
google cloud’s dataflow is a fully managed, serverless stream and batch data processing service. it allows developers and data engineers to create and manage data pipelines with ease, utilizing apache beam as its core engine. dataflow is versatile, powerful, and scalable, making it ideal for big data processing. it automates resource provisioning and scaling, and integrates seamlessly with other google cloud services, making it an invaluable tool for processing, transforming, and analyzing large datasets in real-time.
in this article, we’ll look into what dataflow is, explore how it works with google cloud pub/sub to handle streaming data, and connect it with bigquery for efficient analytics and storage. if you’re working with real-time data and looking for scalable ways to process it, dataflow can be a game-changer.
what is google cloud dataflow?
google cloud dataflow is built on apache beam, an open-source framework for data processing pipelines. with dataflow, you can develop pipelines for both batch and real-time data processing. some of the key features include:
scalability: dataflow automatically scales to handle varying data loads, meaning it can handle huge volumes of data without requiring additional configuration.
real-time processing: it supports real-time data processing, making it possible to analyze data as it arrives, perfect for applications that need instant insights.
integrated with google cloud: dataflow works with other google cloud services, such as bigquery, cloud storage, pub/sub, and bigtable, simplifying data management across the platform.
why use dataflow?
efficiency and speed: dataflow is optimized for low-latency processing, making it ideal for real-time applications.
cost-effective: it provides a pay-as-you-go pricing model, allowing you to only pay for what you use.
support for complex transformations: using apache beam, dataflow can perform sophisticated transformations, aggregations, and data joins across your data pipelines.
connecting pub/sub to dataflow
if you want to know more about what exactly pub sub is then you can checkout my previous article here.
by connecting pub/sub to dataflow, you can create a powerful streaming pipeline that processes data in real-time as it’s ingested. here’s a step-by-step guide to setting it up:
prerequisites
a google cloud project with billing enabled
pub/sub, dataflow, and bigquery apis enabled
basic understanding of google cloud’s console or command line
step 1: create a pub/sub topic and subscription
go to the google cloud console and navigate to pub/sub.
click on create topic, name it (e.g.,
real-time-events
), and save it.under your topic, create a subscription that dataflow can connect to. name it something meaningful, like
real-time-events-sub
.
step 2: create a dataflow pipeline
to build your dataflow pipeline, you’ll write code using apache beam in python or java, or you can use the google cloud console for simpler templates.
if coding, set up an apache beam pipeline that reads from pub/sub, processes data, and writes to bigquery.
here’s a sample code snippet in python:
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.io.gcp.pubsub import ReadFromPubSub
from apache_beam.io.gcp.bigquery import WriteToBigQuery
class ProcessData(beam.DoFn):
def process(self, element):
# decode message, add any processing logic here
decoded_element = element.decode('utf-8')
yield {'data': decoded_element}
pipeline_options = PipelineOptions(
project='your_project_id',
region='your_region',
runner='DataflowRunner',
temp_location='gs://your_bucket/temp'
)
with beam.Pipeline(options=pipeline_options) as pipeline:
(pipeline
| 'Read from Pub/Sub' >> ReadFromPubSub(subscription='projects/your_project_id/subscriptions/real-time-events-sub')
| 'Process Data' >> beam.ParDo(ProcessData())
| 'Write to BigQuery' >> WriteToBigQuery(
table='your_project_id:your_dataset.your_table',
schema='data:STRING'
))
in this pipeline:
ReadFromPubSub reads the data from pub/sub.
ProcessData allows for any custom transformation you’d like to apply to the data.
WriteToBigQuery writes the processed data to bigquery.
run this pipeline to deploy it on dataflow, and it will automatically start processing messages from pub/sub and sending them to bigquery.
step 3: configuring bigquery
go to bigquery in your google cloud console.
create a new dataset and table where dataflow will write data.
define the schema to match the data format coming from dataflow, in this case, a single field for data
(STRING type).
common use cases
real-time monitoring: track events from iot devices, apps, or servers.
fraud detection: process and analyze transactional data instantly to detect anomalies.
log analysis: ingest and analyze application logs for real-time error detection and diagnostics.
user behavior tracking: capture and analyze user events on your platform for personalization and engagement.
closing note
setting up a real-time data pipeline with pub/sub, dataflow, and bigquery is an effective way to manage and analyze data as soon as it’s available. with this setup, you can achieve high-throughput, low-latency processing that scales automatically with your data needs. whether for analytics, monitoring, or real-time decision-making, this pipeline can help you handle data at any scale, delivering insights precisely when they’re needed.
further resources
see ya, keep building :)
always eager to learn and connect with like-minded individuals. happy to connect with you all here!
Subscribe to my newsletter
Read articles from Vinayak Gavariya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Vinayak Gavariya
Vinayak Gavariya
Machine Learning Engineer