Autenticazione con l'integrazione di Amazon Redshift per Apache Spark - Amazon EMR

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Autenticazione con l'integrazione di Amazon Redshift per Apache Spark

Utilizzo AWS Secrets Manager per recuperare le credenziali e connettersi ad Amazon Redshift

Il seguente esempio di codice mostra come recuperare le credenziali AWS Secrets Manager per la connessione a un cluster Amazon Redshift con PySpark l'interfaccia per Apache Spark in Python.

from pyspark.sql import SQLContext import boto3 sc = # existing SparkContext sql_context = SQLContext(sc) secretsmanager_client = boto3.client('secretsmanager') secret_manager_response = secretsmanager_client.get_secret_value( SecretId='string', VersionId='string', VersionStage='string' ) username = # get username from secret_manager_response password = # get password from secret_manager_response url = "jdbc:redshift://redshifthost:5439/database?user=" + username + "&password=" + password # Read data from a table df = sql_context.read \ .format("io.github.spark_redshift_community.spark.redshift") \ .option("url", url) \ .option("dbtable", "my_table") \ .option("tempdir", "s3://path/for/temp/data") \ .load()

Utilizzo IAM per recuperare le credenziali e connettersi ad Amazon Redshift

Puoi utilizzare il driver JDBC versione 2 fornito da Amazon Redshift per connetterti ad Amazon Redshift con il connettore Spark. Per usare AWS Identity and Access Management (IAM), configura la tua autenticazione. JDBC URL IAM Per connetterti a un cluster Redshift da AmazonEMR, devi autorizzare il tuo IAM ruolo a recuperare credenziali temporanee. IAM Assegna le seguenti autorizzazioni al tuo IAM ruolo in modo che possa recuperare le credenziali ed eseguire operazioni su Amazon S3.

Per ulteriori informazioni su GetClusterCredentials, consulta Policy delle risorse per GetClusterCredentials.

Inoltre, devi assicurarti che Amazon Redshift possa assumere il IAM ruolo durante COPY le UNLOAD operazioni.

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "redshift.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }

L'esempio seguente utilizza IAM l'autenticazione tra Spark e Amazon Redshift:

from pyspark.sql import SQLContext import boto3 sc = # existing SparkContext sql_context = SQLContext(sc) url = "jdbc:redshift:iam//redshift-host:redshift-port/db-name" iam_role_arn = "arn:aws:iam::account-id:role/role-name" # Read data from a table df = sql_context.read \ .format("io.github.spark_redshift_community.spark.redshift") \ .option("url", url) \ .option("aws_iam_role", iam_role_arn) \ .option("dbtable", "my_table") \ .option("tempdir", "s3a://path/for/temp/data") \ .mode("error") \ .load()