我有一个python脚本,可以将数据从数据库中提取出来。问题是它只将一个项类型而不是完整的数据集拉入JSON序列化对象。
我试图获得的对象来自于:
STATS = ['min', 'max', 'mean','percentile1', 'percentile5', 'median', 'percentile95', 'percentile99', 'total']
唯一的问题是由于某种原因它只会采取第一个问题。这个例子就是' min'如果我把第一个切换为百分位数'比如:
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
[]
答案 0 :(得分:1)
get_month_report的返回值是
类型<cassandra.cluster.ResultSet object at 0x7fe1a6b6e910>
所以当你耗尽它时,你会遍历它。您需要在多次遍历之前将其转换为列表,通过&#34; list&#34;操作者:
if monthly:
rs = list(self.get_monthly_report(stationid, metric))
else:
rs = list(self.get_daily_report(stationid, metric))