从共享数据中提取BigQuery数据

2024-05-13 09:59:36 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以使用客户端api(python)从共享数据集(我只有查看权限)中提取数据(到google云存储)?在

我可以使用web浏览器手动执行此操作,但无法使用API使其正常工作。在

我已经为MyProject创建了一个项目(MyProject)和一个服务帐户,以便在使用API创建服务时用作凭据。此帐户对共享数据集(MyShareddataset)具有查看权限,并对我的google云存储桶具有写入权限。如果我试图在自己的项目中运行作业以从共享项目中提取数据:

job_data = {
        'jobReference': {
            'projectId': myProjectId,
            'jobId': str(uuid.uuid4())
        },
        'configuration': {
            'extract': {
                'sourceTable': {
                    'projectId': sharedProjectId,
                    'datasetId': sharedDatasetId,
                    'tableId': sharedTableId,
                },
                'destinationUris': [cloud_storage_path],
                'destinationFormat': 'AVRO'
            }
        }
    }

我得到了一个错误:

googleapiclient.errors.HttpError: https://www.googleapis.com/bigquery/v2/projects/sharedProjectId/jobs?alt=json returned "Value 'myProjectId' in content does not agree with value sharedProjectId'. This can happen when a value set through a parameter is inconsistent with a value set in the request.">

在jobReference和sourceTable中使用sharedProjectId得到:

googleapiclient.errors.HttpError: https://www.googleapis.com/bigquery/v2/projects/sharedProjectId/jobs?alt=json returned "Access Denied: Job myJobId: The user myServiceAccountEmail does not have permission to run a job in project sharedProjectId">

对这两个作业使用myProjectId后,作业立即返回状态“DONE”并且没有错误,但没有导出任何内容。我的GCS桶是空的。在

如果使用API确实不可能做到这一点,是否还有其他方法/工具可用于自动从共享数据集中提取数据?在

*更新*

使用在我的GA登录名下运行的API explorer可以很好地工作。在我的代码中,我使用以下方法:

^{pr2}$

并移除包含projectId的jobReference对象

job_data = {
        'configuration': {
            'extract': {
                'sourceTable': {
                    'projectId': sharedProjectId,
                    'datasetId': sharedDatasetId,
                    'tableId': sharedTableId,
                },
                'destinationUris': [cloud_storage_path],
                'destinationFormat': 'AVRO'
            }
        }
    }

但这会返回错误

Access Denied: Table sharedProjectId:sharedDatasetId.sharedTableId: The user 'serviceAccountEmail' does not have permission to export a table in dataset sharedProjectId:sharedDatasetId

我的服务帐户现在是共享数据集的所有者,并且拥有MyProject的编辑权限,还需要在哪里设置权限,或者是否可以使用python API使用我的GA登录凭据而不是服务帐户?在

*更新*

终于成功了。怎样?确保服务帐户具有查看数据集的权限(如果您没有权限自己检查,而有人告诉您,请让他们再次检查/向您发送屏幕截图!)在


Tags: 数据项目inapi权限myproject作业job
2条回答

在试图重现这个问题之后,我遇到了解析错误。 我在开发人员控制台[2]上玩过这个API,它起了作用。 jobs.insert API 我注意到的是,下面的请求代码与网站上的文档格式不同,因为它是单引号而不是双引号。在

这是我运行的代码。在

{
'configuration': {
    'extract': {
        'sourceTable': {
            'projectId': "sharedProjectID",
            'datasetId': "sharedDataSetID",
            'tableId': "sharedTableID"
        },
        'destinationUri': "gs://myBucket/myFile.csv"
    }
}
}

HTTP请求

https://www.googleapis.com/bigquery/v2/projects/myProjectId/jobs

如果您仍然遇到问题,可以尝试作业.插入API在网站[2]或尝试bq命令工具[3]。在

以下命令可以执行相同的操作:

bq摘录共享项目ID:sharedDataSetId.sharedTableId gs://我的bucket/我的文件.csv在

希望这有帮助。在

[2]https://cloud.google.com/bigquery/docs/reference/v2/jobs/insert

[3]https://cloud.google.com/bigquery/bq-command-line-tool

确保服务帐户具有查看数据集的权限(如果您没有权限自己检查,而有人告诉您,请让他们再次检查/向您发送屏幕截图!)在

相关问题 更多 >