如何使用 Python SDK 创建具有生存时间的 Dataproc 集群
How to create a Dataproc cluster with time to live using Python SDK
我尝试使用 python SDK 创建一个 生存时间为 1 天的 Dataproc 集群。为此,Dataproc API 的 v1beta2 引入了 LifecycleConfig object,它是 ClusterConfig 对象的子对象。
我在传递给 create_cluster
方法的 JSON 文件中使用了这个对象。要设置特定的 TTL,我使用字段 auto_delete_ttl
,其值应为 86,400 秒(一天)。
The documentation of Google Protocol Buffers is rather specific 关于如何在 JSON 文件中表示持续时间:持续时间应表示为带有秒后缀 s 的字符串,并且应有 0、3、6 或 9 小数秒:
但是,如果我使用这种格式传递持续时间,我会收到错误消息:
Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Duration got str
这是我创建集群的方式:
from google.cloud import dataproc_v1beta2
project = "your_project_id"
region = "europe-west4"
cluster = "" #see below for cluster JSON file
client = dataproc_v1beta2.ClusterControllerClient(client_options={
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region)
})
# Create the cluster
operation = client.create_cluster(project, region, cluster)
变量 cluster 包含描述所需集群的 JSON 对象:
{
"cluster_name":"my_cluster",
"config":{
"config_bucket":"my_conf_bucket",
"gce_cluster_config":{
"zone_uri":"europe-west4-a",
"metadata":{
"PIP_PACKAGES":"google-cloud-storage google-cloud-bigquery"
},
"subnetwork_uri":"my subnet",
"service_account_scopes":[
"https://www.googleapis.com/auth/cloud-platform"
],
"tags":[
"some tags"
]
},
"master_config":{
"num_instances":1,
"machine_type_uri":"n1-highmem-4",
"disk_config":{
"boot_disk_type":"pd-standard",
"boot_disk_size_gb":200,
"num_local_ssds":0
},
"accelerators":[
]
},
"software_config":{
"image_version":"1.4-debian9",
"properties":{
"dataproc:dataproc.allow.zero.workers":"true",
"yarn:yarn.log-aggregation-enable":"true",
"dataproc:dataproc.logging.stackdriver.job.driver.enable":"true",
"dataproc:dataproc.logging.stackdriver.enable":"true",
"dataproc:jobs.file-backed-output.enable":"true"
},
"optional_components":[
]
},
"lifecycle_config":{
"auto_delete_ttl":"86400s"
},
"initialization_actions":[
{
"executable_file":"gs://some-init-script"
}
]
},
"project_id":"project_id"
}
我使用的软件包版本:
- google-云数据处理:0.6.1
- protobuf: 3.11.3
- googleapis-通用协议:1.6.0
我是不是哪里做错了,是包版本不对还是bug?
当你以文本格式(即json等)构造protobuf时,你应该使用100s
格式作为持续时间类型,但你正在使用Python对象来构造API 请求正文,这就是为什么您需要创建 Duration object 而不是字符串:
duration_message.FromSeconds(86400)
我尝试使用 python SDK 创建一个 生存时间为 1 天的 Dataproc 集群。为此,Dataproc API 的 v1beta2 引入了 LifecycleConfig object,它是 ClusterConfig 对象的子对象。
我在传递给 create_cluster
方法的 JSON 文件中使用了这个对象。要设置特定的 TTL,我使用字段 auto_delete_ttl
,其值应为 86,400 秒(一天)。
The documentation of Google Protocol Buffers is rather specific 关于如何在 JSON 文件中表示持续时间:持续时间应表示为带有秒后缀 s 的字符串,并且应有 0、3、6 或 9 小数秒:
但是,如果我使用这种格式传递持续时间,我会收到错误消息:
Parameter to MergeFrom() must be instance of same class: expected google.protobuf.Duration got str
这是我创建集群的方式:
from google.cloud import dataproc_v1beta2
project = "your_project_id"
region = "europe-west4"
cluster = "" #see below for cluster JSON file
client = dataproc_v1beta2.ClusterControllerClient(client_options={
'api_endpoint': '{}-dataproc.googleapis.com:443'.format(region)
})
# Create the cluster
operation = client.create_cluster(project, region, cluster)
变量 cluster 包含描述所需集群的 JSON 对象:
{
"cluster_name":"my_cluster",
"config":{
"config_bucket":"my_conf_bucket",
"gce_cluster_config":{
"zone_uri":"europe-west4-a",
"metadata":{
"PIP_PACKAGES":"google-cloud-storage google-cloud-bigquery"
},
"subnetwork_uri":"my subnet",
"service_account_scopes":[
"https://www.googleapis.com/auth/cloud-platform"
],
"tags":[
"some tags"
]
},
"master_config":{
"num_instances":1,
"machine_type_uri":"n1-highmem-4",
"disk_config":{
"boot_disk_type":"pd-standard",
"boot_disk_size_gb":200,
"num_local_ssds":0
},
"accelerators":[
]
},
"software_config":{
"image_version":"1.4-debian9",
"properties":{
"dataproc:dataproc.allow.zero.workers":"true",
"yarn:yarn.log-aggregation-enable":"true",
"dataproc:dataproc.logging.stackdriver.job.driver.enable":"true",
"dataproc:dataproc.logging.stackdriver.enable":"true",
"dataproc:jobs.file-backed-output.enable":"true"
},
"optional_components":[
]
},
"lifecycle_config":{
"auto_delete_ttl":"86400s"
},
"initialization_actions":[
{
"executable_file":"gs://some-init-script"
}
]
},
"project_id":"project_id"
}
我使用的软件包版本:
- google-云数据处理:0.6.1
- protobuf: 3.11.3
- googleapis-通用协议:1.6.0
我是不是哪里做错了,是包版本不对还是bug?
当你以文本格式(即json等)构造protobuf时,你应该使用100s
格式作为持续时间类型,但你正在使用Python对象来构造API 请求正文,这就是为什么您需要创建 Duration object 而不是字符串:
duration_message.FromSeconds(86400)