Consulta i connettori supportati per Application Integration.

Inserire i dati in BigQuery utilizzando un'attività For Each parallela

In questo tutorial creerai un'integrazione dell'applicazione e una sottointegrazione per elaborare una serie di record. Per ogni record, l'integrazione principale invoca in modo asincrono l'integrazione secondaria, che prende i dati di ogni record e li inserisce come riga in una tabella di un set di dati BigQuery.

In questo tutorial, completerai le seguenti attività:

Prima di iniziare

  • Assicurati di avere accesso all'integrazione delle applicazioni.
  • Nel tuo progetto Google Cloud:

    • Concedi i seguenti ruoli all'account di servizio che vuoi utilizzare per creare la connessione:
      • roles/bigquery.dataEditor
      • roles/bigquery.readSessionUser
      • roles/secretmanager.viewer
      • roles/secretmanager.secretAccessor
    • Attiva i seguenti servizi:
      • secretmanager.googleapis.com (API Secret Manager)
      • connectors.googleapis.com (API Connectors)

      Se questi servizi non sono stati attivati in precedenza per il tuo progetto, ti verrà chiesto di attivarli durante la creazione della connessione nella pagina Crea connessione.

Configurare una connessione BigQuery

Inizia creando il set di dati e la tabella BigQuery da utilizzare in questo tutorial. Dopo aver creato il set di dati e la tabella, crea una connessione BigQuery. Utilizzerai questa connessione in un'integrazione più avanti in questo tutorial.

Configurare un set di dati e una tabella BigQuery

Per configurare il set di dati e la tabella BigQuery:

  1. Nella pagina Cloud Console, seleziona il tuo Google Cloud progetto.
  2. Per avviare una sessione Cloud Shell dalla console Google Cloud, fai clic sull'icona Icona Attiva Cloud Shell Attiva Cloud Shell nella console Cloud. Viene avviata una sessione nel riquadro inferiore della console Google Cloud.
  3. Per attivare le API BigQuery, inserisci i seguenti comandi nel terminale Cloud Shell:
    export PROJECT_ID=project_id
    export REGION=region
    gcloud services enable --project "${PROJECT_ID}" \
        bigquery.googleapis.com \
        bigquerystorage.googleapis.com
    In questo comando, sostituisci:
    • project_id con l'ID progetto del tuo Google Cloud progetto.
    • region con la regione che vuoi utilizzare per creare il set di dati BigQuery.
  4. Per creare un set di dati BigQuery con il nome bq_tutorial, inserisci il seguente comando nel terminale Cloud Shell:
          bq  --project_id ${PROJECT_ID} --location ${REGION} mk bq_tutorial
        
  5. Per creare una tabella BigQuery con il nome tutorial, inserisci il seguente comando nel terminale Cloud Shell:
          bq --project_id ${PROJECT_ID} \
            query  \
            --nouse_legacy_sql \
          'create table bq_tutorial.tutorial (
          unique_key STRING NOT NULL,
          created_date STRING,
          closed_date STRING,
          agency STRING,
          agency_name STRING,
          complaint_type STRING,
          descriptor STRING,
          location_type STRING,
          incident_zip STRING,
          incident_address STRING,
          street_name STRING,
          cross_street_1 STRING,
          cross_street_2 STRING,
          intersection_street_1 STRING,
          intersection_street_2 STRING,
          address_type STRING,
          city STRING,
          landmark STRING,
          facility_type STRING,
          status STRING,
          due_date STRING,
          resolution_action_updated_date STRING,
          community_board STRING,
          borough STRING,
          x_coordinate_state_plane STRING,
          y_coordinate_state_plane STRING,
          park_facility_name STRING,
          park_borough STRING,
          school_name STRING,
          school_number STRING,
          school_region STRING,
          school_code STRING,
          school_phone_number STRING,
          school_address STRING,
          school_city STRING,
          school_state STRING,
          school_zip STRING,
          school_not_found STRING,
          school_or_citywide_complaint STRING,
          vehicle_type STRING,
          taxi_company_borough STRING,
          taxi_pick_up_location STRING,
          bridge_highway_name STRING,
          bridge_highway_direction STRING,
          bridge_highway_segment STRING,
          road_ramp STRING,
          garage_lot_name STRING,
          ferry_direction STRING,
          ferry_terminal_name STRING,
          latitude STRING,
          longitude STRING,
          location STRING
          ) '
      
  6. Verify that your BigQuery table is created.
    1. In the Cloud console page, click the Navigation menu.
    2. In the Analytics section, click BigQuery.
    3. Expand your project and confirm that the bq_tutorial dataset is listed.
    4. Expand the bq_tutorial dataset and confirm that the tutorial table is listed.
    5. Click the documents table to view the schema.

