Using Python to manipulate influxdb

Source: Internet
Author: User
Tags influxdb

Environment: centos6.5_x64
Influxdb version: 1.1.0
Python version: 2.6

Preparatory work
    • Start the server

Execute the following command:

  service influxdb start

Examples are as follows:

[Email protected] ~~]#
    • Installing Influxdb-python

GitHub Address: Https://github.com/influxdata/influxdb-python

Install PIP:

yum install python-pip

Install Influxdb-python:

Basic operations

Use the Influxdbclient class to manipulate the database with the following example:

= Influxdbclient ('localhost'8086'root'  ") # Initialization
    • Show all databases that already exist

Using the Get_list_database function, the example is as follows:

Print Client.get_list_database () # Show all database names

    • Create a new database

Using the Create_database function, the example is as follows:

Client.create_database (' TestDB ') # CREATE Database

    • Deleting a database

Using the Drop_database function, the example is as follows:

Client.drop_database (' TestDB ') # Delete database

A complete example of database operations is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*- fromInfluxdbImportinfluxdbclientclient= Influxdbclient ('localhost', 8086,'Root',"',"')#InitializePrintClient.get_list_database ()#Show all database namesClient.create_database ('TestDB')#Create a databasePrintClient.get_list_database ()#Show all database namesClient.drop_database ('TestDB')#Deleting a databasePrintClient.get_list_database ()#Show all database names
Table Operations

Influxdbclient to specify a connected database in the following example:

Client = influxdbclient ('localhost'root')  testdb'#  initialization (specifies the database to be manipulated)
    • Displays tables that already exist in the specified database

This can be achieved through the INFLUXQL statement, as follows:

result = Client.query ('show measurements; ' # displaying tables in a database Print ("Result: {0}". Format (Result))
    • Create a new table and add data

INFLUXDB does not provide a separate build statement, and you can build the table by adding data, as shown in the following example:

Json_body = [    {        "Measurement":"Students",        "Tags": {            "Stuid":"s123"        },        #"Time ": "2017-03-12t22:00:00z",        " Fields": {            "score": 89}}]client= Influxdbclient ('localhost', 8086,'Root',"','TestDB')#Initialize (specifies the database to be manipulated)Client.write_points (Json_body)#writing data, creating tables at the same time
    • Delete a table

This can be achieved through the INFLUXQL statement, as follows:

Client.query ("drop measurement students"#  Delete Table

A complete example of a data table operation is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*- fromInfluxdbImportInfluxdbclientjson_body= [    {        "Measurement":"Students",        "Tags": {            "Stuid":"s123"        },        #"Time ": "2017-03-12t22:00:00z",        " Fields": {            "score": 89        }    }]defshowdbnames (client): Result= Client.query ('show measurements;')#displaying tables in a database        Print("Result: {0}". Format (Result) client= Influxdbclient ('localhost', 8086,'Root',"','TestDB')#Initialize (specifies the database to be manipulated)showdbnames (client) client.write_points (json_body)#writing data, creating tables at the same timeshowdbnames (client) client.query ("Drop Measurement Students")#Delete a tableShowdbnames (client)
Data manipulation

Influxdbclient to specify a connected database in the following example:

Client = influxdbclient ('localhost'root')  testdb'#  initialization (specifies the database to be manipulated)
    • Add to

This can be achieved through write_points, with the following examples:

Json_body = [    {        "Measurement":"Students",        "Tags": {            "Stuid":"s123"        },        #"Time ": "2017-03-12t22:00:00z",        " Fields": {            "score": 89}}]client.write_points (Json_body)#Write Data
    • Inquire

This can be achieved through the INFLUXQL statement, as follows:

result = Client.query ('select * from students; ' )    print("Result: {0}". Format (Result))
    • Update

tags and timestamp the same time data will perform overwrite operations, equivalent to INFLUXDB update operations.

    • Delete

Using the INFLUXQL statement implementation, the delete syntax is shown in the following example:

Client.query ('delete from students; ' # Delete Data

A complete example of data manipulation is as follows:

#!/usr/bin/env python#-*-coding:utf-8-*- fromInfluxdbImportInfluxdbclientjson_body= [    {        "Measurement":"Students",        "Tags": {            "Stuid":"s123"        },        #"Time ": "2017-03-12t22:00:00z",        " Fields": {            "score": 89        }    }]defshowdatas (client): Result= Client.query ('SELECT * from students;')        Print("Result: {0}". Format (Result) client= Influxdbclient ('localhost', 8086,'Root',"','TestDB')#InitializeClient.write_points (Json_body)#Write DataShowdatas (client)#Querying DataClient.query ('delete from students;')#Delete DataShowdatas (client)#Querying Data

All right, that's it, I hope it helps you.

This article GitHub address:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170312_ using python operations influxdb.md

Welcome to Supplement

Using Python to manipulate influxdb

Related Article

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.