1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cloud Storage からの CSV データの読み込みでのエラー

Last updated at Posted at 2023-08-23

1.はじめに

  • GCSにアップロードしたcsvデータを、colaboratory上でBigQueryにロードする際に、エラーにぶつかる。
  • 後から見るとなーんだ、という感じだけれど、ネットを探してもすぐに発見できなかったのでメモ。

2.エラー

  • 公式に書いてあるコードをそのまま流用して実行。
from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
table_id = "your-project.your_dataset.your_table_name"

job_config = bigquery.LoadJobConfig(
    schema=[
        bigquery.SchemaField("name", "STRING"),
        bigquery.SchemaField("post_abbr", "STRING"),
    ],
    skip_leading_rows=1,
    # The source format defaults to CSV, so the line below is optional.
    source_format=bigquery.SourceFormat.CSV,
)
uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv"

load_job = client.load_table_from_uri(
    uri, table_id, job_config=job_config
)  # Make an API request.

load_job.result()  # Waits for the job to complete.

destination_table = client.get_table(table_id)  # Make an API request.
print("Loaded {} rows.".format(destination_table.num_rows))

便利―と思いつつ実行するとこんなエラーが!

NotFound: 404 POST https://bigquery.googleapis.com/bigquery/v2/projects//jobs?prettyPrint=false: Request couldn't be served.

エラー内容を調べると、load_job_from_uriのAPIコールに必要な情報が足りない。とのこと。
image.png

ドキュメントを見ると、いろいろ引数があることが判明。
ネット情報ではlocation情報が不足してるから、など言われている。

3.修正

  • いくつか試したところ、こう↓やればできた。load_jobの引数にproject=~~を追加しただけ。
from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
# table_id = "your-project.your_dataset.your_table_name"

job_config = bigquery.LoadJobConfig(
    schema=[
        bigquery.SchemaField("name", "STRING"),
        bigquery.SchemaField("post_abbr", "STRING"),
    ],
    skip_leading_rows=1,
    # The source format defaults to CSV, so the line below is optional.
    source_format=bigquery.SourceFormat.CSV,
)
uri = "gs://cloud-samples-data/bigquery/us-states/us-states.csv"

load_job = client.load_table_from_uri(
    uri, table_id, job_config=job_config,
    project="your-project"
)  # Make an API request.

load_job.result()  # Waits for the job to complete.

destination_table = client.get_table(table_id)  # Make an API request.
print("Loaded {} rows.".format(destination_table.num_rows))

変更部分を再掲。

load_job = client.load_table_from_uri(
    uri, table_id, job_config=job_config,
    project="your-project"  ##これ
)  # Make an API request.
  • またはclientを作るときに
client = bigquery.Client(project="your-project")

でも行けた。単にprojectを特定するだけでよかった。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?