Create a BigQuery connection

Next, you'll create a BigQuery connection. A BigQuery connection lets you insert, read, update and delete rows in a BigQuery table and use the resulting output in an integration. After creating the BigQuery connection, you'll use this connection in an integration later in this tutorial to add rows to the BigQuery table.

To create a BigQuery connection, complete the following steps:

  1. In the Cloud console page, select your Google Cloud project.
  2. Open the connections page.
  3. Click + CREATE NEW to open the Create Connection page.
  4. Configure the connection:
    1. In the Create Connection section, complete the following:
      • Connector: Select BigQuery from the drop down list of available Connectors.
      • Connector version: Select the latest Connector version from the drop down list of available versions.
      • In the Connection Name field, enter a name for the Connection instance. For this tutorial, enter connector-bq-tutorial.
      • Optionally, add a Description of the connection instance.
      • Service Account: Select a service account that has the required roles.
      • Project ID: Enter the ID of the Google Cloud project where the BigQuery data resides.
      • Dataset ID: Enter the ID of the BigQuery dataset that you want to use. For this tutorial, enter bq_tutorial.
      • Optionally, click + ADD LABEL to add a label in the form of a key/value pair.
      • Click Next.
    2. Location: Select a region from where the connection will run. Supported regions for connectors include:

        For the list of all the supported regions, see Locations.

      • Click Next.
    3. Authentication: The BigQuery connection does not require authentication configuration. Click Next.
    4. Review: Review your connection's configuration details. In this section, the connection and authentication details of the new connection are displayed for your review.
  5. Click Create.

Set up a sub-integration

In this tutorial, the sub-integration takes each record sent to it by the main integration and inserts it as a row in the tutorial table in the bq_tutorial dataset.

Create a sub-integration

To create the sub-integration, complete the following steps:

  1. In the Google Cloud console, go to the Application Integration page.

    Go to Application Integration

  2. In the navigation menu, click Integrations. The Integrations page appears.
  3. Click Create integration.
  4. In the Create Integration dialog, do the following:
    • Enter a name, for example, enter Process-each-record
    • Optionally, enter a description. For example, enter API Trigger to process each record (sub-integration)
    • Select the region where you want to create your integration.
  5. Click Create to open the integration editor.

Add an API Trigger

To add an API Trigger to the integration, do the following:

  1. In the integration editor, select Add a task/trigger > Triggers to display a list of available triggers.
  2. Drag the API Trigger element to the integration editor.

Add a Data Mapping task

To add a Data Mapping task in the integration, complete the following steps:

  1. Select +Add a task/trigger > Tasks in the integration editor to display the list of available tasks.
  2. Drag the Data Mapping element to the integration editor.

Configure the BigQuery connection

Now you are ready to use the BigQuery connection that you created earlier in the sub-integration. To configure the BigQuery connection in this integration, complete the following steps:

  1. Select +Add a task/trigger > Tasks in the integration editor to display the list of available tasks.
  2. Drag the Connectors element to the integration editor.
  3. Click the Connectors task element on the designer to view the task configuration pane.
  4. Click the edit icon on the right panel and update the Label to Insert row to BigQuery.
  5. Click Configure task.

    The Configure connector task dialog appears.

  6. In the Configure connector task dialog, do the following:
    1. Select the connection region where you created your BigQuery connection.
    2. Select the BigQuery connection that you want to use. For this tutorial, select connector-bq-tutorial.
    3. Once a connection is chosen, the Type column appears. Select Entities and then tutorial from the list of available entities.
    4. Once a type is chosen, the Operation column appears. Select Create.
    5. Click Done to complete the connection configuration and close the dialog.

