本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
教程:在 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 将数据写入 Amazon Keyspaces 表之前,您必须将当前 cqlsh 会话的写入一致性设置为
LOCAL_QUORUM
。有关支持的一致性级别的更多信息,请参阅写入一致性级别。请注意,如果您在 AWS Management Console中使用 CQL 编辑器,则不需要执行此步骤。CONSISTENCY LOCAL_QUORUM;
-
要插入单个记录,请在 CQL 编辑器中运行以下命令。
INSERT INTO catalog.book_awards (award, year, category, rank, author, book_title, publisher) VALUES ('Wolf', 2023, 'Fiction',3,'Helga Schneider','Mountain', 'AnyPublisher') ;
-
通过运行以下命令,验证数据是否已正确添加到表中。
SELECT * FROM catalog.book_awards ;
语句的输出应如下所示。
year | award | category | rank | author | book_title | publisher ------+-------+----------+------+-----------------+------------+-------------- 2023 | Wolf | Fiction | 3 | Helga Schneider | Mountain | AnyPublisher (1 rows)
使用 cqlsh 从文件插入多个记录
-
下载存档文件 samplemigration.zip 中包含的示例 CSV 文件 (
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 文件。记下文件的路径。
-
在 keyspace 提示符 (
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)
要详细了解如何使用将 csv 文件中的数据上传cqlsh COPY
到 Amazon Keyspaces 表,请参阅。教程:使用 cqlsh 将数据加载到 Amazon Keyspaces