探索和提取数据

多阶段数据架构将数据整理成质量逐步提升的层级:青铜级、白银级和黄金级。此架构提供了一种结构化方法,用于将原始数据转换为干净可靠的资产。

本文档将介绍如何创建青铜层,您可将外部源系统中的数据放入该层。此层级提供单一事实来源,确保完整的数据沿袭,并支持重新处理流水线。

准备工作

  1. In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Cloud de Confiance project.

  3. Enable the Compute Engine, BigQuery, and Cloud Storage APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the APIs

  4. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Cloud de Confiance console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Cloud de Confiance console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the following roles to the service account: Storage Object Admin, Dataproc Worker.

      To grant a role, find the Select a role list, then select the role.

      To grant additional roles, click Add another role and add each additional role.

    7. Click Continue.
    8. Click Done to finish creating the service account.

  5. Install the Google Cloud CLI.

  6. Configure the gcloud CLI to use your federated identity.

    For more information, see Sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init

将原始数据放入 Cloud Storage

将原始数据注入到 Cloud Storage 中的初始存储层。为了模拟数据提取,请将公开数据集从 BigQuery 导出到 Cloud Storage 存储桶。此过程模拟了外部系统将原始数据文件交付到着陆区的过程。

  1. 创建一个 Cloud Storage 存储桶作为数据湖。为了优化性能,请在与 Managed Service for Apache Spark 集群相同的区域中创建该存储桶。

    gsutil mb -l REGION gs://BUCKET_NAME/
    
  2. 以 CSV 格式将 bigquery-public-data:samples.shakespeare 表导出到您的 Cloud Storage 存储桶。

    bq extract \
        --destination_format CSV \
        "bigquery-public-data:samples.shakespeare" \
        "gs://BUCKET_NAME/raw/shakespeare/shakespeare.csv"
    

    此命令会启动一个导出作业,该作业会将表的内容写入指定的 Cloud Storage 路径。

使用 PySpark 读取原始数据

将数据放入 Cloud Storage 后,使用 Managed Service for Apache Spark 集群上的 PySpark 作业读取并探索数据。Apache Spark 通过 Cloud Storage 连接器与 Cloud Storage 进行交互,该连接器可让您使用 gs:// URI 方案来读取和写入数据。

  1. 使用以下 PySpark 脚本创建 SparkSession,将其配置为可访问 Cloud Storage,并将原始 CSV 文件读入 DataFrame。

    from pyspark.sql import SparkSession
    
    # --- Configuration ---
    gcs_bucket = "BUCKET_NAME"
    raw_path = f"gs://{gcs_bucket}/raw/shakespeare/shakespeare.csv"
    # For local development only.
    service_account_key_path = "/path/to/your/service-account-key.json"
    
    # --- Spark Session Initialization ---
    spark = SparkSession.builder \
      .appName("DataprocETL-RawIngestion") \
      .config("spark.jars", "https://storage.googleapis.com/hadoop-lib/gcs/gcs-connector-hadoop3-latest.jar") \
      .getOrCreate()
    
    # --- Authentication for local development ---
    # This step is not necessary when running on a Dataproc cluster
    # with the service account attached to the cluster VMs.
    spark.conf.set("google.cloud.auth.service.account.json.keyfile", service_account_key_path)
    
    # --- Read Raw Data from Cloud Storage ---
    # Read the raw CSV data into a DataFrame.
    # inferSchema=True scans the data to determine column types.
    raw_df = spark.read.csv(raw_path, header=True, inferSchema=True)
    
    # --- Initial Exploration ---
    print("Raw data count:", raw_df.count())
    print("Schema:")
    raw_df.printSchema()
    print("Sample of raw data:")
    raw_df.show(10, truncate=False)
    
    # --- Stop Spark Session ---
    spark.stop()
    
  2. 以 Managed Service for Apache Spark 作业的形式运行该脚本,以注入和探索原始数据。

数据注入模式

除了批量文件上传之外,青铜层还可以支持各种提取模式。Managed Service for Apache Spark 是一款多功能引擎,能够处理各种不同的提取场景。

流式注入

对于 IoT 传感器数据或应用日志等持续数据源,请使用流处理流水线。您可以使用 Managed Service for Apache Spark 处理来自 Pub/Sub 或 Apache Kafka 等服务的大容量数据流,并将数据放入 Bronze 层。

数据库注入

如需使数据湖与运营数据库保持同步,请使用变更数据捕获 (CDC)。Managed Service for Apache Spark 作业可以订阅接收更改事件的 Pub/Sub 主题,处理流,并将更改应用到 Cloud Storage 中的原始数据存储区。

后续步骤