【问题标题】:querying cassandra from cql returns true instead of the query results从 cql 查询 cassandra 返回 true 而不是查询结果
【发布时间】:2017-10-11 20:30:48
【问题描述】:

当通过 pythons csh 模块连接到 cassandra 并尝试运行一个简单的选择查询时,我似乎得到了一个布尔值而不是查询结果。当我进入 cassandra 框并打开 cqlsh shell 并直接查询 cassandra 时,我得到了结果:

$ ./cqlsh
Connected to stg_sal_cluster at localhost:9160.
[cqlsh 4.1.1 | Cassandra 2.0.17.858 | DSE 4.6.11 | CQL spec 3.1.1 | Thrift protocol 19.39.0]
Use HELP for help.
cqlsh> use "SAL";
cqlsh:SAL> select * from sal_metadata where key='972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94';

 key                                                              |    column1                                                                  | value
------------------------------------------------------------------+--------------------------------------------------------------------------+----------------------------------------------------------------------
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 | Alternate|THUMBNAIL_MEDIUM_RESULT:true|h:320|mt:image/jpeg|s:19477|w:320 | sal:5797d15fe18e5d58a74c927d342142f998ea084359f6a789f1cb2ba924231d95
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                              CI_COMPLETE |                                                                 true
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                             Capture-Date |                                             2008-11-08T06:13:02.000Z
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                       ContentPermissions |                                                                SHARE
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                                   Height |                                                                 2112
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                                Mime-Type |                                                           image/jpeg
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                                    Width |                                                                 2816
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                                  sal.loc |                                        /opt/newbay/storage/sal/tank3
 972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94 |                                                                 sal.size |                                                              1385855

(9 rows)

cqlsh:SAL>

但是,当我从远程 python shell 尝试上述操作时,我得到返回的布尔值 'True'。

>>> import cql
>>> con=cql.connect('10.10.10.10', 9160,  'SAL')
>>> cursor = con.cursor()
>>> CQLString = "select * from sal_metadata where key='972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94';"
>>> res=cursor.execute(CQLString)
>>> res.fetch()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'bool' object has no attribute 'fetch'
>>> res
True
>>>

我看到的所有地方似乎都返回了查询结果,所以我错过了什么?

一个

【问题讨论】:

    标签: python cassandra cql


    【解决方案1】:

    您需要遍历结果集

    这是最简单的例子:

    from cassandra.cluster import Cluster
    cluster = Cluster(['10.10.10.10'])
    session = cluster.connect('SAL')
    
    CQLString = "select * from sal_metadata where key='972cca8691c0804f685702d4a307b44f60cc59254094c903354f55779344bd94';"
    
    for row in session.execute(CQLString)
        print row
    

    【讨论】:

    • res 似乎是布尔值“真”。你不能遍历它。这就是我的问题
    • >>> for row in res: ... print row ... Traceback(最近一次调用最后一次):文件“”,第 1 行,在 类型错误:'bool ' 对象不可迭代 >>> type(res) >>>
    • @amadain 更新答案
    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 2014-12-04
    • 2017-11-27
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2015-03-10
    相关资源
    最近更新 更多