Verbindung zu Snowflake Open Catalog mit External OAuth herstellen
Unter diesem Thema wird beschrieben, wie Sie mit External OAuth mithilfe einer Clientanwendung eine Verbindung zu Snowflake Open Catalog herstellen.
Der Beispielcode in diesem Thema zeigt, wie Sie mit Apache Spark™ eine Verbindung herstellen. Der Beispielcode liegt in PySpark vor.
Bemerkung
Wenn Sie Snowflake verwenden, um von Open Catalog verwaltete Tabellen abzufragen, können Sie eine Katalogintegration für Snowflake erstellen, die External OAuth verwendet. Weitere Informationen dazu finden Sie unter CREATE CATALOG INTEGRATION (Snowflake Open Catalog) in der Snowflake-Dokumentation.
Verbindung zu Open Catalog mithilfe eines automatischen Aktualisierungstokens herstellen (bevorzugte Methode)
Verwenden Sie diese Methode, um eine Verbindung über ein automatisches Aktualisierungstoken herzustellen, sodass Sie das Token nicht manuell aktualisieren müssen.
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,<maven_coordinate>') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://<open_catalog_account_identifier>.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.warehouse','<catalog_name>') \
.config('spark.sql.catalog.opencatalog.rest.auth.type','oauth2') \
.config('spark.sql.catalog.opencatalog.oauth2-server-uri','<oauth2_server_uri>') \
.config('spark.sql.catalog.opencatalog.credential','<oauth_client_id>:<oauth_client_secret>') \
.config('spark.sql.catalog.opencatalog.scope','SESSION:ROLE:<custom_role>') \
.config('spark.sql.catalog.opencatalog.audience','https://<open_catalog_account_identifier>.snowflakecomputing.com') \
.getOrCreate()
Verbindung zu Open Catalog mithilfe eines Zugriffstokens herstellen
Bei Bedarf können Sie sich über ein Zugriffstoken mit Open Catalog verbinden. Das Zugriffstoken läuft jedoch ab und Sie müssen es manuell aktualisieren. Alternativ können Sie die Verbindung über ein automatisches Aktualisierungstoken herstellen.
Der folgende Beispielcode zeigt die Verbindungsherstellung zu Open Catalog mithilfe von Spark.
Parameter
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,<maven_coordinate>') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://<open_catalog_account_identifier>.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.token','<access_token>') \
.config('spark.sql.catalog.opencatalog.warehouse','<catalog_name>') \
.getOrCreate()
Verbindung mit einer regionenübergreifenden Verbindung herstellen (nur Amazon S3)
Der folgende Beispielcode dient zur Verbindung mit Open Catalog, wenn Folgendes zutrifft:
Ihr Open Catalog-Konto wird auf Amazon S3 gehostet.
Ihr externer Speicheranbieter ist Amazon S3.
Ihr Open Catalog-Konto wird in einer S3-Region gehostet, die sich von der S3-Region unterscheidet, in der sich der Speicher Bucket mit Ihren Apache Iceberg™-Tabellen befindet.
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,software.amazon.awssdk:bundle:2.20.160') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://<open_catalog_account_identifier>.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.token','<access_token>') \
.config('spark.sql.catalog.opencatalog.warehouse','<catalog_name>') \
.config('spark.sql.catalog.opencatalog.client.region','<target_s3_region>') \
.getOrCreate()
Beispiele
Dieser Abschnitt enthält Beispiele für die Verbindung zu Open Catalog mit Spark:
Beispiel 1: Verbindung herstellen (S3)
Siehe:
Verbindung über automatische Aktualisierung herstellen (S3)
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,software.amazon.awssdk:bundle:2.20.160') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://ab12345.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.warehouse','Catalog1') \
.config('spark.sql.catalog.opencatalog.rest.auth.type','oauth2') \
.config('spark.sql.catalog.opencatalog.oauth2-server-uri','your-tenant.region.auth0.com') \
.config('spark.sql.catalog.opencatalog.credential','11111111111111111111111111111111:222222222222222222222222222222222222222222222222222222222222222222') \
.config('spark.sql.catalog.opencatalog.scope','SESSION:ROLE:DATA_ENG') \
.config('spark.sql.catalog.opencatalog.audience','https://ab12345.snowflakecomputing.com') \
.getOrCreate()
Verbindung über Zugriffstoken herstellen (S3)
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,software.amazon.awssdk:bundle:2.20.160') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://ab12345.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.token','0000000000000000000000000001111111111111111111111111111111111111111111') \
.config('spark.sql.catalog.opencatalog.warehouse','Catalog1') \
.getOrCreate()
Beispiel 2: Verbindung herstellen (Cloud Storage von Google)
Siehe:
Verbindung über die automatische Aktualisierung herstellen (Cloud Storage von Google)
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,org.apache.iceberg:iceberg-gcp-bundle:1.5.2') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://ab12345.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.warehouse','Catalog1') \
.config('spark.sql.catalog.opencatalog.rest.auth.type','oauth2') \
.config('spark.sql.catalog.opencatalog.oauth2-server-uri','your-tenant.region.auth0.com') \
.config('spark.sql.catalog.opencatalog.credential','11111111111111111111111111111111:222222222222222222222222222222222222222222222222222222222222222222') \
.config('spark.sql.catalog.opencatalog.scope','SESSION:ROLE:DATA_ENG') \
.config('spark.sql.catalog.opencatalog.audience','https://ab12345.snowflakecomputing.com') \
.getOrCreate()
Verbindung über Zugriffstoken herstellen (Cloud Storage von Google)
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,org.apache.iceberg:iceberg-gcp-bundle:1.5.2') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://ab12345.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.token','0000000000000000000000000001111111111111111111111111111111111111111111') \
.config('spark.sql.catalog.opencatalog.warehouse','Catalog1') \
.getOrCreate()
Beispiel 3: Verbindung herstellen (Azure)
Siehe:
Verbindung über automatische Aktualisierung herstellen (Azure)
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,org.apache.iceberg:iceberg-azure-bundle:1.5.2') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://ab12345.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.warehouse','Catalog1') \
.config('spark.sql.catalog.opencatalog.rest.auth.type','oauth2') \
.config('spark.sql.catalog.opencatalog.oauth2-server-uri','your-tenant.region.auth0.com') \
.config('spark.sql.catalog.opencatalog.credential','11111111111111111111111111111111:222222222222222222222222222222222222222222222222222222222222222222') \
.config('spark.sql.catalog.opencatalog.scope','SESSION:ROLE:DATA_ENG') \
.config('spark.sql.catalog.opencatalog.audience','https://ab12345.snowflakecomputing.com') \
.getOrCreate()
Verbindung über Zugriffstoken herstellen (Azure)
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('iceberg_lab') \
.config('spark.jars.packages', 'org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.4.1,org.apache.iceberg:iceberg-azure-bundle:1.5.2') \
.config('spark.sql.extensions', 'org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions') \
.config('spark.sql.defaultCatalog', 'opencatalog') \
.config('spark.sql.catalog.opencatalog', 'org.apache.iceberg.spark.SparkCatalog') \
.config('spark.sql.catalog.opencatalog.type', 'rest') \
.config('spark.sql.catalog.opencatalog.uri','https://ab12345.snowflakecomputing.com/polaris/api/catalog') \
.config('spark.sql.catalog.opencatalog.header.X-Iceberg-Access-Delegation','vended-credentials') \
.config('spark.sql.catalog.opencatalog.token','0000000000000000000000000001111111111111111111111111111111111111111111') \
.config('spark.sql.catalog.opencatalog.warehouse','Catalog1') \
.getOrCreate()
Überprüfen Sie die Verbindung zu Open Catalog
Um zu überprüfen, ob Spark mit Open Catalog verbunden ist, listen Sie die Namespaces für den Katalog auf. Weitere Informationen finden Sie unter Namespaces auflisten.