繁体   English   中英

在cassandra python中插入列表

[英]insertion of a list in cassandra python

我在卡桑德拉有一张桌子,像:

CREATE TABLE IF NOT EXISTS article(
         url text PRIMARY KEY, 
         title text, 
         author text, 
         sources list<text>)

我有一个带有以下方法的对象Article ,可以将数据插入cassandra:

def save_to_cassandra(self, session):
    session.execute(
        """
        INSERT INTO article (url, title, author, sources)
        VALUES (%s, %s, %s, ????)
        """,
        (self.url, self.title, self.author, ["http://source1.com", "http://source2.com"])
    )

问题是我应该用什么代替???? 正确格式化字符串

您应该将%s用于所有类型的参数,而不仅仅是字符串
所以使用%s,这是完整的代码

def save_to_cassandra(self, session):
    session.execute(
        """
        INSERT INTO article (url, title, author, sources)
        VALUES (%s, %s, %s, %s)
        """,
        (self.url, self.title, self.author, ["http://source1.com", "http://source2.com"])
    )

来源: http : //datastax.github.io/python-driver/getting_started.html#passing-parameters-to-cql-queries

另一件事,您应该始终使用预处理语句

准备语句是由Cassandra解析并保存以供以后使用的查询。 当驱动程序使用一条准备好的语句时,它只需要发送参数值来绑定。 这降低了Cassandra中的网络流量和CPU利用率,因为Cassandra不必每次都重新解析查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM