

 [適用於 JavaScript 的 AWS SDK V3 API 參考指南](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)詳細說明 第 3 版 適用於 JavaScript 的 AWS SDK (V3) 的所有 API 操作。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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

Amazon Redshift 是一種在雲端中完全受管的 PB 級資料倉儲服務。Amazon Redshift 資料倉儲是稱為節點**的運算資源的集合，組織成稱為叢集**的群組。每個叢集皆執行 Amazon Redshift 引擎並包含一或多個資料庫。

![\[JavaScript 環境、軟體開發套件和 Amazon Redshift 之間的關係\]](http://docs.aws.amazon.com/zh_tw/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 模組用於建立、修改、描述 的參數，然後使用下列 `Redshift`用戶端類別方法刪除 Amazon 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 SDKs * [和工具參考指南中的共用組態和登入資料檔案](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>

此範例示範如何使用 建立 Amazon Redshift 叢集 適用於 JavaScript 的 AWS SDK。如需詳細資訊，請參閱 [CreateCluster](https://docs.aws.amazon.com/redshift/latest/APIReference/API_CreateCluster)。

**重要**  
*您即將建立的叢集是即時的 （而不是在沙盒中執行）。您需要支付叢集的標準 Amazon Redshift 使用費，直到刪除叢集為止。如果您在建立叢集時以相同的坐姿刪除叢集，則總費用最少。 *

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`redshiftClient.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 模組。請務必如先前所示設定軟體開發套件，包括安裝所需的用戶端和套件。建立參數物件，指定要佈建的節點類型，以及在叢集中自動建立的資料庫執行個體主登入憑證，最後是叢集類型。

**注意**  
將 *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>

此範例說明如何使用 修改 Amazon Redshift 叢集的主要使用者密碼 適用於 JavaScript 的 AWS SDK。如需您可以修改哪些其他設定的詳細資訊，請參閱 [ModifyCluster](https://docs.aws.amazon.com/redshift/latest/APIReference/API_ModifyCluster.html)。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`redshiftClient.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 模組。請務必如先前所示設定軟體開發套件，包括安裝所需的用戶端和套件。指定 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>

此範例說明如何使用 檢視 Amazon Redshift 叢集的詳細資訊 適用於 JavaScript 的 AWS SDK。如需選用的詳細資訊，請參閱 [DescribeClusters](https://docs.aws.amazon.com/redshift/latest/APIReference/API_DescribeClusters.html)。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`redshiftClient.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 模組。請務必如先前所示設定軟體開發套件，包括安裝所需的用戶端和套件。指定 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>

此範例說明如何使用 檢視 Amazon Redshift 叢集的詳細資訊 適用於 JavaScript 的 AWS SDK。如需您可以修改哪些其他設定的詳細資訊，請參閱 [DeleteCluster](https://docs.aws.amazon.com/redshift/latest/APIReference/API_DeleteCluster.html)。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`redshiftClient.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)範例程式碼。

使用名為 的檔案建立 Node.js 模組`redshift-delete-clusters.js`。請務必如先前所示設定軟體開發套件，包括安裝所需的用戶端和套件。指定 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)找到這個範本程式碼。