Skip to content

Instantly share code, notes, and snippets.

@rickcrawford
Created September 6, 2018 16:39
Show Gist options
  • Save rickcrawford/8a13b9d4185f43c8c143a48175b1cd5b to your computer and use it in GitHub Desktop.
Save rickcrawford/8a13b9d4185f43c8c143a48175b1cd5b to your computer and use it in GitHub Desktop.
Launch a dataflow template reference
from __future__ import print_function
from apiclient.discovery import build
from oauth2client import service_account
import httplib2
# https://cloud.google.com/dataflow/docs/templates/executing-templates#example-1-custom-template-batch-job
# https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.templates/launch
# https://cloud.google.com/dataflow/docs/templates/executing-templates#using-gcloud
projectId = "my-project-id"
gcsPath = "gs://bucket/folder"
location = "us-central1"
body = {
"jobName": "my-job-name",
"parameters": {
"instanceId": "instance-id",
"databaseId": "spanner-db-id",
"outputDir": "gs://bucket/output"
},
"environment": {
"network": "my-network",
"subnetwork": "regions/us-central1/subnetworks/privatesubnet",
"maxWorkers": 10,
"tempLocation": "gs://bucket/tmp"
}
}
# https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.templates/launch#authorization
SCOPES = [
'https://www.googleapis.com/auth/compute',
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/userinfo.email'
]
credentials = service_account.ServiceAccountCredentials.from_json_keyfile_name(
filename="secret.json", scopes=SCOPES)
http = credentials.authorize(httplib2.Http())
service = build('dataflow', 'v1b3', http=http)
# https://developers.google.com/resources/api-libraries/documentation/dataflow/v1b3/python/latest/dataflow_v1b3.projects.templates.html#launch
results = service.projects().templates().launch(projectId=projectId, location=location, gcsPath=gcsPath, body=body).execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment