【问题标题】:Cassandra ResultSet gets empty after traversing it onceCassandra ResultSet 遍历一次后为空
【发布时间】:2016-04-16 03:46:51
【问题描述】:

我有一个从数据库中提取数据的 python 脚本。问题是它只是将一种项目类型而不是完整的数据集拉入 JSON 序列化对象。

我想要得到的对象来自这个:

STATS = ['min', 'max', 'mean','percentile1', 'percentile5', 'median', 'percentile95', 'percentile99', 'total']

唯一的问题是由于某种原因它只会占用第一个。如果我将第一个切换为“百分位数”,则该示例为“分钟”,例如:

STATS = ['percentile1','min', 'max', 'mean',, 'percentile5', 'median', 'percentile95', 'percentile99', 'total']

那么这将只加载“百分位”数据。它不包括任何其余部分。它正在查询每个数据的正确数据,但只会将第一个数据传递给 Rickshaw.js 以绘制图形。

我正在用这个序列化数据:

def get_series(self, stationid, metric, monthly=True):
    '''
    Format results into json-ready results for Rickshaw.js.
    '''

    allResults = {}
    if monthly:
        rs = self.get_monthly_report(stationid, metric)
    else:
        rs = self.get_daily_report(stationid, metric)
    for field in STATS:
        series = self.format_series(rs, field)
        allResults[field] = series
    return json.dumps(allResults,  default=json_serial)

def format_series(self, records, field):
    '''
    JSON formatting helper.
    '''

    data = []
    for record in records:
        data.append({'x' : time.mktime(record['date'].timetuple()), 'y' :    record[field]})
    return data

如果您需要更多代码。我很乐意提供。谢谢!

我插入了一些打印命令

def get_series(self, stationid, metric, monthly=True):
        '''
        Format results into json-ready results for Rickshaw.js.
        '''

        allResults = {}
        if monthly:
            rs = self.get_monthly_report(stationid, metric)
        else:
            rs = self.get_daily_report(stationid, metric)
        for field in STATS:
            print "The field is"
            print (field)
            series = self.format_series(rs, field)
            print "The Series is"
            print (series)
            allResults[field] = series
        return json.dumps(allResults,  default=json_serial)

这是出现的内容:

The field is
min
The Series is
[{'y': 0, 'x': 1388552400.0}, {'y': 0, 'x': 1391230800.0}, {'y': 0, 'x': 1393650000.0}, {'y': 19, 'x': 1396324800.0}, {'y': 52, 'x': 1398916800.0}, {'y': 13, 'x': 1401595200.0}, {'y': 37, 'x': 1404187200.0}, {'y': 10, 'x': 1406865600.0}, {'y': 4, 'x': 1409544000.0}, {'y': 49, 'x': 1412136000.0}, {'y': 28, 'x': 1414814400.0}, {'y': 0, 'x': 1417410000.0}, {'y': 0, 'x': 1420088400.0}, {'y': 46, 'x': 1422766800.0}, {'y': 60, 'x': 1425186000.0}, {'y': 52, 'x': 1427860800.0}, {'y': 58, 'x': 1430452800.0}, {'y': 69, 'x': 1433131200.0}, {'y': 48, 'x': 1435723200.0}, {'y': 20, 'x': 1438401600.0}, {'y': 22, 'x': 1441080000.0}, {'y': 0, 'x': 1443672000.0}, {'y': 0, 'x': 1446350400.0}, {'y': 0, 'x': 1448946000.0}, {'y': 0, 'x': 1451624400.0}, {'y': 10, 'x': 1454302800.0}, {'y': 48, 'x': 1456808400.0}, {'y': 66, 'x': 1459483200.0}, {'y': 60, 'x': 1462075200.0}, {'y': 58, 'x': 1464753600.0}, {'y': 0, 'x': 1467345600.0}, {'y': 17, 'x': 1470024000.0}, {'y': 27, 'x': 1472702400.0}, {'y': 31, 'x': 1475294400.0}, {'y': 0, 'x': 1477972800.0}, {'y': 10, 'x': 1480568400.0}, {'y': 65, 'x': 1483246800.0}]
The field is
max
The Series is
[]
The field is
mean
The Series is
[]
The field is
percentile1
The Series is
[]
The field is
percentile5
The Series is
[]
The field is
median
The Series is
[]
The field is
percentile95
The Series is
[]
The field is
percentile99
The Series is
[]
The field is
total
The Series is
[]

【问题讨论】:

  • 你能澄清一下吗?您是说 format_series 工作不正常吗?还是 get_series() 返回了意外的结果?
  • 似乎返回的数据仅针对“STATS”变量中的第一项。我需要它显示所有这些。
  • 你能把范围缩小一点吗?只返回部分数据的函数或代码行是什么?是format_series吗?
  • format_series 仅返回 STATS 变量中的第一项。如果我更改 stats 变量中的第一项,则它只显示该项目。因此,由于某种原因,它没有将所有项目添加到要序列化和返回的数组中。
  • 您能否在“series = ...”行之前插入一个打印字段,然后再打印一个系列,然后发送输出?

标签: python json serialization cassandra


【解决方案1】:

get_month_report 的返回值为类型

<cassandra.cluster.ResultSet object at 0x7fe1a6b6e910> 

所以当你遍历它时,一旦它耗尽。在多次遍历它之前,您需要通过“列表”运算符将其转换为列表:

 if monthly:
   rs = list(self.get_monthly_report(stationid, metric))
 else:
   rs = list(self.get_daily_report(stationid, metric))

【讨论】:

    猜你喜欢
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    相关资源
    最近更新 更多