Vorlage „JDBC zu Cloud Storage“

Mit der Vorlage „Managed Service for Apache Spark JDBC zu Cloud Storage“ können Sie Daten aus JDBC-Datenbanken in Cloud Storage 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 die folgenden Platzhalter, bevor Sie die Befehlsdaten verwenden:

  • PROJECT_ID: erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.
  • REGION: erforderlich. Compute Engine -Region.
  • 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
            
    • PostgreSQL :
      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
            
  • 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
              
    • PostgreSQL :
      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=
              
  • DRIVER: erforderlich. Der JDBC-Treiber, der für die Verbindung verwendet wird:
    • MySQL :
      com.mysql.cj.jdbc.Driver
              
    • PostgreSQL :
      org.postgresql.Driver
              
    • Microsoft SQL Server :
        
      com.microsoft.sqlserver.jdbc.SQLServerDriver
              
    • Oracle :
      oracle.jdbc.driver.OracleDriver
              
  • FORMAT: erforderlich. Ausgabedatenformat. Optionen: avro, parquet, csv, oder json. Standardeinstellung: avro. Hinweis: Wenn Sie avro verwenden, müssen Sie dem gcloud CLI-Flag oder API-Feld jars "file:///usr/lib/spark/connector/spark-avro.jar" hinzufügen.

    Beispiel: Das Präfix file:// verweist auf eine Managed Service for Apache Spark-JAR-Datei.

    --jars=file:///usr/lib/spark/connector/spark-avro.jar, [, ... other jars]
  • MODE: erforderlich. Schreibmodus für die Cloud Storage-Ausgabe. Optionen: append, overwrite, ignore, oder errorifexists.
  • 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.
  • CLOUD_STORAGE_OUTPUT_PATH: erforderlich. Cloud Storage Pfad, in dem die Ausgabe gespeichert wird.

    Beispiel:gs://templates/jdbc_to_cloud_storage_output

  • LOG_LEVEL: optional. Protokollierungsstufe. Mögliche Werte: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE oder WARN. Standardeinstellung: INFO.
  • INPUT_PARTITION_COLUMN, LOWERBOUND, UPPERBOUND, NUM_PARTITIONS: optional. Wenn verwendet, müssen alle folgenden Parameter angegeben werden:
    • INPUT_PARTITION_COLUMN: Name der Spalte für die JDBC-Eingabetabellenpartition.
    • LOWERBOUND: Untergrenze der Spalte für die JDBC-Eingabetabellenpartition, die zum Bestimmen der Partitionsgröße verwendet wird.
    • UPPERBOUND:Obergrenze der Spalte für die JDBC-Eingabetabellenpartition, die zum Bestimmen der Partitionsgröße verwendet wird.
    • NUM_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. Standardeinstellung: 10.
  • OUTPUT_PARTITION_COLUMN: optional. Name der Ausgabepartitionspalte.
  • FETCHSIZE: optional. Anzahl der Zeilen, die pro Roundtrip abgerufen werden sollen. Standardeinstellung: 10.
  • QUERY oder QUERY_FILE: erforderlich. Legen Sie entweder QUERY oder QUERY_FILE fest, um die Abfrage anzugeben, mit der Daten aus JDBC extrahiert werden sollen.
  • TEMP_VIEW und TEMP_QUERY: optional. Mit diesen beiden optionalen Parametern können Sie eine Spark SQL-Transformation anwenden, während Sie Daten in Cloud Storage laden. TEMPVIEW muss mit dem in der Abfrage verwendeten Tabellennamen übereinstimmen und TEMP_QUERY ist die Abfrageanweisung.
  • 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.
  • JDBC_SESSION_INIT: optional. Anweisung zur Sitzungsinitialisierung zum Lesen von Java-Vorlagen.
  • 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 \
    --project="PROJECT_ID" \
    --region="REGION" \
    --version="2.3" \
    --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=JDBCTOGCS \
    --templateProperty project.id="PROJECT_ID" \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty jdbctogcs.jdbc.url="JDBC_CONNECTION_URL" \
    --templateProperty jdbctogcs.jdbc.driver.class.name="DRIVER" \
    --templateProperty jdbctogcs.output.format="FORMAT" \
    --templateProperty jdbctogcs.output.location="CLOUD_STORAGE_OUTPUT_PATH" \
    --templateProperty jdbctogcs.sql="QUERY" \
    --templateProperty jdbctogcs.sql.file="QUERY_FILE" \
    --templateProperty jdbctogcs.sql.partitionColumn="INPUT_PARTITION_COLUMN" \
    --templateProperty jdbctogcs.sql.lowerBound="LOWERBOUND" \
    --templateProperty jdbctogcs.sql.upperBound="UPPERBOUND" \
    --templateProperty jdbctogcs.jdbc.fetchsize="FETCHSIZE" \
    --templateProperty jdbctogcs.sql.numPartitions="NUM_PARTITIONS" \
    --templateProperty jdbctogcs.write.mode="MODE" \
    --templateProperty dbctogcs.output.partition.col="OUTPUT_PARTITION_COLUMN" \
    --templateProperty jdbctogcs.temp.table="TEMP_VIEW" \
    --templateProperty jdbctogcs.temp.query="TEMP_QUERY"

