Esegui più job BigQuery in parallelo


BigQuery ospita una serie di set di dati pubblici disponibili per l'esecuzione di query da parte del pubblico. In questo tutorial, crei un flusso di lavoro che esegue più job di query BigQuery in parallelo, dimostrando un miglioramento delle prestazioni rispetto all'esecuzione dei job in serie, uno dopo l'altro.

Obiettivi

In questo tutorial, imparerai a:

  1. Esegui una query su un set di dati pubblico di Wikipedia per determinare i titoli più visualizzati in un mese specifico.
  2. Esegui il deployment ed esegui un flusso di lavoro che esegue più job di query BigQuery in serie, uno dopo l'altro.
  3. Esegui il deployment ed esegui un flusso di lavoro che esegue i job BigQuery utilizzando l'iterazione parallela, e in cui i normali loop for vengono eseguiti in parallelo.

Puoi eseguire i seguenti comandi nella console Google Cloud o utilizzando Google Cloud CLI nel terminale o in Cloud Shell.

Costi

In questo documento utilizzi i seguenti componenti fatturabili di Google Cloud:

Per generare una stima dei costi in base all'utilizzo previsto, utilizza il calcolatore prezzi.

I nuovi utenti di Google Cloud potrebbero avere diritto a una prova gratuita.

Prima di iniziare

I vincoli di sicurezza definiti dalla tua organizzazione potrebbero impedirti di completare i passaggi seguenti. Per informazioni sulla risoluzione dei problemi, vedi Sviluppare applicazioni in un ambiente Google Cloud vincolato.

Console

  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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Workflows API.

    Enable the API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. Grant the following roles to the service account: BigQuery > BigQuery Job User, Logging > Logs Writer.

      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.

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

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

    Go to project selector

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

  8. Enable the Workflows API.

    Enable the API

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. Grant the following roles to the service account: BigQuery > BigQuery Job User, Logging > Logs Writer.

      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.

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

gcloud

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

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

    Go to project selector

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

  4. Enable the Workflows API.

    Enable the API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. Grant the following roles to the service account: roles/bigquery.jobUser, roles/logging.logWriter.

      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.

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

  6. Install the Google Cloud CLI.

  7. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  8. Per inizializzare gcloud CLI, esegui questo comando:

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  11. Enable the Workflows API.

    Enable the API

  12. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. Grant the following roles to the service account: roles/bigquery.jobUser, roles/logging.logWriter.

      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.

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

  13. Install the Google Cloud CLI.

  14. Se utilizzi un provider di identità (IdP) esterno, devi prima accedere alla gcloud CLI con la tua identità federata.

  15. Per inizializzare gcloud CLI, esegui questo comando:

    gcloud init

Esegui un job di query BigQuery

In BigQuery, puoi eseguire un job di query interattiva (on demand). Per ulteriori informazioni, consulta la sezione Esecuzione di job di query interattivi e batch.

Console

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

    Vai a BigQuery

  2. Inserisci la seguente query SQL di BigQuery nell'area di testo Editor query:

    SELECT TITLE, SUM(views)
    FROM `bigquery-samples.wikipedia_pageviews.201207h`
    GROUP BY TITLE
    ORDER BY SUM(views) DESC
    LIMIT 100
    
  3. Fai clic su Esegui.

bq

Nel terminale, inserisci il seguente comando bq query per eseguire una query interattiva utilizzando la sintassi SQL standard:

bq query \
--use_legacy_sql=false \
'SELECT
  TITLE, SUM(views)
FROM
  `bigquery-samples.wikipedia_pageviews.201207h`
GROUP BY
  TITLE
ORDER BY
  SUM(views) DESC
LIMIT 100'

Esegue una query che restituisce i 100 titoli di Wikipedia più visualizzati in un mese specifico e scrive l'output in una tabella temporanea.

Prendi nota del tempo necessario per l'esecuzione della query.

Esegui il deployment di un flusso di lavoro che esegue più query in serie

Una definizione di workflow è costituita da una serie di passaggi descritti utilizzando la sintassi dei workflow. Dopo aver creato un flusso di lavoro, esegui il deployment per renderlo disponibile per l'esecuzione. Il passaggio di deployment convalida anche che il file di origine possa essere eseguito.

Il seguente flusso di lavoro definisce un elenco di cinque tabelle su cui eseguire una query utilizzando il connettore BigQuery di Workflows. Le query vengono eseguite in sequenza, una dopo l'altra, e i titoli più visualizzati di ogni tabella vengono salvati in una mappa dei risultati.

Console

  1. Nella console Google Cloud , vai alla pagina Workflows:

    Vai a Flussi di lavoro

  2. Fai clic su Crea.

  3. Inserisci un nome per il nuovo flusso di lavoro, ad esempio workflow-serial-bqjobs.

  4. Scegli una regione appropriata, ad esempio us-central1.

  5. Seleziona il service account che hai creato in precedenza.

    Dovresti aver già concesso all'account di servizio i ruoli IAM BigQuery > Utente job BigQuery e Logging > Writer log.

  6. Fai clic su Avanti.

  7. Nell'editor del workflow, inserisci la seguente definizione per il workflow:

    main:
        steps:
        - init:
            assign:
                - results : {} # result from each iteration keyed by table name
                - tables:
                    - 201201h
                    - 201202h
                    - 201203h
                    - 201204h
                    - 201205h
        - runQueries:
            for:
                value: table
                in: ${tables}
                steps:
                - logTable:
                    call: sys.log
                    args:
                        text: ${"Running query for table " + table}
                - runQuery:
                    call: googleapis.bigquery.v2.jobs.query
                    args:
                        projectId: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                        body:
                            useLegacySql: false
                            useQueryCache: false
                            timeoutMs: 30000
                            # Find top 100 titles with most views on Wikipedia
                            query: ${
                                "SELECT TITLE, SUM(views)
                                FROM `bigquery-samples.wikipedia_pageviews." + table + "`
                                WHERE LENGTH(TITLE) > 10
                                GROUP BY TITLE
                                ORDER BY SUM(VIEWS) DESC
                                LIMIT 100"
                                }
                    result: queryResult
                - returnResult:
                    assign:
                        # Return the top title from each table
                        - results[table]: {}
                        - results[table].title: ${queryResult.rows[0].f[0].v}
                        - results[table].views: ${queryResult.rows[0].f[1].v}
        - returnResults:
            return: ${results}
  8. Fai clic su Esegui il deployment.

gcloud

  1. Apri un terminale e crea un file di codice sorgente per il tuo workflow:

    touch workflow-serial-bqjobs.yaml
  2. Copia il seguente flusso di lavoro nel file del codice sorgente:

    main:
        steps:
        - init:
            assign:
                - results : {} # result from each iteration keyed by table name
                - tables:
                    - 201201h
                    - 201202h
                    - 201203h
                    - 201204h
                    - 201205h
        - runQueries:
            for:
                value: table
                in: ${tables}
                steps:
                - logTable:
                    call: sys.log
                    args:
                        text: ${"Running query for table " + table}
                - runQuery:
                    call: googleapis.bigquery.v2.jobs.query
                    args:
                        projectId: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                        body:
                            useLegacySql: false
                            useQueryCache: false
                            timeoutMs: 30000
                            # Find top 100 titles with most views on Wikipedia
                            query: ${
                                "SELECT TITLE, SUM(views)
                                FROM `bigquery-samples.wikipedia_pageviews." + table + "`
                                WHERE LENGTH(TITLE) > 10
                                GROUP BY TITLE
                                ORDER BY SUM(VIEWS) DESC
                                LIMIT 100"
                                }
                    result: queryResult
                - returnResult:
                    assign:
                        # Return the top title from each table
                        - results[table]: {}
                        - results[table].title: ${queryResult.rows[0].f[0].v}
                        - results[table].views: ${queryResult.rows[0].f[1].v}
        - returnResults:
            return: ${results}
  3. Esegui il deployment del flusso di lavoro inserendo questo comando:

    gcloud workflows deploy workflow-serial-bqjobs \
       --source=workflow-serial-bqjobs.yaml \
       --service-account=MY_SERVICE_ACCOUNT@MY_PROJECT.iam.gserviceaccount.com

    Sostituisci MY_SERVICE_ACCOUNT@MY_PROJECT.iam.gserviceaccount.com con l'email del account di servizio che hai creato in precedenza.

    Dovresti aver già concesso i ruoli IAM roles/bigquery.jobUser e roles/logging.logWriter al service account.

Esegui il flusso di lavoro ed esegui più query in serie

L'esecuzione di un workflow esegue la definizione attuale del workflow associata al workflow.

Console

  1. Nella console Google Cloud , vai alla pagina Workflows:

    Vai a Flussi di lavoro

  2. Nella pagina Flussi di lavoro, seleziona il workflow workflow-serial-bqjobs per andare alla pagina dei dettagli.

  3. Nella pagina Dettagli workflow, fai clic su Esegui.

  4. Fai di nuovo clic su Esegui.

  5. Visualizza i risultati del flusso di lavoro nel riquadro Output.

gcloud

  1. Apri un terminale.

  2. Esegui il workflow:

     gcloud workflows run workflow-serial-bqjob

L'esecuzione del workflow dovrebbe richiedere circa un minuto o cinque volte il tempo di esecuzione precedente. Il risultato includerà ogni tabella e sarà simile a quanto segue:

{
  "201201h": {
    "title": "Special:Search",
    "views": "14591339"
  },
  "201202h": {
    "title": "Special:Search",
    "views": "132765420"
  },
  "201203h": {
    "title": "Special:Search",
    "views": "123316818"
  },
  "201204h": {
    "title": "Special:Search",
    "views": "116830614"
  },
  "201205h": {
    "title": "Special:Search",
    "views": "131357063"
  }
}

Esegui il deployment ed esegui un flusso di lavoro che esegue più query in parallelo

Anziché eseguire cinque query in sequenza, puoi eseguirle in parallelo apportando alcune modifiche:

 - runQueries:
    parallel:
        shared: [results]
        for:
            value: table
            in: ${tables}
  • Un passaggio parallel consente a ogni iterazione del ciclo for di essere eseguita in parallelo.
  • La variabile results è dichiarata come shared, il che consente a un ramo di scriverla e di aggiungere il risultato di ogni ramo.

