使用 AWS CLI 連線至 Amazon Keyspaces - Amazon Keyspaces (適用於 Apache Cassandra)

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

使用 AWS CLI 連線至 Amazon Keyspaces

您可以使用 AWS Command Line Interface (AWS CLI) 從命令列控制多個 AWS 服務,並透過指令碼自動化。透過 Amazon Keyspaces,您可以使用 AWS CLI 進行資料定義語言 (DDL) 操作,例如建立資料表。此外,您可以使用基礎設施做為程式碼 (IaC) 服務和工具,例如 AWS CloudFormation 和 Terraform。

您必須先取得存取金鑰 ID 和私密存取金鑰,才能 AWS CLI 搭配 Amazon Keyspaces 使用 。如需詳細資訊,請參閱建立和設定 Amazon Keyspaces 的 AWS 登入資料

如需 中可用於 Amazon Keyspaces 的所有命令的完整清單 AWS CLI,請參閱 AWS CLI 命令參考

下載和設定 AWS CLI

AWS CLI 可在 取得https://aws.amazon.com/cli。它可在 Windows、macOS,或 Linux 上執行。下載 之後 AWS CLI,請依照下列步驟安裝和設定它:

AWS CLI 搭配 Amazon Keyspaces 使用

命令列格式包含 Amazon Keyspaces 操作名稱,後面接著該操作的參數。 AWS CLI 支援參數值和 JSON 的速記語法。下列 Amazon Keyspaces 範例使用 AWS CLI 速記語法。如需詳細資訊,請參閱搭配 CLI AWS 使用速記語法

下列命令會使用名稱目錄建立金鑰空間。

aws keyspaces create-keyspace --keyspace-name 'catalog'

命令會在輸出中傳回資源 Amazon Resource Name (ARN)。

{ "resourceArn": "arn:aws:cassandra:us-east-1:111222333444:/keyspace/catalog/" }

若要確認金鑰空間目錄是否存在,您可以使用下列命令。

aws keyspaces get-keyspace --keyspace-name 'catalog'

命令的輸出會傳回下列值。

{ "keyspaceName": "catalog", "resourceArn": "arn:aws:cassandra:us-east-1:111222333444:/keyspace/catalog/" }

下列命令會使用 name book_awards 建立資料表。資料表的分割區索引鍵由資料欄yearaward和 組成,叢集索引鍵由資料欄category和 組成rank,兩個叢集資料欄都使用遞增排序順序。(為確保易讀性,本節的長命令以分行顯示。)

aws keyspaces create-table --keyspace-name 'catalog' --table-name 'book_awards' --schema-definition 'allColumns=[{name=year,type=int},{name=award,type=text},{name=rank,type=int}, {name=category,type=text}, {name=author,type=text},{name=book_title,type=text},{name=publisher,type=text}], partitionKeys=[{name=year},{name=award}],clusteringKeys=[{name=category,orderBy=ASC},{name=rank,orderBy=ASC}]'

此命令會產生下列輸出。

{ "resourceArn": "arn:aws:cassandra:us-east-1:111222333444:/keyspace/catalog/table/book_awards" }

若要確認資料表的中繼資料和屬性,您可以使用下列命令。

aws keyspaces get-table --keyspace-name 'catalog' --table-name 'book_awards'

此命令會傳回下列輸出。

{ "keyspaceName": "catalog", "tableName": "book_awards", "resourceArn": "arn:aws:cassandra:us-east-1:111222333444:/keyspace/catalog/table/book_awards", "creationTimestamp": 1645564368.628, "status": "ACTIVE", "schemaDefinition": { "allColumns": [ { "name": "year", "type": "int" }, { "name": "award", "type": "text" }, { "name": "category", "type": "text" }, { "name": "rank", "type": "int" }, { "name": "author", "type": "text" }, { "name": "book_title", "type": "text" }, { "name": "publisher", "type": "text" } ], "partitionKeys": [ { "name": "year" }, { "name": "award" } ], "clusteringKeys": [ { "name": "category", "orderBy": "ASC" }, { "name": "rank", "orderBy": "ASC" } ], "staticColumns": [] }, "capacitySpecification": { "throughputMode": "PAY_PER_REQUEST", "lastUpdateToPayPerRequestTimestamp": 1645564368.628 }, "encryptionSpecification": { "type": "AWS_OWNED_KMS_KEY" }, "pointInTimeRecovery": { "status": "DISABLED" }, "ttl": { "status": "ENABLED" }, "defaultTimeToLive": 0, "comment": { "message": "" } }

建立具有複雜結構描述的資料表時,從 JSON 檔案載入資料表的結構描述定義會很有幫助。以下是範例。從 schema_definition.zip 下載結構描述定義範例 JSON 檔案,並擷取 schema_definition.json,並記下檔案的路徑。在此範例中,結構描述定義 JSON 檔案位於目前的目錄中。如需不同的檔案路徑選項,請參閱如何從檔案載入參數

aws keyspaces create-table --keyspace-name 'catalog' --table-name 'book_awards' --schema-definition 'file://schema_definition.json'

下列範例示範如何建立名為 myTable 的簡單資料表,以及其他選項。請注意,命令會分成不同的資料列,以改善可讀性。此命令說明如何建立資料表,並:

  • 設定資料表的容量模式

  • 啟用資料表的Point-in-time復原

  • 將資料表的預設存留時間 (TTL) 值設為一年

  • 為資料表新增兩個標籤

aws keyspaces create-table --keyspace-name 'catalog' --table-name 'myTable' --schema-definition 'allColumns=[{name=id,type=int},{name=name,type=text},{name=date,type=timestamp}],partitionKeys=[{name=id}]' --capacity-specification 'throughputMode=PROVISIONED,readCapacityUnits=5,writeCapacityUnits=5' --point-in-time-recovery 'status=ENABLED' --default-time-to-live '31536000' --tags 'key=env,value=test' 'key=dpt,value=sec'

此範例示範如何建立新的資料表,該資料表使用客戶受管金鑰進行加密,並啟用 TTL 以允許您設定資料欄和資料列的過期日期。若要執行此範例,您必須將客戶受管 AWS KMS 金鑰的資源 ARN 取代為您自己的金鑰,並確保 Amazon Keyspaces 可存取該金鑰。

aws keyspaces create-table --keyspace-name 'catalog' --table-name 'myTable' --schema-definition 'allColumns=[{name=id,type=int},{name=name,type=text},{name=date,type=timestamp}],partitionKeys=[{name=id}]' --encryption-specification 'type=CUSTOMER_MANAGED_KMS_KEY,kmsKeyIdentifier=arn:aws:kms:us-east-1:111222333444:key/11111111-2222-3333-4444-555555555555' --ttl 'status=ENABLED'