Skip to content

Instantly share code, notes, and snippets.

@danohu
Last active January 4, 2016 16:48
Show Gist options
  • Save danohu/8649383 to your computer and use it in GitHub Desktop.
Save danohu/8649383 to your computer and use it in GitHub Desktop.
split large mongodumps into files usable for import
#!/usr/bin/env python
from itertools import izip_longest
import json
import subprocess
import sys
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
def split_pretty_array(infh, outfilebase, numperfile):
for i, group in enumerate(grouper(json.loads(infh), numperfile, '')):
with open('%s_%s.json' % (outfilebase, i), 'w') as outputfile:
json.dump(group, outputfile)
if __name__ == '__main__':
split_pretty_array(sys.stdin, sys.argv[1], 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment