如何运行创建Dataproc集群,运行作业,从Cloud Function删除集群 [英] How can I run create Dataproc cluster, run job, delete cluster from Cloud Function

查看:98
本文介绍了如何运行创建Dataproc集群,运行作业,从Cloud Function删除集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想启动Dataproc作业以响应到达GCS存储桶中的日志文件.我也不想保持持久集群的运行,因为新日志文件一天只到达几次,并且大多数时候都处于空闲状态.

I would like to start a Dataproc job in response to log files arriving in GCS bucket. I also do not want to keep a persistent cluster running as new log files arrive only several times a day and it would be idle most of the time.

推荐答案

我可以使用

I can use WorkflowTemplate API to manage the cluster lifecycle for me. With Dataproc Workflows I don't have to poll for either cluster to be created, or job created, or do any error handling.

这是我的云功能.设置为Cloud Storage bucket以在Finalize/Create事件上触发:

Here's my Cloud Function. Set to Cloud Storage bucket to trigger on Finalize/Create event:

index.js:

exports.startWorkflow = (event, callback) => {

  const {
    google
  } = require('googleapis');

  const region = 'global'
  const zone = 'us-central1-a'
  const clusterName = 'my-cluster'

  const file = event.data;
  console.log("Event: ", file);

  if (!file.name) {
    throw "Skipped processing file!";
  }

  const queryFileUri = "gs://" + file.bucket + "/" + file.name

  console.log("Creating auth client: ");
  google.auth.getApplicationDefault(
    (err, authClient, projectId) => {
      if (authClient.createScopedRequired && authClient.createScopedRequired()) {
        authClient = authClient.createScoped([
          'https://www.googleapis.com/auth/cloud-platform',
          'https://www.googleapis.com/auth/userinfo.email'
        ]);
      }

      const request = {
        parent: "projects/" + projectId + "/regions/" + region,
        resource: {
          "placement": {
            "managedCluster": {
              "clusterName": clusterName,
              "config": {
                "gceClusterConfig": {
                  "zoneUri": zone, // Can be omitted if using regional endpoint (like us-central1-a, not global)
                }
              }
            }
          },
          "jobs": [{
            "stepId": "step1",
            "pigJob": {
              "queryFileUri": queryFileUri,
            },
            "prerequisiteStepIds": [],
          }]
        }
      };

      const dataproc = google.dataproc({
        version: 'v1beta2',
        auth: authClient
      });
      dataproc.projects.regions.workflowTemplates.instantiateInline(
        request, (err, result) => {
          if (err) {
            throw err;
          }
          console.log(result);
          callback();
        });
    });
};

确保将要执行的功能设置为startWorkflow.

Make sure to set Function to Execute to startWorkflow.

package.json:

{
  "name": "dataproc-workflow",
  "version": "1.0.0",
  "dependencies":{ "googleapis": "30.0.0"}
}

这篇关于如何运行创建Dataproc集群,运行作业,从Cloud Function删除集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