gpt4 book ai didi

google-cloud-dataproc - 如何运行创建Dataproc集群,运行作业,从Cloud Function删除集群

转载 作者:行者123 更新时间:2023-12-04 05:47:58 25 4
gpt4 key购买 nike

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

最佳答案

我可以使用WorkflowTemplate API来管理集群的生命周期。使用Dataproc工作流,我不必轮询要创建的集群,创建的作业或进行任何错误处理。

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

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

package.json:

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

关于google-cloud-dataproc - 如何运行创建Dataproc集群,运行作业,从Cloud Function删除集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50359270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com