This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchdb_load.py
executable file
·66 lines (54 loc) · 2.15 KB
/
couchdb_load.py
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
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import argparse
import sys
import json
from decimal import Decimal
import ijson
import requests
def parse_args():
parser = argparse.ArgumentParser(description='Load JSON documents into a CouchDB database')
parser.add_argument('-H', '--host', dest='host', default='localhost')
parser.add_argument('-P', dest='port', type=int, default=5984)
parser.add_argument('-u', dest='username')
parser.add_argument('-p', dest='password')
parser.add_argument('-b', dest='bulk_size', type=int, default=1000)
parser.add_argument('target_database', help='database to import data to')
return parser.parse_args()
def default(obj):
if isinstance(obj, Decimal):
return float(obj)
raise TypeError("Object of type '%s' is not JSON serializable" % type(obj).__name__)
def load(args, input_file, bulk_size=1000):
session = requests.session()
bulk = []
for doc in ijson.items(input_file, 'docs.item'):
bulk.append(doc)
if len(bulk) == bulk_size:
bulk_insert(args, session, bulk)
bulk = []
if bulk:
bulk_insert(args, session, bulk)
def bulk_insert(args, session, docs):
url = 'http://{}:{:d}/{}/_bulk_docs'.format(args.host, args.port, args.target_database)
auth = (args.username, args.password or '') if args.username else None
payload = json.dumps({'docs': docs}, separators=(',', ':'), default=default)
response = session.post(url, data=payload, auth=auth, headers={
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Couch-Full-Commit': 'true'
})
if response.status_code == 201:
results = response.json()
for result in results:
if result['error']:
message = 'Failed to import document {}: {}. Reason: {}\n'.format(
result['id'], result['error'], result['reason'])
sys.stderr.write(message)
sys.exit(1)
else:
sys.stderr.write('Failed to import documents into {}\n'.format(args.target_database))
sys.exit(1)
def main():
args = parse_args()
load(args, sys.stdin, bulk_size=args.bulk_size)
main()