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

 

最新文章

  1. python学习之函数
  2. AMD加载器实现笔记(一)
  3. linux下查看某软件是否已安装, ubuntu安装deb包
  4. JavaScript笔记:数据类型
  5. 查看某个线程占得CPU高
  6. MemSQL start[c]up Round 2 - online version C. More Reclamation(博弈)
  7. Swift可空(Optional)类型基础
  8. Laravel 5.3 中文文档翻译完成
  9. POJ 1275 Cashier Employment(差分约束)
  10. String 和 string 的区别
  11. C语言初学 俩数相除问题
  12. Hibernate 框架基本知识
  13. JAVA课程设计---学生基本信息管理系统(201521123039 王兴)
  14. struts2中struts.xml配置文件详解
  15. arcgis api for js入门开发系列二十打印地图的那些事
  16. python中的转义字符
  17. js 计算浮点数
  18. 【CSS】环形进度条
  19. 工具篇-Json处理
  20. python中join()函数的使用方法

热门文章

  1. COM 组件创建实例失败,原因是出现以下错误: c001f011 (Microsoft.SqlServer.ManagedDTS)
  2. web.xml文件配置说明
  3. (扫盲)DTO数据传输对象
  4. python常用模块——random模块
  5. 【转】Git介绍
  6. Sourse Insight使用教程及常见的问题解决办法
  7. PHPExcel读写封装
  8. Linux Shell基础 read命令
  9. 【HackerRank】Sherlock and MiniMax
  10. Android系统源代码的下载与编译