Console

  1. Nella console Google Cloud , vai alla pagina Workflows:

    Vai a Flussi di lavoro

  2. Fai clic su Crea.

  3. Inserisci un nome per il nuovo flusso di lavoro, ad esempio workflow-parallel-bqjobs.

  4. Scegli una regione appropriata, ad esempio us-central1.

  5. Seleziona il service account che hai creato in precedenza.

  6. Fai clic su Avanti.

  7. Nell'editor del workflow, inserisci la seguente definizione per il workflow:

    main:
        steps:
        - init:
            assign:
                - results : {} # result from each iteration keyed by table name
                - tables:
                    - 201201h
                    - 201202h
                    - 201203h
                    - 201204h
                    - 201205h
        - runQueries:
            parallel:
                shared: [results]
                for:
                    value: table
                    in: ${tables}
                    steps:
                    - logTable:
                        call: sys.log
                        args:
                            text: ${"Running query for table " + table}
                    - runQuery:
                        call: googleapis.bigquery.v2.jobs.query
                        args:
                            projectId: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                            body:
                                useLegacySql: false
                                useQueryCache: false
                                timeoutMs: 30000
                                # Find top 100 titles with most views on Wikipedia
                                query: ${
                                    "SELECT TITLE, SUM(views)
                                    FROM `bigquery-samples.wikipedia_pageviews." + table + "`
                                    WHERE LENGTH(TITLE) > 10
                                    GROUP BY TITLE
                                    ORDER BY SUM(VIEWS) DESC
                                    LIMIT 100"
                                    }
                        result: queryResult
                    - returnResult:
                        assign:
                            # Return the top title from each table
                            - results[table]: {}
                            - results[table].title: ${queryResult.rows[0].f[0].v}
                            - results[table].views: ${queryResult.rows[0].f[1].v}
        - returnResults:
            return: ${results}
  8. Fai clic su Esegui il deployment.

  9. Nella pagina Dettagli workflow, fai clic su Esegui.

  10. Fai di nuovo clic su Esegui.

  11. Visualizza i risultati del flusso di lavoro nel riquadro Output.

gcloud

  1. Apri un terminale e crea un file di codice sorgente per il tuo workflow:

    touch workflow-parallel-bqjobs.yaml
  2. Copia il seguente flusso di lavoro nel file del codice sorgente:

    main:
        steps:
        - init:
            assign:
                - results : {} # result from each iteration keyed by table name
                - tables:
                    - 201201h
                    - 201202h
                    - 201203h
                    - 201204h
                    - 201205h
        - runQueries:
            parallel:
                shared: [results]
                for:
                    value: table
                    in: ${tables}
                    steps:
                    - logTable:
                        call: sys.log
                        args:
                            text: ${"Running query for table " + table}
                    - runQuery:
                        call: googleapis.bigquery.v2.jobs.query
                        args:
                            projectId: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
                            body:
                                useLegacySql: false
                                useQueryCache: false
                                timeoutMs: 30000
                                # Find top 100 titles with most views on Wikipedia
                                query: ${
                                    "SELECT TITLE, SUM(views)
                                    FROM `bigquery-samples.wikipedia_pageviews." + table + "`
                                    WHERE LENGTH(TITLE) > 10
                                    GROUP BY TITLE
                                    ORDER BY SUM(VIEWS) DESC
                                    LIMIT 100"
                                    }
                        result: queryResult
                    - returnResult:
                        assign:
                            # Return the top title from each table
                            - results[table]: {}
                            - results[table].title: ${queryResult.rows[0].f[0].v}
                            - results[table].views: ${queryResult.rows[0].f[1].v}
        - returnResults:
            return: ${results}
  3. Esegui il deployment del flusso di lavoro inserendo questo comando:

    gcloud workflows deploy workflow-parallell-bqjobs \
       --source=workflow-parallel-bqjobs.yaml \
       --service-account=MY_SERVICE_ACCOUNT@MY_PROJECT.iam.gserviceaccount.com

    Sostituisci MY_SERVICE_ACCOUNT@MY_PROJECT.iam.gserviceaccount.com con l'email del account di servizio che hai creato in precedenza.

  4. Esegui il workflow:

     gcloud workflows run workflow-parallel-bqjobs

Il risultato sarà simile all'output precedente, ma l'esecuzione del workflow dovrebbe richiedere circa 20 secondi o meno.

Esegui la pulizia

Se hai creato un nuovo progetto per questo tutorial, elimina il progetto. Se hai utilizzato un progetto esistente e vuoi conservarlo senza le modifiche aggiunte in questo tutorial, elimina le risorse create per il tutorial.

Elimina il progetto

Il modo più semplice per eliminare la fatturazione è eliminare il progetto creato per il tutorial.

Per eliminare il progetto:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Eliminare le risorse del tutorial

Elimina i flussi di lavoro creati in questo tutorial:

gcloud workflows delete WORKFLOW_NAME

Passaggi successivi