Vorlage „JDBC für BigQuery“

Mit der Vorlage „JDBC für BigQuery“ des Managed Service for Apache Spark können Sie Daten aus JDBC-Datenbanken in BigQuery extrahieren.

Diese Vorlage unterstützt die folgenden Datenbanken als Eingabe:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle

Vorlage verwenden

Führen Sie die Vorlage mit der gcloud CLI oder der Managed Service for Apache Spark API aus.

gcloud

Ersetzen Sie folgende Werte in den Befehlsdaten:

  • PROJECT_ID: erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.
  • REGION: erforderlich. Compute Engine -Region.
  • TEMPLATE_VERSION: erforderlich. Geben Sie latest für die neueste Vorlagenversion oder das Datum einer bestimmten Version an, z. B. 2023-03-17_v0.1.0-beta . Unter gs://dataproc-templates-binaries oder mit dem Befehl gcloud storage ls gs://dataproc-templates-binaries können Sie die verfügbaren Vorlagenversionen auflisten.
  • SUBNET: optional. Wenn kein Subnetzwerk angegeben ist, wird das Subnetzwerk in der angegebenen REGION im default Netzwerk ausgewählt.

    Beispiel: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME

  • JDBC_CONNECTOR_CLOUD_STORAGE_PATH: erforderlich. Der vollständige Cloud Storage Pfad, einschließlich des Dateinamens, in dem die JDBC-Connector-JAR-Datei gespeichert ist. Mit den folgenden Befehlen können Sie JDBC-Connectors zum Hochladen in Cloud Storage herunterladen:
    • MySQL :
      wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.30.tar.gz
            
    • Postgres SQL :
      wget https://jdbc.postgresql.org/download/postgresql-42.2.6.jar
            
    • Microsoft SQL Server :
        
      wget https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/6.4.0.jre8/mssql-jdbc-6.4.0.jre8.jar
            
    • Oracle :
      wget https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc8/21.7.0.0/ojdbc8-21.7.0.0.jar
            
  • DATASET und TABLE: erforderlich. BigQuery-Ziel-Dataset und -Tabelle.
  • Die folgenden Variablen werden verwendet, um die erforderliche JDBC_CONNECTION_URL:
    • JDBC_HOST
    • JDBC_PORT
    • JDBC_DATABASE oder für Oracle JDBC_SERVICE
    • JDBC_USERNAME
    • JDBC_PASSWORD

    Erstellen Sie die JDBC_CONNECTION_URL mit einem der folgenden connectorspezifischen Formate:

    • MySQL :
      jdbc:mysql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
                
    • Postgres SQL :
      jdbc:postgresql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
                
    • Microsoft SQL Server :
        
      jdbc:sqlserver://JDBC_HOST:JDBC_PORT;databaseName=JDBC_DATABASE;user=JDBC_USERNAME;password=JDBC_PASSWORD
                
    • Oracle :
      jdbc:oracle:thin:@//JDBC_HOST:JDBC_PORT/JDBC_SERVICE?user=JDBC_USERNAME&password=JDBC_PASSWORD
                
  • DRIVER: erforderlich. Der JDBC-Treiber, der für die Verbindung verwendet wird:
    • MySQL :
      com.mysql.cj.jdbc.Driver
              
    • Postgres SQL :
      org.postgresql.Driver
              
    • Microsoft SQL Server :
        
      com.microsoft.sqlserver.jdbc.SQLServerDriver
              
    • Oracle :
      oracle.jdbc.driver.OracleDriver
              
  • QUERY: erforderlich. SQL-Abfrage zum Extrahieren von Daten aus JDBC.
  • MODE: erforderlich. Schreibmodus für die BigQuery-Ausgabe. Optionen: append, overwrite, ignore, oder errorifexists.
  • TEMP_BUCKET: erforderlich. Name des Cloud Storage Buckets. Dieser Bucket wird für das Laden von BigQuery verwendet.

    Beispiel:gs://templates/jdbc_to_cloud_storage_output

  • INPUT_PARTITION_COLUMN, LOWERBOUND, UPPERBOUND, PARTITIONS: optional. Wenn verwendet, müssen alle folgenden Parameter angegeben werden:
    • INPUT_PARTITION_COLUMN: Name der Partitionsspalte der JDBC-Eingabetabelle.
    • LOWERBOUND: Untergrenze der Partitionsspalte der JDBC-Eingabetabelle, die zum Bestimmen der Partitionsgröße verwendet wird.
    • UPPERBOUND:Obergrenze der Partitionsspalte der JDBC-Eingabetabelle, die zum Bestimmen der Partitionsgröße verwendet wird.
    • PARTITIONS:Die maximale Anzahl von Partitionen, die für die Parallelität von Tabellenlese- und -schreibvorgängen verwendet werden können. Wenn angegeben, wird dieser Wert für die JDBC-Ein- und -Ausgabeverbindung verwendet. Standardwert: 10.
  • FETCHSIZE: optional. Anzahl der Zeilen, die pro Roundtrip abgerufen werden sollen. Standardwert: 10.
  • TEMPVIEW und SQL_QUERY: optional. Mit diesen beiden optionalen Parametern können Sie eine Spark SQL-Transformation anwenden, während Sie Daten in BigQuery laden. TEMPVIEW ist der Name der temporären Ansicht und SQL_QUERY ist die Abfrageanweisung. TEMPVIEW und der Tabellenname in SQL_QUERY müssen übereinstimmen.
  • SERVICE_ACCOUNT: optional. Wenn nicht angegeben, wird das Compute Engine-Standarddienstkonto verwendet.
  • PROPERTY und PROPERTY_VALUE: optional. Durch Kommas getrennte Liste von Spark-Eigenschaft=value Paaren.
  • LABEL und LABEL_VALUE: optional. Durch Kommas getrennte Liste von label=value-Paaren.
  • LOG_LEVEL: optional. Protokollierungsstufe. Mögliche Werte: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE oder WARN. Standardwert: INFO.
  • KMS_KEY: optional. Der Cloud Key Management Service-Schlüssel, der für die Verschlüsselung verwendet werden soll. Wenn kein Schlüssel angegeben ist, werden inaktive Daten mit einem verschlüsselt Google-owned and Google-managed encryption key.

    Beispiel: projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME

Führen Sie den folgenden Befehl aus:

Linux, macOS oder Cloud Shell

gcloud dataproc batches submit spark \
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate \
    --version="2.3" \
    --project="PROJECT_ID" \
    --region="REGION" \
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar,JDBC_CONNECTOR_CLOUD_STORAGE_PATH" \
    --subnet="SUBNET" \
    --kms-key="KMS_KEY" \
    --service-account="SERVICE_ACCOUNT" \
    --properties="PROPERTY=PROPERTY_VALUE" \
    --labels="LABEL=LABEL_VALUE" \
    -- --template=JDBCTOBIGQUERY \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty jdbctobq.bigquery.location="DATASET.TABLE" \
    --templateProperty jdbctobq.jdbc.url="JDBC_CONNECTION_URL" \
    --templateProperty jdbctobq.jdbc.driver.class.name="DRIVER" \
    --templateProperty jdbctobq.write.mode="MODE" \
    --templateProperty jdbctobq.temp.gcs.bucket="TEMP_BUCKET" \
    --templateProperty jdbctobq.sql="QUERY" \
    --templateProperty jdbctobq.sql.numPartitions="PARTITIONS" \
    --templateProperty jdbctobq.sql.partitionColumn="INPUT_PARTITION_COLUMN" \
    --templateProperty jdbctobq.sql.lowerBound="LOWERBOUND" \
    --templateProperty jdbctobq.sql.upperBound="UPPERBOUND" \
    --templateProperty jdbctobq.jdbc.fetchsize="FETCHSIZE" \
    --templateProperty jdbctobq.temp.table="TEMPVIEW" \
    --templateProperty jdbctobq.temp.query="SQL_QUERY"

