110人参与 • 2025-02-14 • 云虚拟主机
您需要在计算机上安装 docker desktop for mac、docker desktop for windows 或类似软件。
apache cassandra 也以 tarball 或软件包的形式提供 下载。
docker pull cassandra:latest
docker 网络允许我们访问容器的端口,而无需在主机上公开它们。
docker network create cassandra docker run --rm -d --name cassandra --hostname cassandra --network cassandra cassandra
cassandra 查询语言 (cql) 与 sql 非常相似,但适合 cassandra 的无连接结构。
创建一个名为 data.cql 的文件,并将以下 cql 脚本粘贴到其中。
此脚本将创建一个 keyspace(cassandra 在其中复制数据的层)、一个用于保存数据的表,并将一些数据插入该表
-- create a keyspace create keyspace if not exists store with replication = { 'class' : 'simplestrategy', 'replication_factor' : '1' }; -- create a table create table if not exists store.shopping_cart ( userid text primary key, item_count int, last_update_timestamp timestamp ); -- insert some data insert into store.shopping_cart (userid, item_count, last_update_timestamp) values ('9876', 2, totimestamp(now())); insert into store.shopping_cart (userid, item_count, last_update_timestamp) values ('1234', 5, totimestamp(now()));
cql shell 或 cqlsh 是与数据库交互的一种工具。
我们将使用它来使用您刚刚保存的脚本将一些数据加载到数据库中。
docker run --rm --network cassandra -v "$(pwd)/data.cql:/scripts/data.cql" -e cqlsh_host=cassandra -e cqlsh_port=9042 -e cqlversion=3.4.6 nuvo/docker-cqlsh
注意:cassandra 服务器本身(您运行的第一个 docker run 命令)需要几秒钟才能启动。
如果服务器尚未完成其初始化序列,则上述命令将抛出错误,因此请等待几秒钟以使其启动。
与 sql shell 类似,您也可以使用 cqlsh 交互式地运行 cql 命令。
docker run --rm -it --network cassandra nuvo/docker-cqlsh cqlsh cassandra 9042 --cqlversion='3.4.5'
这应该会为您提供如下提示
connected to test cluster at cassandra:9042. [cqlsh 5.0.1 | cassandra 4.0.4 | cql spec 3.4.5 | native protocol v5] use help for help. cqlsh>
select * from store.shopping_cart;
insert into store.shopping_cart (userid, item_count) values ('4567', 20);
docker kill cassandra docker network rm cassandra
links:
apache cassandra | apache cassandra 文档 中文
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论