cassandra cqlsh 和 python客户端
Keyspaces
A cluster is a container for keyspaces. A keyspace is the outermost container for data in Cassandra, corresponding closely to a schema in a relational database. The keyspace can include operational elements, such as replication factor and data center awareness. Let's create a keyspace:
-- create a keyspace 'my_keyspace'
create keyspace my_keyspace with replication={'class':'SimpleStrategy', 'replication_factor':1};
-- use the keyspace
use my_keyspace;
-- drop the keyspace
drop keyspace my_keyspace;
Creating a table
To create a table type the following in cqlsh, note that you must first create a keyspace and then use that keyspace:
CREATE TABLE users (
firstname text,
lastname text,
age int,
email text,
city text,
PRIMARY KEY (lastname)
);
Describe a table
To see detail information about a table type:
DESCRIBE TABLE users;
Insert records
To insert records in the table type:
INSERT INTO users (firstname, lastname, age, email, city) VALUES ('John', 'Smith', 46, 'johnsmith@email.com', 'Sacramento');
INSERT INTO users (firstname, lastname, age, email, city) VALUES ('Jane', 'Doe', 36, 'janedoe@email.com', 'Beverly Hills');
INSERT INTO users (firstname, lastname, age, email, city) VALUES ('Rob', 'Byrne', 24, 'robbyrne@email.com', 'San Diego');
Querying a table
To query a table type the following:
SELECT * FROM users;
lastname | age | city | email | firstname
----------+-----+---------------+---------------------+-----------
Doe | 36 | Beverly Hills | janedoe@email.com | Jane
Byrne | 24 | San Diego | robbyrne@email.com | Rob
Smith | 46 | Sacramento | johnsmith@email.com | John
(3 rows)
We can filter the result by using a predicate:
SELECT * FROM users WHERE lastname= 'Doe';
lastname | age | city | email | firstname
----------+-----+---------------+-------------------+-----------
Doe | 36 | Beverly Hills | janedoe@email.com | Jane
(1 rows)
Updating records
To update a record in a table type the following:
UPDATE users SET city= 'San Jose' WHERE lastname= 'Doe';
The update should be available almost instantly (remember that cassandra is eventually consistent):
SELECT * FROM users where lastname= 'Doe';
lastname | age | city | email | firstname
----------+-----+----------+-------------------+-----------
Doe | 36 | San Jose | janedoe@email.com | Jane
(1 rows)
Deleting records
To delete a record type:
DELETE from users WHERE lastname = 'Doe';
which should result in:
SELECT * FROM users where lastname= 'Doe';
lastname | age | city | email | firstname
----------+-----+------+-------+-----------
(0 rows)
SELECT * from users;
lastname | age | city | email | firstname
----------+-----+------------+---------------------+-----------
Byrne | 24 | San Diego | robbyrne@email.com | Rob
Smith | 46 | Sacramento | johnsmith@email.com | John
(2 rows)
python cassandra客户端操作:
from cassandra.cluster import Cluster
cluster = Cluster(["10.178.204.225"])
session = cluster.connect('my_keyspace')
session.execute("""
insert into users (lastname, age, city, email, firstname) values ('Jones', 35, 'Austin', 'bob@example.com', 'Bob')
""")
result = session.execute("select * from users where lastname='Jones' ")[0]
print result.firstname, result.age session.execute("update users set age = 36 where lastname = 'Jones'")
result = session.execute("select * from users where lastname='Jones' ")[0]
print result.firstname, result.age session.execute("delete from users where lastname = 'Jones'")
result = session.execute("select * from users")
for x in result:
print x.age
参考:
https://academy.datastax.com/resources/getting-started-apache-cassandra-and-python-part-i?unit=getting-started-apache-cassandra-and-python-part-i
https://github.com/dnvriend/apache-cassandra-test/blob/master/readme.md
最新文章
- python学习之函数
- AMD加载器实现笔记(一)
- linux下查看某软件是否已安装, ubuntu安装deb包
- JavaScript笔记:数据类型
- 查看某个线程占得CPU高
- MemSQL start[c]up Round 2 - online version C. More Reclamation(博弈)
- Swift可空(Optional)类型基础
- Laravel 5.3 中文文档翻译完成
- POJ 1275 Cashier Employment(差分约束)
- String 和 string 的区别
- C语言初学 俩数相除问题
- Hibernate 框架基本知识
- JAVA课程设计---学生基本信息管理系统(201521123039 王兴)
- struts2中struts.xml配置文件详解
- arcgis api for js入门开发系列二十打印地图的那些事
- python中的转义字符
- js 计算浮点数
- 【CSS】环形进度条
- 工具篇-Json处理
- python中join()函数的使用方法