【问题标题】:Gather rows under the same key in Cassandra/Python在 Cassandra/Python 中的同一键下收集行
【发布时间】:2016-09-16 00:05:27
【问题描述】:

我正在通过Pattern 1 工作,想知道是否有办法在python 或其他方式中将具有相同ID 的所有行“收集”到带有字典的一行中。

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 将其变成一行。

例如

{
 "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"
}

cassandra 驱动程序中是否有一些参数可以指定为通过特定 id (weatherstationid) 收集并将其他所有内容转换为字典?或者是否需要一些 Python 魔法才能将行列表转换为每个 id(或一组 ID)的单行?

【问题讨论】:

    标签: python cassandra


    【解决方案1】:

    Alex,您必须进行一些执行后数据处理才能获得这种格式。无论您使用什么 row_factory,驱动程序都会逐行返回。

    驱动程序无法完成您建议的格式的原因之一是内部涉及分页(默认 fetch_size 为 5000)。因此,以您的方式生成的结果可能是部分或不完整的。此外,当查询执行完成并且您确定已获取所有必需的结果时,这可以使用 Python 轻松完成。

    【讨论】:

    • 我以为会是这样,但在任何地方都看不到。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2013-09-09
    • 2023-03-06
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多