-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchbase-migrator.py
executable file
·177 lines (146 loc) · 4.39 KB
/
couchbase-migrator.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
import sys
import getopt
import couchmigrator
#sources: json, csv, sqlite, couchdb, couchbase, membase, memcached, membase-sqlite
#dest: json (chunked), couchbase
def usage(err=None):
if err:
print "Error: %s\n" % err
r = 1
else:
r = 0
print """\
Syntax: couchbase-migrator [options]
Options:
-h, --help
-s <source>, --source=<source>
Data source that will be imported from
-d <destination>, --destination=<destination>
Data destination that will be exported to
-o, --overwrite
Overwrite any items that are in the destination
--dry-run
Do not write any data, just show what would be written
-v, --verbose
-q, --quiet
"""
print "Sources:"
for source in couchmigrator.sources:
print " " + source['type']
print " " + source['example']
print
print
print "Destinations:"
for destination in couchmigrator.destinations:
print " " + destination['type']
print " " + destination['example']
print
"""
Sources:
json
[json:]<filename>
csv
[csv:]<filename>
sqlite
[sqlite:]<filename>
couchdb
[couchdb:]example.com:5984/database
couchbase
[couchbase:]username:password@example.com:8091/bucket
membase
[membase:]username:password@example.com:8091/bucket
membase-sqlite
[membase-sqlite:]<dirname>
memcached
[memcached:]bucket:password@example.com:11211
Destinations:
json
[json:]<filename>
couchbase
[couchbase:]username:password@example.com:8091/bucket
"""
sys.exit(r)
class Config(object):
def __init__(self):
self.source = ''
self.destination = ''
self.overwrite = False
self.dryrun = False
self.verbose = False
self.quiet = False
self.type = ''
self.start = ''
self.end = ''
self.all = ''
def parse_args(argv):
config = Config()
try:
opts, args = getopt.getopt(argv[1:],
'hs:d:t:b:a:e:ovq', [
'help',
'source=',
'destination=',
'overwrite',
'dry-run',
'verbose',
'quiet',
'type',
'start',
'end',
'all'
])
for o, a in opts:
if o == '-h' or o == '--help':
usage()
elif o == '-s' or o == '--source':
config.source = a
elif o == '-d' or o == '--destination':
config.destination = a
elif o == '-t' or o == '--type':
config.type = a
elif o == '-b' or o == '--begin':
config.start = a
elif o == '-e' or o == '--end':
config.end = a
elif o == '-a' or o == '--all':
config.all = a
elif o == '-o' or o == '--overwrite':
config.overwrite = True
elif o == '--dry-run':
config.dryrun = True
elif o == '-v' or o == '--verbose':
config.verbose = True
elif o == '-q' or o == '--quiet':
config.quiet = True
msg = ""
if not config.source or not config.destination:
usage("missing source or destination")
except IndexError:
usage()
except getopt.GetoptError, err:
usage(err)
return config
if __name__ == "__main__":
config = parse_args(sys.argv)
sourcePrefix = "/home/xz93/clusterdata-2011-1/" + config.source + "/part-"
if config.all == '500':
sourceSuffix = "-of-00500.csv"
else:
sourceSuffix = "-of-00001.csv"
count = 0
log = open('log.txt', 'w')
for var in range(int(config.start), int(config.end)):
num = str(var)
config.source = "csv:" + sourcePrefix + '0'*(5-len(num)) + num + sourceSuffix;
reader = couchmigrator.reader(config.source, config.type)
writer = couchmigrator.writer(config.destination, config.type)
for record in reader:
#print "file number: " + num + " id = " + record["id"]
log.write("file number: " + num + " id = " + record["id"] + '\n')
writer.write(record)
count += 1
reader.close()
writer.close()
print 'migrated %d items' % count
log.close()