Google BigQuery で横串検索を実行する
この記事では、 によって管理されていない データに対して横串検索を実行するようにレイクハウスフェデレーションを設定する方法について説明します。BigQueryDatabricksレイクハウスフェデレーションの詳細については、「 レイクハウスフェデレーションとは」を参照してください。
レイクハウスフェデレーションを使用して BigQuery データベースに接続するには、 Databricks Unity Catalog メタストアに次のものを作成する必要があります。
- BigQuery データベースへの接続。
- Unity Catalog のクエリー構文ツールとデータガバナンス ツールを使用してデータベースへの Databricks ユーザー アクセスを管理できるように、Unity Catalog で BigQuery データベースをミラーリングする フォーリンカタログ 。
始める前に
ワークスペースの要件:
- Unity Catalogのワークスペースが有効になっています。
コンピュートの要件:
- Databricks RuntimeクラスターまたはSQLウェアハウスからターゲットデータベースシステムへのネットワーク接続。「レイクハウスフェデレーションのネットワーキングに関する推奨事項」を参照してください。
- Databricks クラスターでは、 Databricks Runtime 16.1 以降と標準または専用アクセス モード (以前の共有および単一ユーザー) を使用する必要があります。
- SQLウェアハウスはProまたはServerlessである必要があります。
必要な権限:
- 接続を作成するには、メタストア管理者か、ワークスペースに接続されているUnity Catalogメタストアの
CREATE CONNECTION
権限を持つユーザーである必要があります。 - フォーリンカタログを作成するには、メタストアに対する
CREATE CATALOG
権限を持ち、接続の所有者であるか、接続に対するCREATE FOREIGN CATALOG
権限を持っている必要があります。
追加の権限要件は、以下の各タスクベースのセクションに記載しています。
接続を作成する
接続では、外部データベースシステムにアクセスするためのパスと認証情報を指定します。接続を作成するには、カタログエクスプローラーを使用するか、Databricksノートブックまたは Databricks SQLクエリーエディタで CREATE CONNECTION
SQLコマンドを使用できます。
Databricks REST API または Databricks CLI を使用して接続を作成することもできます。 POST /api/2.1/unity-catalog/connections を参照してください。 および Unity Catalog コマンド。
必要な権限: メタストア管理者またはCREATE CONNECTION
権限を持つユーザー。
- Catalog Explorer
- SQL
-
In your Databricks workspace, click
Catalog.
-
At the top of the Catalog pane, click the
Add icon and select Add a connection from the menu.
Alternatively, from the Quick access page, click the External data > button, go to the Connections tab, and click Create connection.
-
On the Connection basics page of the Set up connection wizard, enter a user-friendly Connection name.
-
Select a Connection type of Google BigQuery, and then click Next.
-
On the Authentication page, enter the Google service account key json for your BigQuery instance.
This is a raw JSON object that is used to specify the BigQuery project and provide authentication. You can generate this JSON object and download it from the service account details page in Google Cloud under ‘KEYS’. The service account must have proper permissions granted in BigQuery, including BigQuery User and BigQuery Data Viewer. The following is an example.
JSON{
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "KEY_ID",
"private_key": "PRIVATE_KEY",
"client_email": "SERVICE_ACCOUNT_EMAIL",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/SERVICE_ACCOUNT_EMAIL",
"universe_domain": "googleapis.com"
} -
(Optional) Enter the Project ID for your BigQuery instance:
This is a name for the BigQuery project used for billing for all queries run under this connection. Defaults to the project ID of your service account. The service account must have proper permissions granted for this project in BigQuery, including BigQuery User. Additional dataset used for storing temporary tables by BigQuery might be created in this project.
-
(Optional) Add a comment.
-
Click Create connection.
-
On the Catalog basics page, enter a name for the foreign catalog. A foreign catalog mirrors a database in an external data system so that you can query and manage access to data in that database using Databricks and Unity Catalog.
-
(Optional) Click Test connection to confirm that it works.
-
Click Create catalog.
-
On the Access page, select the workspaces in which users can access the catalog you created. You can select All workspaces have access, or click Assign to workspaces, select the workspaces, and then click Assign.
-
Change the Owner who will be able to manage access to all objects in the catalog. Start typing a principal in the text box, and then click the principal in the returned results.
-
Grant Privileges on the catalog. Click Grant:
- Specify the Principals who will have access to objects in the catalog. Start typing a principal in the text box, and then click the principal in the returned results.
- Select the Privilege presets to grant to each principal. All account users are granted
BROWSE
by default.- Select Data Reader from the drop-down menu to grant
read
privileges on objects in the catalog. - Select Data Editor from the drop-down menu to grant
read
andmodify
privileges on objects in the catalog. - Manually select the privileges to grant.
- Select Data Reader from the drop-down menu to grant
- Click Grant.
-
Click Next.
-
On the Metadata page, specify tags key-value pairs. For more information, see Apply tags to Unity Catalog securable objects.
-
(Optional) Add a comment.
-
Click Save.
Run the following command in a notebook or the Databricks SQL query editor. Replace <GoogleServiceAccountKeyJson>
with a raw JSON object that specifies the BigQuery project and provides authentication. You can generate this JSON object and download it from the service account details page in Google Cloud under ‘KEYS’. The service account needs to have proper permissions granted in BigQuery, including BigQuery User and BigQuery Data Viewer. For an example JSON object, view the Catalog Explorer tab on this page.
CREATE CONNECTION <connection-name> TYPE bigquery
OPTIONS (
GoogleServiceAccountKeyJson '<GoogleServiceAccountKeyJson>'
);
We recommend that you use Databricks secrets instead of plaintext strings for sensitive values like credentials. For example:
CREATE CONNECTION <connection-name> TYPE bigquery
OPTIONS (
GoogleServiceAccountKeyJson secret ('<secret-scope>','<secret-key-user>')
)
For information about setting up secrets, see Secret management.
フォーリンカタログの作成
UI を使用してデータソースへの接続を作成する場合は、フォーリンカタログの作成が含まれているため、この手順をスキップできます。
フォーリンカタログは、外部データシステム内のデータベースをミラーリングするため、DatabricksとUnity Catalogを使用して、そのデータベース内のデータへのアクセスを管理できます。フォーリンカタログを作成するには、すでに定義されているデータソースへの接続を使用します。
フォーリンカタログを作成するには、Catalog Explorer を使用するか、 Databricks ノートブックまたは Databricks SQL クエリエディタで CREATE FOREIGN CATALOG
を使用します。 Databricks REST API または Databricks CLI を使用してカタログを作成することもできます。 POST /api/2.1/unity-catalog/catalogs を参照してください。 または Unity Catalog コマンド。
必要な権限: メタストアの CREATE CATALOG
権限、接続の所有権または接続の CREATE FOREIGN CATALOG
権限。
- Catalog Explorer
- SQL
-
In your Databricks workspace, click
Catalog to open Catalog Explorer.
-
At the top of the Catalog pane, click the
Add icon and select Add a catalog from the menu.
Alternatively, from the Quick access page, click the Catalogs button, and then click the Create catalog button.
-
(Optional) Enter the following catalog property:
Data Project Id: A name for the BigQuery project containing data that will be mapped to this catalog. Defaults to the billing project ID set at the connection level.
-
Follow the instructions for creating foreign catalogs in Create catalogs.
Run the following SQL command in a notebook or the Databricks SQL editor. Items in brackets are optional. Replace the placeholder values.
<catalog-name>
: Name for the catalog in Databricks.<connection-name>
: The connection object that specifies the data source, path, and access credentials.
CREATE FOREIGN CATALOG [IF NOT EXISTS] <catalog-name> USING CONNECTION <connection-name>;
サポートされているプッシュダウン
次のプッシュダウンがサポートされています。
- フィルター
- 予測
- 上限
- 関数: 部分的、フィルター式のみ。 (文字列関数、数学関数、データ関数、時刻関数、タイムスタンプ関数、およびエイリアス、キャスト、ソートオーダーなどのその他の関数)
- 集計
- ソート (制限付きで使用した場合)
- 結合 (Databricks Runtime 16.1 以降)
次のプッシュダウンはサポートされていません。
- Windows の機能
データ型マッピング
次の表は、BigQuery から Spark へのデータ型のマッピングを示しています。
BigQuery タイプ | Spark タイプ |
---|---|
bignumeric, numeric | DecimalType |
int64 | LongType |
float64 | DoubleType |
array, geography, interval, json, string, struct | VarcharType |
バイト | BinaryType |
bool | BooleanType |
日付 | DateType |
datetime, time, timestamp | TimestampType/TimestampNTZType |
BigQuery から読み取ると、BigQuery Timestamp
は Spark TimestampType
( preferTimestampNTZ = false
の場合)(デフォルト)にマッピングされます。 BigQuery Timestamp
は、preferTimestampNTZ = true
の場合に TimestampNTZType
にマッピングされます。