准备工作

  • 启动服务器

  执行如下命令:

  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. 用MapReduce读HBase写MongoDB样例

    1.版本信息: Hadoop版本:2.7.1 HBase版本:1.2.1 MongDB版本:3.4.14 2.HBase表名及数据: 3.Maven依赖: <dependency> < ...

  2. [GO]go context的deadline方法

    package main import ( "time" "context" "fmt" ) func main() { d := time ...

  3. (广搜)Dungeon Master -- poj -- 2251

    链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2137 ...

  4. 18个扩展让你的Firefox成为渗透测试工具

    Firefox是一个出自Mozilla组织的流行的web浏览器.Firefox的流行并不仅仅是因为它是一个好的浏览器,而是因为它能够支持插件进而加强它自身的功能.Mozilla有一个插件站点,在那里面 ...

  5. SEGGER J-Link install

    Why J-Link? In case you wonder why GNU ARM Eclipse decided to provide support to SEGGER J-Link, the ...

  6. Rx.net 例子——(1)基础

    1.订阅序列 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  7. ClamAV学习【5】—— cli_scanpe函数浏览

    这近2000行的代码,要是没有Source Insight,都不知道怎么看下去.跟着跟着来到了PE文件查杀的地方,发现前面都中规中矩地进行PE属性检查,中间一段开始扫描每个区块,然后和特征库的size ...

  8. JavaWeb -jsp文件和内置对象的解析

    jsp文件和内置对象的解析 对page解析 JSP九大内置对象(自带,无需new) 1 out:输出对象 2 request:请求对象,存储“客户端像服务端发送的请求信息” 3 response:响应 ...

  9. 【OCP-12c】CUUG 071题库考试原题及答案解析(17)

    17.(7-11) choose twoView the Exhibit and examine the structure of ORDER_ITEMS and ORDERS tables.You ...

  10. 【OCP|052】OCP最新题库解析系列-3

    3.Which structure can span multiple data files?❑ A) a permanent tablespace❑ B) a bigfile tablespace❑ ...