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

 

cassandra cqlsh 和 python客户端的更多相关文章

  1. Redis的Python客户端redis-py的初步使用

    1. Redis的安装 sudo pip install redis sudo pip install hiredis Parser可以控制如何解析redis响应的内容.redis-py包含两个Par ...

  2. thrift例子:python客户端/java服务端

    java服务端的代码请看上文. 1.说明: 这两篇文章其实解决的问题是,当使用python去访问大数据线上集群的时候,遇到两个问题: 1)python-hadoop和python-hive相关包链接不 ...

  3. zookeeper的python客户端安装

    项目中使用了python,需要使用到zookeeper的功能,这里记录一下安装过程. 内核版本:2.6.32 发行版:CentOs-6.6 64bit 1.由于python客户端依赖c的客户端所以要先 ...

  4. 配置百度云盘python客户端bypy上传备份文件

    要求:安装python2.7,安装git 1.git clone https://github.com/houtianze/bypy.git 2.cd bypy 3.sudo python setup ...

  5. Cassandra 数据库, python cqlsh命令

    ★  cql操作数据库(cqlsh.bat: python cqlsh命令操作<优缺点:https://blog.csdn.net/vbirdbest/article/details/77662 ...

  6. Rest Post示例(java服务端、python客户端)

    前提:服务端是现成的,java.springMVC.resttemplate.jboss等:突然有个需要,要在windows上开发一个客户端,作用是定期向服务端上传文件.想了想,如果客户端写一个jav ...

  7. python客户端编程

    上一篇说了最为底层的用来网络通讯的套接字.有很多基于套接字的一些协议,这些协议构成了当今互联网大多数客户服务器应用的核心 其实这些协议时在套接字上面的进一层封装用来完成特定的应用,这些应用主要包括: ...

  8. FFrpc python客户端lib

    摘要: Ffrpc可以很方便的构建c++ server, 在网游服务器程序开发中,进程间通讯非常的重要,比如gateserver和gameserver或dbserver之间的通信.而ffrpc可以使得 ...

  9. openstack私有云布署实践【19 通过python客户端 创建实例VM指定IP地址】

    还有一种创建方式 是使用py开发工具,调用openstackclient的方法进行创建实例 ,好处就是可随意指定我们要的虚拟机IP地址,需求的场景就是,某天我们需要主动分配一个比较熟知的IP用作某个服 ...

随机推荐

  1. 深度学习:Keras入门(二)之卷积神经网络(CNN)(转)

    转自http://www.cnblogs.com/lc1217/p/7324935.html 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的 ...

  2. 《Python 机器学习》笔记(四)

    数据预处理--构建好的训练数据集 机器学习算法最终学习结果的优劣取决于两个主要因素:数据的质量和数据中蕴含的有用信息的数量. 缺失数据的处理 在实际应用过程中,样本由于各种原因缺少一个或多个值得情况并 ...

  3. POSIX相关概念

    POSIX 表示可移植操作系统接口(Portable Operating System Interface ,缩写为 POSIX),POSIX标准定义了操作系统应该为应用程序提供的接口标准,是IEEE ...

  4. JAVA & C++ 多态

    多态,也叫动态绑定. Java: class A { public void f1() { System.out.println("A:f1"); } public void f2 ...

  5. 异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005.

    异常:Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} ...

  6. 01 Spring框架 基本介绍

    相信学习java,并且走Web道路的道友都应该知道Spring的大名,它的地位相信也不需要我在这里多说什么,接下来的文章就Spring的配置和使用来进行一些讲解. 首先学习框架我们都要考虑和做到以下几 ...

  7. 实现对第三方应用任意SO注入

    实现对第三方应用任意SO注入 0x01 应用在Android中运行,从外部对该进程可以进行任意SO文件动态注入,就是应用动态运行我们的SO文件 0x02 基本的逻辑是: 1.    获取目标进程的pi ...

  8. Adjust Linux Mint Mouse Scroll (Normal/Reverse)

    Set Scroll Normal 1 echo "pointer = 1 2 3 4 5 6 7 8 9 10 11 12" > ~/.Xmodmap && ...

  9. Docker容器技术-优化Docker镜像

    一.优化Docker镜像 1.降低部署时间 一个大的Docker应用是如何影响在新Docker宿主机上的部署时间. (1)编写Dockerfile创建一个大Docker镜像 [root@bogon ~ ...

  10. 13.常见模块re-正则模块

    1.正则 正则表达式是计算机科学的一个概念,正则表通常被用来检索.替换那些符合某个模式(规则)的文本.也就是说使用正则表达式可以在字符串中匹配出你需要的字符或者字符串,甚至可以替换你不需要的字符或者字 ...