

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Mengautentikasi dengan integrasi Amazon Redshift untuk Apache Spark
<a name="emr-spark-redshift-auth"></a>

## Menggunakan AWS Secrets Manager untuk mengambil kredensil dan terhubung ke Amazon Redshift
<a name="emr-spark-redshift-secrets"></a>

Contoh kode berikut menunjukkan bagaimana Anda dapat menggunakan AWS Secrets Manager untuk mengambil kredensyal untuk terhubung ke cluster Amazon Redshift dengan PySpark antarmuka untuk Apache Spark di 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()
```

## Menggunakan IAM untuk mengambil kredensyal dan terhubung ke Amazon Redshift
<a name="emr-spark-redshift-iam"></a>

Anda dapat menggunakan driver JDBC versi 2 yang disediakan Amazon Redshift untuk terhubung ke Amazon Redshift dengan konektor Spark. Untuk menggunakan AWS Identity and Access Management (IAM), [konfigurasikan URL JDBC Anda untuk menggunakan](https://docs.aws.amazon.com/redshift/latest/mgmt/generating-iam-credentials-configure-jdbc-odbc.html) otentikasi IAM. Untuk terhubung ke kluster Redshift dari Amazon EMR, Anda harus memberikan izin peran IAM untuk mengambil kredenal IAM sementara. Tetapkan izin berikut ke peran IAM Anda sehingga dapat mengambil kredensyal dan menjalankan operasi Amazon S3. 
+  [Redshift: GetClusterCredentials](https://docs.aws.amazon.com/redshift/latest/APIReference/API_GetClusterCredentials.html) (untuk cluster Amazon Redshift yang disediakan) 
+  [Redshift: DescribeClusters](https://docs.aws.amazon.com/redshift/latest/APIReference/API_DescribeClusters.html) (untuk cluster Amazon Redshift yang disediakan) 
+ [Redshift: GetWorkgroup](https://docs.aws.amazon.com/redshift-serverless/latest/APIReference/API_GetWorkgroup.html) (untuk grup kerja Amazon Redshift Tanpa Server)
+  [Redshift: GetCredentials](https://docs.aws.amazon.com/redshift-serverless/latest/APIReference/API_GetCredentials.html) (untuk grup kerja Amazon Redshift Tanpa Server) 
+  [s3: GetBucket](https://docs.aws.amazon.com/AmazonS3/latest/API/API_control_GetBucket.html) 
+  [s3: GetBucketLocation](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLocation.html) 
+  [s3: GetObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObject.html) 
+  [s3: PutObject](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html) 
+  [s3: GetBucketLifecycleConfiguration](https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketLifecycleConfiguration.html) 

Untuk informasi selengkapnya`GetClusterCredentials`, lihat [Kebijakan sumber daya untuk `GetClusterCredentials`](https://docs.aws.amazon.com/redshift/latest/mgmt/redshift-iam-access-control-identity-based.html#redshift-policy-resources.getclustercredentials-resources).

Anda juga harus memastikan bahwa Amazon Redshift dapat mengambil peran IAM selama `COPY` dan operasi. `UNLOAD`

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "sts:AssumeRole"
      ],
      "Resource": "arn:aws:iam::123456789012:role/RedshiftServiceRole",
      "Sid": "AllowSTSAssumerole"
    }
  ]
}
```

------

Contoh berikut menggunakan otentikasi IAM antara Spark dan 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()
```