python链接,操做 InfluxDB

 

准备工做

  • 启动服务器

  执行以下命令:python

  service influxdb start

  示例以下:git

[root@localhost ~]# service influxdb start
Starting influxdb...
influxdb process was started [ OK ]
[root@localhost ~]#
  • 安装influxdb-python

github地址: https://github.com/influxdata/influxdb-pythongithub

安装pip : 数据库

yum install python-pip

安装influxdb-python :json

pip install influxdb

基本操做

使用InfluxDBClient类操做数据库,示例以下:服务器

from influxdb import InfluxDBClient
client = InfluxDBClient(localhost, 8086, root, ‘‘, ‘‘) # 初始化
  • 显示已存在的全部数据库

  使用get_list_database函数,示例以下:函数

  print client.get_list_database() # 显示全部数据库名称ui

  • 建立新数据库

  使用create_database函数,示例以下:spa

  client.create_database(‘testdb‘) # 建立数据库code

  • 删除数据库

  使用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)  # 查询数据
相关文章
相关标签/搜索