

 [适用于 JavaScript 的 AWS SDK V3 API 参考指南](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)详细描述了 适用于 JavaScript 的 AWS SDK 版本 3 (V3) 的所有 API 操作。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon Redshift 示例
<a name="redshift-examples"></a>

Amazon Redshift 是云中一种完全托管的 PB 级数据仓库服务。Amazon Redshift 数据仓库是一个由称作*节点*的各种计算资源构成的集合，这些节点已整理到名为*集群*的组中。每个集群运行一个 Amazon Redshift 引擎并包含一个或多个数据库。

![\[JavaScript 环境、SDK 与 Amazon Redshift 之间的关系\]](http://docs.aws.amazon.com/zh_cn/sdk-for-javascript/v3/developer-guide/images/code-samples-redshift.png)


适用于 Amazon Redshift 的 JavaScript API 通过 [Amazon Redshift](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/Redshift/) 客户端类公开。

**Topics**
+ [Amazon Redshift 示例](redshift-examples-section.md)

# Amazon Redshift 示例
<a name="redshift-examples-section"></a>

此示例使用一系列 Node.js 模块来创建、修改、描述 Amazon Redshift 集群的参数，然后使用 `Redshift` 客户端类的以下方法删除这些集群：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/CreateClusterCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/CreateClusterCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/ModifyClusterCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/ModifyClusterCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/DescribeClustersCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/DescribeClustersCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/DeleteClusterCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-redshift/Class/DeleteClusterCommand/)

