1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| # -*- coding: utf-8 -*- """Tutorial for using pandas and the InfluxDB client."""
import argparse import pandas as pd
from influxdb import DataFrameClient
def main(host='localhost', port=8086): """Instantiate the connection to the InfluxDB client.""" user = 'root' password = 'root' dbname = 'demo' protocol = 'line'
client = DataFrameClient(host, port, user, password, dbname)
print("Create pandas DataFrame") df = pd.DataFrame(data=list(range(30)), index=pd.date_range(start='2014-11-16', periods=30, freq='H'), columns=['0'])
print("Create database: " + dbname) client.create_database(dbname)
print("Write DataFrame") client.write_points(df, 'demo', protocol=protocol)
print("Write DataFrame with Tags") client.write_points(df, 'demo', {'k1': 'v1', 'k2': 'v2'}, protocol=protocol)
print("Read DataFrame") client.query("select * from demo")
print("Delete database: " + dbname) client.drop_database(dbname)
def parse_args(): """Parse the args from main.""" parser = argparse.ArgumentParser( description='example code to play with InfluxDB') parser.add_argument('--host', type=str, required=False, default='localhost', help='hostname of InfluxDB http API') parser.add_argument('--port', type=int, required=False, default=8086, help='port of InfluxDB http API') return parser.parse_args()
if __name__ == '__main__': args = parse_args() main(host=args.host, port=args.port)
|