

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# 将预置集群迁移到 Amazon Redshift Serverless
<a name="serverless-migration"></a>

可以将现有的预置集群迁移到 Amazon Redshift Serverless，从而实现按需和自动扩展计算资源。将预置集群迁移到 Amazon Redshift Serverless 后，您只需为使用的资源付费，并可根据工作负载需求自动扩展容量，从而优化成本。迁移的常见用例包括运行临时查询、定期数据处理任务，或在不过度预置资源的情况下处理不可预测的工作负载。执行以下一组任务，将预置 Amazon Redshift 集群迁移到无服务器部署选项。

## 创建预置集群的快照
<a name="serverless-migration-snapshots"></a>

**注意**  
在您将预置的集群快照还原到无服务器命名空间时，Amazon Redshift 会自动将交错键转换为复合键。

 要将数据从您的预置集群传输到 Amazon Redshift Serverless，请为您的预置集群创建快照，然后在 Amazon Redshift Serverless 中还原该快照。

**注意**  
在将数据迁移到无服务器工作组之前，请确保您的预调配集群需求与您在 Amazon Redshift Serverless 中选择的 RPU 量兼容。

创建您的预置集群的快照

1. 登录到 AWS 管理控制台并打开 Amazon Redshift 控制台，网址：[https://console.aws.amazon.com/redshiftv2/](https://console.aws.amazon.com/redshiftv2/)。

1. 在导航菜单上，选择**集群**、**快照**，然后选择**创建快照**。

1. 输入快照定义的属性，然后选择**创建快照**。快照可能需要一段时间才可用。

将预置集群快照还原到无服务器命名空间：

1. 登录到 AWS 管理控制台并打开 Amazon Redshift 控制台，网址：[https://console.aws.amazon.com/redshiftv2/](https://console.aws.amazon.com/redshiftv2/)。

1. 在 Amazon Redshift 预置集群控制台上开始，导航到**集群**、**快照**页面。

1. 选择要使用的快照。

1. 选择**还原快照**、**还原到无服务器命名空间**。

1. 选择要将您的快照还原到的命名空间。

1. 确认想要从快照还原。此操作将以来自预置集群的数据替换无服务器端点中的所有数据库。选择**还原**。

有关预置集群快照的更多信息，请参阅 [Amazon Redshift 快照](https://docs.aws.amazon.com/redshift/latest/mgmt/working-with-snapshots.html)。

## 使用驱动程序连接到 Amazon Redshift Serverless
<a name="serverless-migration-drivers"></a>

 要使用首选的 SQL 客户端连接到 Amazon Redshift Serverless，可以使用 Amazon Redshift 提供的 [JDBC 驱动程序版本 2.x](https://docs.aws.amazon.com/redshift/latest/mgmt/jdbc20-install.html)。我们建议使用最新版本的 Amazon Redshift JDBC 驱动程序版本 2.x 连接到 Amazon Redshift。端口号是可选的。如果您未将端口号包括在内，则 Amazon Redshift Serverless 将默认使用端口号 5439。您可以更改为 5431-5455 或 8191-8215 端口范围内的另一个端口。要更改无服务器端点的默认端口，请使用 AWS CLI 和 Amazon Redshift API。

 要查找用于 JDBC、ODBC 或 Python 驱动程序的确切端点，请参阅 Amazon Redshift Serverless 中的**工作组配置**。您还可以使用 Amazon Redshift Serverless API 操作 `GetWorkgroup` 或 AWS CLI 操作 `get-workgroups`，以返回有关您工作组的信息，然后进行连接。

### 使用基于密码的身份验证进行连接
<a name="serverless-migration-drivers-password-auth"></a>

要使用 Amazon Redshift JDBC 驱动程序版本 2.x 以及基于密码的身份验证来建立连接，请使用以下语法：

```
jdbc:redshift://<{{workgroup-name}}>.<{{account-number}}>.<{{aws-region}}>.redshift-serverless.amazonaws.com:5439/?username={{username}}&password={{password}}
```

要使用 Amazon Redshift Python 连接器以及基于密码的身份验证来建立连接，请使用以下语法：

```
import redshift_connector
with redshift_connector.connect(
    host='<{{workgroup-name}}>.<{{account-number}}>.<{{aws-region}}>.redshift-serverless.amazonaws.com',
    database='<{{database-name}}>',
    user='{{username}}',
    password='{{password}}'
    # port value of 5439 is specified by default
) as conn:
    pass
```

要使用 Amazon Redshift ODBC 驱动程序版本 2.x 以及基于密码的身份验证来建立连接，请使用以下语法：

```
Driver={Amazon Redshift ODBC Driver (x64)}; Server=<{{workgroup-name}}>.<{{account-number}}>.<{{aws-region}}>.redshift-serverless.amazonaws.com; Database={{database-name}}; User={{username}}; Password={{password}}
```

### 使用 IAM 进行连接
<a name="serverless-migration-drivers-iam"></a>

 如果您更喜欢使用 IAM 进行登录，请使用 Amazon Redshift Serverless [https://docs.aws.amazon.com//redshift-serverless/latest/APIReference/API_GetCredentials.html](https://docs.aws.amazon.com//redshift-serverless/latest/APIReference/API_GetCredentials.html) API 操作。

要使用 IAM 身份验证，请将 `iam:` 添加到 Amazon Redshift JDBC URL 中 `jdbc:redshift:` 的后面，如以下示例所示。

```
jdbc:redshift:iam://<{{workgroup-name}}>.<{{account-number}}>.<{{aws-region}}>.redshift-serverless.amazonaws.com:5439/<{{database-name}}>
```

此 Amazon Redshift Serverless 端点不支持自定义 dbUser、dbGroup 或 auto-create。默认情况下，该驱动程序会在登录时自动创建数据库用户。然后，它会基于在 IAM 中指定的标签或基于在身份提供者（IdP）中定义的组，将用户分配给 Amazon Redshift 数据库角色。

请确保您的 AWS 身份具有适用于 `redshift-serverless:GetCredentials` 操作的正确 IAM 策略。下面是一个示例 IAM 策略，它授予对 AWS 身份的正确权限，用于连接到 Amazon Redshift Serverless。有关 IAM 权限的更多信息，请参阅《IAM 用户指南》**中的[添加和删除 IAM 身份权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Action": "redshift-serverless:GetCredentials",
            "Resource": "*"
        }
    ]
}
```

------

要使用 Amazon Redshift Python 连接器以及基于 IAM 的身份验证来建立连接，请在代码中使用 `iam=true`，如以下语法所示：

```
import redshift_connector
with redshift_connector.connect(
    iam=True,
    host='<{{workgroup-name}}>.<{{account-number}}>.<{{aws-region}}>.redshift-serverless.amazonaws.com',
    database='<{{database-name}}>'
    <{{IAM credentials}}>
) as conn:
    pass
```

对于 `IAM credentials`，可以使用任何凭证，包括以下各项：
+  AWS 配置文件配置。
+  IAM 凭证（访问密钥 ID、秘密访问密钥和可选的会话令牌）。
+  身份提供者联合身份验证。

要使用 Amazon Redshift ODBC 驱动程序版本 2.x 以及基于 IAM 的身份验证和配置文件来建立连接，请使用以下语法：

```
Driver={Amazon Redshift ODBC Driver (x64)}; IAM=true; Server=<{{workgroup-name}}>.<{{account-number}}>.<{{aws-region}}>.redshift-serverless.amazonaws.com; Database={{database-name}}; Profile={{aws-profile-name}};
```

### 将 IAM 和 GetClusterCredentials API 结合使用来进行连接
<a name="serverless-migration-drivers-iam-dbuser-group"></a>

**注意**  
在连接到 Amazon Redshift Serverless 时，建议您使用 [https://docs.aws.amazon.com//redshift-serverless/latest/APIReference/API_GetCredentials.html](https://docs.aws.amazon.com//redshift-serverless/latest/APIReference/API_GetCredentials.html) API。此 API 提供了全面的基于角色的访问权限控制（RBAC）功能以及 `GetClusterCredentials` 中未提供的其它新功能。我们支持使用 `GetClusterCredentials` API 来简化从预置集群到无服务器工作组的转换，但我们强烈建议尽快迁移到使用 `GetCredentials`，以获得最佳兼容性。

可以使用 [https://docs.aws.amazon.com//redshift/latest/APIReference/API_GetClusterCredentials.html](https://docs.aws.amazon.com//redshift/latest/APIReference/API_GetClusterCredentials.html) API 建立与 Amazon Redshift Serverless 的连接。要实现此身份验证方法，请通过纳入以下参数来修改客户端或应用程序：
+  `iam=true` 
+  `clusterid/cluster_identifier=redshift-serverless-<{{workgroup-name}}>` 
+  `region=<{{aws-region}}>` 

以下示例演示了所有三个驱动程序中的 BrowserSAML 插件。这代表几种可用的身份验证方法之一。可以根据具体要求修改示例，以使用其它身份验证方法或插件。

#### GetClusterCredentials 的 IAM 策略权限
<a name="serverless-migration-drivers-iam-dbuser-group-iam"></a>

以下是一个示例 IAM 策略，该策略具有将 `GetClusterCredentials` 与 Amazon Redshift Serverless 结合使用所需的权限：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "redshift-serverless:GetWorkgroup",
                "redshift-serverless:GetNamespace",
                "redshift:GetClusterCredentials",
                "redshift:CreateClusterUser",
                "redshift:JoinGroup",
                "redshift:ExecuteQuery",
                "redshift:FetchResults",
                "redshift:DescribeClusters",
                "redshift:DescribeTable"
            ],
            "Resource": [
                "arn:aws:redshift-serverless:us-east-1:{{111122223333}}:workgroup/{{workgroup-name}}",
                "arn:aws:redshift-serverless:us-east-1:{{111122223333}}:namespace/{{namespace-name}}",
                "arn:aws:redshift:us-east-1:{{111122223333}}:cluster:{{cluster-name}}",
                "arn:aws:redshift:us-east-1:{{111122223333}}:dbuser:{{database-name}}/${redshift:DbUser}"
            ]
        }
    ]
}
```

------

要使用 Amazon Redshift JDBC 驱动程序版本 2.x 以及 `GetClusterCredentials` 来建立连接，请使用以下语法：

```
jdbc:redshift:iam://redshift-serverless-<{{workgroup-name}}>:<{{aws-region}}>/<{{database-name}}>?plugin_name=com.amazon.redshift.plugin.BrowserSamlCredentialsProvider&login_url=<{{single sign-on URL from IdP}}>"
```

要使用 Amazon Redshift Python 连接器以及 `GetClusterCredentials` 来建立连接，请使用以下语法：

```
import redshift_connector
with redshift_connector.connect(
    iam=True,
    cluster_identifier='redshift-serverless-<{{workgroup-name}}>',
    region='<{{aws-region}}>',
    database='<{{database-name}}>',
    credentials_provider='BrowserSamlCredentialsProvider'
    login_url='<{{single sign-on URL from IdP}}>'
    # port value of 5439 is specified by default
) as conn:
    pass
```

要使用 Amazon Redshift ODBC 驱动程序版本 2.x 以及 `GetClusterCredentials` 来建立连接，请使用以下语法：

```
Driver= {Amazon Redshift ODBC Driver (x64)}; IAM=true; isServerless=true; ClusterId=redshift-serverless-<{{workgroup-name}}>; region=<{{aws-region}}>; plugin_name=BrowserSAML;login_url=<{{single sign-on URL from IdP}}>
```

以下是 Windows 中的 ODBC DSN 配置示例：

![适用于 Windows 的 Amazon Redshift ODBC 驱动程序中的“连接”选项卡。填写了与上述示例语法对应的字段。](http://docs.aws.amazon.com/zh_cn/redshift/latest/mgmt/images/GetClusterCredentials-odbc-windows.png)


## 使用 Amazon Redshift Serverless SDK
<a name="serverless-migration-sdk"></a>

如果您使用 Amazon Redshift SDK 编写了任何管理脚本，则必须使用新的 Amazon Redshift Serverless SDK 来管理您的 Amazon Redshift Serverless 和关联资源。有关可用 API 操作的更多信息，请参阅[《Amazon Redshift Serverless API 参考指南》](https://docs.aws.amazon.com/redshift-serverless/latest/APIReference/Welcome.html)。