2026 年 6 月 30 日之后,Amazon Redshift 不再支持使用 Python UDF。我们将分阶段开始执行。有关 Python 生命周期终止和迁移选项详情的更多信息,请参阅 2025 年 6 月 30 日发布的博客文章
将 ModifyCluster 与 AWS SDK 或 CLI 配合使用
以下代码示例演示了如何使用 ModifyCluster。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- .NET
-
- 适用于 .NET 的 SDK (v4)
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 /// <summary> /// Modify an Amazon Redshift cluster. /// </summary> /// <param name="clusterIdentifier">The identifier for the cluster.</param> /// <param name="preferredMaintenanceWindow">The preferred maintenance window.</param> /// <returns>True if successful.</returns> public async Task<bool> ModifyClusterAsync(string clusterIdentifier, string preferredMaintenanceWindow) { try { var request = new ModifyClusterRequest { ClusterIdentifier = clusterIdentifier, PreferredMaintenanceWindow = preferredMaintenanceWindow }; var response = await _redshiftClient.ModifyClusterAsync(request); Console.WriteLine($"The modified cluster was successfully modified and has {response.Cluster.PreferredMaintenanceWindow} as the maintenance window"); return true; } catch (ClusterNotFoundException ex) { Console.WriteLine($"Cluster {clusterIdentifier} not found: {ex.Message}"); return false; } catch (Exception ex) { Console.WriteLine($"Couldn't modify cluster. Here's why: {ex.Message}"); return false; } }-
有关 API 详细信息,请参阅《适用于 .NET 的 AWS SDK API Reference》中的 ModifyCluster。
-
- CLI
-
- AWS CLI
-
示例 1:将安全组与集群关联
以下
modify-cluster示例将集群安全组与指定的集群关联。aws redshift modify-cluster \ --cluster-identifiermycluster\ --cluster-security-groupsmysecuritygroup输出:
{ "Cluster": { "ClusterIdentifier": "mycluster", "NodeType": "dc2.large", "ClusterStatus": "available", "ClusterAvailabilityStatus": "Available", "MasterUsername": "adminuser", "DBName": "dev", "Endpoint": { "Address": "mycluster.cmeaswqeuae.us-west-2.redshift.amazonaws.com", "Port": 5439 }, "ClusterCreateTime": "2019-12-02T22:45:07.890Z", "AutomatedSnapshotRetentionPeriod": 1, "ManualSnapshotRetentionPeriod": -1, "ClusterSecurityGroups": [ { "ClusterSecurityGroupName": "default", "Status": "removing" }, { "ClusterSecurityGroupName": "mysecuritygroup", "Status": "adding" } ], "VpcSecurityGroups": [], "ClusterParameterGroups": [ { "ParameterGroupName": "default.redshift-1.0", "ParameterApplyStatus": "in-sync" } ], "AvailabilityZone": "us-west-2a", "PreferredMaintenanceWindow": "sat:03:30-sat:04:00", "PendingModifiedValues": {}, "ClusterVersion": "1.0", "AllowVersionUpgrade": true, "NumberOfNodes": 2, "PubliclyAccessible": true, "Encrypted": false, "Tags": [], "EnhancedVpcRouting": false, "IamRoles": [], "MaintenanceTrackName": "current" } }有关更多信息,请参阅《Amazon Redshift 管理指南》中的修改集群。
示例 2:修改集群的维护时段
以下
modify-cluster示例将集群的每周首选维护时段更改为最少四小时的时段,从周日晚上 11:15 开始,到周一凌晨 3:15 结束。aws redshift modify-cluster \ --cluster-identifiermycluster\ --preferred-maintenance-windowSun:23:15-Mon:03:15输出:
{ "Cluster": { "ClusterIdentifier": "mycluster", "NodeType": "dc2.large", "ClusterStatus": "available", "ClusterAvailabilityStatus": "Available", "MasterUsername": "adminuser", "DBName": "dev", "Endpoint": { "Address": "mycluster.cmeaswqeuae.us-west-2.redshift.amazonaws.com", "Port": 5439 }, "ClusterCreateTime": "2019-12-02T22:45:07.890Z", "AutomatedSnapshotRetentionPeriod": 1, "ManualSnapshotRetentionPeriod": -1, "ClusterSecurityGroups": [ { "ClusterSecurityGroupName": "default", "Status": "active" } ], "VpcSecurityGroups": [], "ClusterParameterGroups": [ { "ParameterGroupName": "default.redshift-1.0", "ParameterApplyStatus": "in-sync" } ], "AvailabilityZone": "us-west-2a", "PreferredMaintenanceWindow": "sun:23:15-mon:03:15", "PendingModifiedValues": {}, "ClusterVersion": "1.0", "AllowVersionUpgrade": true, "NumberOfNodes": 2, "PubliclyAccessible": true, "Encrypted": false, "Tags": [], "EnhancedVpcRouting": false, "IamRoles": [], "MaintenanceTrackName": "current" } }有关更多信息,请参阅《Amazon Redshift 管理指南》中的维护窗口。
示例 3:更改集群的主密码
以下
modify-cluster示例更改集群的主密码。aws redshift modify-cluster \ --cluster-identifiermycluster\ --master-user-passwordA1b2c3d4输出:
{ "Cluster": { "ClusterIdentifier": "mycluster", "NodeType": "dc2.large", "ClusterStatus": "available", "ClusterAvailabilityStatus": "Available", "MasterUsername": "adminuser", "DBName": "dev", "Endpoint": { "Address": "mycluster.cmeaswqeuae.us-west-2.redshift.amazonaws.com", "Port": 5439 }, "ClusterCreateTime": "2019-12-02T22:45:07.890Z", "AutomatedSnapshotRetentionPeriod": 1, "ManualSnapshotRetentionPeriod": -1, "ClusterSecurityGroups": [ { "ClusterSecurityGroupName": "default", "Status": "active" } ], "VpcSecurityGroups": [], "ClusterParameterGroups": [ { "ParameterGroupName": "default.redshift-1.0", "ParameterApplyStatus": "in-sync" } ], "AvailabilityZone": "us-west-2a", "PreferredMaintenanceWindow": "sat:03:30-sat:04:00", "PendingModifiedValues": { "MasterUserPassword": "****" }, "ClusterVersion": "1.0", "AllowVersionUpgrade": true, "NumberOfNodes": 2, "PubliclyAccessible": true, "Encrypted": false, "Tags": [], "EnhancedVpcRouting": false, "IamRoles": [], "MaintenanceTrackName": "current" } }有关更多信息,请参阅《Amazon Redshift 管理指南》中的修改集群。
-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ModifyCluster
。
-
- Go
-
- 适用于 Go 的 SDK V2
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 import ( "context" "errors" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/redshift" "github.com/aws/aws-sdk-go-v2/service/redshift/types" ) // RedshiftActions wraps Redshift service actions. type RedshiftActions struct { RedshiftClient *redshift.Client } // ModifyCluster sets the preferred maintenance window for the given cluster. func (actor RedshiftActions) ModifyCluster(ctx context.Context, clusterId string, maintenanceWindow string) *redshift.ModifyClusterOutput { // Modify the cluster's maintenance window input := &redshift.ModifyClusterInput{ ClusterIdentifier: aws.String(clusterId), PreferredMaintenanceWindow: aws.String(maintenanceWindow), } var opErr *types.InvalidClusterStateFault output, err := actor.RedshiftClient.ModifyCluster(ctx, input) if err != nil && errors.As(err, &opErr) { log.Println("Cluster is in an invalid state.") panic(err) } else if err != nil { log.Printf("Failed to modify Redshift cluster: %v\n", err) panic(err) } log.Printf("The cluster was successfully modified and now has %s as the maintenance window\n", *output.Cluster.PreferredMaintenanceWindow) return output }-
有关 API 详细信息,请参阅《适用于 Go 的 AWS SDK API Reference》中的 ModifyCluster
。
-
- Java
-
- 适用于 Java 的 SDK 2.x
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 修改集群。
/** * Modifies an Amazon Redshift cluster asynchronously. * * @param clusterId the identifier of the cluster to be modified * @return a {@link CompletableFuture} that completes when the cluster modification is complete */ public CompletableFuture<ModifyClusterResponse> modifyClusterAsync(String clusterId) { ModifyClusterRequest modifyClusterRequest = ModifyClusterRequest.builder() .clusterIdentifier(clusterId) .preferredMaintenanceWindow("wed:07:30-wed:08:00") .build(); return getAsyncClient().modifyCluster(modifyClusterRequest) .whenComplete((clusterResponse, exception) -> { if (exception != null) { if (exception.getCause() instanceof RedshiftException) { logger.info("Error: {} ", exception.getMessage()); } else { logger.info("Unexpected error: {} ", exception.getMessage()); } } else { logger.info("The modified cluster was successfully modified and has " + clusterResponse.cluster().preferredMaintenanceWindow() + " as the maintenance window"); } }); }-
有关 API 详细信息,请参阅《AWS SDK for Java 2.x API Reference》中的 ModifyCluster。
-
- JavaScript
-
- SDK for JavaScript (v3)
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 创建客户端。
import { RedshiftClient } from "@aws-sdk/client-redshift"; // Set the AWS Region. const REGION = "REGION"; //Set the Redshift Service Object const redshiftClient = new RedshiftClient({ region: REGION }); export { redshiftClient };修改集群。
// Import required AWS SDK clients and commands for Node.js import { ModifyClusterCommand } from "@aws-sdk/client-redshift"; import { redshiftClient } from "./libs/redshiftClient.js"; // Set the parameters const params = { ClusterIdentifier: "CLUSTER_NAME", MasterUserPassword: "NEW_MASTER_USER_PASSWORD", }; const run = async () => { try { const data = await redshiftClient.send(new ModifyClusterCommand(params)); console.log("Success was modified.", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();-
有关 API 详细信息,请参阅《适用于 JavaScript 的 AWS SDK API Reference》中的 ModifyCluster。
-
- Kotlin
-
- SDK for Kotlin
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 修改集群。
suspend fun modifyCluster(clusterId: String?) { val modifyClusterRequest = ModifyClusterRequest { clusterIdentifier = clusterId preferredMaintenanceWindow = "wed:07:30-wed:08:00" } RedshiftClient { region = "us-west-2" }.use { redshiftClient -> val clusterResponse = redshiftClient.modifyCluster(modifyClusterRequest) println( "The modified cluster was successfully modified and has ${clusterResponse.cluster?.preferredMaintenanceWindow} as the maintenance window", ) } }-
有关 API 详细信息,请参阅《AWS SDK for Kotlin API Reference》中的 ModifyCluster
。
-
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 class RedshiftWrapper: """ Encapsulates Amazon Redshift cluster operations. """ def __init__(self, redshift_client): """ :param redshift_client: A Boto3 Redshift client. """ self.client = redshift_client def modify_cluster(self, cluster_identifier, preferred_maintenance_window): """ Modifies a cluster. :param cluster_identifier: The cluster identifier. :param preferred_maintenance_window: The preferred maintenance window. """ try: self.client.modify_cluster( ClusterIdentifier=cluster_identifier, PreferredMaintenanceWindow=preferred_maintenance_window, ) except ClientError as err: logging.error( "Couldn't modify a cluster. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise以下代码会实例化 RedshiftWrapper 对象。
client = boto3.client("redshift") redhift_wrapper = RedshiftWrapper(client)-
有关 API 详细信息,请参阅《AWS SDK for Python(Boto3)API Reference》中的 ModifyCluster。
-
- SAP ABAP
-
- 适用于 SAP ABAP 的 SDK
-
注意
查看 GitHub,了解更多信息。在 AWS 代码示例存储库
中查找完整示例,了解如何进行设置和运行。 修改集群。
TRY. " Example values: iv_cluster_identifier = 'my-redshift-cluster' " Example values: iv_pref_maintenance_wn = 'wed:07:30-wed:08:00' lo_rsh->modifycluster( iv_clusteridentifier = iv_cluster_identifier iv_preferredmaintenancewin00 = iv_pref_maintenance_wn ). MESSAGE 'Redshift cluster modified successfully.' TYPE 'I'. CATCH /aws1/cx_rshclustnotfoundfault. MESSAGE 'Cluster not found.' TYPE 'I'. CATCH /aws1/cx_rshinvcluststatefault. MESSAGE 'Invalid cluster state for modification.' TYPE 'I'. ENDTRY.-
有关 API 详细信息,请参阅《AWS SDK for SAP ABAP API Reference》中的 ModifyCluster。
-
有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。