环境: 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 ~]#
github 地址:
安装 pip :
- yum install python-pip
安装 influxdb-python :
- pip install influxdb
使用 InfluxDBClient 类操作数据库,示例如下:
- from influxdb import InfluxDBClient
- client = InfluxDBClient('localhost', 8086, '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 地址:
欢迎补充
来源: http://www.tuicool.com/articles/NVzyyqI