Detailed instructions on using python to operate InfluxDB

Source: Internet
Author: User
Tags influxdb
This article explains how to use python to operate the InfluxDB method environment: CentOS6.5 _ x64
InfluxDB version: 1.1.0
Python: 2.6

Preparations
  • Start the server

Run the following command:

  service influxdb start

Example:

[root@localhost ~]# service influxdb startStarting 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 operations

The following is an example of using the InfluxDBClient class to operate a database:

From influxdb import InfluxDBClientclient = InfluxDBClient ('localhost', 8086, 'root', '','') # initialization

  • Show all existing databases

Use the get_list_database function, for example:

Print client. get_list_database () # display all database names

  • Create a new database

Use the create_database function, for example:

Client. create_database ('testdb') # create a database

  • Delete database

Use the drop_database function, for example:

Client. drop_database ('testdb') # delete a database

A complete example of database operations is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-from influxdb import InfluxDBClientclient = InfluxDBClient ('localhost', 8086, 'root ','','') # Initialize 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 print client of the database. get_list_database () # show all database names
Table operations

The database to be connected in InfluxDBClient, for example:

Client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # Initialize (specify the database to be operated)

  • Displays existing tables in the specified database.

You can use the influxql statement, for example:

Result = client. query ('Show measurements; ') # display the print ("Result: {0}". format (result) in the database ))

  • Create a new table and add data

InfluxDB does not provide a separate table creation statement. you can 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 ') # Initialize the client (specify the database to be operated. write_points (json_body) # write data and create a table

  • Delete table

You can use the influxql statement, for example:

Client. query ("drop measurement students") # delete a table

A complete example of data table operations is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-from influxdb import InfluxDBClientjson_body = [{"measurement": "students", "tags ": {"stuid": "s123" },# "time": "2017-03-12T22: 00: 00Z", "fields": {"score ": 89 }}] def showDBNames (client): result = client. query ('Show measurements; ') # display the print ("Result: {0}" in the database }". format (result) client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # Initialize (specify the database to be operated) showDBNames (client) client. write_points (json_body) # write data and create the table showDBNames (client) client. query ("drop measurement students") # Delete the table showDBNames (client)

Data operations

The database to be connected in InfluxDBClient, for example:

Client = InfluxDBClient ('localhost', 8086, 'root', '', 'testdb') # Initialize (specify the database to be operated)

  • Add

It can be implemented through write_points, for example:

Json_body = [{"measurement": "students", "tags": {"stuid": "s123" },# "time": "2017-03-12T22: 00: 00Z ", "fields": {"score": 89}] client. write_points (json_body) # write data

  • Query

You can use the influxql statement, for example:

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

  • Update

When tags and timestamp are the same, overwriting is performed, which is equivalent to updating InfluxDB.

  • Delete

Use the influxql statement to implement the delete syntax, for example:

Client. query ('delete from students; ') # delete data

A complete example of data operations is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-from influxdb import InfluxDBClientjson_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') # Initialize the client. write_points (json_body) # write data to 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.

The above is a detailed description of how to use python to operate the InfluxDB method. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.