准备工作

  • 启动服务器

  执行如下命令:

  service influxdb start

  示例如下:

[root@localhost ~]# service influxdb start
Starting 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 InfluxDBClient
client = InfluxDBClient(‘localhost‘, , ‘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 InfluxDBClient
client = 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 InfluxDBClient json_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 InfluxDBClient json_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)) client = InfluxDBClient(‘localhost‘, 8086, ‘root‘, ‘‘, ‘testdb‘) # 初始化
client.write_points(json_body) # 写入数据
showDatas(client) # 查询数据
client.query(‘delete from students;‘) # 删除数据
showDatas(client) # 查询数据

python连接,操作 InfluxDB的更多相关文章

  1. python 连接操作数据库(一)

    一.下面我们所说的就是连接mysql的应用: 1.其实在python中连接操作mysql的模块有多个,在这里我只给大家演示pymysql这一个模块(其实我是感觉它比较好用而已): pymysql是第三 ...

  2. (8)Python连接操作MySQL

    pymysql模块下的方法 '''必须实例化对象才能建立连接''' 1.pymysql.connect  #和MySQL建立连接 '''得由对象去调用定义游标''' 2.xxx.sursor()  # ...

  3. python 连接操作数据库(二)

    一.我们接着上期的博客继续对ORM框架进行补充,顺便把paramiko模块也给大家讲解一下: 1.ORM框架: 在连接操作数据库的第一个博客中也已经说了,sqlalchemy是一个ORM框架,总结就是 ...

  4. python 连接操作 各类数据库

    转载自MySQL Loners 一,python 操作 MySQL:详情见:这里 #!/bin/env python # -*- encoding: utf-8 -*- #-------------- ...

  5. Python连接操作数据库

    步骤: 1.创建与数据库的连接对象: 2.创建游标: 3.通过游标执行语句 4.增删改需要提交(commit)数据 5.关闭连接 如: import MySQLdb   # Python通过MySQL ...

  6. python 连接操作mysql数据库

    开发数据库程序流程: 1.创建connection对象,获取cursor 2.使用cursor执行SQL 3.使用cursor获取数据.判断执行状态 4.提交事务 或者 回滚事务 import: 数据 ...

  7. Python——连接操作数据库

    1.安装MySQL驱动程序.下载自动安装包,双击安装即可,非常简单. 2.连接MySQL,下面是Python示例代码. import MySQLdbconn=MySQLdb.connect(host= ...

  8. Python 使用Python远程连接并操作InfluxDB数据库

    使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...

  9. 使用python操作InfluxDB

    环境: CentOS6.5_x64InfluxDB版本:1.1.0Python版本 : 2.6 准备工作 启动服务器 执行如下命令: service influxdb start 示例如下: [roo ...

  10. R和python连接SQL sever 数据库操作

    在R的使用中,为了方便提取数据, 我们经常要进行数据库进行操作,接下来我们尝试使用R进行连接数据. 这里我们使用R中的RODBC进行操作, 首先,我们需要先配置ODBC资源管理器 通过任务管理器或者w ...

随机推荐

  1. iconv()错误

    //转换字符编码过程中报错,数据会丢失,解决办法:设置第二个参数为gbk//IGNORE $strexport=iconv('UTF-8',"GBK",$strexport); $ ...

  2. matplotlib安装错误依赖问题解决

    When install "matplotlib" with "pip", if you get the following error, it means t ...

  3. cmake 及make 实践记录

    DEBIAN操作系统 预备操作: 安装 gcc g++ make cmake 开启Terminal 切换到超级用户 下载安装上述软件 A@debian:~$ su Password: root@deb ...

  4. Mysql快速重置root密码

    以Mac版本为例 首先关闭mysql服务 然后新建一个Terminal 1.输入 sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables 2 ...

  5. Redis-3.2.0集群配置(redis cluster)

    版本:redis-3.0.5 redis-3.2.0  redis-3.2.9  redis-4.0.11 参考:http://redis.io/topics/cluster-tutorial. 目录 ...

  6. Forward团队-爬虫豆瓣top250项目-模块测试过程

    我所做的模块不需要测试,但在后续其他人编写代码的时候,我需要对网页源码进行进一步的规范,然后指导别人在网页源码中的标签用法.

  7. Ubuntu再图形登录中以root的身份进入???

    Ubuntu再图形登录中以root的身份进入??? 这样做的需求,应该就是,可以再图形页面以root的身份进行图形化操作,比较方便更改配置文件. 1. 可以实现,但是不建议这么做,之后会出现一个警告提 ...

  8. 使用VMWare12.0安装Ubuntu系统

    使用VMWare12.0安装Ubuntu系统 Vmware12的虚拟机的文档说明: http://pubs.vmware.com/workstation-12/index.jsp#com.vmware ...

  9. 如何查看非自己提交的请求的结果 - 深入浅出Oracle EBS之杂项技术荟萃

    如何查看非自己提交的请求的结果定位要找的请求SQL举例:SELECT req.request_id,       fcp.user_concurrent_program_name,       usr ...

  10. How To Use XDOLoader to Manage, Download and Upload Files? (DOC ID 469585.1)

    In this Document Goal Fix     Downloading Files   Uploading Files References Applies to: BI Publishe ...