Skip to content

Instantly share code, notes, and snippets.

@thedod
Last active December 23, 2015 02:39
Show Gist options
  • Save thedod/6568566 to your computer and use it in GitHub Desktop.
Save thedod/6568566 to your computer and use it in GitHub Desktop.
decouch - dump couchdb data as a folder of .json files

As @DEVOPS_BORAT once said: "most big-datas are pretty small", so what if you have one of those and you just want the data as simple and greppable [or whateverable] json?

Here's a small python script ftw.

Requires py-couchdb

#!/usr/bin/env python
# -*- coding: utf-8 -*-
COUCH_SSL = True # only set to False on localhost (or don't come crying)
COUCH_SERVER = 'mycouch.example.com' # can contain a port, e.g. localhost:5984
COUCH_USER = 'myuserid' # change to None if you want to get prompted for it each time
COUCH_PASSWORD = None # I wouldn't advise to explicitly write this here :)
COUCH_DB = 'mydb'
TARGET_DIR = 'data' # relative path for json output files
import pycouchdb,json,sys,getpass
while not COUCH_USER:
COUCH_USER = raw_input('User for {0}: '.format(COUCH_SERVER)).strip()
while not COUCH_PASSWORD:
COUCH_PASSWORD = getpass.getpass('Password for {0}@{1}: '.format(COUCH_USER,COUCH_SERVER)).strip()
server = pycouchdb.Server('{0}://{1}:{2}@{3}'.format(
COUCH_SSL and 'https' or 'http', COUCH_USER, COUCH_PASSWORD, COUCH_SERVER))
db = server.database(COUCH_DB)
for doc in db.all():
if doc['_id'][0]=='_': continue # skip design docunents
print doc['_id']
del doc['_rev'] # If you ever kanso upload it or something, you don't want _rev
json.dump(doc,file('{0}/{1}.json'.format(TARGET_DIR,doc['_id']),'w'),indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment