• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解使用python操作InfluxDB方法

python 搞代码 4年前 (2022-01-09) 30次浏览 已收录 0个评论
文章目录[隐藏]

环境: CentOS6.5_x64
InfluxDB版本:1.1.0
Python版本 : 2.6

准备工作

  • 启动服务器

  执行如下命令:

  service influxdb start

  示例如下:

[root@localhost ~]# service influxdb startStarting influxdb...influxdb process was started [ OK ][root@localhost ~]#
  • 安装influxdb-python

github地址:https://github.com/influxdata/influxdb-python

安装pip :

yum install python-pip

安装influxdb-python :

pip install influxdb

基本操作

使用InfluxDBClient类操作数据库,示例如下:

from influxdb import InfluxDBClientclient = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化
  • 显示已存在的所有数据库

  使用get_list_database函数,示例如下:

  print client.get_list_database() # 显示所有数据库名称

  • 创建新数据库

  使用create_database函数,示例如下:

  client.create_database('testdb') # 创建数据库

  • 删除数据库

  使用drop_database函数,示例如下:

  client.drop_database('testdb') # 删除数据库

数据库操作完整示例如下:

#! /usr/bin/env python#-*- coding:utf-8 -*-from influxdb import InfluxDBClientclient = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化print client.get_list_database() # 显示所有数据库名称client.create_database('testdb') # 创建数据库print client.get_list_database() # 显示所有数据库名称client.drop_database('testdb') # 删除数据库print client.get_list_database() # 显示所有数据库名称

表操作

InfluxDBClient中要指定连接的数据库,示例如下:

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)
  • 显示指定数据库中已存在的表

  可以通过influxql语句实现,示例如下:

result = client.query('show measurements;') # 显示数据库中的表print("Result: {0}".format(result))
  • 创建新表并添加数据

InfluxDB没有提供单独的建表语句,可以通过并添加数据的方式建表,示例如下:

json_body = [    {        "measurement": "students",        "tags": {            "stuid": "s123"        },        #"time": "2017-03-12T22:00:00Z",        "fields": {            "score": 89        }    }]client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)client.write_points(json_body) # 写入数据,同时创建表
  • 删除表

可以通过influxql语句实现,示例如下:

client.query("drop measurement students") # 删除表

数据表操作完整示例如下:

#! /usr/bin/env python#-*- coding:utf-8 -*-from influxdb import InfluxDBClientjson_body = [    {        "measurement": "students",        "tags": {            "stuid": "s123"        },        #"time": "2017-03-12T22:00:00Z",        "fields": {            "score": 89        }    }]def showDBNames(client):        result = client.query('show measurements;') # 显示数据库中的表        print("Result: {0}".format(result))client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)showDBNames(client)client.write_points(json_body) # 写入数据,同时创建表showDBNames(client)client.query("drop measurement students") # 删除表showDBNames(client)

数据操作

InfluxDBClient中要指定连接的数据库,示例如下:

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)
  • 添加

可以通过write_points实现,示例如下:

json_body = [    {        "measurement": "students",        "tags": {            "stuid": "s123"        },        #"time": "2017-03-12T22:00:00Z",        "fields": {            "score": 89        }    }]client.write_points(json_body) # 写入数据
  • 查询

可以通过influxql语句实现,示例如下:

result = client.query('select * from students;')    print("Result: {0}".format(result))
  • 更新

tags 和 timestamp相同时数据会执行覆盖操作,相当于InfluxDB的更新操作。

  • 删除

使用influxql语句实现,delete语法,示例如下:

client.query('delete from students;') # 删除数据

数据操作完整示例如下:

#! /usr/bin/env python#-*- coding:utf-8 -*-from influxdb import InfluxDBClientjson_body = [    {        "measurement": "students",        "tags": {            "stuid": "s123"        },        #"time": "2017-03-12T22:00:00Z",        "fields": {            "score": 89        }    }]def showDatas(client):        result = client.query('select * from students;')        print("Result: {0}".format(result))clien<em style="color:transparent">本文来源[email protected]搞@^&代*@码)网9</em>t = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化client.write_points(json_body) # 写入数据showDatas(client)  # 查询数据client.query('delete from students;') # 删除数据showDatas(client)  # 查询数据

好,就这些了,希望对你有帮助。

以上就是详解使用python操作InfluxDB方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解使用python操作InfluxDB方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址