2017-05-12 103 views
0

當通過pythons csh模塊連接到cassandra並試圖運行一個簡單的select查詢時,我似乎得到一個返回的布爾值而不是查詢結果。當我去到卡桑德拉箱並打開cqlsh殼直接查詢卡桑德拉我得到的結果:從cql查詢cassandra返回true而不是查詢結果

$ ./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> 

然而,當我試圖從遠程蟒蛇外殼上面我得到的布爾值「真」返回。

>>> 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 
>>> 

無論我看起來這似乎返回查詢結果,所以我錯過了什麼?

一個

回答

1

你需要通過ResultSet

迭代這是最簡單的例子:

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 
+0

資源似乎是布爾值 '真'。你不能遍歷它。那是什麼我的問題是 – amadain

+0

>>>中行在res: ...打印行 ... 回溯(最近通話最後一個): 文件「」,1號線,在 類型錯誤:「布爾」對象不可迭代 >>>類型(res) >>> – amadain

+0

@amadain更新回答 –