基本的なコンシューマーが実行するデータ分析

概要

This topic demonstrates a basic consumer-run analysis using the clean rooms API. The example shows how a provider can programmatically create and share a clean room with data, and a consumer can run an analysis against the provider's data. The provider defines the SQL queries that can be run against their data. A provider can define queries that query only the provider's data, only the consumer's data, or that join provider and consumer data.

You can download the full code example to upload and run on your Snowflake account.

次の図は、基本的なコンシューマー実行分析における、主要コンポーネントを通じたデータフローを示しています。

Basic data flow direction in a consumer-run analysis

In a basic consumer-run analysis involving two parties, the provider and consumers link data into the clean room. This data is accessed using a secure view stored in the consumer DB in the clean room application package on the consumer's account.

分析中、コンシューマーのアカウント上のクリーンルームアプリは、指定されたコンシューマーとプロバイダーのセキュアビューを使用し、結果はコンシューマーと共有されます。

プロバイダーのステップ

The following list shows the main steps to create, publish, and share a clean room with a consumer:

環境の設定

To use the API, you must use a warehouse that SAMOOHA_APP_ROLE has privileges in. app_wh is one of a number of warehouses with access to the API. Choose the warehouse that is appropriate for your needs. (You can also use your own warehouse, if you choose.)

The SAMOOHA_APP_ROLE role is required to access the API.

USE WAREHOUSE app_wh;
USE ROLE SAMOOHA_APP_ROLE;
Copy

クリーンルームの作成

The next step is to create a new clean room. This is done with a single API call that specifies whether the clean room is for internal or external use. Internal clean rooms can be accessed only by consumers within the same organization; external clean rooms can be used by consumers outside the organization. For both clean room types, consumers must be invited to use the clean room to be able to access it.

外部クリーンルームは、特定のアクションが実行されると、追加のセキュリティチェックがトリガーされます。これが発生した場合は、``provider.view_cleanroom_scan_status``を呼び出す必要があり、セキュリティスキャンがいつ完了するかを確認します。その後、次のアクションに進むことができます。

次の例では、内部クリーンルームを作成します。

CALL samooha_by_snowflake_local_db.provider.cleanroom_init($cleanroom_name, 'INTERNAL');
Copy

結合ポリシーの設定

If you add a template to the clean room that allows the consumer to join on your data, you should set a clean room join policy on your data. A clean room join policy specifies which columns can be joined on in queries run by collaborators. Your own join policies don't constrain your own queries.

Clean rooms support a few types of data policies that you can set on linked data. These policies are similar to, but not the same as, the equivalent Snowflake policies, and are applied only on the internal view, not on the source data. Any Snowflake policies that are set on the source data are propagated to the views linked into a clean room. Clean room policies are set on the linked data only, not on the source data.

重要

The template is responsible for using JinjaSQL filters to enforce policies. If the template does not use policy filters, the policies will not be respected. Always put policy filters on templates that you write, and examine any templates that you run to confirm that they enforce clean room policies.

ポリシーは、リンクしたデータにのみ設定できます。他の関係者のデータにポリシーを設定することはできません。

次の例は、リンクされたテーブルの2つの列が結合可能となるように許可する結合ポリシーを設定する方法を示しています。

CALL samooha_by_snowflake_local_db.provider.set_join_policy(
  $cleanroom_name,
  ['SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:HASHED_EMAIL',
   'SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:HASHED_PHONE']);
Copy

結合ポリシーの詳細はこちら

クリーンルームにテンプレートを追加する

A clean room template is a valid JinjaSQL template that typically evaluates to a SQL query. A template, sometimes called an analysis, can be passed arguments by the caller, and can access any data linked into the clean room. Both providers and consumers can add templates into a clean room and run them.

Snowflake provides a few standard templates, but you will probably write your own custom templates.

設定したクリーンルームポリシーは、テンプレートにポリシーフィルターが含まれている場合にのみ強制されます。したがって、クリーンルームに追加するテンプレートにこれらのフィルターが確実に含まれるようにしてください。ポリシーの詳細については、 clean roomテーブルのポリシーを理解する をご参照ください。

デフォルトでは、コンシューマーのみがテンプレートを実行できます。:doc:`プロバイダーがテンプレートの実行を希望する</user-guide/cleanrooms/demo-flows/provider-run-analysis>`場合は、コンシューマーに権限を求める必要があります。同様に、:ref:`コンシューマーがテンプレートのアップロードを希望する<label-dcr_consumer_written_templates>`場合、プロバイダーに権限を求める必要があります。

カスタムテンプレートの作成について、詳細は:doc:/user-guide/cleanrooms/demo-flows/custom-templates`および:doc:/user-guide/cleanrooms/custom-templates`をご参照ください。

次の例は、Snowflakeが提供するテンプレートをクリーンルームに追加する方法を示しています。

CALL samooha_by_snowflake_local_db.provider.add_templates(
  $cleanroom_name,
  ['prod_overlap_analysis']);
Copy

列ポリシーを設定する

クリーンルームの列ポリシーは、コラボレーターが実行するクエリで、テーブルのどの列を投影できるかを指定します。列ポリシーは列とテンプレートの両方に関連付いているため、異なる列を異なるテンプレートで投影可能として定義できます。テンプレートは、そのテンプレートの列ポリシーを設定する前に、クリーンルームに存在する必要があります。

Column policies, like all policies, are overwrite-only; this means that setting column policies completely overwrites any existing column policies set by that account. Both the provider and the consumer can set column policies on their data. Learn more about column policies.

次の例は、以前にリンクしたクリーンルームのサンプルデータベースから4つの列を投影できるようにする方法を示しています。

CALL samooha_by_snowflake_local_db.provider.set_column_policy($cleanroom_name, [
  'prod_overlap_analysis:SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:STATUS',
  'prod_overlap_analysis:SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:AGE_BAND',
  'prod_overlap_analysis:SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:DAYS_ACTIVE',
  'prod_overlap_analysis:SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS:REGION_CODE']);
Copy

コンシューマーと共有する

プロバイダーから招待されたコンシューマーのみがクリーンルームにアクセスできます。コンシューマーは、他のコンシューマーとクリーンルームを共有することはできません。指定されたコンシューマーは、クリーンルームが公開されるまでクリーンルームにアクセスできません。クリーンルームへの参加招待は、ユーザーレベルではなく、アカウントレベルです。

次の例は、2人のコンシューマーとクリーンルームを共有する方法を示しています。プロシージャは、コンシューマーアカウントロケーターと:ref:コンシューマーデータ共有アカウント IDs<label-account_name_data_sharing> の2つの並列のコンマ区切りリストを取得します。

CALL samooha_by_snowflake_local_db.provider.add_consumers(
  $cleanroom_name,
  'CONSUMER_LOCATOR1,CONSUMER_LOCATOR2',
  'CONSUMER_DATA_SHARING_ACCOUNT_ID1,CONSUMER_DATA_SHARING_ACCOUNT_ID2');
Copy

他のクラウドホスティング地域のコンシューマーと共有する

コンシューマーとプロバイダーが異なるクラウドリージョンにある場合、プロバイダーとコンシューマーは、コンシューマーがクリーンルームに追加される前に :doc:`クロスクラウドの自動フルフィルメント </user-guide/cleanrooms/enabling-laf>`を有効にする必要があります。:code:`SELECT CURRENT_REGION();`を実行すると、独自のクラウドリージョンを表示できます。通常、コンシューマーのリージョンは表示されませんが、別のリージョンのコンシューマーを追加しようとすると、問題を示すメッセージが表示され、``provider.add_consumers``は失敗します。この失敗が発生した場合は、別のリージョンにあるアカウントを削除するために``provider.remove_consumers``を呼び出す必要があります。その後、クロスクラウドの自動フルフィルメントを有効にしてから、クロスリージョンのアカウントを再度追加します。

デフォルトバージョンを設定する

クリーンルームはバージョン管理されたネイティブアプリケーションです。クリーンルームにコードを追加するなど、特定のアクションはアプリケーションの新しいパッチバージョンを生成します。コンシューマーは自分のアカウントにクリーンルームをインストールする必要があります。インストールするバージョンは、指定したデフォルトのバージョン番号に基づいています。後でクリーンルームの新しいバージョンを公開し、デフォルトのバージョン番号をインクリメントすると、コンシューマーがインストールするバージョンは自動的に更新され、新しいインストールはデフォルトで新しいバージョンになります。クリーンルームのバージョン管理について詳細はこちら

次の例は、コードがアップロードされていない場合、クリーンルームのデフォルトバージョンをV1.0.0に設定する方法を示しています。これは、クリーンルームの初期バージョンです。

CALL samooha_by_snowflake_local_db.provider.set_default_release_directive(
  $cleanroom_name,
  'V1_0',          -- Version number: Never changes.
  '0'              -- Patch number: Can change.
  );
Copy

クリーンルームを公開する

以下の例に示すように、クリーンルームを公開または再公開します。このプロシージャが初めて呼び出されると、クリーンルームが表示され、共有されたコンシューマーは誰でもインストールできるようになります。デフォルトバージョンを更新したり、クリーンルームUIに固有の変更を加えたりするときなど、重要な変更を行うたびに、このプロシージャを呼び出す必要があります。

