安排Google Cloud Dataflow作业的最简便方法

时间:2017-05-06 04:24:35

标签: google-cloud-dataflow

我只需要每天运行一个数据流管道,但在我看来,建议像App Engine Cron Service这样需要构建整个Web应用程序的解决方案似乎有点太多了。 我正在考虑从Compute Engine Linux VM中的cron作业运行管道,但这可能太简单了:)。这样做的问题是什么,为什么没有人(除了我,我猜)建议它?

4 个答案:

答案 0 :(得分:4)

使用cron作业启动Dataflow管道绝对没有错。我们一直为我们的生产系统做这件事,无论是我们的Java还是Python开发的管道。

尽管如此,我们正试图摆脱cron的工作,并更多地使用AWS Lambdas(我们运行多云)或云功能。不幸的是,Cloud Functions don't have scheduling yet。 AWS Lambdas do

答案 1 :(得分:3)

该问题有一个常见问题解答: https://cloud.google.com/dataflow/docs/resources/faq#is_there_a_built-in_scheduling_mechanism_to_execute_pipelines_at_given_time_or_interval

  
      
  • 您可以使用Google App Engine(仅适用于Flexible Environment)或Cloud Functions自动执行管道。
  •   
  • 您可以使用Apache Airflow的Dataflow Operator,它是Cloud Composer工作流程中的几种Google Cloud Platform操作员之一。
  •   
  • 您可以在Compute Engine上使用自定义(cron)作业流程。
  •   

Cloud Function方法被描述为“ Alpha”,仍然没有调度(不等同于AWS cloudwatch调度事件),只有发布/订阅消息,云存储更改,HTTP调用。

Cloud Composer看起来是一个不错的选择。有效地重新标记了Apache Airflow,它本身就是一个很棒的编排工具。绝对不是像cron那样“太简单”:)

答案 2 :(得分:2)

您也可以使用云调度程序来调度您的工作。看我的帖子

https://medium.com/@zhongchen/schedule-your-dataflow-batch-jobs-with-cloud-scheduler-8390e0e958eb

Terraform脚本

    "error": {
        "code": "VmAgentNotRunning",
        "message": "The VM agent in Virtual Machine: '/subscriptions/{GUID}/resourceGroups/{ResourceGroup}/providers/Microsoft.Compute/virtualMachines/{VmName}' is not in running state. Please make sure it is installed and in running state and try again later."
    }

答案 3 :(得分:1)

这是我使用Cloud Functions,PubSub和Cloud Scheduler完成的方式 (假设您已经创建了一个Dataflow模板,并且该模板存在于GCS存储桶中的某个位置)

  1. 在PubSub中创建一个新主题。这将用于触发云功能

  2. 创建一个Cloud Function,该Cloud Function从模板启动Dataflow作业。我发现最简单的方法是从CF Console创建它。确保您选择的服务帐户具有创建数据流作业的权限。该函数的index.js类似于:

const google = require('googleapis');

exports.triggerTemplate = (event, context) => {
  // in this case the PubSub message payload and attributes are not used
  // but can be used to pass parameters needed by the Dataflow template
  const pubsubMessage = event.data;
  console.log(Buffer.from(pubsubMessage, 'base64').toString());
  console.log(event.attributes);

  google.google.auth.getApplicationDefault(function (err, authClient, projectId) {
  if (err) {
    console.error('Error occurred: ' + err.toString());
    throw new Error(err);
  }

  const dataflow = google.google.dataflow({ version: 'v1b3', auth: authClient });

  dataflow.projects.templates.create({
        projectId: projectId,
        resource: {
          parameters: {},
          jobName: 'SOME-DATAFLOW-JOB-NAME',
          gcsPath: 'gs://PATH-TO-YOUR-TEMPLATE'
        }
      }, function(err, response) {
        if (err) {
          console.error("Problem running dataflow template, error was: ", err);
        }
        console.log("Dataflow template response: ", response);
      });
  });
};

package.json看起来像

{
  "name": "pubsub-trigger-template",
  "version": "0.0.1",
  "dependencies": {
    "googleapis": "37.1.0",
    "@google-cloud/pubsub": "^0.18.0"
  }
}
  1. 转到PubSub,然后创建您创建的主题,手动发布消息。这应该会触发Cloud Function并启动数据流作业

  2. 使用Cloud Scheduler按计划发布PubSub消息 https://cloud.google.com/scheduler/docs/tut-pub-sub