0
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?

Databricks にて BigQuery から Spark コネクターにより Spark データフレームを作成する方法

Last updated at Posted at 2025-05-01

概要

Databricks にて BigQuery から Spark コネクター により Spark データフレームを作成する方法を共有します。

本記事は下記シリーズの一部です。

image.png

出所:Databricks で BigQuery のデータを取得する方法の徹底ガイド - Qiita

事前準備

BigQuery に対する認証情報を取得

下記の記事を参考に BigQuery に対する認証情報(キー)を取得してください。

手順

1-1. 認証情報を変数にセット

json_str = b"""{json_key}"""

image.png

1-2. Google Cloud のプロジェクト ID を変数にセット

# プロジェクト ID を設定
parent_project_id = "axial-triode-XXXX"

image.png

1-2. Spark データフレームを作成

import base64

encoded_string = base64.b64encode(json_str).decode('utf-8')

# データ取得元のテーブル名を設定
table_name = "bigquery-public-data.google_analytics_sample.ga_sessions_20170801"

df = (
    spark.read
    .format("bigquery")
    .option("parentProject", parent_project_id)
    .option("credentials", encoded_string)
    .option("table",table_name)
    .load()
)

df.limit(50).display()

image.png

0
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
0
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?