Connect the integration elements

Next, add edge connections to connect the API Trigger to the Data Mapping task and the Data Mapping task to the Connectors task. An edge connection is a connection between any two elements in an integration. For more information on edges and edge conditions, see Edges.

To add the edge connections, complete the following steps:

  1. Click the Fork control point at the bottom of the API Trigger element. Drag and drop the edge connection at the Join control point at the top of the Data Mapping element.
  2. Click the Fork control point at the bottom of the Data Mapping element. Drag and drop the edge connection at the Join control point at the top of the Connectors element.

Configure the Data Mapping task

To configure the Data Mapping task, complete the following steps:

  1. In the integration editor, click the Data Mapping task to view the task configuration pane.
  2. Click Open Data Mapping Editor.
  3. In the Data Mapping Editor, click Add to add a new variable.
  4. In the Create Variable dialog, enter the following information:
    • Name: Enter record.
    • Data Type: Select JSON.
    • Schema: Select Infer from a sample JSON payload. Enter the following sample JSON payload:
                  {
                    "unique_key":"304271",
                    "created_date":"02/06/2007 12:00:00 AM",
                    "closed_date":"03/01/2007 12:00:00 AM",
                    "agency":"TLC",
                    "agency_name":"Taxi and Limousine Commission",
                    "complaint_type":"Taxi Complaint",
                    "descriptor":"Driver Complaint",
                    "location_type":"Street",
                    "incident_zip":"10001",
                    "incident_address":"",
                    "street_name":"",
                    "cross_street_1":"",
                    "cross_street_2":"",
                    "intersection_street_1":"WEST 29 STREET",
                    "intersection_street_2":"7 AVENUE",
                    "address_type":"INTERSECTION",
                    "city":"NEW YORK",
                    "landmark":"",
                    "facility_type":"N/A",
                    "status":"Closed",
                    "due_date":"02/28/2007 12:00:00 AM",
                    "resolution_action_updated_date":"03/01/2007 12:00:00 AM",
                    "community_board":"05 MANHATTAN",
                    "borough":"MANHATTAN",
                    "x_coordinate_state_plane":"986215",
                    "y_coordinate_state_plane":"211740",
                    "park_facility_name":"",
                    "park_borough":"MANHATTAN",
                    "school_name":"",
                    "school_number":"",
                    "school_region":"",
                    "school_code":"",
                    "school_phone_number":"",
                    "school_address":"",
                    "school_city":"",
                    "school_state":"",
                    "school_zip":"",
                    "school_not_found":"",
                    "school_or_citywide_complaint":"",
                    "vehicle_type":"",
                    "taxi_company_borough":"",
                    "taxi_pick_up_location":"Other",
                    "bridge_highway_name":"",
                    "bridge_highway_direction":"",
                    "road_ramp":"",
                    "bridge_highway_segment":"",
                    "garage_lot_name":"",
                    "ferry_direction":"",
                    "ferry_terminal_name":"",
                    "latitude":"40.74785373937869",
                    "longitude":"-73.99290823133913",
                    "location":"(40.74785373937869, -73.99290823133913)"
                  }
                
    • Fai clic su Crea.
    • Una volta creata la variabile, nell'editor di mappatura dei dati, segui questi passaggi:
      • Trascina la nuova variabile record nella colonna Input.
      • Trascina la variabile connectorInputPayload nella colonna Output.
    • Chiudi l'editor di mappatura dei dati per tornare all'editor di integrazione.

Pubblica la sottointegrazione

Per pubblicare la sottointegrazione, fai clic su Pubblica nell'editor dell'integrazione.

Configura l'integrazione principale

In questa sezione, configuri l'integrazione principale, che utilizza un'attività Per ogni paralle per elaborare ogni record. Questa integrazione principale invoca quindi l'integrazione secondaria una volta per ogni record.

Crea l'integrazione principale