Windows (PowerShell)

gcloud dataproc batches submit spark `
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate `
    --version="2.3" `
    --project="PROJECT_ID" `
    --region="REGION" `
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar,JDBC_CONNECTOR_CLOUD_STORAGE_PATH" `
    --subnet="SUBNET" `
    --kms-key="KMS_KEY" `
    --service-account="SERVICE_ACCOUNT" `
    --properties="PROPERTY=PROPERTY_VALUE" `
    --labels="LABEL=LABEL_VALUE" `
    -- --template=JDBCTOBIGQUERY `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty jdbctobq.bigquery.location="DATASET.TABLE" `
    --templateProperty jdbctobq.jdbc.url="JDBC_CONNECTION_URL" `
    --templateProperty jdbctobq.jdbc.driver.class.name="DRIVER" `
    --templateProperty jdbctobq.write.mode="MODE" `
    --templateProperty jdbctobq.temp.gcs.bucket="TEMP_BUCKET" `
    --templateProperty jdbctobq.sql="QUERY" `
    --templateProperty jdbctobq.sql.numPartitions="PARTITIONS" `
    --templateProperty jdbctobq.sql.partitionColumn="INPUT_PARTITION_COLUMN" `
    --templateProperty jdbctobq.sql.lowerBound="LOWERBOUND" `
    --templateProperty jdbctobq.sql.upperBound="UPPERBOUND" `
    --templateProperty jdbctobq.jdbc.fetchsize="FETCHSIZE" `
    --templateProperty jdbctobq.temp.table="TEMPVIEW" `
    --templateProperty jdbctobq.temp.query="SQL_QUERY"

Windows (cmd.exe)

gcloud dataproc batches submit spark ^
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^
    --version="2.3" ^
    --project="PROJECT_ID" ^
    --region="REGION" ^
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar,JDBC_CONNECTOR_CLOUD_STORAGE_PATH" ^
    --subnet="SUBNET" ^
    --kms-key="KMS_KEY" ^
    --service-account="SERVICE_ACCOUNT" ^
    --properties="PROPERTY=PROPERTY_VALUE" ^
    --labels="LABEL=LABEL_VALUE" ^
    -- --template=JDBCTOBIGQUERY ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty jdbctobq.bigquery.location="DATASET.TABLE" ^
    --templateProperty jdbctobq.jdbc.url="JDBC_CONNECTION_URL" ^
    --templateProperty jdbctobq.jdbc.driver.class.name="DRIVER" ^
    --templateProperty jdbctobq.write.mode="MODE" ^
    --templateProperty jdbctobq.temp.gcs.bucket="TEMP_BUCKET" ^
    --templateProperty jdbctobq.sql="QUERY" ^
    --templateProperty jdbctobq.sql.numPartitions="PARTITIONS" ^
    --templateProperty jdbctobq.sql.partitionColumn="INPUT_PARTITION_COLUMN" ^
    --templateProperty jdbctobq.sql.lowerBound="LOWERBOUND" ^
    --templateProperty jdbctobq.sql.upperBound="UPPERBOUND" ^
    --templateProperty jdbctobq.jdbc.fetchsize="FETCHSIZE" ^
    --templateProperty jdbctobq.temp.table="TEMPVIEW" ^
    --templateProperty jdbctobq.temp.query="SQL_QUERY"

REST

