使用google-cloud-dataproc客户端列出Dataproc操作遇参数错误
Dataproc List Operations API调用报错排查
问题场景
想要实现类似gcloud dataproc operations list --filter "..."的功能,编写了如下Python代码:
from google.cloud import dataproc_v1 region = 'us-west1' client_options = {"api_endpoint": f"{region}-dataproc.googleapis.com:443"} dataproc_cluster_client = dataproc_v1.ClusterControllerClient(client_options=client_options) def list_operations(dataproc_cluster_client, region): for op in dataproc_cluster_client.list_operations( request={"filter": f"operationType = CREATE AND labels.goog-dataproc-location:{region}"} ): print(op) list_operations(dataproc_cluster_client, region)
运行后抛出错误:
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.INVALID_ARGUMENT details = "Invalid resource field value in the request." debug_error_string = "UNKNOWN:Error received from peer ipv4:xxx.xxx.xx.xxx:443 {created_time:"2023-03-28T11:23:00.466125+02:00", grpc_status:3, grpc_message:"Invalid resource field value in the request."}"
问题原因
- Filter字段格式错误:
- 操作类型的正确字段是
metadata.operationType,而非直接写operationType; - 操作类型的有效值不是
CREATE,而是Dataproc定义的枚举值,比如集群创建对应的是CREATE_CLUSTER。
- 操作类型的正确字段是
- 冗余的Location过滤:
你已经通过区域化API端点(us-west1-dataproc.googleapis.com)限定了操作的区域,ClusterControllerClient的list_operations方法会自动返回该区域内的集群操作,无需额外通过labels.goog-dataproc-location过滤。
修正后的代码
from google.cloud import dataproc_v1 region = 'us-west1' client_options = {"api_endpoint": f"{region}-dataproc.googleapis.com:443"} dataproc_cluster_client = dataproc_v1.ClusterControllerClient(client_options=client_options) def list_operations(dataproc_cluster_client): # 使用正确的filter字段和操作类型枚举值 for op in dataproc_cluster_client.list_operations( request={"filter": "metadata.operationType = CREATE_CLUSTER"} ): print(op) list_operations(dataproc_cluster_client)
补充说明
- 如果需要获取所有类型的Dataproc操作(包括作业、集群、工作流等),请使用
dataproc_v1.OperationsClient,而非仅处理集群操作的ClusterControllerClient; - Dataproc操作类型的有效值包括:
CREATE_CLUSTER、DELETE_CLUSTER、UPDATE_CLUSTER、RUN_JOB、SUBMIT_WORKFLOW等,对应不同的操作场景。
内容的提问来源于stack exchange,提问作者egordoe
相关产品推荐
相关产品推荐

