Skip to content

Instantly share code, notes, and snippets.

@marcsello
Created November 8, 2019 17:10
Show Gist options
  • Save marcsello/cb1df25be60afbc78eb252624f745a94 to your computer and use it in GitHub Desktop.
Save marcsello/cb1df25be60afbc78eb252624f745a94 to your computer and use it in GitHub Desktop.
InfluxDB to CSV dumper
#!/usr/bin/env python3
from influxdb import InfluxDBClient # <- install this first
import csv
import os
import argparse
#
# This script dumps all avliliable measurements into separate CSV files from InfluxDB
#
parser = argparse.ArgumentParser(description='Simple InfluxDB to CSV dumper')
parser.add_argument("--ip", type=str, required=True, help="IP address of the InfluxDB server")
parser.add_argument("--port", type=int, required=False, default=8086, help="Listening port of the InfluxDB server")
parser.add_argument("--user", type=str, required=True, help="Username")
parser.add_argument("--password", type=str, required=True, help="Password")
parser.add_argument("--db", type=str, required=True, help="Database which contains the measures to be dumped")
args = parser.parse_args()
client = InfluxDBClient(args.ip, args.port, args.user, args.password, args.db)
# begin
result = client.query("SHOW MEASUREMENTS;")
tables = list(result[('measurements', None)])
print("Tables will be dumped: \n - {}\n".format("\n - ".join([t['name'] for t in tables])))
os.makedirs('influx_dumps', exist_ok=True)
for i in tables:
tablename = i['name']
print("Dumping {}...".format(tablename))
filename = tablename.replace('+', '__').replace('/', '_').replace(':', '-')
print("Waiting for InfluxDB query...")
tableresult = client.query("SELECT * FROM \"{}\"".format(tablename))
j = 0
with open('influx_dumps/{}.csv'.format(filename), "w") as f:
first = True
for row in tableresult[(tablename, None)]:
if first:
wr = csv.DictWriter(f, row.keys())
wr.writeheader()
first = False
wr.writerow(row)
j += 1
if (j % 1000) == 0:
print("{} rows dumped".format(j))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment