Esegui carichi di lavoro Dataproc serverless con Cloud Composer

Cloud Composer 1 | Cloud Composer 2

Questa pagina descrive come utilizzare Cloud Composer 2 per eseguire carichi di lavoro Dataproc Serverless su Google Cloud.

Gli esempi nelle sezioni seguenti mostrano come utilizzare gli operatori per la gestione dei carichi di lavoro batch di Dataproc Serverless. Utilizza questi operatori nei DAG che creano, eliminano, elencano e ricevono un carico di lavoro batch di Dataproc Serverless Spark:

Prima di iniziare

  1. Abilita l'API Dataproc:

    Console

    Attiva l'API Dataproc.

    Abilita l'API

    gcloud

    Attiva l'API Dataproc.

    gcloud services enable dataproc.googleapis.com

  2. Seleziona la posizione per il file del carico di lavoro Batch. Puoi utilizzare una delle seguenti opzioni:

    • Crea un bucket Cloud Storage in cui venga archiviato questo file.
    • Utilizza il bucket del tuo ambiente. Poiché non è necessario sincronizzare questo file con Airflow, puoi creare una sottocartella separata all'esterno delle cartelle /dags o /data. Ad esempio: /batches.
    • Utilizza un bucket esistente.

Configurazione di file e variabili Airflow

Questa sezione mostra come impostare i file e le variabili Airflow per questo tutorial.

Carica un file di carico di lavoro Spark ML serverless di Dataproc in un bucket

Il carico di lavoro in questo tutorial esegue uno script pyspark:

  1. Salva qualsiasi script pyspark in un file locale denominato spark-job.py. Ad esempio, puoi utilizzare lo script pyspark di esempio.

  2. Carica il file nella posizione selezionata in Prima di iniziare.

Imposta le variabili Airflow

Gli esempi nelle sezioni seguenti utilizzano le variabili Airflow. Se imposti i valori di queste variabili in Airflow, il codice DAG potrà accedere a questi valori.

Gli esempi di questo tutorial utilizzano le seguenti variabili Airflow. Puoi impostarli come necessario, a seconda dell'esempio utilizzato.

Imposta le seguenti variabili Airflow da utilizzare nel codice DAG:

Utilizzare la console Google Cloud e la UI di Airflow per impostare ogni variabile di Airflow

  1. Nella console Google Cloud, vai alla pagina Ambienti.

    Vai a Ambienti

  2. Nell'elenco degli ambienti, fai clic sul link Airflow per il tuo ambiente. Si apre la UI di Airflow.

  3. Nella UI di Airflow, seleziona Amministrazione > Variabili.

  4. Fai clic su Add a new record (Aggiungi un nuovo record).

  5. Specifica il nome della variabile nel campo Chiave e impostane il valore nel campo Val.

  6. Fai clic su Salva.

Crea un server di cronologia permanente

Utilizza un server di cronologia permanente (PHS) per visualizzare i file di cronologia Spark dei carichi di lavoro batch:

  1. Crea un server di cronologia permanente.
  2. Assicurati di aver specificato il nome del cluster PHS nella phs_cluster variabile Airflow.

DataprocCreateBatchOperator

Il seguente DAG avvia un carico di lavoro batch Dataproc Serverless.

Per maggiori informazioni sugli argomenti DataprocCreateBatchOperator, consulta il codice sorgente dell'operatore.

Per ulteriori informazioni sugli attributi che puoi passare nel parametro batch di DataprocCreateBatchOperator, consulta la descrizione della classe Batch.


"""
Examples below show how to use operators for managing Dataproc Serverless batch workloads.
 You use these operators in DAGs that create, delete, list, and get a Dataproc Serverless Spark batch workload.
https://airflow.apache.org/docs/apache-airflow/stable/concepts/variables.html
* project_id is the Google Cloud Project ID to use for the Cloud Dataproc Serverless.
* bucket_name is the URI of a bucket where the main python file of the workload (spark-job.py) is located.
* phs_cluster is the Persistent History Server cluster name.
* image_name is the name and tag of the custom container image (image:tag).
* metastore_cluster is the Dataproc Metastore service name.
* region_name is the region where the Dataproc Metastore service is located.
"""

import datetime

from airflow import models
from airflow.providers.google.cloud.operators.dataproc import (
    DataprocCreateBatchOperator,
    DataprocDeleteBatchOperator,
    DataprocGetBatchOperator,
    DataprocListBatchesOperator,
)
from airflow.utils.dates import days_ago

PROJECT_ID = "{{ var.value.project_id }}"
REGION = "{{ var.value.region_name}}"
BUCKET = "{{ var.value.bucket_name }}"
PHS_CLUSTER = "{{ var.value.phs_cluster }}"
METASTORE_CLUSTER = "{{var.value.metastore_cluster}}"
DOCKER_IMAGE = "{{var.value.image_name}}"

PYTHON_FILE_LOCATION = "gs://{{var.value.bucket_name }}/spark-job.py"
# for e.g.  "gs//my-bucket/spark-job.py"
# Start a single node Dataproc Cluster for viewing Persistent History of Spark jobs
PHS_CLUSTER_PATH = "projects/{{ var.value.project_id }}/regions/{{ var.value.region_name}}/clusters/{{ var.value.phs_cluster }}"
# for e.g. projects/my-project/regions/my-region/clusters/my-cluster"
SPARK_BIGQUERY_JAR_FILE = "gs://spark-lib/bigquery/spark-bigquery-latest_2.12.jar"
# use this for those pyspark jobs that need a spark-bigquery connector
# https://cloud.google.com/dataproc/docs/tutorials/bigquery-connector-spark-example
# Start a Dataproc MetaStore Cluster
METASTORE_SERVICE_LOCATION = "projects/{{var.value.project_id}}/locations/{{var.value.region_name}}/services/{{var.value.metastore_cluster }}"
# for e.g. projects/my-project/locations/my-region/services/my-cluster
CUSTOM_CONTAINER = "us.gcr.io/{{var.value.project_id}}/{{ var.value.image_name}}"
# for e.g. "us.gcr.io/my-project/quickstart-image",

default_args = {
    # Tell airflow to start one day ago, so that it runs as soon as you upload it
    "start_date": days_ago(1),
    "project_id": PROJECT_ID,
    "region": REGION,
}
with models.DAG(
    "dataproc_batch_operators",  # The id you will see in the DAG airflow page
    default_args=default_args,  # The interval with which to schedule the DAG
    schedule_interval=datetime.timedelta(days=1),  # Override to match your needs
) as dag:
    create_batch = DataprocCreateBatchOperator(
        task_id="batch_create",
        batch={
            "pyspark_batch": {
                "main_python_file_uri": PYTHON_FILE_LOCATION,
                "jar_file_uris": [SPARK_BIGQUERY_JAR_FILE],
            },
            "environment_config": {
                "peripherals_config": {
                    "spark_history_server_config": {
                        "dataproc_cluster": PHS_CLUSTER_PATH,
                    },
                },
            },
        },
        batch_id="batch-create-phs",
    )
    list_batches = DataprocListBatchesOperator(
        task_id="list-all-batches",
    )

    get_batch = DataprocGetBatchOperator(
        task_id="get_batch",
        batch_id="batch-create-phs",
    )
    delete_batch = DataprocDeleteBatchOperator(
        task_id="delete_batch",
        batch_id="batch-create-phs",
    )
    create_batch >> list_batches >> get_batch >> delete_batch

Utilizzare l'immagine container personalizzata con DataprocCreateBatchOperator

L'esempio seguente mostra come utilizzare un'immagine container personalizzata per eseguire i carichi di lavoro. Puoi utilizzare un container personalizzato, ad esempio, per aggiungere dipendenze Python non fornite dall'immagine container predefinita.

Per utilizzare un'immagine container personalizzata:

  1. Crea un'immagine container personalizzata e caricala in Container Registry.

  2. Specifica l'immagine nella variabile Airflow image_name.

  3. Utilizza DataprocCreateBatchOperator con l'immagine personalizzata:

create_batch_with_custom_container = DataprocCreateBatchOperator(
    task_id="dataproc_custom_container",
    batch={
        "pyspark_batch": {
            "main_python_file_uri": PYTHON_FILE_LOCATION,
            "jar_file_uris": [SPARK_BIGQUERY_JAR_FILE],
        },
        "environment_config": {
            "peripherals_config": {
                "spark_history_server_config": {
                    "dataproc_cluster": PHS_CLUSTER_PATH,
                },
            },
        },
        "runtime_config": {
            "container_image": CUSTOM_CONTAINER,
        },
    },
    batch_id="batch-custom-container",
)
get_batch_custom = DataprocGetBatchOperator(
    task_id="get_batch_custom",
    batch_id="batch-custom-container",
)
delete_batch_custom = DataprocDeleteBatchOperator(
    task_id="delete_batch_custom",
    batch_id="batch-custom-container",
)
create_batch_with_custom_container >> get_batch_custom >> delete_batch_custom

Utilizzo del servizio Dataproc Metastore con DataprocCreateBatchOperator

Per utilizzare un servizio Dataproc Metastore da un DAG:

  1. Verifica che il servizio metastore sia già avviato.

    Per scoprire di più sull'avvio di un servizio metastore, consulta Abilitare e disabilitare Dataproc Metastore.

    Per informazioni dettagliate sull'operatore batch per la creazione della configurazione, consulta PeripheralsConfig.

  2. Una volta che il servizio metastore è in esecuzione, specificane il nome nella variabile metastore_cluster e la sua regione nella variabile Airflow region_name.

  3. Utilizza il servizio metastore in DataprocCreateBatchOperator:

create_batch_with_metastore = DataprocCreateBatchOperator(
    task_id="dataproc_metastore",
    batch={
        "pyspark_batch": {
            "main_python_file_uri": PYTHON_FILE_LOCATION,
            "jar_file_uris": [SPARK_BIGQUERY_JAR_FILE],
        },
        "environment_config": {
            "peripherals_config": {
                "metastore_service": METASTORE_SERVICE_LOCATION,
                "spark_history_server_config": {
                    "dataproc_cluster": PHS_CLUSTER_PATH,
                },
            },
        },
    },
    batch_id="dataproc-metastore",
)
get_batch_metastore = DataprocGetBatchOperator(
    task_id="get_batch_metatstore",
    batch_id="dataproc-metastore",
)
delete_batch_metastore = DataprocDeleteBatchOperator(
    task_id="delete_batch_metastore",
    batch_id="dataproc-metastore",
)

create_batch_with_metastore >> get_batch_metastore >> delete_batch_metastore

DataprocDeleteBatchOperator

Puoi utilizzare DataprocDeleteBatchOperator per eliminare un batch in base all'ID batch del carico di lavoro.

delete_batch = DataprocDeleteBatchOperator(
    task_id="delete_batch",
    batch_id="batch-create-phs",
)

DataprocListBatchesOperator

DataprocDeleteBatchOperator elenca i batch esistenti all'interno di una regione e di un project_id specifici.

list_batches = DataprocListBatchesOperator(
    task_id="list-all-batches",
)

DataprocGetBatchOperator

DataprocGetBatchOperator recupera un determinato carico di lavoro batch.

get_batch = DataprocGetBatchOperator(
    task_id="get_batch",
    batch_id="batch-create-phs",
)

Passaggi successivi