在 Amazon Keyspaces 中創建一個表 - Amazon Keyspaces (適用於 Apache Cassandra)

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

在 Amazon Keyspaces 中創建一個表

在本節中,您會使用主控台cqlsh、或 AWS CLI.

表格是您資料的組織和儲存位置。資料表的主索引鍵會決定資料表中資料分割的方式。主索引鍵是由必要的分割索引鍵和一或多個選擇性叢集資料行組成。構成主鍵的組合值在所有表的數據中必須是唯一的。如需表格的相關資訊,請參閱下列主題:

當您建立資料表時,請指定下列項目:

  • 資料表的名稱。

  • 資料表中每個資料行的名稱和資料類型。

  • 資料表的主索引鍵。

    • 分割區索引鍵 — 必要

    • 叢集資料欄 — 選用

使用下列程序來建立含有指定資料行、資料類型、分割索引鍵和叢集資料行的資料表。

下列程序會book_awards使用這些資料行和資料類型建立資料表。

year int award text rank int category text book_title text author text publisher text
若要使用主控台建立資料表
  1. 登入 AWS Management Console,然後在https://console.aws.amazon.com/keyspaces/家中打開 Amazon Keyspaces 控制台。

  2. 在導覽窗格中,選擇 [Keyspaces]。

  3. 選擇catalog作為您要在其中創建此表的密鑰空間。

  4. 選擇 建立資料表

  5. 在 [表格名稱] 方塊中,輸入book_awards做為表格的名稱。

    名稱限制:

    • 名稱不能為空。

    • 允許的字元:英數字元和底線 (_)。

    • 長度上限為 48 個字元。

  6. 在「」段落中,針對要新增至此表格的每個資料欄重複下列步驟。

    新增下列欄和資料類型。

    year int award text rank int category text book_title text author text publisher text
    1. 名稱 — 輸入欄的名稱。

      名稱限制:

      • 名稱不能為空。

      • 允許的字元:英數字元和底線 (_)。

      • 長度上限為 48 個字元。

    2. — 在資料類型清單中,選擇此欄的資料類型。

    3. 若要新增其他欄,請選擇 [新增欄]。

  7. 選擇awardyear作為分區鍵下的分區鍵。每個表都需要一個分區鍵。磁碟分割索引鍵可以由一或多個資料行組成。

  8. 新增categoryrank做為叢集資料行。叢集資料行是選擇性的,可決定每個分割區內的排序順序。

    1. 若要新增叢集資料行,請選擇 [新增叢集資料行]。

    2. 在「」清單中,選擇「品類」。在「序」清單中,選擇ASC以遞增順序排序此欄中的值。(選擇DESC降序排列。)

    3. 然後選擇添加聚類列並選擇排名

  9. [表格設定] 區段中,選擇 [預設設定]

  10. 選擇 建立資料表

  11. 確認您的資料表是否已建立。

    1. 在導覽窗格中,選擇 Tables (資料表)。

    2. 確認您的表格位於表格清單中。

    3. 選擇表格的名稱。

    4. 確認所有資料欄和資料類型都正確無誤。

      注意

      欄的列示順序可能與您新增至表格的順序不一樣。

此程序會使用建立包含下列欄和資料類型的資料表CQL。year和資award料行是具有categoryrank作為叢集資料行的分割索引鍵,它們共同構成資料表的主索引鍵。

