程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS?

开发过程中遇到为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS的问题如何解决?下面主要结合日常开发的经验,给出你关于为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS的解决方法建议,希望对你解决为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS有所启发或帮助;

首先-感谢您的代码-该代码非常有用。我还建议您直接在代码中添加设置环境变量-而不是为您工作的每个环境都设置它。您可以使用以下代码:

import os
os.environ["Google_APPliCATION_CREDENTIALS"] = "path_to_your_.Json_credential_file"

在需要不同凭据的不同项目之间进行切换时,我发现这很有用。

解决方法

我正在尝试使用Python通过BigQuery API连接到Google BigQuery。

我在这里关注此页面:https :
//cloud.google.com/bigquery/bigquery-api-
quickstart

我的代码如下:

import os
import argparse

from apiclient.discovery import build
from apiclient.errors import httpError
from oauth2client.client import GoogleCredentials

GOOGLE_APPLICATION_CREDENTIALS = './Peepl-cb1dac99bdc0.json'

def main(project_id):
    # Grab the application's default credentials from the environment.
    credentials = GoogleCredentials.get_application_default()
    print(credentials)
    # Construct the service object for interacTing with the BigQuery API.
    bigquery_service = build('bigquery','v2',credentials=credentials)

    try:
        query_request = bigquery_service.jobs()
        query_data = {
            'query': (
                'SELECT TOP(corpus,10) as title,'
                'COUNT(*) as unique_words '
                'FROM [publicdata:samples.shakespeare];')
        }

        query_response = query_request.query(
            projectId=project_id,body=query_data).execute()

        print('Query Results:')
        for row in query_response['rows']:
            print('\t'.join(field['v'] for field in row['f']))

    except httpError as err:
        print('Error: {}'.format(err.content))
        raise err


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('project_id',Help='Your Google Cloud Project ID.')

    args = parser.parse_args()

    main(args.project_id)

但是,当我通过终端运行此代码时,出现以下错误:

oauth2client.client.ApplicationDefaultCredentialsError: The Application Default Credentials are not available. They are available if running in Google Compute ENGIne. Otherwise,the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined poinTing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

正如您在代码中看到的那样,我尝试GOOGLE_APPLICATION_CREDENTIALS根据错误中的链接设置。但是,错误仍然存​​在。有人知道这个问题是什么吗?

先感谢您。

大佬总结

以上是大佬教程为你收集整理的为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS全部内容,希望文章能够帮你解决为BigQuery Python CLI设置GOOGLE_APPLICATION_CREDENTIALS所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。