从cthon导入csv数据到Influxdb

时间:2017-09-23 22:30:46

标签: influxdb-python

几乎可以肯定的是。

我在timeseries文件中有.csv个数据。数据没有标题,但这是不同的字段:date,time,open,high,low,close,volume,0

2017-09-22,21:50:00,131.36,131.415,131.35,131.39,78489,0
2017-09-22,21:55:00,131.39,131.4,131.37,131.38,95322,0
2017-09-22,22:00:00,131.38,131.6,131.31,131.39,1212804,0

我正在尝试将此数据从InfluxDB导入python。我在文档中看到你必须逐行创建json,但我不确定如何做到这一点:

这很接近,但没有雪茄。假设我已经拆分了字符串,并且日期时间戳已经采用正确的格式。当我打印下面的json_body时,我得到了例如,

[{'fields': {'Int_value': '1212804', 'Float_value': '131.39'}, 'time': datetime.datetime(2017, 9, 22, 22, 0), 'measurement': 'quote'}]

这看起来并不正确 - 我似乎只获得一个浮动值。

关于InfluxDB输入的正确JSON是什么?

import datetime
import random
import time
import os
import csv
from csv import reader
import argparse
from influxdb import client as influxdb


db = influxdb.InfluxDBClient("127.0.0.1", 8086, "", "", "stocks")


def read_data(filename):
    print filename
    with open(filename) as f:
        lines = f.readlines()[1:]

    return lines

if __name__ == '__main__':
    filename = r"jnj.us.csv"
    lines = read_data(filename)
    for rawline in lines:
        line = rawline.split(",")
        d= getPythonDateTimeFromStr(line[0], line[1])
        #EVERYTHING UP TO HERE WORKS. Not sure how to create the json below
        #====================================
        json_body = [
        {
            "measurement": "quote",
            "time": d,
            "fields": {
                "Float_value": line[2],
                "Float_value": line[3],
                "Float_value": line[4],
                "Float_value": line[5],
                "Int_value": line[6]
            }
        }
        ]

        print json_body

        db.write_points(json_body )

1 个答案:

答案 0 :(得分:1)

咄,

"fields": {
            "Float_value": line[2],
            "Float_value": line[3],
            "Float_value": line[4],
            "Float_value": line[5],
            "Int_value": line[6]
 }

应该是

"fields": {
            "Open": line[2],
            "High": line[3],
            "Low": line[4],
            "Close": line[5],
            "Volume": line[6]
 }