【问题标题】:How to provide authentication to Google Cloud remote instance for BigQuery?如何为 BigQuery 的 Google Cloud 远程实例提供身份验证?
【发布时间】:2018-02-06 05:12:22
【问题描述】:

我正在努力使数据收集部分由远程机器完成。我的远程实例将使用我的 python 代码在 BigQuery 中运行查询并获取数据是什么意思。然后它将使用该数据并在其上启动 ML 作业。所有这些步骤都可以通过我本地机器上的脚本来完成。但是,通过执行我的 python 模块“trainer.task.py”的云实例(远程)获取数据的最佳方法是什么?

【问题讨论】:

  • 为什么这是一个machine-learning 的问题?
  • 我想根据从 BigQuery 查询的数据训练 ML 模型,所有这两个步骤都必须自动完成 - 无需任何人工参与。只是想知道是否有更好的方法,可能是通过相同的 python 脚本(trainer.task.py)或通过许多不同的脚本。所以我的问题有 BigQuery 和 ML 两部分,更具体地说,如何在允许的云实例中将它们组合在一起。
  • 您的问题(对于 SO 来说太宽泛了,并且已经排队等待关闭)与使用 Big Query 中的数据在 Google Cloud 中开始工作有关;在这种情况下,工作恰好是 ML 的事实是无关紧要的。
  • 我重申了这个问题。我希望将数据查询到 BigQuery 并在该数据上自动执行机器学习作业。在 bash 脚本的帮助下,我可以在我的本地机器上执行这些步骤(在谷歌云上查询和启动 ML 作业)。查询结果的处理发生在我不想要的本地机器上。所以我想验证云实例,所以它和我的本地机器一样。但是,我解决了它并将分享我的答案。
  • 听起来您应该使用服务帐号从 Google Cloud 中的机器访问 BigQuery:cloud.google.com/docs/authentication

标签: python-3.x machine-learning google-bigquery google-cloud-platform


【解决方案1】:

这个想法是使用服务帐户凭据从 BigQuery 查询数据,如 Authenticating With a Service Account Key File 中所述

自动化过程可以通过一个 bash 脚本(我们称之为“deploy_cloud.sh”)来处理,该脚本将包含所有配置参数,如下所示:

export JOB_NAME="your_job_$(date +%Y%m%d_%H%M%S)"
export BUCKET_NAME=your_job
export CLOUD_FOLDER=test-folder
export JOB_DIR=$BUCKET_NAME/$CLOUD_FOLDER/$JOB_NAME
export CLOUD_SECRET_FOLDER=credentials
export SERVICE_ACC_SECRET=service_account_secret.json
# copy your service account credential to cloud storage
# you can do this step manually in your storage
gsutil cp -r path_to_your_service_account_file/$SERVICE_ACC_SECRET gs://$BUCKET_NAME/$CLOUD_FOLDER/$CLOUD_SECRET_FOLDER/$SERVICE_ACC_SECRET
# define your query params
export YOUR_QUERY_PARAM1="provide_param1_value"
export YOUR_QUERY_PARAM2="provide_param2_value"
export YOUR_QUERY_PARAM3="provide_param3_value"
export YOUR_QUERY_PARAM4="provide_param4_value"
# Then submit your training job 

gcloud ml-engine jobs submit training $JOB_NAME \
--job-dir $JOB_DIR \
--runtime-version 1.4 \
--module-name trainer.task \
--package-path ./trainer \
--region $REGION \
--config=trainer/config-gpu.yaml \
-- \
--client-sec gs://$BUCKET_NAME/$CLOUD_FOLDER/$CLOUD_SECRET_FOLDER/$SERVICE_ACC_SECRET \
--query-param1 $YOUR_QUERY_PARAM1 \
--query-param2 $YOUR_QUERY_PARAM2 \
--query-param3 $YOUR_QUERY_PARAM3 \
--query-param4 $YOUR_QUERY_PARAM4 

在您的 task.py 文件中,您将需要使用参数解析器来使用这些传递的值并相应地构建您的查询。在您的 task.py 文件中,使用以下代码从存储桶中读取数据并将其写入云实例的本地内存,如下所示:

def read_and_write_client_secret(input_file, output_file):
    with file_io.FileIO(input_file, 'r') as f:
        secret = json.load(f)

    with open(output_file, 'w') as outfile:
        json.dump(secret, outfile)

您可以按如下方式调用上述函数(在您的 task.py 中):

read_and_write_client_secret(args_dict['client_sec'], 'temp.json')

考虑到您的参数解析器将所有参数作为字典存储在 args_dict 中。请注意,您的服务帐户文件将在云实例当前/工作目录中写为“temp.json”。

现在您需要在创建 BigQuery 客户端时指定文件位置(包含凭据的“temp.json”),如下所示;

# my_query = build_my_query(args_dict['query_param1'], args_dict['query_param2'], args_dict['query_param3'], args_dict['query_param4'])

bigquery_client = bigquery.Client.from_service_account_json(
    'temp.json')
query_job = client.query(my_query)
results = query_job.result()  # Waits for job to complete.

# suggestion: extract data from iterator and create a pandas data frame for further processing

现在您的云实例将能够运行您的查询并使用该数据。您可以根据所获得的数据开始机器学习工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2013-10-29
    相关资源
    最近更新 更多