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

Detailed explanation of how to operate InfluxDB using python

高洛峰
Release: 2017-03-14 15:28:01
Original
11395 people have browsed it

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

Preparation

  • Start the server

Execute the following command:

  service influxdb start
Copy after login

The example is as follows:

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

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

Install pip:

yum install python-pip
Copy after login
Install influxdb-python:

pip install influxdb
Copy after login
Basic operations

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

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

  • 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() # 显示所有数据库名称
Copy after login

Table operation

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

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

  • 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))
Copy after login

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

  • Delete table

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

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

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)
Copy after login

Data operation

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

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

  • 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) # 写入数据
Copy after login

  • Query

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

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

  • 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;') # 删除数据
Copy after login

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)  # 查询数据
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template