有关 Amazon Redshift 用户的更多信息，请参阅 [Amazon Redshift 入门指南](https://docs.aws.amazon.com/redshift/latest/gsg/getting-started.html)。

## 先决条件任务
<a name="s3-example-configuring-buckets-prerequisites"></a>

要设置和运行此示例，您必须先完成以下任务：
+ 设置项目环境以运行这些 Node TypeScript 示例，并安装所需的 适用于 JavaScript 的 AWS SDK 和第三方模块。请按照 [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/redshift/README.md) 上的说明进行操作。
+ 使用用户凭证创建共享配置文件。有关提供共享凭证文件的更多信息，请参阅《AWS SDK 和工具参考指南》** 中的[共享配置和凭证文件](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)。

**重要**  
这些示例演示了如何使用 ECMAScript6（ES6）导入/导出客户端服务对象和命令。  
这需要使用 Node.js 版本 13.x 或更高版本。要下载并安装最新版本的 Node.js，请参阅 [Node.js 下载](https://nodejs.org/en/download)。
如果您更喜欢使用 CommonJS 语法，请参阅 [JavaScript ES6/CommonJS 语法](sdk-example-javascript-syntax.md)

## 创建 Amazon Redshift 集群
<a name="redshift-create-cluster"></a>

此示例演示如何使用 适用于 JavaScript 的 AWS SDK 创建 Amazon Redshift 集群。有关更多信息，请参阅 [CreateCluster](https://docs.aws.amazon.com/redshift/latest/APIReference/API_CreateCluster)。

**重要**  
*您即将创建的集群将是活跃的（且不在沙盒中运行）。您需要为该集群支付标准 Amazon Redshift 使用费，直到删除它为止。如果您在创建集群的相同位置删除了集群，则产生的总费用其实是很少的。*

创建一个 `libs` 目录，然后使用文件名 `redshiftClient.js` 创建一个 Node.js 模块。将以下代码复制并粘贴到其中，这将创建 Amazon Redshift 客户端对象。将 *REGION* 替换为您的 AWS 区域。

```
import  { RedshiftClient }  from  "@aws-sdk/client-redshift";
// Set the AWS Region.
const REGION = "REGION"; //e.g. "us-east-1"
// Create Redshift service object.
const redshiftClient = new RedshiftClient({ region: REGION });
export { redshiftClient };
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/libs/redshiftClient.js)找到。

创建文件名为 `redshift-create-cluster.js` 的 Node.js 模块。确保如前所示配置 SDK，包括安装所需的客户端和软件包。创建参数对象，指定要预置的节点类型，以及在集群中自动创建的数据库实例的主登录凭证，最后指定集群类型。

**注意**  
将 *CLUSTER\$1NAME* 替换为集群的名称。对于 *NODE\$1TYPE*，请指定要预置的节点类型，例如 “dc2.large”。*MASTER\$1USERNAME* 和 *MASTER\$1USER\$1PASSWORD* 是集群中数据库实例的主用户的登录凭证。对于 *CLUSTER\$1TYPE*，输入集群的类型。如果指定 `single-node`，则不需要 `NumberOfNodes` 参数。其余参数均为可选参数。

```
// Import required AWS SDK clients and commands for Node.js
import { CreateClusterCommand } from "@aws-sdk/client-redshift";
import { redshiftClient } from "./libs/redshiftClient.js";

const params = {
  ClusterIdentifier: "CLUSTER_NAME", // Required
  NodeType: "NODE_TYPE", //Required
  MasterUsername: "MASTER_USER_NAME", // Required - must be lowercase
  MasterUserPassword: "MASTER_USER_PASSWORD", // Required - must contain at least one uppercase letter, and one number
  ClusterType: "CLUSTER_TYPE", // Required
  IAMRoleARN: "IAM_ROLE_ARN", // Optional - the ARN of an IAM role with permissions your cluster needs to access other AWS services on your behalf, such as Amazon S3.
  ClusterSubnetGroupName: "CLUSTER_SUBNET_GROUPNAME", //Optional - the name of a cluster subnet group to be associated with this cluster. Defaults to 'default' if not specified.
  DBName: "DATABASE_NAME", // Optional - defaults to 'dev' if not specified
  Port: "PORT_NUMBER", // Optional - defaults to '5439' if not specified
};

const run = async () => {
  try {
    const data = await redshiftClient.send(new CreateClusterCommand(params));
    console.log(
      `Cluster ${data.Cluster.ClusterIdentifier} successfully created`,
    );
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err);
  }
};
run();
```

要运行示例，请在命令提示符中键入以下内容。

```
node redshift-create-cluster.js  
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/redshift-create-cluster.js)找到。

## 修改 Amazon Redshift 集群
<a name="redshift-modify-cluster"></a>

此示例展示了如何使用 适用于 JavaScript 的 AWS SDK 修改 Amazon Redshift 集群的主用户密码。有关您可以修改的其他设置的更多信息，请参阅 [ModifyCluster](https://docs.aws.amazon.com/redshift/latest/APIReference/API_ModifyCluster.html)。

创建一个 `libs` 目录，然后使用文件名 `redshiftClient.js` 创建一个 Node.js 模块。将以下代码复制并粘贴到其中，这将创建 Amazon Redshift 客户端对象。将 *REGION* 替换为您的 AWS 区域。

```
import  { RedshiftClient }  from  "@aws-sdk/client-redshift";
// Set the AWS Region.
const REGION = "REGION"; //e.g. "us-east-1"
// Create Redshift service object.
const redshiftClient = new RedshiftClient({ region: REGION });
export { redshiftClient };
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/libs/redshiftClient.js)找到。

创建文件名为 `redshift-modify-cluster.js` 的 Node.js 模块。确保如前所示配置 SDK，包括安装所需的客户端和软件包。指定 AWS 区域、要修改的集群名称和新的主用户密码。

**注意**  
将 *CLUSTER\$1NAME* 替换为集群名称，将 *MASTER\$1USER\$1PASSWORD* 替换为新的主用户密码。

```
// 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();
```

要运行示例，请在命令提示符中键入以下内容。

```
node redshift-modify-cluster.js 
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/redshift-modify-cluster.js)找到。

## 查看 Amazon Redshift 集群的详细信息
<a name="redshift-describe-cluster"></a>

此示例说明了如何使用 适用于 JavaScript 的 AWS SDK 查看 Amazon Redshift 集群的详细信息。有关可选内容的更多信息，请参见 [DescribeClusters](https://docs.aws.amazon.com/redshift/latest/APIReference/API_DescribeClusters.html)。

创建一个 `libs` 目录，然后使用文件名 `redshiftClient.js` 创建一个 Node.js 模块。将以下代码复制并粘贴到其中，这将创建 Amazon Redshift 客户端对象。将 *REGION* 替换为您的 AWS 区域。

```
import  { RedshiftClient }  from  "@aws-sdk/client-redshift";
// Set the AWS Region.
const REGION = "REGION"; //e.g. "us-east-1"
// Create Redshift service object.
const redshiftClient = new RedshiftClient({ region: REGION });
export { redshiftClient };
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/libs/redshiftClient.js)找到。

创建文件名为 `redshift-describe-clusters.js` 的 Node.js 模块。确保如前所示配置 SDK，包括安装所需的客户端和软件包。指定 AWS 区域、要修改的集群名称和新的主用户密码。

**注意**  
将 *CLUSTER\$1NAME* 替换为集群的名称。

```
// Import required AWS SDK clients and commands for Node.js
import { DescribeClustersCommand } from "@aws-sdk/client-redshift";
import { redshiftClient } from "./libs/redshiftClient.js";

const params = {
  ClusterIdentifier: "CLUSTER_NAME",
};

const run = async () => {
  try {
    const data = await redshiftClient.send(new DescribeClustersCommand(params));
    console.log("Success", data);
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err);
  }
};
run();
```

要运行示例，请在命令提示符中键入以下内容。

```
node redshift-describe-clusters.js 
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/redshift-describe-clusters.js)找到。