CALL samooha_By_snowflake_local_db.provider.create_or_update_cleanroom_listing(
  $cleanroom_name);
Copy

これで、コンシューマーは次に説明するように、クリーンルームのインストール、データのリンク、ポリシーの設定、テンプレートの実行ができるようになります。

Tip

When you no longer need a clean room, you should delete the clean room on the provider and consumer accounts (provider.drop_cleanroom and consumer.uninstall_cleanroom). There is a limit to the number of clean rooms and collaborators per account. When you leave many unused clean rooms in your account, you can reach your quota.

コンシューマーのステップ

プロバイダーがクリーンルームを公開すると、コラボレーターとして追加されたすべてのコンシューマーは、UIまたはAPIのどちらかを使用してクリーンルームを表示したり、インストールしたりすることができます。このセクションでは、コンシューマーがどのようにクリーンルームをインストールし、APIを使用して分析を実行するかを示します。

Here is a quick overview of the steps the consumer takes to install a clean room and run an analysis:

環境の設定

Like the provider, the consumer must use a warehouse that SAMOOHA_APP_ROLE can access. However, unlike the provider, the consumer can either use the SAMOOHA_APP_ROLE role directly for full API access, or a clean room administrator in that account can grant a more limited role that gives privileges to run a subset of the API for consumers. This limited role, sometimes generically called a "run role," is granted by a user with full clean room privileges. Learn how to grant limited API access.

実行ロールではクリーンルームをインストールできないため、次の例に示すように、SAMOOHA_APP_ROLEを使用する必要があります。

USE WAREHOUSE app_wh;
USE ROLE SAMOOHA_APP_ROLE;
Copy

クリーンルームをインストールする

次のスニペットは、インストールするよう招待されたすべてのクリーンルームを一覧表示する方法を示しています。

-- See all clean rooms, installed and not.
CALL samooha_by_snowflake_local_db.consumer.view_cleanrooms();

-- See only clean rooms that aren't installed.
CALL samooha_by_snowflake_local_db.consumer.view_cleanrooms() ->>
  SELECT * FROM $1
    WHERE IS_ALREADY_INSTALLED = false;
Copy

次の例に示すように、プロバイダーから共有されたクリーンルームをインストールします。クリーンルームをインストールする際には、プロバイダーのアカウントロケーターを指定する必要があります。

CALL samooha_by_snowflake_local_db.consumer.install_cleanroom(
  $cleanroom_name,
  '<PROVIDER_ACCOUNT_LOCATOR>');
Copy

Tip

クリーンルームには名前とIDの両方があります。APIを使用して作成されたクリーンルームの場合は、APIのプロシージャでクリーンルームの名前が必要な場合は常に、そのクリーンルーム名を使用します。UIで作成されたクリーンルームの場合は、APIのプロシージャでクリーンルームの名前が必要な場合は常に、そのクリーンルームの名前ではなく、IDを使用します。

クリーンルームUIでは、API を使用して:ui:`Supported with Developer APIs`として作成されたクリーンルームをラベル付けします。

データを追加してポリシーを設定する

If the clean room templates allow the consumer to include their own data in a query, the consumer registers data, links data, and sets policies like the provider does. Be sure to use the consumer versions of the procedures, as shown in the following example:

-- You must use a role with MANAGE GRANTS privilege on an object to register it.
USE ROLE ACCOUNTADMIN;
CALL samooha_by_snowflake_local_db.consumer.register_db('MY_DATABASE');

-- Link some tables.
CALL samooha_by_snowflake_local_db.consumer.link_datasets(
  $cleanroom_name,
  [
    'SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS',
    'MY_DATABASE.PUBLIC.EXPOSURES'
  ]);
Copy

プロバイダーの結合ポリシーは、どのプロバイダー列を結合できるかを示します。この例では、どのプロバイダー列を結合できるか確認する方法を示しています。

CALL samooha_by_snowflake_local_db.consumer.view_provider_join_policy($cleanroom_name);
Copy

A provider needs the consumer's approval to run a template in the clean room. As a result, most consumers don't bother setting policies on the tables that they link in. Nevertheless, we recommend that you consider adding policies in case a provider asks to run a template later, because you might forget to add appropriate policies at that time.

ポリシーを設定すると、テンプレートにテンプレートの列に対する join_policy または column_policy フィルターが含まれている場合にのみポリシーが強制されます。したがって、クリーンルームに追加するテンプレートには、ポリシーを強制するこれらのフィルターが確実に含まれるようにしてください。クリーンルームのテンプレートを調べるには、 consumer.view_added_templates を呼び出します。ポリシーの詳細については、 clean roomテーブルのポリシーを理解する をご参照ください。

