How to trigger a dataflow with a cloud function? (Python SDK)

前端 未结 2 606
南笙
南笙 2021-01-07 11:10

I have a cloud function that is triggered by cloud Pub/Sub. I want the same function trigger dataflow using Python SDK. Here is my code:

import base64
def h         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-07 11:50

    You have to embed your pipeline python code with your function. When your function is called, you simply call the pipeline python main function which executes the pipeline in your file.

    If you developed and tried your pipeline in Cloud Shell and you already ran it in Dataflow pipeline, your code should have this structure:

    def run(argv=None, save_main_session=True):
      # Parse argument
      # Set options
      # Start Pipeline in p variable
      # Perform your transform in Pipeline
      # Run your Pipeline
      result = p.run()
      # Wait the end of the pipeline
      result.wait_until_finish()
    

    Thus, call this function with the correct argument especially the runner=DataflowRunner to allow the python code to load the pipeline in Dataflow service.

    Delete at the end the result.wait_until_finish() because your function won't live all the dataflow process long.

    You can also use template if you want.

提交回复
热议问题