From 85e2284f354e867f4fbffca6946aa57544105b31 Mon Sep 17 00:00:00 2001 From: Shuhei Tanuma Date: Fri, 4 Oct 2013 01:41:50 +0900 Subject: [PATCH] add batch option to `td job:result` and `td query`. This idea comes from MySQL's batch mode. You know that mode print the results using tab as the column separator, with each row on a new line. This feature is very useful when copying result and past to Microsoft Excel and some Applications. --- lib/td/command/job.rb | 27 +++++++++++++++++++++++---- lib/td/command/query.rb | 6 +++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/td/command/job.rb b/lib/td/command/job.rb index aa718784..4c8a1435 100644 --- a/lib/td/command/job.rb +++ b/lib/td/command/job.rb @@ -88,6 +88,7 @@ def job_show(op) format = 'tsv' render_opts = {} exclude = false + batch = false op.on('-v', '--verbose', 'show logs', TrueClass) {|b| verbose = b @@ -110,6 +111,9 @@ def job_show(op) op.on('-x', '--exclude', 'do not automatically retrieve the job result', TrueClass) {|b| exclude = b } + op.on('-B', '--batch', 'print results using tab as the column separator, with each row on a new line.', TrueClass) {|b| + batch = b + } job_id = op.cmd_parse @@ -132,13 +136,13 @@ def job_show(op) wait_job(job) if job.success? && [:hive, :pig, :impala].include?(job.type) && !exclude puts "Result :" - show_result(job, output, format, render_opts) + show_result(job, output, format, render_opts, batch) end else if job.success? && [:hive, :pig, :impala].include?(job.type) && !exclude puts "Result :" - show_result(job, output, format, render_opts) + show_result(job, output, format, render_opts, batch) end if verbose @@ -213,8 +217,23 @@ def wait_job(job) end end - def show_result(job, output, format, render_opts={}) - if output + def show_result(job, output, format, render_opts={}, batch = nil) + if batch + print "\n" + job.result_each {|row| + first = true + row.each {|col| + if first + first = false + else + print "\t" + end + print dump_column(col) + } + print "\n" + } + print "\n" + elsif output write_result(job, output, format) puts "written to #{output} in #{format} format" else diff --git a/lib/td/command/query.rb b/lib/td/command/query.rb index 2586f5fc..50673efa 100644 --- a/lib/td/command/query.rb +++ b/lib/td/command/query.rb @@ -18,6 +18,7 @@ def query(op) sampling_all = nil type = nil exclude = false + batch = false op.on('-g', '--org ORGANIZATION', "issue the query under this organization") {|s| org = s @@ -70,6 +71,9 @@ def query(op) op.on('-x', '--exclude', 'do not automatically retrieve the job result', TrueClass) {|b| exclude = b } + op.on('-B', '--batch', 'print results using tab as the column separator, with each row on a new line.', TrueClass) {|b| + batch = b + } sql = op.cmd_parse @@ -114,7 +118,7 @@ def query(op) puts "Status : #{job.status}" if job.success? && !exclude puts "Result :" - show_result(job, output, format, render_opts) + show_result(job, output, format, render_opts, batch) end end end