Created
March 21, 2016 19:39
-
-
Save Hidendra/2ca48a95c193be6b1585 to your computer and use it in GitHub Desktop.
Create postgres import query files for MCStats graph data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import glob | |
import os.path | |
def main(): | |
if len(sys.argv) != 3: | |
print('Usage: %s <directory of csv files> <output directory>' % sys.argv[0]) | |
sys.exit(1) | |
csv_directory = sys.argv[1] | |
output_directory = sys.argv[2] | |
for file_path in glob.glob('%s/*.csv.gz' % csv_directory): | |
abs_path = os.path.abspath(file_path) | |
file_name = os.path.basename(file_path) | |
file_name_no_ext = file_name[0:len(file_name) - len('.csv.gz')] | |
with open('%s/%s.sql' % (output_directory, file_name_no_ext), 'w') as f: | |
f.write('CREATE TABLE %s ( time timestamp NOT NULL, graph text NOT NULL COLLATE "en_US.utf8", data jsonb );\n' % file_name_no_ext) | |
f.write('ALTER TABLE %s ADD PRIMARY KEY (time, graph) NOT DEFERRABLE INITIALLY IMMEDIATE;\n' % file_name_no_ext) | |
f.write('COPY %s FROM PROGRAM \'gunzip --to-stdout %s | grep -v "\u0000"\' CSV;\n' % (file_name_no_ext, abs_path)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment