使用python操作InfluxDB
环境: CentOS6.5_x64
InfluxDB版本:1.1.0
Python版本 : 2.6
准备工作
- 启动服务器
执行如下命令:
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(, '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) # 查询数据
好,就这些了,希望对你有帮助。
本文github地址:
https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170312_使用python操作InfluxDB.md
欢迎补充
使用python操作InfluxDB的更多相关文章
- 使用Python操作InfluxDB时序数据库
使用Python操作InfluxDB时序数据库 安装python包 influxdb,这里我安装的是5.3.0版本 pip install influxdb==5.3.0 使用 from infl ...
- Python操作Influxdb数据库
1.influxdb基本操作[root@test ~]# wget https://dl.influxdata.com/influxdb/releases/influxdb-1.2.4.x86_64. ...
- Python 使用Python远程连接并操作InfluxDB数据库
使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...
- Python(九) Python 操作 MySQL 之 pysql 与 SQLAchemy
本文针对 Python 操作 MySQL 主要使用的两种方式讲解: 原生模块 pymsql ORM框架 SQLAchemy 本章内容: pymsql 执行 sql 增\删\改\查 语句 pymsql ...
- Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- 练习:python 操作Mysql 实现登录验证 用户权限管理
python 操作Mysql 实现登录验证 用户权限管理
- Python操作MySQL
本篇对于Python操作MySQL主要使用两种方式: 原生模块 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb ...
- Python操作Mysql之基本操作
pymysql python操作mysql依赖pymysql这个模块 下载安装 pip3 install pymysql 操作mysql python操作mysql的时候,是通过”游标”来进行操作的. ...
- Python操作RabbitMQ
RabbitMQ介绍 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现的产品,RabbitMQ是一个消息代理,从“生产者”接收消息并传递消 ...
随机推荐
- --@angularJS--一个最简单的指令demo
<!DOCTYPE HTML><html ng-app="app"><head> <title>custom-directiv ...
- HTML URL
HTML 统一资源定位器(Uniform Resource Locators) URL 是一个网页地址. URL可以由字母组成,如"runoob.com",或互联网协议(IP)地址 ...
- FineUI表格模板列Undefined问题
一般是配置文件未添加ClientID="AutoID"引起
- MyBatis中别名的设置
在sqlMapperConfig中进行设置: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...
- Unity 容器教程
文章摘自: http://www.cnblogs.com/qqlin/archive/2012/10/18/2720830.html
- office如何去除多页签
写文档会遇到同时打开多个文档,偶尔可能需要对比,而有时office会出现跟浏览器类似的多页签界面.如何去除多页签,office本身没有此加载项,一般都是作为插件或组件形式另外安装,导致我们不知道从哪里 ...
- java连接ms sql server各类问题解析
首先先来说下使用微软自己开发的架包进行ms sql server数据库的连接时,sql 2000与sql 2005的连接方式略有不同: 1.首先驱动不一样,sql 2000的连接驱动包有三个,分别是: ...
- 编写JQuery插件-4
封装对象方法的插件 jQuery.fn.extend() 的两种写法 以添加一个点击按钮为例: 方法一: (function ($) { $.fn.mask = function(options){ ...
- 蓝桥网试题 java 基础练习 数列特征
----------------------------------- Collections.sort(list);是个好东西 但是要学会排列 然后你才能浪 -------------------- ...
- yii2 邮件发送(有图有真相)
经典的密码找回方案是发送邮件到用户邮箱然后修改密码,下面利用yii2 高级版的mail功能,进行邮件的发送,如下图 1.在comm/config/main-local.php中添加 'mailer' ...