本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
您可以使用 AWS Command Line Interface (AWS CLI) 控制多個 AWS 來自命令行的服務,並通過腳本自動化它們。使用 Amazon Keyspaces,您可以使用 AWS CLI 用於資料定義語言 (DDL) 作業,例如建立資料表。此外,您可以使用基礎結構作為代碼(IaC)服務和工具,例如 AWS CloudFormation 和地形。
在您可以使用 AWS CLI 使用 Amazon 密 Keyspaces,您必須獲得訪問密鑰 ID 和秘密訪問密鑰。如需詳細資訊,請參閱建立和設定 Amazon Keyspaces 的 AWS 登入資料。
有關 Amazon 密鑰空間中可用的所有命令的完整列表 AWS CLI,請參閱 AWS CLI 指令參考
下載和配置 AWS CLI
所以此 AWS CLI 可在以下位置取得https://aws.amazon.com/cli
使用 AWS CLI 與 Amazon Keyspaces
命令行格式由 Amazon Keyspaces 操作名稱後跟該操作的參數組成。所以此 AWS CLI 支援參數值的速記語法,以及。JSON以下 Amazon Keyspaces 示例使用 AWS CLI 速記語法。如需詳細資訊,請參閱搭配使用速記語法 AWS CLI.
以下命令創建具有名稱目錄的密鑰空間。
aws keyspaces create-keyspace --keyspace-name 'catalog'
該命令返回資源 Amazon 資源名稱(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/" }
下面的命令創建一個名為 book_ 獎項的表。資料表的資料分割索引鍵是由資料行組成,year
award
而叢集索引鍵則由資料行組成rank
,category
而且兩個叢集資料行都使用遞增排序順序。(為確保易讀性,本節的長命令以分行顯示。)
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的簡單資料表。請注意,這些命令會分成單獨的列,以提高可讀性。這個命令顯示了如何創建一個表,並:
設置表的容量模式
啟用表格的 P oint-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若要執行此範例,您必須取代受管理客戶的資源 ARN AWS KMS 使用您自己的密鑰進行密鑰,並確保 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'