Home >Backend Development >Python Tutorial >Detailed explanation of how to operate InfluxDB using python

Detailed explanation of how to operate InfluxDB using python

高洛峰
高洛峰Original
2017-03-14 15:28:0111381browse

Environment: CentOS6.5_x64
InfluxDB version: 1.1.0
PythonVersion: 2.6

Preparation

  • Start the server

Execute the following command:

  service influxdb start

The example is as follows:

[root@localhost ~]# service influxdb start
Starting influxdb...
influxdb process was started [ OK ]
[root@localhost ~]#

##github address: https://github.com/influxdata/influxdb-python

Install pip:

yum install python-pip
Install influxdb-python:

pip install influxdb
Basic operations

Use the InfluxDBClient class operation Database, the example is as follows:

from influxdb import InfluxDBClient
client = InfluxDBClient('localhost', 8086, 'root', '', '') # 初始化

  • Display all existing databases

Use get_list_database

Function, the example is as follows:

 

print client.get_list_database() # Display all database names

  • Create a new database

Use the create_database function, the example is as follows:

client.create_database('testdb')

#Create database

Use the drop_database function, the example is as follows:

client.drop_database('testdb') # Delete the database

Database operationThe complete example is as follows:

#! /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() # 显示所有数据库名称

Table operation

InfluxDBClient needs to specify the database to connect to, the example is as follows:

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)

  • Display existing tables in the specified database

This can be achieved through the influxql statement. The example is as follows:

result = client.query('show measurements;') # 显示数据库中的表print("Result: {0}".format(result))

  • Create a new table and add data

InfluxDB does not provide a separate table creation statement, you can pass and Create a table by adding data. The example is as follows:

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) # 写入数据,同时创建表

  • Delete table

Yes Implemented through influxql statements, the example is as follows:

client.query("drop measurement students") # 删除表

Data table operationThe complete example is as follows:

#! /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)

Data operation

InfluxDBClient, you need to specify the database to connect to. The example is as follows:

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # 初始化(指定要操作的数据库)

  • Adding

can be achieved through write_points, the example is as follows:

json_body = [
    {
        "measurement": "students",
        "tags": {
            "stuid": "s123"
        },
        #"time": "2017-03-12T22:00:00Z",
        "fields": {
            "score": 89
        }
    }
]

client.write_points(json_body) # 写入数据

  • Query

can be implemented through influxql statements, the example is as follows:

result = client.query('select * from students;')    
print("Result: {0}".format(result))

  • Update

When tags is the same as timestamp, the data will perform an overwrite operation, which is equivalent to the update operation of InfluxDB.

  • Delete

Use influxql statement,

delete syntax, the example is as follows:

client.query('delete from students;') # 删除数据

The complete example of data operation is as follows:

#! /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)  # 查询数据

Okay, that’s it, I hope it will be helpful to you.


The above is the detailed content of Detailed explanation of how to operate InfluxDB using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn