Authentification avec l'intégration Amazon Redshift pour Apache Spark - Amazon EMR

Authentification avec l'intégration Amazon Redshift pour Apache Spark

Utilisation de AWS Secrets Manager pour récupérer les informations d'identification et se connecter à Amazon Redshift

L'exemple de code suivant montre comment utiliser AWS Secrets Manager pour récupérer les informations d'identification pour vous connecter à un cluster Amazon Redshift avec l'interface PySpark pour Apache Spark en 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()

Utilisation d'IAM pour récupérer les informations d'identification et se connecter à Amazon Redshift

Vous pouvez utiliser le JDBC version 2 fourni par Amazon Redshift pour vous connecter à Amazon Redshift avec le connecteur Spark. Pour utiliser AWS Identity and Access Management (IAM), configurez votre URL JDBC pour utiliser l'authentification IAM. Pour vous connecter à un cluster Redshift depuis Amazon EMR, vous devez donner à votre rôle IAM l'autorisation de récupérer des informations d'identification IAM temporaires. Attribuez les autorisations suivantes à votre rôle IAM pour qu'il puisse récupérer les informations d'identification et exécuter les opérations Amazon S3.

Pour plus d'informations sur GetClusterCredentials, consultez Stratégies de ressources pour GetClusterCredentials.

Vous devez également vous assurer qu'Amazon Redshift peut assumer le rôle IAM pendant les opérations COPY et UNLOAD.

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

L'exemple suivant utilise l'authentification IAM entre Spark et 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()