python连接,操作 InfluxDB
准备工作
- 启动服务器
执行如下命令:
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的更多相关文章
- python 连接操作数据库(一)
一.下面我们所说的就是连接mysql的应用: 1.其实在python中连接操作mysql的模块有多个,在这里我只给大家演示pymysql这一个模块(其实我是感觉它比较好用而已): pymysql是第三 ...
- (8)Python连接操作MySQL
pymysql模块下的方法 '''必须实例化对象才能建立连接''' 1.pymysql.connect #和MySQL建立连接 '''得由对象去调用定义游标''' 2.xxx.sursor() # ...
- python 连接操作数据库(二)
一.我们接着上期的博客继续对ORM框架进行补充,顺便把paramiko模块也给大家讲解一下: 1.ORM框架: 在连接操作数据库的第一个博客中也已经说了,sqlalchemy是一个ORM框架,总结就是 ...
- python 连接操作 各类数据库
转载自MySQL Loners 一,python 操作 MySQL:详情见:这里 #!/bin/env python # -*- encoding: utf-8 -*- #-------------- ...
- Python连接操作数据库
步骤: 1.创建与数据库的连接对象: 2.创建游标: 3.通过游标执行语句 4.增删改需要提交(commit)数据 5.关闭连接 如: import MySQLdb # Python通过MySQL ...
- python 连接操作mysql数据库
开发数据库程序流程: 1.创建connection对象,获取cursor 2.使用cursor执行SQL 3.使用cursor获取数据.判断执行状态 4.提交事务 或者 回滚事务 import: 数据 ...
- Python——连接操作数据库
1.安装MySQL驱动程序.下载自动安装包,双击安装即可,非常简单. 2.连接MySQL,下面是Python示例代码. import MySQLdbconn=MySQLdb.connect(host= ...
- Python 使用Python远程连接并操作InfluxDB数据库
使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...
- 使用python操作InfluxDB
环境: CentOS6.5_x64InfluxDB版本:1.1.0Python版本 : 2.6 准备工作 启动服务器 执行如下命令: service influxdb start 示例如下: [roo ...
- R和python连接SQL sever 数据库操作
在R的使用中,为了方便提取数据, 我们经常要进行数据库进行操作,接下来我们尝试使用R进行连接数据. 这里我们使用R中的RODBC进行操作, 首先,我们需要先配置ODBC资源管理器 通过任务管理器或者w ...
随机推荐
- android屏幕页面实现滚动,页面跳转
在 在LinearLayout外面包一层ScrollView即可,如下代码 Apidemo 中关于如何使用ScrollView说明,请参考:<ScrollView xmlns:android=& ...
- UI7Kit
[UI7Kit] UI7Kit is a GUI toolkit which can backport flat-style UIKit from iOS7 to iOS5/iOS6. Additio ...
- redis centos 上以 tar.gz 安装redis
1.下载安装文件#wget http://download.redis.io/releases/redis-3.2.3.tar.gz 2.删除文件 rm -rf /usr/local/redisrm ...
- iOS隐藏导航条1px的底部横线
第二种方法:1)声明UIImageView变量,存储底部横线 @implementation MyViewController { UIImageView *navBarHairlineImageVi ...
- GNOME桌面的安装
首先搭建yum仓库 http://www.cnblogs.com/jw35/p/5967677.html yum grouplist #列出yum仓库里的软件组 ...
- 洛谷P3224 [HNOI2012]永无乡(线段树合并+并查集)
题目描述 永无乡包含 nnn 座岛,编号从 111 到 nnn ,每座岛都有自己的独一无二的重要度,按照重要度可以将这 nnn 座岛排名,名次用 111 到 nnn 来表示.某些岛之间由巨大的桥连接, ...
- Oracle EBS学习网站列表
Oracle官方文档库http://tahiti.oracle.com/http://www.oracle.com/technetwork/documentation/index.html#apps其 ...
- Android-工作总结-LX-2018-08-20-判断数据库表字段是否为空
问题的因素: 调试了一上午,我要判断数据库表的name字段是否为空,使用了TextUtils.isEmpty(nameStr):来判断name字段是否为空,明明数据库是没有值,却一直显示有值,然后还去 ...
- [Erlang21]Erlang性能分析工具eprof fporf的应用
前段时间项目改代码突然cpu波动很大,排查了好久都没有找到原因,只能求助于性能测试工具 : <<Erlang程序设计>>----Joe Armstorng[哈哈,登月第一人 ...
- 微信小程序web-view之动态加载html页面
官方推出的web-view方便了很多开发人员. 我们在做的时候,经常会想到写一个小程序的page然后通过动态加载web-view的形式来完成其他功能页面的开发. 之前研究web-view的时候发现网上 ...