Apache Cassandra:开源事务型分布式数据库
英文标题:🔥 apache/cassandra
Apache Cassandra 是一款高度可扩展的分区式行存储数据库,能够在普通硬件或云基础设施上提供线性扩展能力和经过验证的容错性。它通过分区机制将数据以对应用透明的方式分布到多台机器,并能在集群节点增减时自动重新分区。Cassandra 使用与 SQL 接近的 Cassandra Query Language(CQL),但不支持连接和子查询,同时提供集合类型等能力。对开发者而言,本文展示了如何启动单节点集群,并通过 cqlsh 创建键空间、建表、写入和查询数据。
64.0 KB
中文
Apache Cassandra 是一款高度可扩展的分区式行存储数据库。数据行被组织在表中,并且必须定义主键。
分区意味着 Cassandra 能够以对应用透明的方式,将数据分布到多台机器上。随着机器被添加到集群或从集群移除,Cassandra 会自动重新分区。
行存储意味着 Cassandra 与关系型数据库一样,按行和列组织数据。Cassandra Query Language(CQL)与 SQL 密切相关。
-
Java:请在 build.xml 中查看支持的版本(搜索属性
java.supported)。 -
Python:对于
cqlsh,请查看bin/cqlsh(搜索函数is_supported_version)。
本简要指南将带你启动并运行一个基础的单节点集群,并演示一些简单的读写操作。如需更完整的指南,请参阅 Apache Cassandra 网站的入门指南。
首先,解压归档文件:
$ tar -zxvf apache-cassandra-$VERSION.tar.gz
$ cd apache-cassandra-$VERSION
之后启动服务器。使用 -f 参数运行启动脚本会让 Cassandra 保持在前台运行,并将日志输出到标准输出;可通过 ctrl-C 停止。
现在,让我们尝试使用 Cassandra Query Language 读写一些数据:
命令行客户端是交互式的,因此如果一切正常,你应该会看到如下提示符:
Connected to Test Cluster at localhost:9160.
[cqlsh 6.3.0 | Cassandra 7.0-SNAPSHOT | CQL spec 3.4.8 | Native protocol v5]
Use HELP for help.
cqlsh>
如欢迎信息所示,你可以使用 help; 或 ? 查看 CQL 提供的功能;操作完成后,可使用 quit; 或 exit; 退出。不过,让我们尝试一些更有意思的操作:
cqlsh> CREATE KEYSPACE schema1
WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
cqlsh> USE schema1;
cqlsh:Schema1> CREATE TABLE users (
user_id varchar PRIMARY KEY,
first varchar,
last varchar,
age int
);
cqlsh:Schema1> INSERT INTO users (user_id, first, last, age)
VALUES ('jsmith', 'John', 'Smith', 42);
cqlsh:Schema1> SELECT * FROM users;
user_id | age | first | last
---------+-----+-------+-------
jsmith | 42 | john | smith
cqlsh:Schema1>
如果你的会话与上面的内容类似,恭喜,你的单节点集群已经正常运行!
如需了解 CQL 支持哪些命令,请参阅 CQL 参考文档。可以将它理解为:“SQL 减去连接和子查询,再加上集合。”
接下来该做什么?
English / 原文
Repository files navigation
Apache Cassandra is a highly-scalable partitioned row store. Rows are organized into tables with a required primary key.
Partitioning means that Cassandra can distribute your data across multiple machines in an application-transparent matter. Cassandra will automatically repartition as machines are added and removed from the cluster.
Row store means that like relational databases, Cassandra organizes data by rows and columns. The Cassandra Query Language (CQL) is a close relative of SQL.
-
Java: see supported versions in build.xml (search for property "java.supported").
-
Python: for
cqlsh, seebin/cqlsh(search for function "is_supported_version").
This short guide will walk you through getting a basic one node cluster up and running, and demonstrate some simple reads and writes. For a more-complete guide, please see the Apache Cassandra website’s Getting Started Guide.
First, we’ll unpack our archive:
$ tar -zxvf apache-cassandra-$VERSION.tar.gz
$ cd apache-cassandra-$VERSION
After that we start the server. Running the startup script with the -f argument will cause Cassandra to remain in the foreground and log to standard out; it can be stopped with ctrl-C.
Now let’s try to read and write some data using the Cassandra Query Language:
The command line client is interactive so if everything worked you should be sitting in front of a prompt:
Connected to Test Cluster at localhost:9160.
[cqlsh 6.3.0 | Cassandra 7.0-SNAPSHOT | CQL spec 3.4.8 | Native protocol v5]
Use HELP for help.
cqlsh>
As the banner says, you can use 'help;' or '?' to see what CQL has to offer, and 'quit;' or 'exit;' when you’ve had enough fun. But lets try something slightly more interesting:
cqlsh> CREATE KEYSPACE schema1
WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
cqlsh> USE schema1;
cqlsh:Schema1> CREATE TABLE users (
user_id varchar PRIMARY KEY,
first varchar,
last varchar,
age int
);
cqlsh:Schema1> INSERT INTO users (user_id, first, last, age)
VALUES ('jsmith', 'John', 'Smith', 42);
cqlsh:Schema1> SELECT * FROM users;
user_id | age | first | last
---------+-----+-------+-------
jsmith | 42 | john | smith
cqlsh:Schema1>
If your session looks similar to what’s above, congrats, your single node cluster is operational!
For more on what commands are supported by CQL, see the CQL reference. A reasonable way to think of it is as, "SQL minus joins and subqueries, plus collections."
Wondering where to go from here?
You can’t perform that action at this time.