2016-09-15 21 views
0

我正在研究Pattern 1,並想知道是否有方法,以python或其他方式將具有相同id的所有行「收集」爲包含字典的行。在Cassandra/Python中的同一個鍵下收集行

CREATE TABLE temperature (
       weatherstation_id text, 
       event_time timestamp, 
       temperature text, 
       PRIMARY KEY (weatherstation_id,event_time) 
       ); 

插入一些數據

INSERT INTO temperature(weatherstation_id,event_time,temperature) 
       VALUES ('1234ABCD','2013-04-03 07:01:00','72F'); 
INSERT INTO temperature(weatherstation_id,event_time,temperature) 
       VALUES ('1234ABCD','2013-04-03 07:02:00','73F'); 
INSERT INTO temperature(weatherstation_id,event_time,temperature) 
       VALUES ('1234ABCD','2013-04-03 07:03:00','73F'); 
INSERT INTO temperature(weatherstation_id,event_time,temperature) 
       VALUES ('1234ABCD','2013-04-03 07:04:00','74F'); 

查詢數據庫。

SELECT weatherstation_id,event_time,temperature 
       FROM temperature 
       WHERE weatherstation_id='1234ABCD'; 

結果:

weatherstation_id | event_time    | temperature 
-------------------+--------------------------+------------- 
      1234ABCD | 2013-04-03 06:01:00+0000 |   72F 
      1234ABCD | 2013-04-03 06:02:00+0000 |   73F 
      1234ABCD | 2013-04-03 06:03:00+0000 |   73F 
      1234ABCD | 2013-04-03 06:04:00+0000 |   74F 

其中一期工程,但我想知道如果我能變成一個排每weatherstationid

E.g.

{ 
"weatherstationid": "1234ABCD", 
"2013-04-03 06:01:00+0000": "72F", 
"2013-04-03 06:02:00+0000": "73F", 
"2013-04-03 06:03:00+0000": "73F", 
"2013-04-03 06:04:00+0000": "74F" 
} 

是否有卡桑德拉司機一些參數可以指定某些ID(weatherstationid)收集並把其他一切字典?或者是否需要有一些Python魔法來將每行的列表變成單行(或一組ID)?

回答

1

亞歷克斯,你將不得不做一些執行後數據處理來獲得這種格式。無論您使用哪種row_factory,驅動程序都會逐行返回。

驅動程序無法完成您建議的格式的原因之一是內部涉及分頁(默認fetch_size爲5000)。所以你產生的結果可能是部分或不完整的。此外,當查詢執行完成並且您確定獲取了所有需要的結果時,可以使用Python輕鬆完成此操作。

+0

我認爲會是這樣,但在任何地方都看不到。 – Alex