Snowflakeでフェデレーションクエリを実行する
この記事では、 によって管理されていない データに対して横串検索を実行するようにレイクハウスフェデレーションを設定する方法について説明します。SnowflakeDatabricksレイクハウスフェデレーションの詳細については、「 レイクハウスフェデレーションとは」を参照してください。
レイクハウスフェデレーションを使用して Snowflake データベースに接続するには、 Databricks Unity Catalog メタストアに次のものを作成する必要があります。
- Snowflake データベースへの接続。
- Unity Catalog 内のSnowflakeデータベースをミラーリングする フォーリンカタログ で、 Unity Catalog クエリー構文およびデータガバナンスツールを使用して、データベースへのDatabricksユーザーアクセスを管理できます。
始める前に
ワークスペースの要件:
- Unity Catalogのワークスペースが有効になっています。
コンピュートの要件:
- コンピュート・リソースからターゲット・データベース・システムへのネットワーク接続。 「レイクハウスフェデレーションのネットワーキングに関する推奨事項」を参照してください。
- Databricks コンピュートは、 Databricks Runtime 13.3 LTS 以上、 および標準 または 専用 アクセスモードを使用する必要があります。
- SQLウェアハウスはProまたはServerlessで、2023.40以上を使用している必要があります。
必要な権限:
- 接続を作成するには、メタストア管理者か、ワークスペースに接続されているUnity Catalogメタストアの
CREATE CONNECTION
権限を持つユーザーである必要があります。 - フォーリンカタログを作成するには、メタストアに対する
CREATE CATALOG
権限を持ち、接続の所有者であるか、接続に対するCREATE FOREIGN CATALOG
権限を持っている必要があります。
追加の権限要件は、以下の各タスクベースのセクションに記載しています。
- OAuth を使用して認証する予定の場合は、Snowflakeコンソールでセキュリティ統合を作成します。
- OAuth アクセス トークンを使用して認証する予定の場合は、アクセス トークンも要求する必要があります。
(オプション)Snowflakeコンソールでセキュリティ統合を作成する
OAuth を使用して認証する場合は、Snowflake接続を作成する前に、このステップに従ってください。 代わりにユーザー名とパスワードを使用して認証する場合は、このセクションをスキップしてください。
Snowflakeの組み込みOAuthインテグレーションのみがサポートされています。Okta や Microsoft Entra ID などの外部 OAuth 統合はサポートされていません。
Snowflakeコンソールで、 CREATE SECURITY INTEGRATION
を実行します。 次の値を置き換えます。
-
<integration-name>
: OAuth 統合の一意の名前。 -
<workspace-url>
: Databricks ワークスペースの URL。OAUTH_REDIRECT_URI
をhttps://<workspace-url>/login/oauth/snowflake.html
に設定する必要があります。ここで、<workspace-url>
は、Snowflake 接続を作成する Databricks ワークスペースの一意の URL です。 -
<duration-in-seconds>
: 更新トークンの時間の長さ。
OAUTH_REFRESH_TOKEN_VALIDITY
は、デフォルトで 90 日に設定されているカスタムフィールドです。 更新トークンの有効期限が切れたら、接続を再認証する必要があります。 フィールドを妥当な時間の長さに設定します。
CREATE SECURITY INTEGRATION <integration-name>
TYPE = oauth
ENABLED = true
OAUTH_CLIENT = custom
OAUTH_CLIENT_TYPE = 'CONFIDENTIAL'
OAUTH_REDIRECT_URI = 'https://<workspace-url>/login/oauth/snowflake.html'
OAUTH_ISSUE_REFRESH_TOKENS = TRUE
OAUTH_REFRESH_TOKEN_VALIDITY = <duration-in-seconds>
OAUTH_ENFORCE_PKCE = TRUE;
(オプション)OAuth アクセス トークンを要求する
「How To: Snowflake OAuth を使用してカスタムクライアント用に OAuth トークンを生成して使用する」に従ってください Snowflakeナレッジベースで。
接続を作成する
接続では、外部データベースシステムにアクセスするためのパスと認証情報を指定します。接続を作成するには、カタログエクスプローラーを使用するか、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 Snowflake.
-
For Auth type, select
Username and password
(basic authentication),OAuth access token
,PEM Private Key
, orOAuth
from the drop-down menu. -
(Optional) Add a comment.
-
Click Next.
-
Enter the following authentication and connection details for your Snowflake warehouse. Properties that are specific to the authentication method you selected are preceded by the
Auth type
in parentheses.-
Host: For example,
snowflake-demo.east-us-2.azure.snowflakecomputing.com
-
Port: For example,
443
-
User: For example,
snowflake-user
-
(Basic authentication) Password: For example,
password123
-
(OAuth access token) Access token: Access token from (Optional) Request an OAuth access token.
-
(OAuth access token) Expires in secs: The expiration time (in seconds) for the access token from (Optional) Request an OAuth access token (
expires_in
). -
(OAuth) Client ID: In the Snowflake console, run
SELECT SYSTEM$SHOW_OAUTH_CLIENT_SECRETS('<security-integration-name>')
to retrieve the client ID for the security integration. -
(OAuth) Client secret: In the Snowflake console, run
SELECT SYSTEM$SHOW_OAUTH_CLIENT_SECRETS('<security-integration-name>')
to retrieve the client secret for the security integration. -
(OAuth) OAuth scope:
refresh_token session:role:<role-name>
. Specify the Snowflake role to use in<role-name>
. -
(OAuth) Sign in with Snowflake: Click and sign in to Snowflake using your OAuth credentials.
-
(PEM Private Key) PEM private key: Private key from RSA key par in HEX64 format.
-
(PEM Private Key) Expires in secs: The expiration time (in seconds) for the connection made with a private key.
Upon successful sign-in, you are directed back to the Set up connection wizard.
-
-
Click Create connection.
-
(Basic authentication) On the Connection details page, specify the following:
- Snowflake warehouse: For example,
my-snowflake-warehouse
- (Optional) Proxy host: Host of the proxy used to connect to Snowflake. You must also select Use proxy and specify Proxy port.
- (Optional) Use proxy: Whether to connect to Snowflake using a proxy server.
- (Optional) Proxy port: Port of the proxy used to connect to Snowflake. You must also select Use proxy and specify Proxy host.
- (Optional) Snowflake role: The default security role to use for the session after connecting.
- Snowflake warehouse: For example,
-
Click Next.
-
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.
CREATE CONNECTION <connection-name> TYPE snowflake
OPTIONS (
host '<hostname>',
port '<port>',
sfWarehouse '<warehouse-name>',
user '<user>',
password '<password>'
);
We recommend that you use Databricks secrets instead of plaintext strings for sensitive values like credentials. For example:
CREATE CONNECTION <connection-name> TYPE snowflake
OPTIONS (
host '<hostname>',
port '<port>',
sfWarehouse '<warehouse-name>',
user secret ('<secret-scope>','<secret-key-user>'),
password secret ('<secret-scope>','<secret-key-password>')
)
For information about setting up secrets, see Secret management.
フォーリンカタログの作成
UI を使用してデータソースへの接続を作成する場合は、フォーリンカタログの作成が含まれているため、この手順をスキップできます。
フォーリンカタログは、外部データシステム内のデータベースをミラーリングするため、DatabricksとUnity Catalogを使用して、そのデータベース内のデータへのアクセスを管理できます。フォーリンカタログを作成するには、すでに定義されているデータソースへの接続を使用します。
フォーリンカタログを作成するには、Catalog ExplorerCREATE FOREIGN CATALOG
SQL を使用するか、Databricks ノートブックまたは クエリSQL エディターの コマンドを使用します。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.
-
Follow the instructions for creating foreign catalogs in Create catalogs.
Run the following SQL command in a notebook or SQL query 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.<database-name>
: Name of the database you want to mirror as a catalog in Databricks.
CREATE FOREIGN CATALOG [IF NOT EXISTS] <catalog-name> USING CONNECTION <connection-name>
OPTIONS (database '<database-name>');
大文字と小文字を区別するデータベース識別子
フォーリンカタログのdatabase
フィールドは、Snowflake データベース識別子にマップされます。 Snowflake データベース識別子が大文字と小文字を区別しない場合は、フォーリンカタログ<database-name>
で使用する大文字と小文字が保持されます。 ただし、Snowflake データベース識別子が大文字と小文字を区別する場合は、大文字と小文字を保持するためにフォーリンカタログ<database-name>
を二重引用符で囲む必要があります。
例えば:
-
database
は次のように変換されます。DATABASE
-
"database"
は次のように変換されます。database
-
"database"""
は次のように変換されます。database"
二重引用符をエスケープするには、別の二重引用符を使用します。
-
"database""
二重引用符が正しくエスケープされないため、エラーが発生します。
詳細については、Snowflake ドキュメントの 「識別子の要件 」を参照してください。
サポートされているプッシュダウン
次のプッシュダウンがサポートされています。
- フィルター
- 予測
- 上限
- テーブルのJOIN
- 集計 (Average、Corr、CovPopulation、CovSample、Count、Max、Min、StddevPop、StddevSamp、Sum、VariancePop、VarianceSamp)
- 関数 (文字列関数、数学関数、データ関数、時刻関数、タイムスタンプ関数、およびその他の関数 (Alias、Cast、SortOrder など))
- Windows の機能 (密度ランク、ランク、行番号)
- 分別
データ型マッピング
SnowflakeからSparkに読み取ると、データ型は次のようにマップされます。
Snowflake タイプ | Spark タイプ |
---|---|
decimal, number, numeric | DecimalType |
bigint, byteint, int, integer, smallint, tinyint | IntegerType |
float, float4, float8 | FloatType |
double, double precision, real | DoubleType |
char, character, string, text, time, varchar | StringType |
binary | BinaryType |
ブーリアン | BooleanType |
日付 | DateType |
datetime, timestamp, timestamp_ltz, timestamp_ntz, timestamp_tz | TimestampType |
OAuth の制限
OAuth サポートの制限は次のとおりです。
- Snowflake OAuth エンドポイントは、Databricks コントロールプレーン IP からアクセスできる必要があります。 「Databricks のサービスと資産の IP アドレスとドメイン」を参照してください。Snowflake は、セキュリティ統合レベルでのネットワーク ポリシーの設定をサポートしており、これにより、 Databricks コントロール プレーンから OAuth エンドポイントへの直接接続を可能にする別のネットワーク ポリシーを使用して承認を行うことができます。
- プロキシの使用 、 プロキシホスト 、 プロキシポート 、およびSnowflakeロール構成オプションはサポートされていません。 Snowflake ロール を OAuth スコープの一部として指定します。
PEM 秘密鍵の制限
PEM 秘密鍵のサポート制限は次のとおりです。
-
Snowflake JDBC ドライバーは、暗号化された秘密キーによる認証をサポートしていません。 エラーを回避するには、次のように
-nocrypt
オプションを追加したキーを生成します。openssl genrsa 2048 | openssl pkcs8 -topk8 -inform PEM -out rsa_key.p8 -nocrypt
追加のリソース
Snowflakeドキュメンテーションの次の記事をご参照ください。