Vorlage „Cloud Storage zu JDBC“
Mit der Dataproc-Vorlage für serverlose Cloud Storage-zu-JDBC-Datenübertragung können Sie Daten aus Cloud Storage in JDBC-Datenbanken extrahieren.
Vorlage verwenden
Führen Sie die Vorlage mit der gcloud CLI oder der Dataproc API aus.
Ersetzen Sie folgende Werte, bevor sie einen der Befehlsdaten verwenden:
PROJECT_ID : erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.REGION : erforderlich. Compute Engine-RegionSUBNET : Optional. Wenn kein Subnetz angegeben ist, wird das Subnetz in der angegebenen REGION imdefault
-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, unter dem die JAR-Datei des JDBC-Connectors 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
- MySQL:
CLOUD_STORAGE_PATH : erforderlich. Cloud Storage-Pfad, in dem Eingabedateien gespeichert sind.Beispiel:
gs://dataproc-templates/cloud_storage_to_jdbc_input
FORMAT : erforderlich. Ausgabedatenformat. Optionen:avro
,parquet
,csv
oderorc
. Standardeinstellung:avro
. Hinweis:Wennavro
, müssen Sie demjars
-Flag oder API-Feld der gcloud CLI „file:///usr/lib/spark/connector/spark-avro.jar
“ hinzufügen.Beispiel (das Präfix
file://
verweist auf eine Dataproc Serverless-JAR-Datei):--jars=file:///usr/lib/spark/connector/spark-avro.jar,
[, ... other jars]MODE : Optional. Schreibmodus für Cloud Storage-Ausgabe. Optionen:Append
,Overwrite
,Ignore
oderErrorIfExists
. Standardeinstellung:ErrorIfExists
.- Die folgenden Variablen werden verwendet, um die erforderliche
JDBC_CONNECTION_URL zu erstellen:JDBC_HOST JDBC_PORT JDBC_DATABASE oder für OracleJDBC_SERVICE JDBC_USERNAME JDBC_PASSWORD
Erstellen Sie die JDBC_CONNECTION_URL in 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_TABLE : erforderlich. Name der Tabelle, in die die Ausgabe geschrieben wird.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
- MySQL:
TEMPLATE_VERSION : erforderlich. Geben Sielatest
für die neueste Vorlagenversion oder das Datum einer bestimmten Version an, z. B.2023-03-17_v0.1.0-beta
. Rufen Sie gs://dataproc-templates-binaries auf oder führen Siegcloud storage ls gs://dataproc-templates-binaries
aus, um eine Liste der verfügbaren Vorlagenversionen aufzurufen.LOG_LEVEL : Optional. Protokollierungsebene. KannALL
,DEBUG
,ERROR
,FATAL
,INFO
,OFF
,TRACE
oderWARN
sein. Standard:INFO
.NUM_PARTITIONS : Optional. Die maximale Anzahl von Partitionen, die für die Parallelität von Tabelleneinträgen verwendet werden können. Wenn angegeben, wird dieser Wert für die JDBC-Ausgabeverbindung verwendet. Standardmäßig werden die von Sparkread()
festgelegten ursprünglichen Partitionen verwendet.BATCH_SIZE : Optional. Anzahl der Einträge, die pro Roundtrip eingefügt werden sollen. Standardeinstellung:1000
SERVICE_ACCOUNT : Optional. Wenn nicht angegeben, wird das Compute Engine-Standarddienstkonto verwendet.PROPERTY undPROPERTY_VALUE : Optional. Durch Kommas getrennte Liste von Spark-Property=value
-Paaren.LABEL undLABEL_VALUE : Optional. Durch Kommas getrennte Liste vonlabel
=value
-Paaren.-
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 die Daten inaktiv verschlüsselt. Dazu wird ein Google-owned and Google-managed encryption keyverwendet.Beispiel:
projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME
Führen Sie 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="1.2" \ --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=GCSTOJDBC \ --templateProperty project.id="PROJECT_ID " \ --templateProperty log.level="LOG_LEVEL " \ --templateProperty gcs.jdbc.input.location="CLOUD_STORAGE_PATH " \ --templateProperty gcs.jdbc.input.format="FORMAT " \ --templateProperty gcs.jdbc.output.saveMode="MODE " \ --templateProperty gcs.jdbc.output.url="JDBC_CONNECTION_URL " \ --templateProperty gcs.jdbc.output.table="JDBC_TABLE " \ --templateProperty gcs.jdbc.output.driver="DRIVER " \ --templateProperty gcs.jdbc.spark.partitions="NUM_PARTITIONS " \ --templateProperty gcs.jdbc.output.batchInsertSize="BATCH_SIZE "
Windows (PowerShell)
gcloud dataproc batches submit spark ` --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ` --project="PROJECT_ID " ` --region="REGION " ` --version="1.2" ` --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=GCSTOJDBC ` --templateProperty project.id="PROJECT_ID " ` --templateProperty log.level="LOG_LEVEL " ` --templateProperty gcs.jdbc.input.location="CLOUD_STORAGE_PATH " ` --templateProperty gcs.jdbc.input.format="FORMAT " ` --templateProperty gcs.jdbc.output.saveMode="MODE " ` --templateProperty gcs.jdbc.output.url="JDBC_CONNECTION_URL " ` --templateProperty gcs.jdbc.output.table="JDBC_TABLE " ` --templateProperty gcs.jdbc.output.driver="DRIVER " ` --templateProperty gcs.jdbc.spark.partitions="NUM_PARTITIONS " ` --templateProperty gcs.jdbc.output.batchInsertSize="BATCH_SIZE "
Windows (cmd.exe)
gcloud dataproc batches submit spark ^ --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^ --project="PROJECT_ID " ^ --region="REGION " ^ --version="1.2" ^ --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=GCSTOJDBC ^ --templateProperty project.id="PROJECT_ID " ^ --templateProperty log.level="LOG_LEVEL " ^ --templateProperty gcs.jdbc.input.location="CLOUD_STORAGE_PATH " ^ --templateProperty gcs.jdbc.input.format="FORMAT " ^ --templateProperty gcs.jdbc.output.saveMode="MODE " ^ --templateProperty gcs.jdbc.output.url="JDBC_CONNECTION_URL " ^ --templateProperty gcs.jdbc.output.table="JDBC_TABLE " ^ --templateProperty gcs.jdbc.output.driver="DRIVER " ^ --templateProperty gcs.jdbc.spark.partitions="NUM_PARTITIONS " ^ --templateProperty gcs.jdbc.output.batchInsertSize="BATCH_SIZE "
Ersetzen Sie diese Werte in den folgenden Anfragedaten:
PROJECT_ID : erforderlich. Ihre Google Cloud Projekt-ID, die in den IAM-Einstellungen aufgeführt ist.REGION : erforderlich. Compute Engine-RegionSUBNET : Optional. Wenn kein Subnetz angegeben ist, wird das Subnetz in der angegebenen REGION imdefault
-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, unter dem die JAR-Datei des JDBC-Connectors 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
- MySQL:
CLOUD_STORAGE_PATH : erforderlich. Cloud Storage-Pfad, in dem Eingabedateien gespeichert sind.Beispiel:
gs://dataproc-templates/cloud_storage_to_jdbc_input
FORMAT : erforderlich. Ausgabedatenformat. Optionen:avro
,parquet
,csv
oderorc
. Standardeinstellung:avro
. Hinweis:Wennavro
, müssen Sie demjars
-Flag oder API-Feld der gcloud CLI „file:///usr/lib/spark/connector/spark-avro.jar
“ hinzufügen.Beispiel (das Präfix
file://
verweist auf eine Dataproc Serverless-JAR-Datei):--jars=file:///usr/lib/spark/connector/spark-avro.jar,
[, ... other jars]MODE : Optional. Schreibmodus für Cloud Storage-Ausgabe. Optionen:Append
,Overwrite
,Ignore
oderErrorIfExists
. Standardeinstellung:ErrorIfExists
.- Die folgenden Variablen werden verwendet, um die erforderliche
JDBC_CONNECTION_URL zu erstellen:JDBC_HOST JDBC_PORT JDBC_DATABASE oder für OracleJDBC_SERVICE JDBC_USERNAME JDBC_PASSWORD
Erstellen Sie die JDBC_CONNECTION_URL in 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_TABLE : erforderlich. Name der Tabelle, in die die Ausgabe geschrieben wird.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
- MySQL:
TEMPLATE_VERSION : erforderlich. Geben Sielatest
für die neueste Vorlagenversion oder das Datum einer bestimmten Version an, z. B.2023-03-17_v0.1.0-beta
. Rufen Sie gs://dataproc-templates-binaries auf oder führen Siegcloud storage ls gs://dataproc-templates-binaries
aus, um eine Liste der verfügbaren Vorlagenversionen aufzurufen.LOG_LEVEL : Optional. Protokollierungsebene. KannALL
,DEBUG
,ERROR
,FATAL
,INFO
,OFF
,TRACE
oderWARN
sein. Standard:INFO
.NUM_PARTITIONS : Optional. Die maximale Anzahl von Partitionen, die für die Parallelität von Tabelleneinträgen verwendet werden können. Wenn angegeben, wird dieser Wert für die JDBC-Ausgabeverbindung verwendet. Standardmäßig werden die von Sparkread()
festgelegten ursprünglichen Partitionen verwendet.BATCH_SIZE : Optional. Anzahl der Einträge, die pro Roundtrip eingefügt werden sollen. Standardeinstellung:1000
SERVICE_ACCOUNT : Optional. Wenn nicht angegeben, wird das Compute Engine-Standarddienstkonto verwendet.PROPERTY undPROPERTY_VALUE : Optional. Durch Kommas getrennte Liste von Spark-Property=value
-Paaren.LABEL undLABEL_VALUE : Optional. Durch Kommas getrennte Liste vonlabel
=value
-Paaren.-
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 die Daten inaktiv verschlüsselt. Dazu wird ein Google-owned and Google-managed encryption keyverwendet.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 anfordern:
{ "environmentConfig": { "executionConfig": { "subnetworkUri": "SUBNET ", "kmsKey": "KMS_KEY ", "serviceAccount": "SERVICE_ACCOUNT " } }, "labels": { "LABEL ": "LABEL_VALUE " }, "runtimeConfig": { "version": "1.2", "properties": { "PROPERTY ": "PROPERTY_VALUE " } }, "sparkBatch": { "mainClass": "com.google.cloud.dataproc.templates.main.DataProcTemplate", "args": [ "--template=GCSTOJDBC", "--templateProperty","project.id=PROJECT_ID ", "--templateProperty","log.level=LOG_LEVEL ", "--templateProperty","gcs.jdbc.input.location=CLOUD_STORAGE_PATH ", "--templateProperty","gcs.jdbc.input.format=FORMAT ", "--templateProperty","gcs.jdbc.output.saveMode=MODE ", "--templateProperty","gcs.jdbc.output.url=JDBC_CONNECTION_URL ", "--templateProperty","gcs.jdbc.output.table=JDBC_TABLE ", "--templateProperty","gcs.jdbc.output.driver=DRIVER ", "--templateProperty","gcs.jdbc.spark.partitions=NUM_PARTITIONS ", "--templateProperty","gcs.jdbc.output.batchInsertSize=BATCH_SIZE " ], "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:
curl (Linux, macOS oder Cloud Shell)
Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json
und führen Sie den folgenden Befehl aus:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://dataproc.googleapis.com/v1/projects/PROJECT_ID /locations/REGION /batches"
PowerShell (Windows)
Speichern Sie den Anfragetext in einer Datei mit dem Namen request.json
und führen Sie den folgenden Befehl aus:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://dataproc.googleapis.com/v1/projects/PROJECT_ID /locations/REGION /batches" | Select-Object -Expand Content
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" } }