Create a streaming pipeline using a Dataflow template
This quickstart shows you how to create a streaming pipeline using a Google-provided Dataflow template. Specifically, this quickstart uses the Pub/Sub to BigQuery template as an example.
The Pub/Sub to BigQuery template is a streaming pipeline that can read JSON-formatted messages from a Pub/Sub topic and write them to a BigQuery table.
To follow step-by-step guidance for this task directly in the Google Cloud console, click Guide me:
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataflow, Compute Engine, Cloud Logging, Cloud Storage, Google Cloud Storage JSON, BigQuery, Pub/Sub, and Resource Manager APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataflow, Compute Engine, Cloud Logging, Cloud Storage, Google Cloud Storage JSON, BigQuery, Pub/Sub, and Resource Manager APIs.
- Create a Cloud Storage bucket:
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click Create bucket.
- On the Create a bucket page, enter your bucket information. To go to the next
step, click Continue.
- For Name your bucket, enter a unique bucket name. Don't include sensitive information in the bucket name, because the bucket namespace is global and publicly visible.
-
For Choose where to store your data, do the following:
- Select a Location type option.
- Select a Location option.
- For Choose a default storage class for your data, select the following: Standard.
- For Choose how to control access to objects, select an Access control option.
- For Advanced settings (optional), specify an encryption method, a retention policy, or bucket labels.
- Click Create.
- Copy the following, as you need them in a later section:
- Your Cloud Storage bucket name.
- Your Google Cloud project ID.
To find this ID, see Identifying projects.
To complete the steps in this quickstart, your user account must have the Dataflow Admin role and the Service Account User role. The Compute Engine default service account must have the Dataflow Worker role, the Storage Object Admin role, the Pub/Sub Editor role, the BigQuery Data Editor role, and the Viewer role. To add the required roles in the Google Cloud console:
- Go to the IAM page and select your project.
Go to IAM - In the row containing your user account, click Edit principal. Click Add another role, and add the following roles: Dataflow Admin and Service Account User.
- Click Save.
- In the row containing the Compute Engine default service account (PROJECT_NUMBER[email protected]), click Edit principal.
- Click Add another role, and add the following roles: Dataflow Worker, Storage Object Admin, Pub/Sub Editor, BigQuery Data Editor, Viewer.
Click Save.
For more information about granting roles, see Grant an IAM role by using the console.
- Go to the IAM page and select your project.
- By default, each new project starts with a
default network.
If the default network for your project is disabled
or was deleted, you need to have a network in your project for which your user account has the
Compute Network User role
(
roles/compute.networkUser
).
Create a BigQuery dataset and table
Create a BigQuery dataset and table with the appropriate schema for your Pub/Sub topic using the Google Cloud console.
In this example, the name of the dataset is taxirides
and the name of the
table is realtime
. To create this dataset and table, follow these steps:
- Go to the BigQuery page.
Go to BigQuery - In the Explorer panel, next to the project where you want to create the dataset, click View actions, and then click Create dataset.
- On the Create dataset panel, follow these steps:
- For Dataset ID, enter
taxirides
. Dataset IDs are unique for each Google Cloud project. - For Location type, choose Multi-region, and then select US (multiple regions in United States). Public datasets are stored in
the
US
multi-region location. For simplicity, place your dataset in the same location. - Leave the other default settings, and then click Create dataset
- In the
Explorer panel, expand your project. - Next to your
taxirides
dataset, click View actions, and then click Create table. - On the Create table panel, follow these steps:
- In the Source section, for Create table from, select Empty table.
- In the Destination section, for Table, enter
realtime
. - In the Schema section, click the Edit as text toggle and paste
the following schema definition into the box:
ride_id:string,point_idx:integer,latitude:float,longitude:float,timestamp:timestamp, meter_reading:float,meter_increment:float,ride_status:string,passenger_count:integer
- In the Partition and cluster settings section, for Partitioning, select the timestamp field.
- Leave the other default settings in place and click Create table.
Run the pipeline
Run a streaming pipeline using the Google-provided Pub/Sub to BigQuery template. The pipeline gets incoming data from the input topic.
- Go to the Dataflow Jobs page.
Go to Jobs - Click
Create job from template . - Enter
taxi-data
as the Job name for your Dataflow job. - For Dataflow template, select the Pub/Sub to BigQuery template.
- For BigQuery output table, enter the following:
PROJECT_ID:taxirides.realtime
Replace
PROJECT_ID
with the project ID of the project where you created your BigQuery dataset. - In the section Optional source parameters, for Input Pub/Sub topic, click Enter topic manually.
- In the dialog, for Topic name enter the following, and then click Save:
projects/pubsub-public-data/topics/taxirides-realtime
This publicly available Pub/Sub topic is based on the NYC Taxi & Limousine Commission's open dataset. The following is a sample message from this topic, in the JSON format:
{ "ride_id": "19c41fc4-e362-4be5-9d06-435a7dc9ba8e", "point_idx": 217, "latitude": 40.75399, "longitude": -73.96302, "timestamp": "2021-03-08T02:29:09.66644-05:00", "meter_reading": 6.293821, "meter_increment": 0.029003782, "ride_status": "enroute", "passenger_count": 1 }
- For Temp location, enter the following:
gs://BUCKET_NAME/temp/
Replace
BUCKET_NAME
with the name of your Cloud Storage bucket. Thetemp
folder stores temporary files, like the staged pipeline job. - If your project does not have a default network, enter a Network and a Subnetwork. For more information, see Specify a network and subnetwork.
- Click Run job.
View your results
To view the data written to yourrealtime
table, follow these steps:
Go to the BigQuery page.
Click
Compose a new query. A new Editor tab opens.SELECT * FROM `PROJECT_ID.taxirides.realtime` WHERE `timestamp` > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY) LIMIT 1000
Replace
PROJECT_ID
with the project ID of the project where you created your BigQuery dataset. It can take up to five minutes for data to start appearing in your table.Click Run.
The query returns rows that have been added to your table in the past 24 hours. You can also run queries using standard SQL.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.
Delete the project
The easiest way to eliminate billing is to delete the Google Cloud project that you created for the quickstart.- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Delete the individual resources
If you want to keep the Google Cloud project that you used in this quickstart, then delete the individual resources:
- Go to the Dataflow Jobs page.
Go to Jobs - Select your streaming job from the job list.
- In the navigation, click Stop.
- In the Stop job dialog, either cancel or drain your pipeline, and then click Stop job.
- Go to the BigQuery page.
Go to BigQuery - In the Explorer panel, expand your project.
- Next to the dataset you want to delete, click View actions, and then click Open.
- In the details panel, click Delete dataset, and then follow the instructions.
- In the Google Cloud console, go to the Cloud Storage Buckets page.
- Click the checkbox for the bucket that you want to delete.
- To delete the bucket, click Delete, and then follow the instructions.