python - Cassandra ResultSet 遍历一次后就变空了

标签 python json serialization cassandra

我有一个 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
[]

最佳答案

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

关于python - Cassandra ResultSet 遍历一次后就变空了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34728236/

相关文章:

python - 自然键序列化以一对一字段为主键的Django模型

php从文本字段到数组

javascript - 如何在 jQuery serialize() 函数中包含按钮值?

java - 如何在不覆盖序列化方法的情况下为特定类覆盖 JsonSerializer 的 isEmpty 方法?

json - 是否可以重写未知 json 结构中的字符串?

python - 写入的不仅仅是 "hello"

python - pip安装错误 "No such file or directory: setup.py"

python - 当子类化 threading.Thread 时,我是否必须调用 super.join() ?

Python/Seaborn : What does the inside horizontal distribution of the data-points means or is it random?

javascript - JSON 数组和 Javascript