cqlsh [hostname] [port] [username] [password]
例如:
cqlsh localhost 9042
help
CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};
USE mykeyspace;
CREATE TABLE users (
id uuid PRIMARY KEY,
username text,
email text,
created_at timestamp
);
INSERT INTO users (id, username, email, created_at) VALUES (uuid(), 'user1', '[email protected]', toTimestamp(now()));
SELECT * FROM users WHERE id = 6b5e7cfc-8a12-11ec-bf63-0242ac130003;
UPDATE users SET email = '[email protected]' WHERE id = 6b5e7cfc-8a12-11ec-bf63-0242ac130003;
DELETE FROM users WHERE id = 6b5e7cfc-8a12-11ec-bf63-0242ac130003;
DESCRIBE keyspaces;
DESCRIBE TABLE users;
ALTER TABLE users ADD last_login_time timestamp;
DROP TABLE users;
DROP KEYSPACE mykeyspace;
CREATE USER user1 WITH PASSWORD 'password';
GRANT SELECT ON ALL KEYSPACES TO user1;
CREATE INDEX ON users (username);
BEGIN BATCH
INSERT INTO ...;
UPDATE ...;
APPLY BATCH;
INSERT INTO logs (id, message) VALUES (uuid(), 'log message') USING TTL 3600;
SELECT * FROM users WHERE username = 'user1' AND email LIKE '%example.com';
SELECT * FROM users LIMIT 10 OFFSET 20;
SELECT COUNT(*) FROM users;
或者按字段进行计数:
SELECT COUNT(username) FROM users WHERE created_at > '2020-01-01';
DESCRIBE INDEXES ON users;
DROP INDEX users_by_username;
LIST ALL PERMISSIONS OF user1;
REVOKE SELECT ON KEYSPACE mykeyspace FROM user1;
CREATE ROLE admin WITH LOGIN = true AND SUPERUSER = true;
GRANT ALL PERMISSIONS ON ALL KEYSPACES TO admin;
ALTER USER user1 WITH PASSWORD 'new_password';
ALTER USER user1 WITH PASSWORD 'password' AND OPTIONS = {'login': 'false'};
在Cassandra中,可以为每个查询设置一致性级别以控制读取和写入的保证程度。例如:
CONSISTENCY LEVEL ONE; -- 设置当前会话的一致性级别为ONE(至少一个副本响应)
INSERT INTO users (...) VALUES (...) IF NOT EXISTS; -- 使用当前会话的一致性级别执行插入
CREATE TYPE address (
street text,
city text,
zip_code int,
country text
);
CREATE TABLE users (
id uuid PRIMARY KEY,
name text,
addresses map>,
contact_info frozen
);
CREATE FUNCTION concat_ws(separator text, values set)
RETURNS text LANGUAGE java AS '...'; -- 实现函数逻辑的Java代码
SELECT key, concat_ws(',', tokens) FROM mytable;
CREATE AGGREGATE average(int)
SFUNC avg_update STYPE tuple INITCOND (0,0);
SELECT key, average(value) FROM mytable GROUP BY key;
CREATE MATERIALIZED VIEW user_by_email AS
SELECT * FROM users
WHERE email IS NOT NULL AND id IS NOT NULL
PRIMARY KEY (email, id);
虽然CQL本身不直接支持备份和恢复,但可以通过驱动程序或工具(如nodetool snapshot
)执行这些操作。
nodetool snapshot -t backup_keyspace keyspace_name
nodetool clearsnapshot [snapshot-name]
请注意,在实际操作中,可能需要结合其他运维命令、工具及策略来实现更复杂的数据管理任务。对于Cassandra的所有命令以及其具体用法,请查阅官方文档以获取详细信息和最新指南。
python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)
50个开发必备的Python经典脚本(11-20)
50个开发必备的Python经典脚本(21-30)
50个开发必备的Python经典脚本(31-40)
50个开发必备的Python经典脚本(41-50)
————————————————
最后我们放松一下眼睛