Skip to content
Browse files

added table:export

  • Loading branch information...
1 parent 3880389 commit fab1d714fde5acdaada38f0e88c153358d9a1d56 @frsyuki frsyuki committed Mar 5, 2012
Showing with 62 additions and 0 deletions.
  1. +1 −0 lib/td/command/list.rb
  2. +61 −0 lib/td/command/table.rb
View
1 lib/td/command/list.rb
@@ -220,6 +220,7 @@ def self.finishup
add_list 'table:create', %w[db table], 'Create a table', 'table:create example_db table1'
add_list 'table:delete', %w[db table], 'Delete a table', 'table:delete example_db table1'
add_list 'table:import', %w[db table files_], 'Parse and import files to a table', 'table:import example_db table1 --apache access.log', 'table:import example_db table1 --json -t time - < test.json'
+ add_list 'table:export', %w[db table], 'Dump logs in a table to the specified storage', 'table:export example_db table1 --s3-bucket mybucket'
add_list 'table:tail', %w[db table], 'Get recently imported logs', 'table:tail example_db table1', 'table:tail example_db table1 -t "2011-01-02 03:04:05" -n 30'
add_list 'result:info', %w[], 'Show information of the MySQL server', 'result:info'
View
61 lib/td/command/table.rb
@@ -135,13 +135,15 @@ def table_tail(op)
if s.to_i.to_s == s
to = s
else
+ require 'time'
to = Time.parse(s).to_i
end
}
op.on('-f', '--from TIME', 'start time of logs to get') {|s|
if s.to_i.to_s == s
from = s
else
+ require 'time'
from = Time.parse(s).to_i
end
}
@@ -178,7 +180,66 @@ def table_tail(op)
}
end
+ def table_export(op)
+ from = nil
+ to = nil
+ s3_bucket = nil
+ wait = false
+
+ ## TODO
+ #op.on('-t', '--to TIME', 'end time of logs to get') {|s|
+ # if s.to_i.to_s == s
+ # to = s
+ # else
+ # require 'time'
+ # to = Time.parse(s).to_i
+ # end
+ #}
+ #op.on('-f', '--from TIME', 'start time of logs to get') {|s|
+ # if s.to_i.to_s == s
+ # from = s
+ # else
+ # require 'time'
+ # from = Time.parse(s).to_i
+ # end
+ #}
+ op.on('-w', '--wait', 'wait for finishing the job', TrueClass) {|b|
+ wait = b
+ }
+ op.on('--s3-bucket NAME', 'name of the s3 bucket to output') {|s|
+ s3_bucket = s
+ }
+
+ db_name, table_name = op.cmd_parse
+
+ unless s3_bucket
+ $stderr.puts "--s3-bucket NAME option is required"
+ exit 1
+ end
+
+ client = get_client
+
+ table = get_table(client, db_name, table_name)
+
+ opts = {}
+ opts['s3_bucket'] = s3_bucket
+ opts['s3_file_format'] ='json.gz'
+ opts['from'] = from.to_s if from
+ opts['to'] = to.to_s if to
+
+ job = table.export('s3', opts)
+
+ $stderr.puts "Export job #{job.job_id} is queued."
+ $stderr.puts "Use '#{$prog} job:show #{job.job_id}' to show the status."
+
+ if wait && !job.finished?
+ wait_job(job)
+ puts "Status : #{job.status}"
+ end
+ end
+
require 'td/command/import' # table:import
+ require 'td/command/job' # wait_job
end
end

0 comments on commit fab1d71

Please sign in to comment.
Something went wrong with that request. Please try again.