InfluxDB——python使用手册
InfluxDB——python使用手册

准备工作
安装InfluxDB:
请参考笔者相关博文:Centos7安装InfluxDB1.7
安装pip :
yum install python-pip
安装influxdb-python :
pip install influxdb
实际上py的influx官方包的doc也已经足够详细,值得过一遍:py-influxdb
基本操作
使用InfluxDBClient类操作数据库,示例如下:
# 初始化
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname')
- 显示已存在的所有数据库
使用get_list_database函数,示例如下:
print client.get_list_database() # 显示所有数据库名称
- 创建新数据库
使用create_database函数,示例如下:
client.create_database('testdb') # 创建数据库
- 删除数据库
使用drop_database函数,示例如下:
client.drop_database('testdb') # 删除数据库
数据库操作完整示例如下:
from influxdb import InfluxDBClient
# 初始化(指定要操作的数据库)
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname')
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, 'your_username', 'yuor_password', 'your_dbname')
- 显示指定数据库中已存在的表
可以通过influxql语句实现,示例如下:
result = client.query('show measurements;') # 显示数据库中的表
print("Result: {0}".format(result))
- 创建新表并添加数据
InfluxDB没有提供单独的建表语句,可以通过并添加数据的方式建表,示例如下:
current_time = datetime.datetime.utcnow().isoformat("T")
body = [
{
"measurement": "students",
"time": current_time,
"tags": {
"class": 1
},
"fields": {
"name": "Hyc",
"age": 3
},
}
]
res = client.write_points(body)
- 删除表
可以通过influxql语句实现,示例如下:
client.query("drop measurement students") # 删除表
数据表操作完整示例如下:
import datetime
from influxdb import InfluxDBClient
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname')
current_time = datetime.datetime.utcnow().isoformat("T")
body = [
{
"measurement": "students",
"time": current_time,
"tags": {
"class": 1
},
"fields": {
"name": "Hyc",
"age": 3
},
}
]
res = client.write_points(body)
client.query("drop measurement students")
数据操作
InfluxDBClient中要指定连接的数据库,示例如下:
# 初始化(指定要操作的数据库)
client = InfluxDBClient('localhost', 8086, 'your_username', 'yuor_password', 'your_dbname')
- 添加
经过笔者测试write_points相当于其它数据库的批量写入操作,建议处理大量数据是对数据进行缓存后利用write_points一次批量写入。
可以通过write_points实现,示例如下:
body = [
{
"measurement": "students",
"time": current_time,
"tags": {
"class": 1
},
"fields": {
"name": "Hyc",
"age": 3
},
},
{
"measurement": "students",
"time": current_time,
"tags": {
"class": 2
},
"fields": {
"name": "Ncb",
"age": 21
},
},
]
res = client.write_points(body)
- 查询
可以通过influxql语句实现,示例如下:
result = client.query('select * from students;')
print("Result: {0}".format(result))
- 更新
tags 和 timestamp相同时数据会执行覆盖操作,相当于InfluxDB的更新操作。
- 删除
使用influxql语句实现,delete语法,示例如下:
client.query('delete from students;') # 删除数据
参考文章
InfluDB官方文档:https://docs.influxdata.com/influxdb/v1.7/introduction/installation/
python-influx doc:https://influxdb-python.readthedocs.io/en/latest/include-readme.html
Mike_Zhang:使用python操作InfluxDB
InfluxDB——python使用手册的更多相关文章
- (转)Python实例手册
原文地址:http://hi.baidu.com/quanzhou722/item/cf4471f8e23d3149932af2a7 实在是太好的资料了,不得不转 python实例手册 #encodi ...
- 《Python学习手册》读书笔记
之前为了编写一个svm分词的程序而简单学了下Python,觉得Python很好用,想深入并系统学习一下,了解一些机制,因此开始阅读<Python学习手册(第三版)>.如果只是想快速入门,我 ...
- 《Python学习手册》读书笔记【转载】
转载:http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html 之前为了编写一个svm分词的程序而简单学了下Python,觉 ...
- 《python参考手册(第四版)》【PDF】下载
<python参考手册(第四版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382222 内容介绍 本书是权威的Python语 ...
- 转载 python实例手册
python实例手册 #encoding:utf8# 设定编码-支持中文 0说明 手册制作: 雪松 更新日期: 2013-12-19 欢迎系统运维加入Q群: 198173206 # 加群请回答问题 请 ...
- 转载-《Python学习手册》读书笔记
转载-<Python学习手册>读书笔记 http://www.cnblogs.com/wuyuegb2312/archive/2013/02/26/2910908.html
- global语句(python学习手册422页)
# -*- coding: cp936 -*- #python 27 #xiaodeng #global语句(python学习手册422页) #实际上就是一个名为__builtin__的模块,但是必须 ...
- 【转载】python实例手册
今天写爬虫的时候遇到了问题,在网上不停地查找资料,居然碰到两篇好文章: 1.python实例手册 作者:没头脑的土豆 另一篇在这:shell实例手册 python实例手册 #encoding:ut ...
- 《Python学习手册》(二)
<Python学习手册>(二) --类型和运算 数字 十六进制 八进制 二进制 0x 0o 0b hex() oct() bin() >>>int('10',2) 2 & ...
随机推荐
- python之OpenCv(五)---抓取摄像头视频图像
OpenCV 可以通过 头videoCapture()方法打开摄像 摄像头变量 = cv2.VideoCapture(n) n为整数,内置摄像头为0,若有其他摄像头则依次为1,2,3,4,... ...
- GO语言系列(五)- 结构体和接口
结构体(Struct) Go中struct的特点 1. 用来自定义复杂数据结构 2. struct里面可以包含多个字段(属性) 3. struct类型可以定义方法,注意和函数的区分 4. struct ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- CMDB服务器管理系统【s5day88】:采集资产之Agent、SSH和Salt模式讲解
在对获取资产信息时,简述有四种方案. 1.Agent (基于shell命令实现) 原理图 Agent方式,可以将服务器上面的Agent程序作定时任务,定时将资产信息提交到指定API录入数据库 优点: ...
- Best Practice API
# 建议直接使用的第三方类 Common Lang =>StringUtils =>Validate Guava =>Cache =>Ordering JDK7(LTS JDK ...
- (二)Java工程化--Maven实践
Maven项目版本号 默认版本号: 1.0-SNAPSHOT 最佳实践是约定该版本为不稳定版本,如果发布一定要删除; 建议的版本规则: 主版本号.次版本号.增量版本号- 如:1.0.0-RELEASE ...
- codeblocks添加编译选项
-std=c++0x
- SQL join 连接时 条件加在 on后面和 where 的区别
task 是用户任务表,manageuser是用户表,以left join 为参考: 此时主表是task,三条sql语句:注意区别.第一句无筛选条件,第二句筛选条件在on后面,第三句sql的筛选语句放 ...
- 第一章 Bootstrap简介
一.Bootstrap简介 Bootstrap是基于 HTML.CSS.JAVASCRIPT 的前端框架,它简洁灵活,使得 Web 开发更加快捷.它由Twitter的设计师Mark Otto和Jaco ...
- Innodb和Myisam数据恢复
(转自)https://www.cnblogs.com/DwyaneTalk/p/4113829.html 背景 这次恢复oracle和sqlserver,想想也不能把mysql落下了吧.三剑合一.都 ...