year int award text rank int category text book_title text author text publisher text
若要使用建立表格 CQL
  1. 開啟 AWS CloudShell 並使用以下命令連接到 Amazon Keyspaces。確保更新 us-east-1 與您自己的區域。

    cqlsh-expansion cassandra.us-east-1.amazonaws.com 9142 --ssl

    該命令的輸出應該是這樣的。

    Connected to Amazon Keyspaces at cassandra.us-east-1.amazonaws.com:9142 [cqlsh 6.1.0 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4] Use HELP for help. cqlsh current consistency level is ONE.
  2. 在 keyspace 提示符(cqlsh:keyspace_name>)中,通過在命令窗口中輸入以下代碼來創建表格。

    CREATE TABLE catalog.book_awards ( year int, award text, rank int, category text, book_title text, author text, publisher text, PRIMARY KEY ((year, award), category, rank) );
    注意

    ASC是預設的叢集順序。您也可以指DESC定遞減順序。

    請注意,yearaward是分區索引鍵資料行。然後,categoryrank按升序(ASC)排序的聚類列。這些欄共同構成表格的主索引鍵。

  3. 確認您的資料表是否已建立。

    SELECT * from system_schema.tables WHERE keyspace_name='catalog.book_awards' ;

    輸出看起來應該類似於這個。

    keyspace_name | table_name | bloom_filter_fp_chance | caching | cdc | comment | compaction | compression | crc_check_chance | dclocal_read_repair_chance | default_time_to_live | extensions | flags | gc_grace_seconds | id | max_index_interval | memtable_flush_period_in_ms | min_index_interval | read_repair_chance | speculative_retry ---------------+------------+------------------------+---------+-----+---------+------------+-------------+------------------+----------------------------+----------------------+------------+-------+------------------+----+--------------------+-----------------------------+--------------------+--------------------+------------------- (0 rows)>
  4. 驗證表格的結構。

    SELECT * FROM system_schema.columns WHERE keyspace_name = 'catalog' AND table_name = 'book_awards';

    這個陳述式的輸出看起來應該類似於這個範例。

    keyspace_name | table_name | column_name | clustering_order | column_name_bytes | kind | position | type ---------------+-------------+-------------+------------------+------------------------+---------------+----------+------ catalog | book_awards | year | none | 0x79656172 | partition_key | 0 | int catalog | book_awards | award | none | 0x6177617264 | partition_key | 1 | text catalog | book_awards | category | asc | 0x63617465676f7279 | clustering | 0 | text catalog | book_awards | rank | asc | 0x72616e6b | clustering | 1 | int catalog | book_awards | author | none | 0x617574686f72 | regular | -1 | text catalog | book_awards | book_title | none | 0x626f6f6b5f7469746c65 | regular | -1 | text catalog | book_awards | publisher | none | 0x7075626c6973686572 | regular | -1 | text (7 rows)

    確認所有資料行和資料類型都如預期般。列的順序可能與CREATE語句中的不同。

此程序會使用建立具有下列欄和資料類型的資料表 AWS CLI。 year和資award料行會rank以及叢集資料行category組成分割索引鍵。

year int award text rank int category text book_title text author text publisher text
若要使用建立資料表 AWS CLI

下面的命令創建一個名為 book_ 獎獎表。資料表的資料分割索引鍵是由資料行組成,yearaward而叢集索引鍵則由資料行組成rankcategory而且兩個叢集資料行都使用遞增排序順序。(為了更容易閱讀,本節中schema-definition的表創建命令被分成單獨的行。)

  1. 您可以使用下面的語句創建表。

    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" }
  2. 要確認表的元數據和屬性,可以使用以下命令。

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

    此命令會傳回下列輸出。

    { "keyspaceName": "catalog", "tableName": "book_awards", "resourceArn": "arn:aws:cassandra:us-east-1:123SAMPLE012:/keyspace/catalog/table/book_awards", "creationTimestamp": "2024-07-11T15:12:55.571000+00:00", "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": "2024-07-11T15:12:55.571000+00:00" }, "encryptionSpecification": { "type": "AWS_OWNED_KMS_KEY" }, "pointInTimeRecovery": { "status": "DISABLED" }, "defaultTimeToLive": 0, "comment": { "message": "" }, "replicaSpecifications": [] }

若要對表格中的資料執行 CRUD (建立、讀取、更新和刪除) 作業,請繼續執行創建,讀取,更新和刪除數據(CRUD)使用 CQL Amazon Keyspaces