本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
將數據插入並將其加載到 Amazon Keyspaces 表中
若要在資料book_awards
表中建立資料,請使用INSERT
陳述式新增單一資料列。
開啟 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.
您必須先將目前 cqlsh 工作階段的寫入一致性設定為,才能使用 cqlsh 將資料寫入 Amazon Keyspaces 資料表。
LOCAL_QUORUM
如需有關支援一致性層級的詳細資訊,請參閱寫入一致性層級。請注意,如果您使用中的CQL編輯器,則不需要執行此步驟 AWS Management Console.CONSISTENCY LOCAL_QUORUM;
-
若要插入單一記錄,請在CQL編輯器中執行下列命令。
INSERT INTO catalog.book_awards (award, year, category, rank, author, book_title, publisher) VALUES ('Wolf', 2023, 'Fiction',3,'Shirley Rodriguez','Mountain', 'AnyPublisher') ;
-
執行下列命令,確認資料已正確新增至資料表。
SELECT * FROM catalog.book_awards ;
陳述式的輸出應該是這樣的。
year | award | category | rank | author | book_title | publisher ------+-------+----------+------+-------------------+------------+-------------- 2023 | Wolf | Fiction | 3 | Shirley Rodriguez | Mountain | AnyPublisher (1 rows)
若要使用 cqlsh 從檔案插入多筆記錄
-
下載封存CSV檔案 samplemigration.zip 中包含的範例檔案 (
keyspaces_sample_table.csv
)。解壓縮封存並記下的路徑。keyspaces_sample_table.csv
-
開啟 AWS CloudShell 在 AWS Management Console 並使用以下命令連接到 Amazon Keyspaces。確保更新
us-east-1
與您自己的區域。cqlsh-expansion cassandra.
us-east-1
.amazonaws.com 9142 --ssl -
在
cqlsh
提示符(cqlsh>
)下,指定密鑰空間。USE
catalog
; 將寫入一致性設定為
LOCAL_QUORUM
。如需有關支援一致性層級的詳細資訊,請參閱寫入一致性層級。CONSISTENCY LOCAL_QUORUM;
在 AWS CloudShell 選擇屏幕右上角的「操作」,然後選擇「上傳文件」以上傳之前下載的 csv 文件。記下檔案的路徑。
-
在密鑰空間提示符(
cqlsh:
),運行下面的語句。catalog
>COPY book_awards (award, year, category, rank, author, book_title, publisher) FROM '/home/cloudshell-user/keyspaces_sample_table.csv' WITH header=TRUE ;
陳述式的輸出看起來應該類似於這個。
cqlsh:catalog> COPY book_awards (award, year, category, rank, author, book_title, publisher) FROM '/home/cloudshell-user/keyspaces_sample_table.csv' WITH delimiter=',' AND header=TRUE ; cqlsh current consistency level is LOCAL_QUORUM. Reading options from /home/cloudshell-user/.cassandra/cqlshrc:[copy]: {'numprocesses': '16', 'maxattempts': '1000'} Reading options from /home/cloudshell-user/.cassandra/cqlshrc:[copy-from]: {'ingestrate': '1500', 'maxparseerrors': '1000', 'maxinserterrors': '-1', 'maxbatchsize': '10', 'minbatchsize': '1', 'chunksize': '30'} Reading options from the command line: {'delimiter': ',', 'header': 'TRUE'} Using 16 child processes Starting copy of catalog.book_awards with columns [award, year, category, rank, author, book_title, publisher]. OSError: handle is closed 0 rows/s; Avg. rate: 0 rows/s Processed: 9 rows; Rate: 0 rows/s; Avg. rate: 0 rows/s 9 rows imported from 1 files in 0 day, 0 hour, 0 minute, and 26.706 seconds (0 skipped).
-
執行下列查詢,確認資料已正確新增至資料表。
SELECT * FROM book_awards ;
您應該會看到下列輸出。
year | award | category | rank | author | book_title | publisher ------+------------------+-------------+------+--------------------+-----------------------+--------------- 2020 | Wolf | Non-Fiction | 1 | Wang Xiulan | History of Ideas | Example Books 2020 | Wolf | Non-Fiction | 2 | Ana Carolina Silva | Science Today | SomePublisher 2020 | Wolf | Non-Fiction | 3 | Shirley Rodriguez | The Future of Sea Ice | AnyPublisher 2020 | Kwesi Manu Prize | Fiction | 1 | Akua Mansa | Where did you go? | SomePublisher 2020 | Kwesi Manu Prize | Fiction | 2 | John Stiles | Yesterday | Example Books 2020 | Kwesi Manu Prize | Fiction | 3 | Nikki Wolf | Moving to the Chateau | AnyPublisher 2020 | Richard Roe | Fiction | 1 | Alejandro Rosalez | Long Summer | SomePublisher 2020 | Richard Roe | Fiction | 2 | Arnav Desai | The Key | Example Books 2020 | Richard Roe | Fiction | 3 | Mateo Jackson | Inside the Whale | AnyPublisher (9 rows)
若要進一步了解如cqlsh COPY
何使用將 csv 檔案中的資料上傳至 Amazon Keyspaces 表格,請參閱教學課程:使用 cqlsh 將資料載入 Amazon Keyspaces。