将 ModifyCluster
与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 ModifyCluster
。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- CLI
-
- AWS CLI
-
将安全组与集群关联 此示例演示了如何将群集安全组与指定的集群相关联。命令:
aws redshift modify-cluster --cluster-identifier mycluster --cluster-security-groups mysecuritygroup
修改集群的维护时段 此示例演示了如何将集群的每周首选维护时段更改为最少四小时的时段,从周日晚上 11:15 开始,到周一凌晨 3:15 结束。命令:
aws redshift modify-cluster --cluster-identifier mycluster --preferred-maintenance-window Sun:23:15-Mon:03:15
更改集群的主密码 此示例演示了如何更改集群的主密码。命令:
aws redshift modify-cluster --cluster-identifier mycluster --master-user-password A1b2c3d4
-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ModifyCluster
。
-
- Go
-
- 适用于 Go V2 的 SDK
-
注意
查看 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 详细信息,请参阅《AWS SDK for Go API 参考》中的 ModifyCluster
。
-
- Java
-
- SDK for Java 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 参考》中的 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 详细信息,请参阅《AWS SDK for JavaScript API 参考》中的 ModifyCluster。
-
- Kotlin
-
- 适用于 Kotlin 的 SDK
-
注意
查看 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 参考》中的 ModifyCluster
。
-
- Python
-
- SDK for Python (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 参考》中的 ModifyCluster。
-
有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 将此服务与 AWS SDK 结合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。