עיון בנתונים וחילוץ שלהם

ארכיטקטורת נתונים רב-שלבית מארגנת את הנתונים בשכבות איכות הולכות ומשתפרות: ארד, כסף וזהב. הארכיטקטורה הזו מספקת מתודולוגיה מובנית לשינוי נתונים גולמיים לנכסים נקיים ואמינים.

במאמר הזה נסביר איך ליצור את רמת הברונזה, שאליה מעבירים נתונים ממערכות מקור חיצוניות. השכבה הזו מספקת מקור אמין אחד, מבטיחה שושלת נתונים מלאה ומאפשרת לעבד מחדש צינורות.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 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 Google Cloud 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 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.

    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.

  6. התקינו את ה-CLI של Google Cloud.

  7. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  8. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  9. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. Verify that billing is enabled for your Google Cloud project.

  11. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  12. 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 Google Cloud 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 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.

    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.

  13. התקינו את ה-CLI של Google Cloud.

  14. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  15. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    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. מייצאים את הטבלה bigquery-public-data:samples.shakespeare לקטגוריה של Cloud Storage בפורמט CSV.

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

    הפקודה הזו מפעילה משימת ייצוא שכותבת את התוכן של הטבלה לנתיב Cloud Storage שצוין.

קריאת נתונים גולמיים באמצעות PySpark

אחרי שהנתונים מגיעים ל-Cloud Storage, אפשר להשתמש במשימת PySpark באשכול Managed Service for Apache Spark כדי לקרוא את הנתונים ולבדוק אותם. ‫Apache Spark מתקשר עם Cloud Storage באמצעות המחבר של Cloud Storage, שמאפשר להשתמש בסכימת ה-URI‏ gs:// כדי לקרוא ולכתוב נתונים.

  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, ולהעביר את הנתונים לרמת הברונזה.

הטמעה של מסדי נתונים

כדי לשמור על סנכרון אגם הנתונים עם מסדי נתונים תפעוליים, אפשר להשתמש בסימון נתונים שהשתנו (CDC). עבודת Managed Service for Apache Spark יכולה להירשם לנושא Pub/Sub שמקבל אירועי שינוי, לעבד את הזרם ולהחיל את השינויים במאגר הנתונים הגולמי ב-Cloud Storage.

המאמרים הבאים