2

I want to execute a Google Dataflow Template using PYTHON. Actually, I have been executing Dataflow Templates using the Dataflow REST API or the Cloud Functions Integration. This is my Dataflow template execution in Postman:

URL: https://dataflow.googleapis.com/v1b3/projects/{{my-project-id}}/templates:launch?gcsPath=gs://{{my-cloud-storage-bucket}}/temp/cloud-dataprep-template

    {
    "jobName": "test-datfalow-job",
    "parameters": {
        "inputLocations" : "{\"location1\":\"gs://{{my-cloud-storage-bucket}}/my-folder/**/*\"}",
        "outputLocations": "{\"location1\":\"gs://{{my-cloud-storage-bucket}}/my-output/output.csv\"}"
    },
    "environment": {
        "tempLocation": "gs://{{my-cloud-storage-bucket}}/tmp",
        "zone": "us-central1-f"
    }
}

I don't know if there's any chance to use the google-api-python-client or I have to execute this HTTP POST using python's requests.post and Google Cloud Authentication

bigbounty
  • 16,526
  • 5
  • 37
  • 65
Viento
  • 109
  • 2
  • 12

1 Answers1

8

You can do that using the template launch method from the Dataflow API Client Library for Python like so:

import googleapiclient.discovery
from oauth2client.client import GoogleCredentials

project = PROJECT_ID
location = LOCATION

credentials = GoogleCredentials.get_application_default()

dataflow = googleapiclient.discovery.build('dataflow', 'v1b3', credentials=credentials)
result = dataflow.projects().templates().launch(
        projectId=project,
        body={
          "environment": {
            "zone": "us-central1-f",
            "tempLocation": "gs://{{my-cloud-storage-bucket}}/tmp"
          },
          "parameters": {
              "inputLocations" : "{\"location1\":\"gs://{{my-cloud-storage-bucket}}/my-folder/**/*\"}",
              "outputLocations": "{\"location1\":\"gs://{{my-cloud-storage-bucket}}/my-output/output.csv\"}"
          },
          "jobName": SOME_NAME
        },
        gcsPath = PATH_TO_TEMPLATE
).execute()
Lefteris S
  • 1,614
  • 1
  • 7
  • 14