Per creare l'integrazione principale, completa i seguenti passaggi:

  1. Nella console Google Cloud, vai alla pagina Integrazione delle applicazioni.

    Vai ad Application Integration

  2. Nel menu di navigazione, fai clic su Integrations (Integrazioni). Viene visualizzata la pagina Integrazioni.
  3. Fai clic su Crea integrazione.
  4. Nella finestra di dialogo Crea integrazione, procedi nel seguente modo:
    • Inserisci un nome, ad esempio process-records.
    • Se vuoi, inserisci una descrizione. Ad esempio, inserisci Trigger API per l'elaborazione dei record (integrazione principale)
    • Seleziona la regione in cui vuoi creare l'integrazione.
  5. Fai clic su Crea per aprire l'editor di integrazione.

Aggiungere un trigger API

Per aggiungere un trigger API all'integrazione:

  1. Nell'editor di integrazione, seleziona Aggiungi un'attività/trigger > Trigger per visualizzare un elenco degli attivatori disponibili.
  2. Trascina l'elemento Trigger API nell'editor di integrazione.

Aggiungere un'attività per singola parallela

Per aggiungere un'attività per singola parallela nell'integrazione, segui questi passaggi:

  1. Seleziona + Aggiungi un'attività/un trigger > Attività nell'editor di integrazione per visualizzare l'elenco delle attività disponibili.
  2. Trascina l'elemento Per ogni parametro parallelo nell'editor di integrazione.

Collega gli elementi di integrazione

Aggiungi un collegamento di bordo per collegare l'attivatore API all'attività For Each Parallel.

Per aggiungere la connessione del bordo, fai clic sul punto di controllo Fork nella parte inferiore dell'elemento Trigger API. Trascina il collegamento dell'elemento di bordo nel punto di controllo Unisci nella parte superiore dell'elemento dell'attività Per ogni paralle.

Configura l'attività per ogni paralle

Per configurare l'attività For Each Parallel, completa i seguenti passaggi:

  1. Nell'editor di integrazione, fai clic sull'attività Per ogni paralle per visualizzare il riquadro di configurazione dell'attività.
  2. In Selezione array > Elenco da iterare, fai clic su Aggiungi nuova variabile per aggiungere una nuova variabile.
  3. Nella finestra di dialogo Crea variabile, inserisci le seguenti informazioni:
    • Nome: inserisci records
    • Tipo di dati:seleziona JSON.
    • Schema:seleziona Deduzione da un payload JSON di esempio. Inserisci il seguente payload JSON di esempio:
                    [{
                      "unique_key":"304271",
                      "created_date":"02/06/2007 12:00:00 AM",
                      "closed_date":"03/01/2007 12:00:00 AM",
                      "agency":"TLC",
                      "agency_name":"Taxi and Limousine Commission",
                      "complaint_type":"Taxi Complaint",
                      "descriptor":"Driver Complaint",
                      "location_type":"Street",
                      "incident_zip":"10001",
                      "incident_address":"",
                      "street_name":"",
                      "cross_street_1":"",
                      "cross_street_2":"",
                      "intersection_street_1":"WEST 29 STREET",
                      "intersection_street_2":"7 AVENUE",
                      "address_type":"INTERSECTION",
                      "city":"NEW YORK",
                      "landmark":"",
                      "facility_type":"N/A",
                      "status":"Closed",
                      "due_date":"02/28/2007 12:00:00 AM",
                      "resolution_action_updated_date":"03/01/2007 12:00:00 AM",
                      "community_board":"05 MANHATTAN",
                      "borough":"MANHATTAN",
                      "x_coordinate_state_plane":"986215",
                      "y_coordinate_state_plane":"211740",
                      "park_facility_name":"",
                      "park_borough":"MANHATTAN",
                      "school_name":"",
                      "school_number":"",
                      "school_region":"",
                      "school_code":"",
                      "school_phone_number":"",
                      "school_address":"",
                      "school_city":"",
                      "school_state":"",
                      "school_zip":"",
                      "school_not_found":"",
                      "school_or_citywide_complaint":"",
                      "vehicle_type":"",
                      "taxi_company_borough":"",
                      "taxi_pick_up_location":"Other",
                      "bridge_highway_name":"",
                      "bridge_highway_direction":"",
                      "road_ramp":"",
                      "bridge_highway_segment":"",
                      "garage_lot_name":"",
                      "ferry_direction":"",
                      "ferry_terminal_name":"",
                      "latitude":"40.74785373937869",
                      "longitude":"-73.99290823133913",
                      "location":"(40.74785373937869, -73.99290823133913)"
                    }]
                  
  4. Fai clic su Crea.
  5. Nella sezione Dettagli della sottointegrazione, inserisci le seguenti informazioni:
    • ID trigger API:seleziona l'elemento Trigger API nella sottointegrazione. Ad esempio, seleziona Process-each-record_API_1.
    • Strategia di esecuzione:seleziona ASYNC.
    • Seleziona Esegui una singola integrazione.
  6. Nella sezione Ad ogni esecuzione, in Dove mappare i singoli elementi dell'array, inserisci il nome della variabile nell'attività di mappatura dei dati nella sottointegrazione. In questo caso, inserisci record. Le variabili di sottointegrazione sono elencate solo per le integrazioni pubblicate. Se le variabili non sono elencate, aggiorna la pagina, poiché è necessario del tempo prima che le variabili diventino visibili dopo la pubblicazione della sottointegrazione.

Pubblicare l'integrazione principale

Per pubblicare l'integrazione principale, fai clic su Pubblica nell'editor di integrazione.

Testa l'integrazione

Per testare l'integrazione, segui questi passaggi:

  1. Scarica i dati di esempio in Cloud Shell:
    1. Per avviare una sessione Cloud Shell dalla console Google Cloud, fai clic sull'icona Icona Attiva Cloud Shell Attiva Cloud Shell nella console Cloud. Viene avviata una sessione nel riquadro inferiore della console Google Cloud.
    2. Inserisci il comando seguente nel terminale Cloud Shell:
      wget https://raw.githubusercontent.com/GoogleCloudPlatform/application-integration-samples/main/assets/bq-sample-dataset.json
              
    3. Per verificare che i dati di esempio siano stati scaricati, inserisci il seguente comando nel terminale Cloud Shell:
      ls -la bq-sample-dataset.json
      Il file scaricato è elencato nel terminale Cloud Shell.
  2. Per selezionare tre voci casuali dal set di dati di esempio e archiviarle in modo da poterle passare all'integrazione, inserisci i seguenti comandi nel terminale Cloud Shell:
    AUTH=$(gcloud auth print-access-token)
    export SAMPLE_DOCS=$(jq $(r=$((RANDOM % 1000)) ; echo ".[$r:$((r + 3))]") < bq-sample-dataset.json | jq -Rs '.')
                
    generate_post_data()
      {
        cat <<EOF
          {
            "triggerId": "api_trigger/process-records_API_1",
            "inputParameters": 
              {
                "records": 
                  {
                    "jsonValue": $SAMPLE_DOCS
                  }
              }
          }
          EOF
      }
  3. Per avviare i test, inserisci il seguente comando nel terminale Cloud Shell:
    curl -X POST \
      https://integrations.googleapis.com/v1/projects/project_id/locations/region/integrations/process-records:execute \
      -H "Authorization: Bearer $AUTH" \
      -H "Content-Type: application/json" \
      -d "$(generate_post_data)"
    In questo comando, sostituisci:
    • project_id con l'ID progetto del tuo Google Cloud progetto.
    • region con la regione in cui hai creato l'integrazione.
    Questo comando richiama l'integrazione principale e passa le voci del set di dati di esempio all'integrazione principale. L'integrazione principale passa quindi ogni voce all'integrazione secondaria, che aggiunge i dati come riga nella tabella BigQuery.
  4. Per verificare che la tabella BigQuery ora contenga questi record:
    1. Nella pagina Cloud Console, fai clic su Menu di navigazione.
    2. Nella sezione Analytics, fai clic su BigQuery.
    3. Espandi il progetto e fai clic sul set di dati bq_tutorial.
    4. Espandi il set di dati bq_tutorial e fai clic sulla tabella tutorial.
    5. Fai clic sulla scheda Esploratore tabelle per visualizzare i record inseriti.

Passaggi successivi

Prova a creare integrazioni con altri connettori. Per l'elenco di tutti i connettori supportati, consulta la documentazione di riferimento dei connettori.