分析を実行する

テンプレートを実行する前に、通常は、テンプレートが何を行い、どのような変数を受け入れるのかを調べ、次にクリーンルームで利用可能なプロバイダーテーブルを調べます。

テンプレートを調査する

クリーンルームのテンプレートを一覧表示し、それぞれのコードを調べることができます(プロバイダーが明示的に :ref:`コードを難読化<dcr_provider_add_custom_sql_template>`している場合を除く)。これは、クエリをよりよく理解するのに役立ちます。また、クリーンルームにテンプレートを解析させ、コードを実行するときにどの変数を渡すことができるのかを示すように依頼することもできます。

テンプレートの設計に従って、クエリで使用するテーブルのリストを渡すことができます。クリーンルームにリンクされたテーブルは、テンプレートに渡すことができます。

Many templates also support variables that you can specify at run time; for example, to match a particular value or to specify which columns to show. Ideally, the provider should let you know what the template does and what arguments it accepts. But typically, you also want to examine a template to see the code. The following snippet lists the templates added to the clean room by any collaborator, and gets the arguments supported for a specific template:

-- View the list of templates available in this clean room,
-- and the source code for each template.
CALL samooha_by_snowflake_local_db.consumer.view_added_templates($cleanroom_name);

-- Show which variables can be passed in when running the specified template.
CALL samooha_by_snowflake_local_db.consumer.get_arguments_from_template(
  $cleanroom_name,
  $template_name
);
Copy

Tip

テンプレートで使用される``my_table`` 配列変数が表示された場合、この変数はテンプレートの実行時に渡すコンシューマーテーブル名のリストを保持します。``source_table``配列変数が表示された場合、この変数はテンプレートの実行時に渡すプロバイダーテーブル名のリストを保持します。

どのデータが利用可能か確認する

次の例に示すように、あなた自身やプロバイダーがクリーンルームにリンクしたデータセットを表示することができます。

-- See which datasets you have linked into the clean room.
CALL samooha_by_snowflake_local_db.consumer.view_consumer_datasets($cleanroom_name);

-- See which datasets the provider has linked into the clean room.
CALL samooha_by_snowflake_local_db.consumer.view_provider_datasets($cleanroom_name);
Copy

テーブル名を渡す場合は、これらのプロシージャの結果からの取得したテーブル名を使用します。ビュー名は使用 しません

テンプレートを実行する

前の2つのステップでは、どのようなデータを持っていて、どのような変数を渡すことができるのかを学びました。これで分析を実行する準備が整いました。

クエリとデータサイズに応じて、ウェアハウスのサイズを:ref:より適切なもの <label-cleanrooms_installation_details_warehouses> に変更することができます。

The following example shows how a user might call a template that takes both consumer and provider tables, and two variables: dimensions, which is used as a grouping column, and an optional where_clause, which is used in a WHERE clause in the query.

テンプレートは単一のプロバイダーテーブルに対してクエリを実行するため、リクエストはコンシューマーテーブルを省略します。

次の例では、 dimensions 値が`p`を先頭にした列名であることに注意してください。その p は、この列が渡されたプロバイダーテーブルからのものであることを示します。通常、列名には`p` または c を追加する必要があります。プロバイダーまたはコンシューマーのどのテーブルからのものであるかを示し、列名を区別するためです。ただし、この要件は非常にテンプレート固有のものです。これらの接頭語がいつ必要かを理解するには、テンプレートプロバイダーとコミュニケーションをとるか、テンプレートコードを調べる必要があります。

CALL samooha_by_snowflake_local_db.consumer.run_analysis(
$cleanroom_name,
$template_name,
[],                                              -- This template doesn't accept consumer tables.
['SAMOOHA_SAMPLE_DATABASE.DEMO.CUSTOMERS'],      -- Provider tables.
object_construct(                                -- Template-specific arguments.
  'dimensions', ['p.STATUS'],                    -- Template takes a variable named 'dimensions'.
  'where_clause', 'p.REGION_CODE=$$REGION_10$$'  -- Template allows you to pass in a WHERE clause.
                                                 -- $$ is used to wrap string literals
  )
);
Copy

コード例

The following worksheet files demonstrate how to create, share, and run a clean room analysis.

Download the following examples, and then upload them as worksheet files in your Snowflake account. You need separate accounts for the provider and consumer, each with the clean rooms API installed. See instructions to upload a SQL worksheet into your Snowflake account.