Ersetzen Sie folgende Werte in den Anfragedaten:

  • PROJECT_ID: erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.
  • REGION: erforderlich. Compute Engine -Region.
  • TEMPLATE_VERSION: erforderlich. Geben Sie latest für die neueste Vorlagenversion oder das Datum einer bestimmten Version an, z. B. 2023-03-17_v0.1.0-beta . Unter gs://dataproc-templates-binaries oder mit dem Befehl gcloud storage ls gs://dataproc-templates-binaries können Sie die verfügbaren Vorlagenversionen auflisten.
  • SUBNET: optional. Wenn kein Subnetzwerk angegeben ist, wird das Subnetzwerk in der angegebenen REGION im default Netzwerk ausgewählt.

    Beispiel: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME

  • JDBC_CONNECTOR_CLOUD_STORAGE_PATH: erforderlich. Der vollständige Cloud Storage Pfad, einschließlich des Dateinamens, in dem die JDBC-Connector-JAR-Datei gespeichert ist. Mit den folgenden Befehlen können Sie JDBC-Connectors zum Hochladen in Cloud Storage herunterladen:
    • MySQL :
      wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.30.tar.gz
            
    • Postgres SQL :
      wget https://jdbc.postgresql.org/download/postgresql-42.2.6.jar
            
    • Microsoft SQL Server :
        
      wget https://repo1.maven.org/maven2/com/microsoft/sqlserver/mssql-jdbc/6.4.0.jre8/mssql-jdbc-6.4.0.jre8.jar
            
    • Oracle :
      wget https://repo1.maven.org/maven2/com/oracle/database/jdbc/ojdbc8/21.7.0.0/ojdbc8-21.7.0.0.jar
            
  • DATASET und TABLE: erforderlich. BigQuery-Ziel-Dataset und -Tabelle.
  • Die folgenden Variablen werden verwendet, um die erforderliche JDBC_CONNECTION_URL:
    • JDBC_HOST
    • JDBC_PORT
    • JDBC_DATABASE oder für Oracle JDBC_SERVICE
    • JDBC_USERNAME
    • JDBC_PASSWORD

    Erstellen Sie die JDBC_CONNECTION_URL mit einem der folgenden connectorspezifischen Formate:

    • MySQL :
      jdbc:mysql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
                
    • Postgres SQL :
      jdbc:postgresql://JDBC_HOST:JDBC_PORT/JDBC_DATABASE?user=JDBC_USERNAME&password=JDBC_PASSWORD
                
    • Microsoft SQL Server :
        
      jdbc:sqlserver://JDBC_HOST:JDBC_PORT;databaseName=JDBC_DATABASE;user=JDBC_USERNAME;password=JDBC_PASSWORD
                
    • Oracle :
      jdbc:oracle:thin:@//JDBC_HOST:JDBC_PORT/JDBC_SERVICE?user=JDBC_USERNAME&password=JDBC_PASSWORD
                
  • DRIVER: erforderlich. Der JDBC-Treiber, der für die Verbindung verwendet wird:
    • MySQL :
      com.mysql.cj.jdbc.Driver
              
    • Postgres SQL :
      org.postgresql.Driver
              
    • Microsoft SQL Server :
        
      com.microsoft.sqlserver.jdbc.SQLServerDriver
              
    • Oracle :
      oracle.jdbc.driver.OracleDriver
              
  • QUERY: erforderlich. SQL-Abfrage zum Extrahieren von Daten aus JDBC.
  • MODE: erforderlich. Schreibmodus für die BigQuery-Ausgabe. Optionen: append, overwrite, ignore, oder errorifexists.
  • TEMP_BUCKET: erforderlich. Name des Cloud Storage Buckets. Dieser Bucket wird für das Laden von BigQuery verwendet.

    Beispiel:gs://templates/jdbc_to_cloud_storage_output

  • INPUT_PARTITION_COLUMN, LOWERBOUND, UPPERBOUND, PARTITIONS: optional. Wenn verwendet, müssen alle folgenden Parameter angegeben werden:
    • INPUT_PARTITION_COLUMN: Name der Partitionsspalte der JDBC-Eingabetabelle.
    • LOWERBOUND: Untergrenze der Partitionsspalte der JDBC-Eingabetabelle, die zum Bestimmen der Partitionsgröße verwendet wird.
    • UPPERBOUND:Obergrenze der Partitionsspalte der JDBC-Eingabetabelle, die zum Bestimmen der Partitionsgröße verwendet wird.
    • PARTITIONS:Die maximale Anzahl von Partitionen, die für die Parallelität von Tabellenlese- und -schreibvorgängen verwendet werden können. Wenn angegeben, wird dieser Wert für die JDBC-Ein- und -Ausgabeverbindung verwendet. Standardwert: 10.
  • FETCHSIZE: optional. Anzahl der Zeilen, die pro Roundtrip abgerufen werden sollen. Standardwert: 10.
  • TEMPVIEW und SQL_QUERY: optional. Mit diesen beiden optionalen Parametern können Sie eine Spark SQL-Transformation anwenden, während Sie Daten in BigQuery laden. TEMPVIEW ist der Name der temporären Ansicht und SQL_QUERY ist die Abfrageanweisung. TEMPVIEW und der Tabellenname in SQL_QUERY müssen übereinstimmen.
  • SERVICE_ACCOUNT: optional. Wenn nicht angegeben, wird das Compute Engine-Standarddienstkonto verwendet.
  • PROPERTY und PROPERTY_VALUE: optional. Durch Kommas getrennte Liste von Spark-Eigenschaft=value Paaren.
  • LABEL und LABEL_VALUE: optional. Durch Kommas getrennte Liste von label=value-Paaren.
  • LOG_LEVEL: optional. Protokollierungsstufe. Mögliche Werte: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE oder WARN. Standardwert: INFO.
  • KMS_KEY: optional. Der Cloud Key Management Service-Schlüssel, der für die Verschlüsselung verwendet werden soll. Wenn kein Schlüssel angegeben ist, werden inaktive Daten mit einem verschlüsselt Google-owned and Google-managed encryption key.

    Beispiel: projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME

