Python操作Influxdb数据库

1、influxdb基本操作
[root@test ~]# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.2.4.x86_64.rpm
[root@test ~]# yum localinstall influxdb-1.2.4.x86_64.rpm
[root@test ~]# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.1.0.x86_64.rpm
[root@test ~]# yum localinstall influxdb-1.1.0.x86_64.rpm
[root@test ~]# influx -precision rfc3339      #更改influxdb的时间显示方式为utc显示方式
>precision rfc3339
insert disk_free,hostname=server01 value=4422i 1435362189575692182     #添加数据并自定义时间戳
注意:
       插入一条带时间戳的数据时,时间必需在数据保留策略时间内,否则该条数据无法插入
       InfluxDB的insert中,表名与数据之间用逗号(,)分隔,tag和field之间用 空格分隔,多个tag或者多个field之间用逗号(,)分隔
> show measurements           #显示所有表
name: measurements
name
----
DomainBps
> show series from [表名]       #series表示这个表里面的数据可以在图表上画成几条线
> drop measurement [表名]    #删除一张表
> show retention policies on aliyun     #查询aliyun数据库的默认策略
name      duration      shardGroupDuration      replicaN     default
----           ----------        ------------------                 --------        -------
autogen      0s               168h0m0s                       1              true
> alter retention POLICY "autogen" on "aliyun" duration 200h     #更改数据的保留时间
name        duration        shardGroupDuration   replicaN   default
-------         ----------         ---------------------------   ----------     -------
autogen  200h0m0s           168h0m0s                   1            true
> drop retention policy [策略名] on [数据库名]        #删除策略
> create retention policy [策略名] on [数据库名] duration 48h/2d replication 1 default      #创建策略
说明:
       name字段为策略名
       duration字段的0表示数据永远保留,不自动删除
       shardGroupDuration字段的时间表示查询时间在168h内的查询效率高,查询大于这个时间的效率会降低
       replicaN字段表示副本个数

2、python往influxbd插入数据
yum -y install epel-release     #Centos6.8安装epel源
yum -y install python-pip

[root@test ~]# pip install influxdb
[root@test ~]# pip install --upgrade influxdb
[root@test ~]# pip uninstall influxdb
root@debian:~# apt-get install python-influxdb

from influxdb import InfluxDBClient

json_body = [
    {
        "measurement": "table_name",  # 表名
        "tags": {
            "host": "server01",    #host和region为字段名,server01和us-west分别为host和region的值
            "region": "us-west"
        },
        "time": "2009-11-10T23:00:00Z",  # 插入的时间必需在数据保留时间范围内,如策略保留时间为7天,则传的时间不能为7天前
        "fields": {
            "value": 240.64345           #插入的值不能为0
        }
    },
    {
        "measurement": "table_name",
        "tags": {
            "host": "server01",
            "region": "us-west"
        },
        "time": "2018-04-22T011:05:00Z",
        "fields": {
            "value": 240
        }
    }
]

client = InfluxDBClient('localhost', 8086, 'root', 'root', 'database')
client.write_points(json_body)

参考链接:
         https://www.cnblogs.com/shhnwangjian/p/6897216.html?utm_source=itdadao&utm_medium=referral
         https://www.cnblogs.com/mafeng/p/6848166.html
         https://github.com/influxdata/influxdb-python
         https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017
         https://www.cnblogs.com/MikeZhang/p/InfluxDBPythonOpt20170312.html
         https://www.cnblogs.com/saneri/p/7528283.html       #psutil模块获取监控数据

原文地址:https://www.cnblogs.com/xwupiaomiao/p/8898110.html