## 删除 Amazon Redshift 集群
<a name="redshift-delete-cluster"></a>

此示例说明了如何使用 适用于 JavaScript 的 AWS SDK 查看 Amazon Redshift 集群的详细信息。有关您可以删除的其他设置的更多信息，请参阅 [DeleteCluster](https://docs.aws.amazon.com/redshift/latest/APIReference/API_DeleteCluster.html)。

创建一个 `libs` 目录，然后使用文件名 `redshiftClient.js` 创建一个 Node.js 模块。将以下代码复制并粘贴到其中，这将创建 Amazon Redshift 客户端对象。将 *REGION* 替换为您的 AWS 区域。

```
import  { RedshiftClient }  from  "@aws-sdk/client-redshift";
// Set the AWS Region.
const REGION = "REGION"; //e.g. "us-east-1"
// Create Redshift service object.
const redshiftClient = new RedshiftClient({ region: REGION });
export { redshiftClient };
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/libs/redshiftClient.js)找到。

创建文件名为 `redshift-delete-clusters.js` 的 Node.js 模块。确保如前所示配置 SDK，包括安装所需的客户端和软件包。指定 AWS 区域、要修改的集群名称和新的主用户密码。指定是否要在删除集群之前保存集群的最终快照，如果是，则指定快照的 ID。

**注意**  
将 *CLUSTER\$1NAME* 替换为集群的名称。对于 *SkipFinalClusterSnapshot*，请指定是否要在删除集群前对其创建最终快照。如果指定“false”，请在 *CLUSTER\$1SNAPSHOT\$1ID* 中指定最终集群快照的 ID。要获取此 ID，请单击**集群**仪表板上集群对应的**快照**列中的链接，然后滚动到**快照**窗格。请注意，词干 `rs:` 不是快照 ID 的一部分。

```
// Import required AWS SDK clients and commands for Node.js
import { DeleteClusterCommand } from "@aws-sdk/client-redshift";
import { redshiftClient } from "./libs/redshiftClient.js";

const params = {
  ClusterIdentifier: "CLUSTER_NAME",
  SkipFinalClusterSnapshot: false,
  FinalClusterSnapshotIdentifier: "CLUSTER_SNAPSHOT_ID",
};

const run = async () => {
  try {
    const data = await redshiftClient.send(new DeleteClusterCommand(params));
    console.log("Success, cluster deleted. ", data);
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err);
  }
};
run();
```

要运行示例，请在命令提示符中键入以下内容。

```
node redshift-delete-cluster.js  
```

此示例代码可在 [GitHub 上的此处](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/redshift/src/redshift-delete-cluster.js)找到。