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 & ...
随机推荐
- jvm学习笔记二(减少GC开销的建议)
一:触发主GC(Garbage Collector)的条件 JVM进行次GC的频率很高,但因为这种GC占用时间极短,所以对系统产生的影响不大.更值得关注的是主GC的触发条件,因为它对系统影响很明显.总 ...
- LVM备份(3)- pg_dumpall
- echarts 修改y轴name的样式
option = { xAxis: { type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sa ...
- 第七节:利用CancellationTokenSource实现任务取消和利用CancellationToken类检测取消异常。
一. 传统的线程取消 所谓的线程取消,就是线程正在执行的过程中取消线程任务. 传统的线程取消,是通过一个变量来控制,但是这种方式,在release模式下,被优化从cpu高速缓存中读取,而不是从内存中读 ...
- 阿里巴巴图标库iconfont上传svg后,显示不了图片
AI里面选中图形,点对象-路径-轮廓化描边
- 使用SO_REVTIMEO套接字选项为recvfrom设置超时
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n; ]; struct timeval ...
- 学习python笔记 协程
下面将一个经典的消费者和生产者的案例进行分析: import time def consumer(): r = '' while True: n = yield r if not n: return ...
- 高并发秒杀系统--Service事务管理与继承测试
[Spring IoC的类型及应用场景] [Spring事务使用方式] [Spring事务的特性] [Spring事务回滚的理解] [Service声明式事务的配置] 1.配置事务管理器 2.配置基 ...
- Silverlight Visifire控件应用去除图标的水印
首先,新建一个类,继承自Chart,重写LoadWatermark方法. 再创建图表实例的时候就不能new Chart实例了,直接使用MyCharts. 使用去除水印之前的图片: 使用去除水印之后的图 ...
- BootStrap分页教程
https://www.cnblogs.com/laowangc/p/8875526.html https://www.cnblogs.com/yinglunstory/p/6092834.html ...