Operating InfluxDB with python

Keywords: Python Database InfluxDB github

Environment: CentOS 6.5_x64
InfluxDB version: 1.1.0
Python version: 2.6

Dead work

  • Start the server

Execute the following commands:

  service influxdb start

Examples are as follows:

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

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

Install pip:

yum install python-pip

Install influxdb-python:

pip install influxdb 

basic operation

Using the InfluxDBClient class to operate the database, the example is as follows:

from influxdb import InfluxDBClient
client = InfluxDBClient('localhost', 8086, 'root', '', '') # Initialization
  • Display all existing databases

Using the get_list_database function, an example is as follows:

print client.get_list_database() Displays all database names

  • Create a new database

Using the create_database function, the example is as follows:

client.create_database('testdb') # create database

  • Delete the database

Using the drop_database function, the following examples are given:

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

A complete example of database operation is as follows:

#! /usr/bin/env python
#-*- coding:utf-8 -*-

from influxdb import InfluxDBClient
client = InfluxDBClient('localhost', 8086, 'root', '', '') # Initialization
print client.get_list_database() # Display all database names
client.create_database('testdb') # Create a database
print client.get_list_database() # Display all database names
client.drop_database('testdb') # Delete the database
print client.get_list_database() # Display all database names

Table operation

The database to be connected is specified in the Influx DBClient as follows:

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # Initialization (specifying the database to operate)
  • Display existing tables in the specified database

It can be implemented by influxql statement, as follows:

result = client.query('show measurements;') # Display tables in the database
print("Result: {0}".format(result))
  • Create new tables and add data

InfluxDB does not provide a separate statement to build tables. It can build tables by adding data. Examples are as follows:

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

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # Initialization (specifying the database to operate)
client.write_points(json_body) # Write data while creating tables
  • Delete table

It can be implemented through the influxql statement as follows:

client.query("drop measurement students") # Delete table

A complete example of data table 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 showDBNames(client):
        result = client.query('show measurements;') # Display tables in the database
        print("Result: {0}".format(result))

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # Initialization (specifying the database to operate)
showDBNames(client)
client.write_points(json_body) # Write data while creating tables
showDBNames(client)
client.query("drop measurement students") # Delete table
showDBNames(client)

Data manipulation

The database to be connected is specified in the Influx DBClient as follows:

client = InfluxDBClient('localhost', 8086, 'root', '', 'testdb') # Initialization (specifying the database to operate)
  • Add to

It can be implemented by write_points, as follows:

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

client.write_points(json_body) # Write data
  • query

It can be implemented through the influxql statement as follows:

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

When tags and timestamp s are the same, the data will perform coverage operations, which is equivalent to the update operation of InfluxDB.

  • delete

Use influxql statement to implement, delete grammar, examples are as follows:

client.query('delete from students;') # Delete data

A complete example of data manipulation 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') # Initialization
client.write_points(json_body) # Write data
showDatas(client)  # Query data
client.query('delete from students;') # Delete data
showDatas(client)  # Query data

Okay, that's all. I hope it will help you.

This article github address:

Https://github.com/mike-zhang/mikeBlog Essays/blob/master/2017/20170312_Use python to operate InfluxDB.md

Welcome to add

Posted by fxb9500 on Tue, 16 Apr 2019 16:21:33 -0700