HTTP-Methode und URL:

POST https://dataproc.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/batches

JSON-Text der Anfrage:


{
  "environmentConfig": {
    "executionConfig": {
      "subnetworkUri": "SUBNET",
      "kmsKey": "KMS_KEY",
      "serviceAccount": "SERVICE_ACCOUNT"
    }
  },
  "labels": {
    "LABEL": "LABEL_VALUE"
  },
  "runtimeConfig": {
    "version": "2.3",
    "properties": {
      "PROPERTY": "PROPERTY_VALUE"
    }
  },
  "sparkBatch": {
    "mainClass": "com.google.cloud.dataproc.templates.main.DataProcTemplate",
    "args": [
      "--template","JDBCTOBIGQUERY",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","jdbctobq.bigquery.location=DATASET.TABLE",
      "--templateProperty","jdbctobq.jdbc.url=JDBC_CONNECTION_URL",
      "--templateProperty","jdbctobq.jdbc.driver.class.name=DRIVER",
      "--templateProperty","jdbctobq.sql=QUERY",
      "--templateProperty","jdbctobq.write.mode=MODE",
      "--templateProperty","jdbctobq.temp.gcs.bucket=TEMP_BUCKET",
      "--templateProperty","jdbctobq.sql.partitionColumn=INPUT_PARTITION_COLUMN",
      "--templateProperty","jdbctobq.sql.lowerBound=LOWERBOUND",
      "--templateProperty","jdbctobq.sql.upperBound=UPPERBOUND",
      "--templateProperty","jdbctobq.sql.numPartitions=PARTITIONS",
      "--templateProperty","jdbctobq.jdbc.fetchsize=FETCHSIZE"
    ],
    "jarFileUris": [
      "gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar","JDBC_CONNECTOR_CLOUD_STORAGE_PATH"
    ]
  }
}

Wenn Sie die Anfrage senden möchten, maximieren Sie eine der folgenden Optionen:

Sie sollten eine JSON-Antwort ähnlich wie diese erhalten:


{
  "name": "projects/PROJECT_ID/regions/REGION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.dataproc.v1.BatchOperationMetadata",
    "batch": "projects/PROJECT_ID/locations/REGION/batches/BATCH_ID",
    "batchUuid": "de8af8d4-3599-4a7c-915c-798201ed1583",
    "createTime": "2023-02-24T03:31:03.440329Z",
    "operationType": "BATCH",
    "description": "Batch"
  }
}