Daten untersuchen und extrahieren
Bei einer mehrstufigen Datenarchitektur werden Daten in immer weiter verfeinerte Qualitätsstufen unterteilt: Bronze, Silber und Gold. Diese Architektur bietet eine strukturierte Methodik zum Umwandeln von Rohdaten in saubere, zuverlässige Assets.
In diesem Dokument erfahren Sie, wie Sie die Bronze-Stufe erstellen, in der Sie Daten aus externen Quellsystemen ablegen. Diese Stufe bietet eine zentrale, zuverlässige Quelle, sorgt für eine vollständige Datenabstammung und ermöglicht die Neuverarbeitung von Pipelines.
Hinweis
- Melden Sie sich in Ihrem Google Cloud Konto an. Wenn Sie noch kein Konto bei Google Cloudhaben, erstellen Sie ein Konto, um die Leistung unserer Produkte in der Praxis sehen und bewerten zu können. Neukunden erhalten außerdem ein Guthaben von 300 $, um Arbeitslasten auszuführen, zu testen und bereitzustellen.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, BigQuery, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
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. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud 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. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
Installieren Sie die Google Cloud CLI.
-
Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.
-
Führen Sie den folgenden Befehl aus, um die gcloud CLI zu initialisieren:
gcloud init -
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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Compute Engine, BigQuery, and Cloud Storage APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
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. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud 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. - Click Create and continue.
-
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.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
Installieren Sie die Google Cloud CLI.
-
Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.
-
Führen Sie den folgenden Befehl aus, um die gcloud CLI zu initialisieren:
gcloud init
Rohdaten in Cloud Storage ablegen
Nehmen Sie Rohdaten in einer ersten Speicherebene in Cloud Storage auf. Um die Datenaufnahme zu simulieren, exportieren Sie ein öffentliches Dataset aus BigQuery in einen Cloud Storage-Bucket. Dieser Vorgang ahmt ein externes System nach, das Rohdatendateien an eine Landing Zone liefert.
Erstellen Sie einen Cloud Storage-Bucket als Data Lake. Erstellen Sie ihn in derselben Region wie Ihren Managed Service for Apache Spark-Cluster, um die Leistung zu optimieren.
gsutil mb -l REGION gs://BUCKET_NAME/Exportieren Sie die Tabelle
bigquery-public-data:samples.shakespeareim CSV-Format in Ihren Cloud Storage-Bucket.bq extract \ --destination_format CSV \ "bigquery-public-data:samples.shakespeare" \ "gs://BUCKET_NAME/raw/shakespeare/shakespeare.csv"Mit diesem Befehl wird ein Exportjob gestartet, der den Inhalt der Tabelle in den angegebenen Cloud Storage-Pfad schreibt.
Rohdaten mit PySpark lesen
Nachdem Sie die Daten in Cloud Storage abgelegt haben, können Sie sie mit einem PySpark-Job in einem Managed Service for Apache Spark-Cluster lesen und untersuchen. Apache Spark interagiert über den Cloud Storage-Connector mit Cloud Storage. So können Sie das URI-Schema gs:// verwenden, um Daten zu lesen und zu schreiben.
Verwenden Sie das folgende PySpark-Skript, um eine SparkSession zu erstellen, sie für den Cloud Storage-Zugriff zu konfigurieren und die CSV-Rohdatendatei in ein DataFrame zu lesen.
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()Führen Sie das Skript als Managed Service for Apache Spark-Job aus, um die Rohdaten aufzunehmen und zu untersuchen.
Muster für die Datenaufnahme
Die Bronze-Stufe kann neben Batch-Datei-Uploads auch verschiedene Aufnahmemuster unterstützen. Managed Service for Apache Spark ist eine vielseitige Engine, die verschiedene Aufnahmeszenarien verarbeiten kann.
Streamingaufnahme
Verwenden Sie für kontinuierliche Datenquellen wie IoT-Sensordaten oder Anwendungsprotokolle eine Streaming-Pipeline. Mit Managed Service for Apache Spark können Sie Streams mit hohem Volumen aus einem Dienst wie Pub/Sub oder Apache Kafka verarbeiten und die Daten in der Bronze-Stufe ablegen.
Datenbankaufnahme
Verwenden Sie Change Data Capture (CDC), um den Data Lake mit Betriebsdatenbanken zu synchronisieren. Ein Managed Service for Apache Spark-Job kann ein Pub/Sub-Thema abonnieren, das Änderungsereignisse empfängt, den Stream verarbeiten und die Änderungen auf den Rohdatenspeicher in Cloud Storage anwenden.
Nächste Schritte
- Weitere Informationen zu Managed Service for Apache Spark
- Weitere Informationen zum Cloud Storage-Connector.
- Öffentliche BigQuery-Datasets erkunden.