2929info = "'rethinkdb export` exports data from a RethinkDB cluster into a directory"
3030usage = "\
3131 rethinkdb export [-c HOST:PORT] [-a AUTH_KEY] [-d DIR] [-e (DB | DB.TABLE)]...\n \
32- [--format (csv | json | nsj )] [--fields FIELD,FIELD...] [--delimiter CHARACTER]\n \
32+ [--format (csv | json | ndjson )] [--fields FIELD,FIELD...] [--delimiter CHARACTER]\n \
3333 [--clients NUM]"
3434
3535def print_export_help ():
@@ -42,8 +42,8 @@ def print_export_help():
4242 print (" -a [ --auth ] AUTH_KEY authorization key for rethinkdb clients" )
4343 print (" -d [ --directory ] DIR directory to output to (defaults to" )
4444 print (" rethinkdb_export_DATE_TIME)" )
45- print (" --format (csv | json | nsj) format to write (defaults to json." )
46- print (" nsj is newline separated json.)" )
45+ print (" --format (csv | json | ndjson) format to write (defaults to json." )
46+ print (" ndjson is newline delimited json.)" )
4747 print (" --fields FIELD,FIELD... limit the exported fields to those specified" )
4848 print (" (required for CSV format)" )
4949 print (" -e [ --export ] (DB | DB.TABLE) limit dump to the given database or table (may" )
@@ -75,7 +75,7 @@ def parse_options():
7575 parser = OptionParser (add_help_option = False , usage = usage )
7676 parser .add_option ("-c" , "--connect" , dest = "host" , metavar = "HOST:PORT" , default = "localhost:28015" , type = "string" )
7777 parser .add_option ("-a" , "--auth" , dest = "auth_key" , metavar = "AUTHKEY" , default = "" , type = "string" )
78- parser .add_option ("--format" , dest = "format" , metavar = "json | csv | nsj " , default = "json" , type = "string" )
78+ parser .add_option ("--format" , dest = "format" , metavar = "json | csv | ndjson " , default = "json" , type = "string" )
7979 parser .add_option ("-d" , "--directory" , dest = "directory" , metavar = "DIRECTORY" , default = None , type = "string" )
8080 parser .add_option ("-e" , "--export" , dest = "tables" , metavar = "DB | DB.TABLE" , default = [], action = "append" , type = "string" )
8181 parser .add_option ("--fields" , dest = "fields" , metavar = "<FIELD>,<FIELD>..." , default = None , type = "string" )
@@ -99,8 +99,8 @@ def parse_options():
9999 (res ["host" ], res ["port" ]) = parse_connect_option (options .host )
100100
101101 # Verify valid --format option
102- if options .format not in ["csv" , "json" , "nsj " ]:
103- raise RuntimeError ("Error: Unknown format '%s', valid options are 'csv', 'json', and 'nsj '" % options .format )
102+ if options .format not in ["csv" , "json" , "ndjson " ]:
103+ raise RuntimeError ("Error: Unknown format '%s', valid options are 'csv', 'json', and 'ndjson '" % options .format )
104104 res ["format" ] = options .format
105105
106106 # Verify valid directory option
@@ -242,7 +242,7 @@ def json_writer(filename, fields, task_queue, error_queue, format):
242242 try :
243243 with open (filename , "w" ) as out :
244244 first = True
245- if format != "nsj " :
245+ if format != "ndjson " :
246246 out .write ("[" )
247247 item = task_queue .get ()
248248 while not isinstance (item , StopIteration ):
@@ -252,18 +252,18 @@ def json_writer(filename, fields, task_queue, error_queue, format):
252252 if item not in fields :
253253 del row [item ]
254254 if first :
255- if format == "nsj " :
255+ if format == "ndjson " :
256256 out .write (json .dumps (row ))
257257 else :
258258 out .write ("\n " + json .dumps (row ))
259259 first = False
260- elif format == "nsj " :
260+ elif format == "ndjson " :
261261 out .write ("\n " + json .dumps (row ))
262262 else :
263263 out .write (",\n " + json .dumps (row ))
264264
265265 item = task_queue .get ()
266- if format != "nsj " :
266+ if format != "ndjson " :
267267 out .write ("\n ]\n " )
268268 except :
269269 ex_type , ex_class , tb = sys .exc_info ()
@@ -306,15 +306,18 @@ def csv_writer(filename, fields, delimiter, task_queue, error_queue):
306306 pass
307307
308308def launch_writer (format , directory , db , table , fields , delimiter , task_queue , error_queue ):
309- if format == "json" or format == "nsj" :
309+ if format == "json" :
310310 filename = directory + "/%s/%s.json" % (db , table )
311311 return multiprocessing .Process (target = json_writer ,
312312 args = (filename , fields , task_queue , error_queue , format ))
313313 elif format == "csv" :
314314 filename = directory + "/%s/%s.csv" % (db , table )
315315 return multiprocessing .Process (target = csv_writer ,
316316 args = (filename , fields , delimiter , task_queue , error_queue ))
317-
317+ elif format == "ndjson" :
318+ filename = directory + "/%s/%s.ndjson" % (db , table )
319+ return multiprocessing .Process (target = json_writer ,
320+ args = (filename , fields , task_queue , error_queue , format ))
318321 else :
319322 raise RuntimeError ("unknown format type: %s" % format )
320323
0 commit comments