![](/img/trans.png)
[英]How to run a bigquery SQL query in python jupyter notebook
[英]Vertex workbench - how to run BigQueryExampleGen in Jupyter notebook
嘗試在
InvalidUserInputError: Request missing required parameter projectId [while running 'InputToRecord/QueryTable/ReadFromBigQuery/Read/SDFBoundedSourceReader/ParDo(SDFBoundedSourceDoFn)/SplitAndSizeRestriction']
BigQueryExampleGen設置 GCP 項目和交互式 TFX 上下文。
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path_to_credential_file"
from tfx.v1.extensions.google_cloud_big_query import BigQueryExampleGen
from tfx.v1.components import (
StatisticsGen,
SchemaGen,
)
from tfx.orchestration.experimental.interactive.interactive_context import InteractiveContext
%load_ext tfx.orchestration.experimental.interactive.notebook_extensions.skip
context = InteractiveContext(pipeline_root='./data/artifacts')
運行 BigqueryExampleGen。
query = """
SELECT
* EXCEPT (trip_start_timestamp, ML_use)
FROM
{PROJECT_ID}.public_dataset.chicago_taxitrips_prep
""".format(PROJECT_ID=PROJECT_ID)
example_gen = context.run(
BigQueryExampleGen(query=query)
)
得到錯誤。
InvalidUserInputError: Request missing required parameter projectId [while running 'InputToRecord/QueryTable/ReadFromBigQuery/Read/SDFBoundedSourceReader/ParDo(SDFBoundedSourceDoFn)/SplitAndSizeRestriction']
請參閱mlops-with-vertex-ai/01-dataset-management.ipynb為 CThe Chicago Taxi Trips 數據集設置 BigQuery 數據集。
要在 GCP 中運行,需要通過beam_pipeline_args
參數提供項目 ID。
已經提出#888 來完成這項工作。 有了這個改變,你就可以做到
context.run(..., beam_pipeline_args=['--project', 'my-project'])
query = """
SELECT
* EXCEPT (trip_start_timestamp, ML_use)
FROM
{PROJECT_ID}.public_dataset.chicago_taxitrips_prep
""".format(PROJECT_ID=PROJECT_ID)
example_gen = context.run(
BigQueryExampleGen(query=query),
beam_pipeline_args=[
'--project', PROJECT_ID,
]
)
但是,它仍然失敗並出現另一個錯誤。
ValueError: ReadFromBigQuery requires a GCS location to be provided. Neither gcs_location in the constructor nor the fallback option --temp_location is set. [while running 'InputToRecord/QueryTable/ReadFromBigQuery/Read/SDFBoundedSourceReader/ParDo(SDFBoundedSourceDoFn)/SplitAndSizeRestriction']
它查看 GCP 內部,交互式上下文通過 Dataflow 運行 BigQueryExampleGen,因此需要通過beam_pipeline_args
參數提供 GCS 存儲桶 URL。
運行 Dataflow 管道時,傳遞參數 --temp_location gs://bucket/subfolder/
query = """
SELECT
* EXCEPT (trip_start_timestamp, ML_use)
FROM
{PROJECT_ID}.public_dataset.chicago_taxitrips_prep
""".format(PROJECT_ID=PROJECT_ID)
example_gen = context.run(
BigQueryExampleGen(query=query),
beam_pipeline_args=[
'--project', PROJECT_ID,
'--temp_location', BUCKET
]
)
statistics_gen = context.run(
StatisticsGen(examples=example_gen.component.outputs['examples'])
)
context.show(statistics_gen.component.outputs['statistics'])
schema_gen = SchemaGen(
statistics=statistics_gen.component.outputs['statistics'],
infer_feature_shape=True
)
context.run(schema_gen)
context.show(schema_gen.outputs['schema'])
此基於筆記本的教程將使用 Google Cloud BigQuery 作為數據源來訓練 ML 模型。 ML 管道將使用 TFX 構建並在 Google Cloud Vertex Pipelines 上運行。 在本教程中,我們將使用 BigQueryExampleGen 組件,該組件將數據從 BigQuery 讀取到 TFX 管道。
我們還需要為 BigQueryExampleGen 傳遞 beam_pipeline_args 。 它包括GCP 項目的名稱和 BigQuery 執行的臨時存儲等配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.