python cassandra 创建space table并写入和查询数据
2023-09-14 09:11:56 时间
from cassandra.cluster import Cluster cluster = Cluster(["10.178.209.161"]) session = cluster.connect() keyspacename = "demo_space" session.execute("create keyspace %s with replication = {'class': 'SimpleStrategy', 'replication_factor': 1};" % keyspacename) # use keyspace; create a sample table session.set_keyspace(keyspacename) s = session try: s.execute("CREATE TABLE blobbytes (a ascii PRIMARY KEY, b blob)") except: pass params = ['key1', bytearray(b'blob1')] s.execute("INSERT INTO blobbytes (a, b) VALUES (%s, %s)", params) results = s.execute("SELECT * FROM blobbytes") print "********************" for x in results: print x.a, x.b try: s.execute("CREATE TABLE list_test (a ascii PRIMARY KEY, b list<blob>)") except: pass params = ['some key here', [bytearray(b'blob1'), bytearray(b'hello world')]] s.execute("INSERT INTO list_test (a, b) VALUES (%s, %s)", params) results = s.execute("SELECT * FROM list_test") print "********************" for x in results: print x.a, x.b
结果:
******************** key1 blob1 ******************** some key here ['blob1', 'hello world']
最后补充:
cassandra的update和mongo的upsert效果一样!如果where的条件不满足,则会insert into!
params2 = [[bytearray(b'blob2'), bytearray(b'hello world2')], "other key"] s.execute("UPDATE list_test set b = b + %s WHERE a = %s", params2) # 会直接将other key的东西插入数据库!如果other key不存在的话!
见:http://stackoverflow.com/questions/17348558/does-an-update-become-an-implied-insert
相关文章
- Python常用代码_python画图代码大全
- python fileinput_Python之fileinput模块学习「建议收藏」
- python识别文字位置_如何利用Python识别图片中的文字
- python读取modis数据
- python海龟作图红绿灯_海龟作图—用Python绘图
- 终于来了,【第二期】 彭涛Python 爬虫特训营! !爬虫课福利,走过路过,务必不要错过!
- 【说站】python scrapy.Request发送请求的方式
- 【说站】python字典如何遍历数据
- 【说站】Python中time模块的方法整理
- 遗传算法的应用实例python实现_遗传算法Python解决一个问题
- python通过字符串调用方法_python调用shell脚本
- Python udp编程_python socket udp
- python抛出异常和捕获异常_Python异常
- python django 数据库_Apache+Mysql+PHP/Python简单项目
- python判断文件后缀名是否是jpg 或者png_python判断文件名是否包含某字段
- 【测试开发】python系列教程:Python 运算符
- 对未来的技能,Go和Python哪个更好?
- PYTHON用户流失数据挖掘:建立逻辑回归、XGBOOST、随机森林、决策树、支持向量机、朴素贝叶斯和KMEANS聚类用户画像|附代码数据
- 【错误记录】PyCharm 运行 Python 程序报错 ( PEP 8: W292 no newline at end of file )
- python-Python与SQLite数据库-使用Python执行SQLite查询(一)
- python-Python与PostgreSQL数据库-处理PostgreSQL查询结果
- python连接mysql查询数据示例详解编程语言
- Python推导式(列表推导式、元组推导式、字典推导式和集合推导式)详解
- 使用 Pygal 在 Python 中设置数据图的样式
- 如何用Python编写信息收集之子域名收集脚本
- 使用Python连接MySQL数据库,实现高效数据交互(python连接mysql)
- python用Redis与Python实现大数据收集与分析(redis 联合)
- Python变量类型及命名规则介绍