Windows (PowerShell)

gcloud dataproc batches submit spark `
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate `
    --project="PROJECT_ID" `
    --region="REGION" `
    --version="2.3" `
    --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=JDBCTOGCS `
    --templateProperty project.id="PROJECT_ID" `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty jdbctogcs.jdbc.url="JDBC_CONNECTION_URL" `
    --templateProperty jdbctogcs.jdbc.driver.class.name="DRIVER" `
    --templateProperty jdbctogcs.output.format="FORMAT" `
    --templateProperty jdbctogcs.output.location="CLOUD_STORAGE_OUTPUT_PATH" `
    --templateProperty jdbctogcs.sql="QUERY" `
    --templateProperty jdbctogcs.sql.file="QUERY_FILE" `
    --templateProperty jdbctogcs.sql.partitionColumn="INPUT_PARTITION_COLUMN" `
    --templateProperty jdbctogcs.sql.lowerBound="LOWERBOUND" `
    --templateProperty jdbctogcs.sql.upperBound="UPPERBOUND" `
    --templateProperty jdbctogcs.jdbc.fetchsize="FETCHSIZE" `
    --templateProperty jdbctogcs.sql.numPartitions="NUM_PARTITIONS" `
    --templateProperty jdbctogcs.write.mode="MODE" `
    --templateProperty dbctogcs.output.partition.col="OUTPUT_PARTITION_COLUMN" `
    --templateProperty jdbctogcs.temp.table="TEMP_VIEW" `
    --templateProperty jdbctogcs.temp.query="TEMP_QUERY"

Windows (cmd.exe)

gcloud dataproc batches submit spark ^
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^
    --project="PROJECT_ID" ^
    --region="REGION" ^
    --version="2.3" ^
    --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=JDBCTOGCS ^
    --templateProperty project.id="PROJECT_ID" ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty jdbctogcs.jdbc.url="JDBC_CONNECTION_URL" ^
    --templateProperty jdbctogcs.jdbc.driver.class.name="DRIVER" ^
    --templateProperty jdbctogcs.output.format="FORMAT" ^
    --templateProperty jdbctogcs.output.location="CLOUD_STORAGE_OUTPUT_PATH" ^
    --templateProperty jdbctogcs.sql="QUERY" ^
    --templateProperty jdbctogcs.sql.file="QUERY_FILE" ^
    --templateProperty jdbctogcs.sql.partitionColumn="INPUT_PARTITION_COLUMN" ^
    --templateProperty jdbctogcs.sql.lowerBound="LOWERBOUND" ^
    --templateProperty jdbctogcs.sql.upperBound="UPPERBOUND" ^
    --templateProperty jdbctogcs.jdbc.fetchsize="FETCHSIZE" ^
    --templateProperty jdbctogcs.sql.numPartitions="NUM_PARTITIONS" ^
    --templateProperty jdbctogcs.write.mode="MODE" ^
    --templateProperty dbctogcs.output.partition.col="OUTPUT_PARTITION_COLUMN" ^
    --templateProperty jdbctogcs.temp.table="TEMP_VIEW" ^
    --templateProperty jdbctogcs.temp.query="TEMP_QUERY"

REST

Ersetzen Sie die folgenden Platzhalter, bevor Sie die Anfragedaten verwenden:

  • PROJECT_ID: erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.
  • REGION: erforderlich. Compute Engine -Region.
  • 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
            
    • PostgreSQL :
      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
            
  • 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
              
    • PostgreSQL :
      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=
              
  • DRIVER: erforderlich. Der JDBC-Treiber, der für die Verbindung verwendet wird:
    • MySQL :
      com.mysql.cj.jdbc.Driver
              
    • PostgreSQL :
      org.postgresql.Driver
              
    • Microsoft SQL Server :
        
      com.microsoft.sqlserver.jdbc.SQLServerDriver
              
    • Oracle :
      oracle.jdbc.driver.OracleDriver
              
  • FORMAT: erforderlich. Ausgabedatenformat. Optionen: avro, parquet, csv, oder json. Standardeinstellung: avro. Hinweis: Wenn Sie avro verwenden, müssen Sie dem gcloud CLI-Flag oder API-Feld jars "file:///usr/lib/spark/connector/spark-avro.jar" hinzufügen.

    Beispiel: Das Präfix file:// verweist auf eine Managed Service for Apache Spark-JAR-Datei.

    --jars=file:///usr/lib/spark/connector/spark-avro.jar, [, ... other jars]
  • MODE: erforderlich. Schreibmodus für die Cloud Storage-Ausgabe. Optionen: append, overwrite, ignore, oder errorifexists.
  • 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.
  • CLOUD_STORAGE_OUTPUT_PATH: erforderlich. Cloud Storage Pfad, in dem die Ausgabe gespeichert wird.

    Beispiel:gs://templates/jdbc_to_cloud_storage_output

  • LOG_LEVEL: optional. Protokollierungsstufe. Mögliche Werte: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE oder WARN. Standardeinstellung: INFO.
  • INPUT_PARTITION_COLUMN, LOWERBOUND, UPPERBOUND, NUM_PARTITIONS: optional. Wenn verwendet, müssen alle folgenden Parameter angegeben werden:
    • INPUT_PARTITION_COLUMN: Name der Spalte für die JDBC-Eingabetabellenpartition.
    • LOWERBOUND: Untergrenze der Spalte für die JDBC-Eingabetabellenpartition, die zum Bestimmen der Partitionsgröße verwendet wird.
    • UPPERBOUND:Obergrenze der Spalte für die JDBC-Eingabetabellenpartition, die zum Bestimmen der Partitionsgröße verwendet wird.
    • NUM_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. Standardeinstellung: 10.
  • OUTPUT_PARTITION_COLUMN: optional. Name der Ausgabepartitionspalte.
  • FETCHSIZE: optional. Anzahl der Zeilen, die pro Roundtrip abgerufen werden sollen. Standardeinstellung: 10.
  • QUERY oder QUERY_FILE: erforderlich. Legen Sie entweder QUERY oder QUERY_FILE fest, um die Abfrage anzugeben, mit der Daten aus JDBC extrahiert werden sollen.
  • TEMP_VIEW und TEMP_QUERY: optional. Mit diesen beiden optionalen Parametern können Sie eine Spark SQL-Transformation anwenden, während Sie Daten in Cloud Storage laden. TEMPVIEW muss mit dem in der Abfrage verwendeten Tabellennamen übereinstimmen und TEMP_QUERY ist die Abfrageanweisung.
  • 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.
  • JDBC_SESSION_INIT: optional. Anweisung zur Sitzungsinitialisierung zum Lesen von Java-Vorlagen.
  • 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=JDBCTOGCS",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","project.id=PROJECT_ID",
      "--templateProperty","jdbctogcs.jdbc.url=JDBC_CONNECTION_URL",
      "--templateProperty","jdbctogcs.jdbc.driver.class.name=DRIVER",
      "--templateProperty","jdbctogcs.output.location=CLOUD_STORAGE_OUTPUT_PATH",
      "--templateProperty","jdbctogcs.write.mode=MODE",
      "--templateProperty","jdbctogcs.output.format=FORMAT",
      "--templateProperty","jdbctogcs.sql.numPartitions=NUM_PARTITIONS",
      "--templateProperty","jdbctogcs.jdbc.fetchsize=FETCHSIZE",
      "--templateProperty","jdbctogcs.sql=QUERY",
      "--templateProperty","jdbctogcs.sql.file=QUERY_FILE",
      "--templateProperty","jdbctogcs.sql.partitionColumn=INPUT_PARTITION_COLUMN",
      "--templateProperty","jdbctogcs.sql.lowerBound=LOWERBOUND",
      "--templateProperty","jdbctogcs.sql.upperBound=UPPERBOUND",
      "--templateProperty","jdbctogcs.output.partition.col=OUTPUT_PARTITION_COLUMN",
      "--templateProperty","jdbctogcs.temp.table=TEMP_VIEW",
      "--templateProperty","jdbctogcs.temp.query=TEMP_QUERY",
      "--templateProperty","jdbctogcs.jdbc.sessioninitstatement=JDBC_SESSION_INIT"
    ],
    "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"
  }
}