From 3c0a1a5c1a77ca1148bef1712a2859400d85ea8c Mon Sep 17 00:00:00 2001 From: Aaron Ploetz Date: Tue, 5 May 2015 13:13:55 -0400 Subject: [PATCH] (cqlsh) allow custom time_format for COPY TO patch by Aaron Ploetz; reviewed by Carl Yeksigian for CASSANDRA-8970 --- CHANGES.txt | 1 + bin/cqlsh | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9a827802e6..1338c6f504 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 2.1.12 + * (cqlsh) allow custom time_format for COPY TO (CASSANDRA-8970) * Don't allow startup if the node's rack has changed (CASSANDRA-10242) * (cqlsh) show partial trace if incomplete after max_trace_wait (CASSANDRA-7645) diff --git a/bin/cqlsh b/bin/cqlsh index 2376d43985..21f8ffe6ec 100755 --- a/bin/cqlsh +++ b/bin/cqlsh @@ -409,7 +409,7 @@ def complete_copy_column_names(ctxt, cqlsh): return set(colnames[1:]) - set(existcols) -COPY_OPTIONS = ('DELIMITER', 'QUOTE', 'ESCAPE', 'HEADER', 'ENCODING', 'NULL') +COPY_OPTIONS = ('DELIMITER', 'QUOTE', 'ESCAPE', 'HEADER', 'ENCODING', 'TIMEFORMAT', 'NULL') @cqlsh_syntax_completer('copyOption', 'optnames') @@ -419,6 +419,7 @@ def complete_copy_options(ctxt, cqlsh): opts = set(COPY_OPTIONS) - set(optnames) if direction == 'FROM': opts -= ('ENCODING',) + opts -= ('TIMEFORMAT',) return opts @@ -1499,12 +1500,14 @@ class Shell(cmd.Cmd): Available options and defaults: - DELIMITER=',' - character that appears between records - QUOTE='"' - quoting character to be used to quote fields - ESCAPE='\' - character to appear before the QUOTE char when quoted - HEADER=false - whether to ignore the first line - NULL='' - string that represents a null value - ENCODING='utf8' - encoding for CSV output (COPY TO only) + DELIMITER=',' - character that appears between records + QUOTE='"' - quoting character to be used to quote fields + ESCAPE='\' - character to appear before the QUOTE char when quoted + HEADER=false - whether to ignore the first line + NULL='' - string that represents a null value + ENCODING='utf8' - encoding for CSV output (COPY TO only) + TIME_FORMAT= - timestamp strftime format (COPY TO only) + '%Y-%m-%d %H:%M:%S%z' defaults to time_format value in cqlshrc When entering CSV data on STDIN, you can use the sequence "\." on a line by itself to end the data input. @@ -1802,6 +1805,7 @@ class Shell(cmd.Cmd): def perform_csv_export(self, ks, cf, columns, fname, opts): dialect_options = self.csv_dialect_defaults.copy() + if 'quote' in opts: dialect_options['quotechar'] = opts.pop('quote') if 'escape' in opts: @@ -1811,6 +1815,7 @@ class Shell(cmd.Cmd): encoding = opts.pop('encoding', 'utf8') nullval = opts.pop('null', '') header = bool(opts.pop('header', '').lower() == 'true') + timestamp_format = opts.pop('time_format', self.display_timestamp_format) if dialect_options['quotechar'] == dialect_options['escapechar']: dialect_options['doublequote'] = True del dialect_options['escapechar'] @@ -1833,7 +1838,7 @@ class Shell(cmd.Cmd): meter = RateMeter(10000) try: - + dtformats = DateTimeFormat(timestamp_format, self.display_date_format, self.display_nanotime_format) dump = self.prep_export_dump(ks, cf, columns) writer = csv.writer(csvdest, **dialect_options) if header: @@ -1841,7 +1846,7 @@ class Shell(cmd.Cmd): for row in dump: fmt = lambda v: \ format_value(v, output_encoding=encoding, nullval=nullval, - time_format=self.display_time_format, + time_format=dtformats, float_precision=self.display_float_precision).strval writer.writerow(map(fmt, row.values())) meter.increment()