Load a config file
In [1]:
import json
In [2]:
with open('db.json','r') as fp:
c = json.load(fp)
Load InfluxDB package
In [3]:
from influxdb import InfluxDBClient
Set your database information
In [4]:
client = InfluxDBClient(
c['DBHOST'],
443,
c['DBUSER'],
c['DBPASS'],
c['DBNAME'],
ssl=True,
verify_ssl=True
)
Print measurements (Simular to a MySQL's table)
In [5]:
rs = client.query("SHOW MEASUREMENTS")
for row in rs.get_points():
print(row)
Example query
Ref: https://docs.influxdata.com/influxdb/v1.3/query_language/data_exploration/
In [6]:
query = """SELECT * FROM "weather" WHERE time > now() - 7d"""
In [7]:
import csv
In [8]:
rs = client.query(query)
with open('data.csv', 'w') as csvfile:
firstrow = next(rs.get_points())
writer = csv.DictWriter(csvfile, fieldnames=firstrow.keys())
writer.writeheader()
writer.writerow(firstrow)
for row in rs.get_points():
writer.writerow(row)
In [9]:
%matplotlib inline
In [10]:
import matplotlib
import matplotlib.pyplot as plt
In [11]:
import numpy as np
In [12]:
temp = []
rs = client.query(query)
for item in rs.get_points():
temp.append(item['T1H'])
In [13]:
temp = np.array(temp)
In [14]:
plt.plot(temp)
Out[14]:
In [15]:
from array import array
In [16]:
import ROOT
In [17]:
%jsroot on
In [18]:
h1f = ROOT.TH1F('h1f', 'Y2 Temperature', 100, 15, 40)
In [19]:
rs = client.query(query)
for item in rs.get_points():
h1f.Fill(item['T1H'])
In [20]:
c = ROOT.TCanvas("CanvasName","Canvas Title",800,600)
h1f.Draw()
c.Draw()
Export data
In [21]:
f = ROOT.TFile('test.root', 'RECREATE')
d = array( 'f', [ 0 ])
tree = ROOT.TTree("tree", "tree")
tree.Branch('temperature', d, 'temp/F')
rs = client.query(query)
for item in rs.get_points():
d[0] = item['T1H']
tree.Fill()
f.Write()
f.Close()