| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Sample(s) showing how to use Google Cloud Pub/Sub with Google Cloud Dataflow.
Install the Cloud SDK.
Note: This is not required in Cloud Shell since it already has the Cloud SDK pre-installed.
Create a new Google Cloud project via the New Project page, or via the gcloud command line tool.
export PROJECT_ID=your-google-cloud-project-id
gcloud projects create $PROJECT_IDSetup the Cloud SDK to your GCP project.
gcloud initEnable the APIs: Dataflow, Compute Engine, Cloud Logging, Cloud Storage, Cloud Storage JSON, Pub/Sub, Cloud Scheduler, Cloud Resource Manager, and App Engine.
Create a service account JSON key via the Create service account key page, or via the gcloud command line tool. Here is how to do it through the Create service account key page.
Alternatively, you can use gcloud through the command line.
export PROJECT_ID=$(gcloud config get-value project)
export SERVICE_ACCOUNT_NAME=samples
export IAM_ACCOUNT=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com
# Create the service account.
gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \
--display-name $SERVICE_ACCOUNT_NAME
# Set the role to Project Owner (*).
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:$IAM_ACCOUNT \
--role roles/owner
# Create a JSON file with the service account credentials.
gcloud iam service-accounts keys create path/to/your/credentials.json \
--iam-account=$IAM_ACCOUNT(*) Note: The Role field authorizes your service account to access resources. You can view and change this field later by using the GCP Console IAM page. If you are developing a production app, specify more granular permissions than Project > Owner. For more information, see Granting roles to service accounts.
For more information, see Creating and managing service accounts.
Set your GOOGLE_APPLICATION_CREDENTIALS environment variable to point to your service account key file.
export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/credentials.jsonCreate a Cloud Storage bucket.
export BUCKET_ID=your-gcs-bucket-id
gcloud storage buckets create gs://$BUCKET_IDStart a Google Cloud Scheduler job that publishes one message to a Google Cloud Pub/Sub topic every minute. This will create an App Engine app if one has never been created on the project.
export TOPIC_ID=your-topic-id
# Create a Pub/Sub topic.
gcloud pubsub topics create $TOPIC_ID
# Create a Cloud Scheduler job
gcloud scheduler jobs create pubsub publisher-job --schedule="* * * * *" \
--topic=cron-topic --message-body="Hello!"
# Run the job.
gcloud scheduler jobs run publisher-jobThe following instructions will help you prepare your development environment.
Clone the python-docs-samples repository.
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.gitNavigate to the sample code directory.
cd python-docs-samples/pubsub/streaming-analyticsCreate a virtual environment and activate it.
virtualenv env
source env/bin/activateOnce you are finished with the tutorial, you can deactivate the virtualenv and go back to your global Python environment by running deactivate.
pip install -U -r requirements.txtThe following example will run a streaming pipeline. The pipeline does the following:
python PubSubToGCS.py \
--project=$PROJECT_ID \
--region=us-central1 \
--input_topic=projects/$PROJECT_ID/topics/$TOPIC_ID \
--output_path=gs://$BUCKET_ID/samples/output \
--runner=DataflowRunner \
--window_size=1 \
# If set, you will write up to `num_shards` files per window to GCS.
# --num_shards=2 \
--temp_location=gs://$BUCKET_ID/tempAfter the job has been submitted, you can check its status in the GCP Console Dataflow page.
You can also check the output to your GCS bucket using the command line below or in the GCP Console Storage page. You may need to wait a few minutes for the files to appear.
gcloud storage ls gs://$BUCKET_ID/samples/Delete the Google Cloud Scheduler job.
gcloud scheduler jobs delete publisher-jobCtrl+C to stop the program in your terminal. Note that this does not actually stop the job if you use DataflowRunner.
If you use DirectRunner, you can skip this step. Stop the Dataflow job in GCP Console Dataflow page. Cancel the job instead of draining it. This may take some minutes.
Delete the topic. Google Cloud Dataflow will automatically delete the subscription associated with the streaming pipeline when the job is canceled.
gcloud pubsub topics delete $TOPIC_IDLastly, to avoid incurring charges to your GCP account for the resources created in this tutorial:
# Delete only the files created by this sample.
gcloud storage rm --recursive --continue-on-error "gs://$BUCKET_ID/samples/output*"
# [optional] Remove the Cloud Storage bucket.
gcloud storage buckets delete gs://$BUCKET_ID| Back | FazBrowse Home | New Git URL |