Use python to operate InfluxDB and python to operate influxdb

Source: Internet
Author: User
Tags influxdb

Use python to operate InfluxDB and python to operate influxdb

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.

Github address:

Bytes

Please add

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.