首页 > 编程语言 >python cassandra 创建space table并写入和查询数据

python cassandra 创建space table并写入和查询数据

时间:2023-05-31 16:05:49浏览次数:45  
标签:bytearray execute space python list results session key table

 

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

标签:bytearray,execute,space,python,list,results,session,key,table
From: https://blog.51cto.com/u_11908275/6387668

相关文章

  • python berkeley DB操作——打开btree索引文件中的database
    打开BDB中某个索引中的数据库代码: frombsddb3importdbimportbsddb3asbsddbprintdb.DB_VERSION_STRINGmydb=db.DB()mydb.open('your_btree_db_filename','databsename',dbtype=db.DB_BTREE)rec=cur.first()whilerec:#printkeyvaluepri......
  • 爬虫-Python爬虫常用库
    一、常用库1、requests做请求的时候用到。requests.get("url")2、selenium自动化会用到。3、lxml4、beautifulsoup5、pyquery网页解析库说是比beautiful好用,语法和jquery非常像。6、pymysql存储库。操作mysql数据的。7、pymongo操作MongoDB数据库。8、redis非关......
  • Python解析XML文件
    今天学习如何利用Python来解析XML文档。给定一个XML文件,现在我们用Python来提取里面的内容。<deals><data><deal><deal_id>11111111</deal_id><sales_num>120</sales_num><price>15.0</price>......
  • Python 发送微信消息
    Python发送微信消息安装pipinstallitchat1、基本使用#使用微信接口给微信好友发送消息,importitchatnickname="迪丽热巴"send_message="测试消息"try:#1.自动登录方法,hotReload=True可以缓存,不用每次都登录,但是第一次执行时会出现一个二维码,需要......
  • Python 发送邮件
    Python发送邮件1、案例一(发送普通邮件)importsmtplibfromemail.mime.textimportMIMEText#发送普通邮件#POP3服务器地址:pop.qq.com#SMTP服务器地址:smtp.qq.comclassSendEmail:def__init__(self):#发送邮件的用户self.send_user=......
  • 《最新出炉》系列初窥篇-Python+Playwright自动化测试-2-playwright的API及其他知识
    1.简介上一篇宏哥已经将Python+Playwright的环境搭建好了,而且也简单的演示了一下三款浏览器的启动和关闭,是不是很简单啊。今天主要是把一篇的中的代码进行一次详细的注释,然后说一下playwright的API和其他相关知识点。那么首先将上一篇中的代码进行一下详细的解释。2.代码解释2.......
  • Python 函数
    函数返回多个返回值defmultiple_return_value():importdatetimed=datetime.date.today()val_1='年份为:{}'.format(d.year)val_2='月份为:{}'.format(d.month)returnval_1,val_2#只需在return关键字后跟多个值(依次用逗号分隔)val=mult......
  • python 中 re.match和re.search()函数
     两者都返回首次匹配字符串的索引,re.match函数只从头开始匹配,re.search函数不限制只从头开始匹配。001、re.match函数[root@PC1test2]#python3Python3.10.9(main,Mar12023,18:23:06)[GCC11.2.0]onlinuxType"help","copyright","credits"or"license"......
  • python 视频拆分成帧,帧合成视频
    参考python将视频切分成帧&&帧合成视频,下面的代码来自这篇博客。#====================视频拆分成帧===================================importcv2defvideo2frame(videos_path,frames_save_path,time_interval):''':paramvideos_path:视频的存放路径:par......
  • 果然python是直接可以使用requests去请求https站点的,意味着一般的扫描工具可以直接扫
    #coding:utf-8importrequests#请求地址#url="https://www.qlchat.com"url="https://www.baidu.com"headers={'user-agent':'Mozilla/5.0(WindowsNT10.0;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chro......