在 BigLake 元資料庫中,使用資料主體建立 Apache Iceberg 資料表

本文說明如何執行 Serverless for Apache Spark SQL 和 PySpark 批次工作負載,以建立 Apache Iceberg 資料表,並將中繼資料儲存在 BigLake Metastore 中。如要瞭解執行 Spark 程式碼的其他方式,請參閱「在 BigQuery 筆記本中執行 PySpark 程式碼」和「執行 Apache Spark 工作負載」一文。

事前準備

請建立 Google Cloud 專案和 Cloud Storage bucket

  1. 設定專案

    1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
    2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    3. Enable the Dataproc, BigQuery, and Cloud Storage APIs.

      Enable the APIs

    4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    5. Enable the Dataproc, BigQuery, and Cloud Storage APIs.

      Enable the APIs

    6. 在專案中建立 Cloud Storage bucket

      1. In the Google Cloud console, go to the Cloud Storage Buckets page.

        Go to Buckets

      2. Click Create.
      3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
        1. In the Get started section, do the following:
          • Enter a globally unique name that meets the bucket naming requirements.
          • To add a bucket label, expand the Labels section (), click Add label, and specify a key and a value for your label.
        2. In the Choose where to store your data section, do the following:
          1. Select a Location type.
          2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
          3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

            Set up cross-bucket replication

            1. In the Bucket menu, select a bucket.
            2. In the Replication settings section, click Configure to configure settings for the replication job.

              The Configure cross-bucket replication pane appears.

              • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
              • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
              • Click Done.
        3. In the Choose how to store your data section, do the following:
          1. Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
          2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
        4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
        5. In the Choose how to protect object data section, do the following:
          • Select any of the options under Data protection that you want to set for your bucket.
            • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
            • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
            • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
              • To enable Object Retention Lock, click the Enable object retention checkbox.
              • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
          • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
      4. Click Create.

    7. BigQuery 資料編輯者 (roles/bigquery.dataEditor) 角色授予 Compute Engine 預設服務帳戶 (PROJECT_NUMBER-compute@developer.gserviceaccount.com)。如需操作說明,請參閱「授予單一角色」。

      Google Cloud CLI 範例:

      gcloud projects add-iam-policy-binding PROJECT_ID \
          --member PROJECT_NUMBER-compute@developer.gserviceaccount.com \
      --role roles/bigquery.dataEditor
      

      注意:

      • PROJECT_IDPROJECT_NUMBER 會列在 Google Cloud 控制台資訊主頁的「專案資訊」部分。
    8. OSS 資源到 BigQuery 資源的對應關係

      請注意開放原始碼資源與 BigQuery 資源字詞之間的對應關係:

      OSS 資源 BigQuery 資源
      命名空間、資料庫 資料集
      分區或未分區資料表 資料表
      查看 查看

      建立 Iceberg 資料表

      本節說明如何使用 Serverless for Apache Spark Spark SQL 和 PySpark 批次工作負載,在 BigLake 中繼資料存放區建立含有中繼資料的 Iceberg 資料表。

      Spark SQL

      執行 Spark SQL 工作負載,建立 Iceberg 資料表

      下列步驟說明如何執行無伺服器 Apache Spark Spark SQL 批次工作負載,以建立 Iceberg 資料表,並將資料表的中繼資料儲存在 BigLake 中繼資料存放區。

      1. 在本機或 Cloud Shell 中,將下列 Spark SQL 指令複製到 iceberg-table.sql 檔案。

        USE CATALOG_NAME;
        CREATE NAMESPACE IF NOT EXISTS example_namespace;
        DROP TABLE IF EXISTS example_table;
        CREATE TABLE example_table (id int, data string) USING ICEBERG LOCATION 'gs://BUCKET/WAREHOUSE_FOLDER';
        INSERT INTO example_table VALUES (1, 'first row');
        ALTER TABLE example_table ADD COLUMNS (newDoubleCol double);
        DESCRIBE TABLE example_table;
        

        更改下列內容:

        • CATALOG_NAME:Iceberg 目錄名稱。
        • BUCKETWAREHOUSE_FOLDER:做為 Iceberg 倉庫目錄的 Cloud Storage bucket 和資料夾。
      2. 在含有 iceberg-table.sql 的目錄中,於本機或 Cloud Shell 執行下列指令,提交 Spark SQL 工作負載。

        gcloud dataproc batches submit spark-sql iceberg-table.sql \
            --project=PROJECT_ID \
            --region=REGION \
            --deps-bucket=BUCKET_NAME \
            --version=2.2 \
            --subnet=SUBNET_NAME \
            --properties="spark.sql.catalog.CATALOG_NAME=org.apache.iceberg.spark.SparkCatalog,spark.sql.catalog.CATALOG_NAME.catalog-impl=org.apache.iceberg.gcp.bigquery.BigQueryMetastoreCatalog,spark.sql.catalog.CATALOG_NAME.gcp_project=PROJECT_ID,spark.sql.catalog.CATALOG_NAME.gcp_location=LOCATION,spark.sql.catalog.CATALOG_NAME.warehouse=gs://BUCKET/WAREHOUSE_FOLDER"
          

        注意:

        • PROJECT_ID:您的 Google Cloud 專案 ID。 專案 ID 會列在 Google Cloud 控制台資訊主頁的「專案資訊」部分。
        • REGION:可執行工作負載的 Compute Engine 區域
        • BUCKET_NAME:Cloud Storage bucket 的名稱。Spark 會先將工作負載依附元件上傳至這個值區的 /dependencies 資料夾,再執行批次工作負載。WAREHOUSE_FOLDER 位於這個值區中。
        • --versionServerless for Apache Spark 執行階段 2.2 以上版本。
        • SUBNET_NAMEREGION 中的虛擬私有雲子網路名稱。如果省略這個旗標,Serverless for Apache Spark 會在工作階段區域中選取 default 子網路。Serverless for Apache Spark 會在子網路上啟用私人 Google 存取權 (PGA)。如需網路連線需求,請參閱「Google Cloud Serverless for Apache Spark 網路設定」。
        • LOCATION:支援的 BigQuery 位置。預設位置為「美國」。
        • --properties 目錄資源
      3. 在 BigQuery 中查看資料表的中繼資料

        1. 前往 Google Cloud 控制台的「BigQuery」頁面。

          前往 BigQuery Studio

        2. 查看 Iceberg 資料表中繼資料。

      PySpark

      下列步驟說明如何執行 Serverless for Apache Spark PySpark 批次工作負載,建立 Iceberg 資料表,並將資料表的中繼資料儲存在 BigLake Metastore 中。

      1. 在本地或 Cloud Shell 中,將下列 PySpark 程式碼複製到 iceberg-table.py 檔案。
        from pyspark.sql import SparkSession
        spark = SparkSession.builder.appName("iceberg-table-example").getOrCreate()
        
        catalog = "CATALOG_NAME"
        namespace = "NAMESPACE"
        
        spark.sql(f"USE `{catalog}`;")
        spark.sql(f"CREATE NAMESPACE IF NOT EXISTS `{namespace}`;")
        spark.sql(f"USE `{namespace}`;")
        
        # Create table and display schema
        spark.sql("DROP TABLE IF EXISTS example_iceberg_table")
        spark.sql("CREATE TABLE example_iceberg_table (id int, data string) USING ICEBERG") 
        spark.sql("DESCRIBE example_iceberg_table;")
        
        # Insert table data.
        spark.sql("INSERT INTO example_iceberg_table VALUES (1, 'first row');")
        
        # Alter table, then display schema. 
        spark.sql("ALTER TABLE example_iceberg_table ADD COLUMNS (newDoubleCol double);")
        spark.sql("DESCRIBE example_iceberg_table;")

        更改下列內容:

        • CATALOG_NAMENAMESPACE:Iceberg 目錄名稱和命名空間會合併,以識別 Iceberg 資料表 (catalog.namespace.table_name)。
      2. 在本機或 Cloud Shell 中,從含有 iceberg-table.py 的目錄執行下列指令,提交 PySpark 工作負載。
        gcloud dataproc batches submit pyspark iceberg-table.py \
            --project=PROJECT_ID \
            --region=REGION \
            --deps-bucket=BUCKET_NAME \
            --version=2.2 \
            --subnet=SUBNET_NAME \
            --properties="spark.sql.catalog.CATALOG_NAME=org.apache.iceberg.spark.SparkCatalog,spark.sql.catalog.CATALOG_NAME.catalog-impl=org.apache.iceberg.gcp.bigquery.BigQueryMetastoreCatalog,spark.sql.catalog.CATALOG_NAME.gcp_project=PROJECT_ID,spark.sql.catalog.CATALOG_NAME.gcp_location=LOCATION,spark.sql.catalog.CATALOG_NAME.warehouse=gs://BUCKET/WAREHOUSE_FOLDER"
            

        注意:

        • PROJECT_ID:您的 Google Cloud 專案 ID。 專案 ID 會列在 Google Cloud 控制台資訊主頁的「專案資訊」部分。
        • REGION:可執行工作負載的 Compute Engine 區域
        • BUCKET_NAME:Cloud Storage bucket 的名稱。Spark 會先將工作負載依附元件上傳至這個值區的 /dependencies 資料夾,再執行批次工作負載。
        • --versionServerless for Apache Spark 執行階段 2.2 以上版本。
        • SUBNET_NAME:虛擬私有雲子網路的名稱,位於 REGION。如果省略這個旗標,Serverless for Apache Spark 會在工作階段區域中選取 default 子網路。Serverless for Apache Spark 會在子網路上啟用私人 Google 存取權 (PGA)。如需網路連線需求,請參閱「Google Cloud Serverless for Apache Spark 網路設定」。
        • LOCATION支援的 BigQuery 位置。預設位置為「美國」。
        • BUCKETWAREHOUSE_FOLDER:做為 Iceberg 倉庫目錄的 Cloud Storage bucket 和資料夾。
        • --properties目錄屬性
      3. 在 BigQuery 中查看資料表結構定義。
        1. 前往 Google Cloud 控制台的「BigQuery」頁面。BigQuery前往 BigQuery Studio
        2. 查看 Iceberg 資料表中繼資料。