分析和验证数据质量
本快速入门介绍了如何使用 Knowledge Catalog(以前称为 Dataplex Universal Catalog)分析 BigQuery 表,根据分析洞见定义数据质量规则,以及运行数据质量扫描。
您将完成以下步骤:
准备工作
设置项目:
- 登录您的 Google Cloud 账号。如果您是新手 Google Cloud, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Knowledge Catalog and BigQuery APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Knowledge Catalog and BigQuery APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
所需的角色
如需获得创建和运行数据分析扫描和数据质量扫描以及管理 BigQuery 资源所需的权限,请让管理员向您授予项目的以下 IAM 角色:
-
创建、运行和删除数据扫描:
Dataplex DataScan Editor (
roles/dataplex.dataScanEditor) -
创建、填充和删除示例表:
BigQuery Data Owner (
roles/bigquery.dataOwner) -
在 BigQuery 中运行 SQL 查询:
BigQuery Job User (
roles/bigquery.jobUser)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。
如果您拥有在项目中管理 IAM 访问权限的必要权限,则可以通过运行以下 gcloud 命令向自己的用户账号授予这些角色:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="user:USER_EMAIL" \
--role="roles/dataplex.dataScanEditor"
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="user:USER_EMAIL" \
--role="roles/bigquery.dataOwner"
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="user:USER_EMAIL" \
--role="roles/bigquery.jobUser"
替换以下内容:
PROJECT_ID:您的 Google Cloud 项目 ID。USER_EMAIL:您的用户账号电子邮件地址(例如name@example.com)。
向 Knowledge Catalog 服务代理授予权限
服务代理是 Google 托管式服务 账号,Knowledge Catalog 会使用该账号代表您在 BigQuery 中运行扫描查询。
在 Google Cloud 控制台中,点击工具栏中的激活 Cloud Shell。 预配和连接到环境需要一些时间。
创建 Knowledge Catalog 服务代理:
gcloud beta services identity create --service=dataplex.googleapis.com如果服务代理尚未预配,此命令会创建并输出其电子邮件地址。如果您的项目已具有 Knowledge Catalog 服务代理,该命令会返回现有身份,而不会进行任何更改。
输出类似于以下内容:
serviceAccount:service-PROJECT_NUMBER@gcp-sa-dataplex.iam.gserviceaccount.com请注意输出中的
PROJECT_NUMBER,以便进行后续步骤。授予 BigQuery Job User (
roles/bigquery.jobUser) 角色,以便 Knowledge Catalog 可以在您的项目中运行查询作业:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-dataplex.iam.gserviceaccount.com" \ --role="roles/bigquery.jobUser"
替换以下内容:
PROJECT_ID:您的 Google Cloud 项目 ID。PROJECT_NUMBER:您的 Google Cloud 项目编号。
授予 BigQuery Data Viewer (
roles/bigquery.dataViewer) 角色,以便服务代理可以读取您的表数据和架构:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:service-PROJECT_NUMBER@gcp-sa-dataplex.iam.gserviceaccount.com" \ --role="roles/bigquery.dataViewer"
替换以下内容:
PROJECT_ID:您的 Google Cloud 项目 ID。PROJECT_NUMBER:您的 Google Cloud 项目编号。
创建示例数据集和表
如需安全地试用分析和数据质量扫描,而无需触及生产数据,请设置专用的 BigQuery 数据集,并在您的项目中直接创建一个包含示例数据的表。
控制台
在 Google Cloud 控制台中,前往 BigQuery 页面。
在探索器 窗格中,点击 查看操作 项目 ID 旁边的 ,然后点击 创建数据集。
在数据集 ID 字段中,输入
quickstart_data_profile。在数据位置 列表中,选择 us-central1 (Iowa) 。
点击创建数据集 。
在查询编辑器中,输入以下 SQL 查询,以在
bikeshare_trips表中生成示例共享单车数据:CREATE OR REPLACE TABLE `PROJECT_ID.quickstart_data_profile.bikeshare_trips` AS SELECT -- Duplicate and null IDs IF(MOD(x, 100) = 0, NULL, IF(x > 9900, 1000 + (x - 9900), 1000 + x)) AS trip_id, -- Nulls and unrecognized category values CASE WHEN MOD(x, 50) = 0 THEN 'INVALID_TIER' WHEN MOD(x, 25) = 0 THEN NULL WHEN MOD(x, 4) = 0 THEN 'Local Rider' WHEN MOD(x, 4) = 1 THEN 'Walk Up' WHEN MOD(x, 4) = 2 THEN 'Student Membership' ELSE 'Weekender' END AS subscriber_type, -- Nulls and malformed bike IDs CASE WHEN MOD(x, 60) = 0 THEN 'UNKNOWN' WHEN MOD(x, 30) = 0 THEN NULL ELSE CAST(2000 + x AS STRING) END AS bike_id, -- Null dates and future timestamps CASE WHEN MOD(x, 70) = 0 THEN NULL WHEN MOD(x, 40) = 0 THEN TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL x MINUTE) ELSE