最新消息:重新回归WordPress,我要比较认真的开始更新我的博客了。

python操作InfluxDB

笔记 hanlei 1195浏览

网上查了一下InfluxDB,好像是一个开源的数据库程序。fastdb以及其它的名字我也不清楚。我在网上找了用python读取InfluxDB数据库的方法,大概的记录一下。

安装

pip install influxdb

使用

#引入库
from influxdb import InfluxDBClient

#联接库
client = InfluxDBClient(host=self.ip, port=self.port, username=self.username, password=self.password,timeout=self.timeout, database=self.dbname)

#显示所有数据库
client.get_list_database()
#[{'name': 'telegraf'}, {'name': '_internal'}]
#name后的就是库名

#显示所有表
client.query('show measurements;', database=dbname)
#ResultSet({'('measurements', None)': [{'name': 'tag_value'}]})
#name后的就是表名

#查询表
tables=client.query(sql, database=dbname)
#就用标准sql语名
list(tables.get_points())[0]['key']
for table in tables:
    print(table)
    for record in table:
        print(record[0])

#插入表
#样例数据:               json_body = [
                                {
                                    "measurement": "cpu_load_short",
                                    "tags": {
                                        "host": "server01",
                                        "region": "us-west"
                                    },
                                    "time": "2009-11-10T23:00:00Z",
                                    "fields": {
                                        "Float_value": 0.64,
                                        "Int_value": 3,
                                        "String_value": "Text",
                                        "Bool_value": True
                                    }
                                }
                            ]
client.write_points(json_body, database=dbname)

#其它
client.create_database(dbname)
client.drop_database(dbname)
client.query('drop measurement %s;' % tablename, database=dbname)
client.delete_series(database=dbname, measurement=measurement, tags=tags)

转载请注明:HANLEI'BLOG » python操作InfluxDB