Module: TreasureData::Command

Includes:
Updater
Defined in:
lib/td/command/db.rb,
lib/td/command/acl.rb,
lib/td/command/job.rb,
lib/td/command/help.rb,
lib/td/command/list.rb,
lib/td/command/user.rb,
lib/td/command/query.rb,
lib/td/command/sched.rb,
lib/td/command/table.rb,
lib/td/command/apikey.rb,
lib/td/command/common.rb,
lib/td/command/export.rb,
lib/td/command/import.rb,
lib/td/command/result.rb,
lib/td/command/runner.rb,
lib/td/command/sample.rb,
lib/td/command/schema.rb,
lib/td/command/server.rb,
lib/td/command/status.rb,
lib/td/command/update.rb,
lib/td/command/account.rb,
lib/td/command/password.rb,
lib/td/command/bulk_import.rb

Defined Under Namespace

Modules: List Classes: BulkImportExecutionError, DownloadProgressIndicator, JsonParser, MessagePackParser, ParameterConfigurationError, Runner, TextParser, TimeBasedDownloadProgressIndicator, UpdateError

Constant Summary collapse

JOB_WAIT_MAX_RETRY_COUNT_ON_NETWORK_ERROR =

TODO

10
PRIORITY_FORMAT_MAP =
{
  -2 => 'VERY LOW',
  -1 => 'LOW',
  0 => 'NORMAL',
  1 => 'HIGH',
  2 => 'VERY HIGH',
}
PRIORITY_PARSE_MAP =
{
  /\Avery[ _\-]?low\z/i => -2,
  /\A-2\z/ => -2,
  /\Alow\z/i => -1,
  /\A-1\z/ => -1,
  /\Anorm(?:al)?\z/i => 0,
  /\A[\-\+]?0\z/ => 0,
  /\Ahigh\z/i => 1,
  /\A[\+]?1\z/ => 1,
  /\Avery[ _\-]?high\z/i => 2,
  /\A[\+]?2\z/ => 2,
}
HIVE_RESERVED_KEYWORDS =
%W[
  TRUE FALSE ALL AND OR NOT LIKE ASC DESC ORDER BY GROUP WHERE FROM AS SELECT DISTINCT INSERT OVERWRITE
  OUTER JOIN LEFT RIGHT FULL ON PARTITION PARTITIONS TABLE TABLES TBLPROPERTIES SHOW MSCK DIRECTORY LOCAL
  TRANSFORM USING CLUSTER DISTRIBUTE SORT UNION LOAD DATA INPATH IS NULL CREATE EXTERNAL ALTER DESCRIBE
  DROP REANME TO COMMENT BOOLEAN TINYINT SMALLINT INT BIGINT FLOAT DOUBLE DATE DATETIME TIMESTAMP STRING
  BINARY ARRAY MAP REDUCE PARTITIONED CLUSTERED SORTED INTO BUCKETS ROW FORMAT DELIMITED FIELDS TERMINATED
  COLLECTION ITEMS KEYS LINES STORED SEQUENCEFILE TEXTFILE INPUTFORMAT OUTPUTFORMAT LOCATION TABLESAMPLE BUCKET OUT
  OF CAST ADD REPLACE COLUMNS RLIKE REGEXP TEMPORARY FUNCTION EXPLAIN EXTENDED SERDE WITH SERDEPROPERTIES LIMIT SET TBLPROPERTIES
]
IMPORT_TEMPLATES =
{
  'apache' => [
                /^([^ ]*) [^ ]* ([^ ]*) \[([^\]]*)\] "(\S+)(?: +([^ ]*) +\S*)?" ([^ ]*) ([^ ]*)(?: "([^\"]*)" "([^\"]*)")?$/,
                ['host', 'user', 'time', 'method', 'path', 'code', 'size', 'referer', 'agent'],
                "%d/%b/%Y:%H:%M:%S %z"],
  'syslog' => [
                /^([^ ]* [^ ]* [^ ]*) ([^ ]*) ([a-zA-Z0-9_\/\.\-]*)(?:\[([0-9]+)\])?[^\:]*\: *(.*)$/,
                ['time', 'host', 'ident', 'pid', 'message'],
                "%b %d %H:%M:%S"],
}
SUPPORTED_FORMATS =
%W[json.gz line-json.gz]
JAVA_COMMAND =
"java"
JAVA_MAIN_CLASS =
"com.treasure_data.td_import.BulkImportCommand"
JVM_OPTS =

TODO

["-Xmx1024m"]

Instance Method Summary collapse

Methods included from Updater

client_version_from_path, compare_versions, disable, disable?, disable_message, endpoint_root, fetch, get_client_version_file, home_directory, inject_libpath, installed_client_path, #jarfile_dest_path, last_toolbelt_autoupdate_timestamp, latest_local_version, on_mac?, on_windows?, package_category, raise_error, update, update_package_endpoint, updated_client_path, updating_lock_path, version_endpoint, wait_for_lock

Instance Method Details

#account(op) ⇒ Object



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
# File 'lib/td/command/account.rb', line 6

def (op)
  op.banner << "\noptions:\n"

  force = false
  op.on('-f', '--force', 'overwrite current account setting', TrueClass) {|b|
    force = true
  }

  user_name = op.cmd_parse

  endpoint = nil
  # user may be calling 'td account' with the -e / --endpoint
  # option, which we want to preserve and save
  begin
     endpoint = Config.endpoint
  rescue ConfigNotFoundError => e
    # the endpoint is neither stored in the config file
    # nor passed as option on the command line
  end

  conf = nil
  begin
    conf = Config.read
  rescue ConfigError
  end

  if conf && conf['account.apikey']
    unless force
      if conf['account.user']
        $stderr.puts "Account is already configured with '#{conf['account.user']}' account."
      else
        $stderr.puts "Account is already configured."
      end
      $stderr.puts "Add '-f' option to overwrite."
      exit 0
    end
  end

  puts "Enter your Treasure Data credentials."
  unless user_name
    begin
      print "Email: "
      line = STDIN.gets || ""
      user_name = line.strip
    rescue Interrupt
      $stderr.puts "\ncanceled."
      exit 1
    end
  end

  if user_name.empty?
    $stderr.puts "canceled."
    exit 0
  end

  client = nil

  3.times do
    begin
      print "Password (typing will be hidden): "
      password = get_password
    rescue Interrupt
      $stderr.print "\ncanceled."
      exit 1
    ensure
      system "stty echo"   # TODO termios
      print "\n"
    end

    if password.empty?
      $stderr.puts "canceled."
      exit 0
    end

    begin
      # enalbe SSL for the authentication
      opts = {}
      opts[:ssl] = true
      opts[:endpoint] = endpoint if endpoint
      client = Client.authenticate(user_name, password, opts)
    rescue TreasureData::AuthError
      $stderr.puts "User name or password mismatched."
    end

    break if client
  end
  return unless client

  puts "Authenticated successfully."

  conf ||= Config.new
  conf["account.user"] = user_name
  conf["account.apikey"] = client.apikey
  conf['account.endpoint'] = endpoint if endpoint
  conf.save

  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "db:create <db_name>' to create a database."
end

#account_usage(op) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/td/command/account.rb', line 105

def (op)
  op.cmd_parse

  client = get_client
  a = client.

  $stderr.puts "Storage:  #{a.storage_size_string}"
end

#acl_grant(op) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/td/command/acl.rb', line 25

def acl_grant(op)
  grant_option = true

  op.on('--no-grant-option', '-N', 'Grant without grant option', TrueClass) {|b|
    grant_option = !b
  }

  subject, action, scope = op.cmd_parse

  client = get_client

  client.grant_access_control(subject, action, scope, grant_option)

  $stderr.puts "Access control [#{subject} #{action} #{scope}] is created #{grant_option ? 'with' : 'without'} grant option."
end

#acl_list(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/td/command/acl.rb', line 5

def acl_list(op)
  op.cmd_parse

  client = get_client

  acl = client.access_controls

  rows = []
  acl.each {|ac|
    rows << {:Subject => ac.subject, :Action => ac.action, :Scope => ac.scope, :"Grant option" => ac.grant_option}
  }

  puts cmd_render_table(rows, :fields => [:Subject, :Action, :Scope, :"Grant option"])

  if rows.empty?
    $stderr.puts "There are no access controls."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "acl:grant <subject> <action> <scope>' to grant permissions."
  end
end

#acl_revoke(op) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/td/command/acl.rb', line 41

def acl_revoke(op)
  subject, action, scope = op.cmd_parse

  client = get_client

  client.revoke_access_control(subject, action, scope)

  $stderr.puts "Access control [#{subject} #{action} #{scope}] is removed."
end

#apikey_set(op) ⇒ Object



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
# File 'lib/td/command/apikey.rb', line 26

def apikey_set(op)
  op.banner << "\noptions:\n"

  force = false
  op.on('-f', '--force', 'overwrite current account setting', TrueClass) {|b|
    force = true
  }

  apikey = op.cmd_parse

  conf = nil
  begin
    conf = Config.read
  rescue ConfigError
  end
  if conf && conf['account.apikey']
    unless force
      if conf['account.user']
        $stderr.puts "Account is already configured with '#{conf['account.user']}' account."
      else
        $stderr.puts "Account is already configured."
      end
      $stderr.puts "Add '-f' option to overwrite."
      exit 0
    end
  end

  conf ||= Config.new
  conf.delete("account.user")
  conf["account.apikey"] = apikey
  conf.save

  puts "API key is set."
  puts "Use '#{$prog} db:create <db_name>' to create a database."
end

#apikey_show(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/td/command/apikey.rb', line 5

def apikey_show(op)
  if Config.apikey
    puts Config.apikey
    return
  end

  conf = nil
  begin
    conf = Config.read
  rescue ConfigError
  end

  if !conf || !conf['account.apikey']
    $stderr.puts "Account is not configured yet."
    $stderr.puts "Use '#{$prog} apikey:set' or '#{$prog} account' first."
    exit 1
  end

  puts conf['account.apikey']
end

#bulk_import_commit(op) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/td/command/bulk_import.rb', line 270

def bulk_import_commit(op)
  wait = false

  op.on('-w', '--wait', 'wait for finishing the commit', TrueClass) {|b|
    wait = b
  }

  name = op.cmd_parse

  client = get_client

  job = client.commit_bulk_import(name)

  $stderr.puts "Bulk import session '#{name}' started to commit."

  if wait
    wait_commit(name) # wait the commit
  end
end

#bulk_import_create(op) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/td/command/bulk_import.rb', line 27

def bulk_import_create(op)
  name, db_name, table_name = op.cmd_parse

  client = get_client

  table = get_table(client, db_name, table_name)

  opts = {}
  client.create_bulk_import(name, db_name, table_name, opts)

  $stderr.puts "Bulk import session '#{name}' is created."
end

#bulk_import_delete(op) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/td/command/bulk_import.rb', line 40

def bulk_import_delete(op)
  name = op.cmd_parse

  client = get_client

  begin
    client.delete_bulk_import(name)
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Bulk import session '#{name}' does not exist."
    exit 1
  end

  $stderr.puts "Bulk import session '#{name}' is deleted."
end

#bulk_import_delete_part(op) ⇒ Object

obsoleted



196
197
198
199
200
201
202
203
204
# File 'lib/td/command/bulk_import.rb', line 196

def bulk_import_delete_part(op)
  name, part_name = op.cmd_parse

  client = get_client

  client.bulk_import_delete_part(name, part_name)

  $stderr.puts "Part '#{part_name}' is deleted."
end

#bulk_import_delete_parts(op) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/td/command/bulk_import.rb', line 206

def bulk_import_delete_parts(op)
  part_prefix = ""

  op.on('-P', '--prefix NAME', 'add prefix to parts name') {|s|
    part_prefix = s
  }

  name, *part_names = op.cmd_parse

  client = get_client

  part_names.each {|part_name|
    part_name = part_prefix + part_name

    $stderr.puts "Deleting '#{part_name}'..."
    client.bulk_import_delete_part(name, part_name)
  }

  $stderr.puts "done."
end

#bulk_import_list(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/td/command/bulk_import.rb', line 5

def bulk_import_list(op)
  set_render_format_option(op)

  op.cmd_parse

  client = get_client

  bis = client.bulk_imports

  rows = []
  bis.each {|bi|
    rows << {:Name=>bi.name, :Table=>"#{bi.database}.#{bi.table}", :Status=>bi.status.to_s.capitalize, :Frozen=>bi.upload_frozen? ? 'Frozen' : '', :JobID=>bi.job_id, :"Valid Parts"=>bi.valid_parts, :"Error Parts"=>bi.error_parts, :"Valid Records"=>bi.valid_records, :"Error Records"=>bi.error_records}
  }

  puts cmd_render_table(rows, :fields => [:Name, :Table, :Status, :Frozen, :JobID, :"Valid Parts", :"Error Parts", :"Valid Records", :"Error Records"], :max_width=>200, :render_format => op.render_format)

  if rows.empty?
    $stderr.puts "There are no bulk import sessions."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "bulk_import:create <name> <db> <table>' to create a session."
  end
end

#bulk_import_perform(op) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/td/command/bulk_import.rb', line 227

def bulk_import_perform(op)
  wait = false
  force = false

  op.on('-w', '--wait', 'wait for finishing the job', TrueClass) {|b|
    wait = b
  }
  op.on('-f', '--force', 'force start performing', TrueClass) {|b|
    force = b
  }

  name = op.cmd_parse

  client = get_client

  unless force
    bis = client.bulk_imports
    bi = bis.find {|bi| name == bi.name }
    if bi
      if bi.status == 'performing'
        $stderr.puts "Bulk import session '#{name}' is already performing."
        $stderr.puts "Add '-f' option to force start."
        $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "job:kill #{bi.job_id}' to cancel the last trial."
        exit 1
      elsif bi.status == 'ready'
        $stderr.puts "Bulk import session '#{name}' is already ready to commit."
        $stderr.puts "Add '-f' option to force start."
        exit 1
      end
    end
  end

  job = client.perform_bulk_import(name)

  $stderr.puts "Job #{job.job_id} is queued."
  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "job:show [-w] #{job.job_id}' to show the status."

  if wait
    require 'td/command/job'  # wait_job
    wait_job(job)
  end
end

#bulk_import_show(op) ⇒ Object



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
# File 'lib/td/command/bulk_import.rb', line 56

def bulk_import_show(op)
  name = op.cmd_parse

  client = get_client

  bi = client.bulk_import(name)
  unless bi
    $stderr.puts "Bulk import session '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "bulk_import:create <name> <db> <table>' to create a session."
    exit 1
  end

  $stderr.puts "Name         : #{bi.name}"
  $stderr.puts "Database     : #{bi.database}"
  $stderr.puts "Table        : #{bi.table}"
  $stderr.puts "Status       : #{bi.status.to_s.capitalize}"
  $stderr.puts "Frozen       : #{bi.upload_frozen?}"
  $stderr.puts "JobID        : #{bi.job_id}"
  $stderr.puts "Valid Records: #{bi.valid_records}"
  $stderr.puts "Error Records: #{bi.error_records}"
  $stderr.puts "Valid Parts  : #{bi.valid_parts}"
  $stderr.puts "Error Parts  : #{bi.error_parts}"
  $stderr.puts "Uploaded Parts :"

  list = client.list_bulk_import_parts(name)
  list.each {|name|
    puts "  #{name}"
  }
end

#bulk_import_upload_part(op) ⇒ Object

obsoleted



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/td/command/bulk_import.rb', line 87

def bulk_import_upload_part(op)
  retry_limit = 10
  retry_wait = 1

  name, part_name, path = op.cmd_parse

  File.open(path, "rb") {|io|
    bulk_import_upload_impl(name, part_name, io, io.size, retry_limit, retry_wait)
  }

  $stderr.puts "Part '#{part_name}' is uploaded."
end

#bulk_import_upload_parts(op) ⇒ Object



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
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/td/command/bulk_import.rb', line 100

def bulk_import_upload_parts(op)
  retry_limit = 10
  retry_wait = 1
  suffix_count = 0
  part_prefix = ""
  auto_perform = false
  parallel = 2

  op.on('-P', '--prefix NAME', 'add prefix to parts name') {|s|
    part_prefix = s
  }
  op.on('-s', '--use-suffix COUNT', 'use COUNT number of . (dots) in the source file name to the parts name', Integer) {|i|
    suffix_count = i
  }
  op.on('--auto-perform', 'perform bulk import job automatically', TrueClass) {|b|
    auto_perform = b
  }
  op.on('--parallel NUM', 'perform uploading in parallel (default: 2; max 8)', Integer) {|i|
    parallel = i
  }

  name, *files = op.cmd_parse

  # validate the session
  bi = get_client.bulk_imports.find {|bi| name == bi.name }
  unless bi
    $stderr.puts "Bulk import session '#{name}' does not exist. Please check the first argument of the command."
    exit 1
  end

  parallel = 1 if parallel <= 1
  parallel = 8 if parallel >= 8

  threads = (1..parallel).map {|i|
    Thread.new do
      errors = []
      until files.empty?
        ifname = files.shift
        basename = File.basename(ifname)
        begin
          part_name = part_prefix + basename.split('.')[0..suffix_count].join('.')

          File.open(ifname, "rb") {|io|
            size = io.size
            $stderr.write "Uploading '#{ifname}' -> '#{part_name}'... (#{size} bytes)\n"

            bulk_import_upload_impl(name, part_name, io, size, retry_limit, retry_wait)
          }
        rescue
          errors << [ifname, $!]
        end
      end
      errors
    end
  }

  errors = []
  threads.each {|t|
    errors.concat t.value
  }

  unless errors.empty?
    $stderr.puts "failed to upload #{errors.size} files."
    $stderr.puts "backtraces:"
    errors.each {|(ifname,ex)|
      $stderr.puts "  #{ifname}: #{ex}"
      ex.backtrace.each {|bt|
        $stderr.puts "      #{ifname}: #{bt}"
      }
    }
    $stderr.puts "files:"
    ifnames = errors.map {|(ifname,ex)| ifname }
    ifnames.each {|ifname|
      $stderr.puts "  #{ifname}"
    }
    $stderr.puts "You can retry uploading by following command:"
    $stderr.puts "td bulk_import:upload_parts #{name} #{ifnames.map {|ifname| "'#{ifname}'" }.join(' ')}"
    exit 1
  end

  $stderr.puts "done."

  if auto_perform
    client = get_client
    job = client.perform_bulk_import(name)

    $stderr.puts "Job #{job.job_id} is queued."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "job:show [-w] #{job.job_id}' to show the status."
  end
end

#bulk_import_upload_parts2(op) ⇒ Object



191
192
193
# File 'lib/td/command/bulk_import.rb', line 191

def bulk_import_upload_parts2(op)
  puts "This command is moved to 'td import:upload' since 0.10.85."
end

#db_create(op) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/td/command/db.rb', line 48

def db_create(op)
  db_name = op.cmd_parse

  API.validate_database_name(db_name)

  client = get_client

  opts = {}
  begin
    client.create_database(db_name, opts)
  rescue AlreadyExistsError
    $stderr.puts "Database '#{db_name}' already exists."
    exit 1
  end

  $stderr.puts "Database '#{db_name}' is created."
  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "table:create #{db_name} <table_name>' to create a table."
end

#db_delete(op) ⇒ Object



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
# File 'lib/td/command/db.rb', line 67

def db_delete(op)
  force = false
  op.on('-f', '--force', 'clear tables and delete the database', TrueClass) {|b|
    force = true
  }

  db_name = op.cmd_parse

  client = get_client

  begin
    db = client.database(db_name)

    if !force && !db.tables.empty?
      $stderr.puts "Database '#{db_name}' is not empty. Use '-f' option or drop tables first."
      exit 1
    end

    db.delete
  rescue NotFoundError
    $stderr.puts "Database '#{db_name}' does not exist."
    exit 1
  end

  $stderr.puts "Database '#{db_name}' is deleted."
end

#db_list(op) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/td/command/db.rb', line 28

def db_list(op)
  set_render_format_option(op)

  op.cmd_parse

  client = get_client
  dbs = client.databases

  rows = []
  dbs.each {|db|
    rows << {:Name=>db.name, :Count=>db.count}
  }
  puts cmd_render_table(rows, :fields => [:Name, :Count], :render_format => op.render_format)

  if dbs.empty?
    $stderr.puts "There are no databases."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "db:create <db_name>' to create a database."
  end
end

#db_show(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/td/command/db.rb', line 5

def db_show(op)
  set_render_format_option(op)

  db_name = op.cmd_parse

  client = get_client

  db = get_database(client, db_name)

  rows = []
  db.tables.each {|table|
    pschema = table.schema.fields.map {|f|
      "#{f.name}:#{f.type}"
    }.join(', ')
    rows << {:Table => table.name, :Type => table.type.to_s, :Count => table.count.to_s, :Schema=>pschema.to_s}
  }
  rows = rows.sort_by {|map|
    [map[:Type].size, map[:Table]]
  }

  puts cmd_render_table(rows, :fields => [:Table, :Type, :Count, :Schema], :render_format => op.render_format)
end

#help(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/td/command/help.rb', line 5

def help(op)
  cmd = op.cmd_parse

  c = List.get_option(cmd)
  if c == nil
     $stderr.puts "'#{cmd}' is not a td command. Run '#{$prog}' to show the list."
     List.show_guess(cmd)
     exit 1

  elsif c.name != cmd && c.group == cmd
    # group command
    puts List.cmd_usage(cmd)
    exit 1

  else
    method = List.get_method(cmd)
    method.call(['--help'])
  end
end

#help_all(op) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/td/command/help.rb', line 25

def help_all(op)
  cmd = op.cmd_parse

  TreasureData::Command::List.show_help(op.summary_indent)
  puts ""
  puts "Type '#{$prog} help COMMAND' for more information on a specific command."
end

#import_auto(op) ⇒ Object



44
45
46
# File 'lib/td/command/import.rb', line 44

def import_auto(op)
  import_by_java('auto')
end

#import_commit(op) ⇒ Object



58
59
60
61
# File 'lib/td/command/import.rb', line 58

def import_commit(op)
  require 'td/command/bulk_import'
  bulk_import_commit(op)
end

#import_create(op) ⇒ Object



22
23
24
25
# File 'lib/td/command/import.rb', line 22

def import_create(op)
  require 'td/command/bulk_import'
  bulk_import_create(op)
end

#import_delete(op) ⇒ Object



63
64
65
66
# File 'lib/td/command/import.rb', line 63

def import_delete(op)
  require 'td/command/bulk_import'
  bulk_import_delete(op)
end

#import_error_records(op) ⇒ Object



53
54
55
56
# File 'lib/td/command/import.rb', line 53

def import_error_records(op)
  require 'td/command/bulk_import'
  bulk_import_error_records(op)
end

#import_freeze(op) ⇒ Object



68
69
70
71
# File 'lib/td/command/import.rb', line 68

def import_freeze(op)
  require 'td/command/bulk_import'
  bulk_import_freeze(op)
end

#import_jar_update(op) ⇒ Object



32
33
34
# File 'lib/td/command/import.rb', line 32

def import_jar_update(op)
  check_n_update_jar(false)
end

#import_jar_version(op) ⇒ Object



27
28
29
30
# File 'lib/td/command/import.rb', line 27

def import_jar_version(op)
  version = find_version_file
  puts "td-import-java #{File.open(version, 'r').read}"
end

#import_list(op) ⇒ Object



12
13
14
15
# File 'lib/td/command/import.rb', line 12

def import_list(op)
  require 'td/command/bulk_import'
  bulk_import_list(op)
end

#import_perform(op) ⇒ Object



48
49
50
51
# File 'lib/td/command/import.rb', line 48

def import_perform(op)
  require 'td/command/bulk_import'
  bulk_import_perform(op)
end

#import_prepare(op) ⇒ Object



36
37
38
# File 'lib/td/command/import.rb', line 36

def import_prepare(op)
  import_by_java('prepare')
end

#import_show(op) ⇒ Object



17
18
19
20
# File 'lib/td/command/import.rb', line 17

def import_show(op)
  require 'td/command/bulk_import'
  bulk_import_show(op)
end

#import_unfreeze(op) ⇒ Object



73
74
75
76
# File 'lib/td/command/import.rb', line 73

def import_unfreeze(op)
  require 'td/command/bulk_import'
  bulk_import_unfreeze(op)
end

#import_upload(op) ⇒ Object



40
41
42
# File 'lib/td/command/import.rb', line 40

def import_upload(op)
  import_by_java('upload')
end

#job_kill(op) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/td/command/job.rb', line 236

def job_kill(op)
  job_id = op.cmd_parse

  client = get_client

  former_status = client.kill(job_id)
  if TreasureData::Job::FINISHED_STATUS.include?(former_status)
    $stderr.puts "Job #{job_id} is already finished (#{former_status})"
    exit 0
  end

  if former_status == TreasureData::Job::STATUS_RUNNING
    $stderr.puts "Job #{job_id} is killed."
  else
    $stderr.puts "Job #{job_id} is canceled."
  end
end

#job_list(op) ⇒ Object



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
# File 'lib/td/command/job.rb', line 29

def job_list(op)
  page = 0
  skip = 0
  status = nil
  slower_than = nil

  op.on('-p', '--page PAGE', 'skip N pages', Integer) {|i|
    page = i
  }
  op.on('-s', '--skip N', 'skip N jobs', Integer) {|i|
    skip = i
  }
  op.on('-R', '--running', 'show only running jobs', TrueClass) {|b|
    status = 'running'
  }
  op.on('-S', '--success', 'show only succeeded jobs', TrueClass) {|b|
    status = 'success'
  }
  op.on('-E', '--error', 'show only failed jobs', TrueClass) {|b|
    status = 'error'
  }
  op.on('--slow [SECONDS]', 'show slow queries (default threshold: 3600 seconds)', Integer) {|i|
    slower_than = i || 3600
  }
  set_render_format_option(op)

  max = op.cmd_parse

  max = (max || 20).to_i

  client = get_client

  if page
    skip += max * page
  end

  conditions = nil
  if slower_than
    conditions = {:slower_than => slower_than}
  end

  jobs = client.jobs(skip, skip + max - 1, status, conditions)

  rows = []
  jobs.each {|job|
    start = job.start_at
    elapsed = Command.humanize_elapsed_time(start, job.end_at)
    cpu_time = Command.humanize_time(job.cpu_time, true)
    priority = job_priority_name_of(job.priority)
    rows << {
      :JobID => job.job_id,
      :Database => job.db_name,
      :Status => job.status,
      :Type => job.type,
      :Query => job.query.to_s[0,50] + " ...",
      :Start => (start ? start.localtime : ''),
      :Elapsed => elapsed.rjust(11),
      :CPUTime => cpu_time.rjust(17),
      :Priority => priority,
      :Result => job.result_url
    }
  }

  puts cmd_render_table(rows,
    :fields => [:JobID, :Status, :Start, :Elapsed, :CPUTime, :Priority, :Result, :Type, :Database, :Query],
    :max_width => 1000,
    :render_format => op.render_format
  )
end

#job_show(op) ⇒ Object



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/td/command/job.rb', line 99

def job_show(op)
  verbose = nil
  wait = false
  output = nil
  format = nil
  render_opts = {:header => false}
  limit = nil
  exclude = false

  op.on('-v', '--verbose', 'show logs', TrueClass) {|b|
    verbose = b
  }
  op.on('-w', '--wait', 'wait for finishing the job', TrueClass) {|b|
    wait = b
  }
  op.on('-G', '--vertical', 'use vertical table to show results', TrueClass) {|b|
    render_opts[:vertical] = b
  }
  op.on('-o', '--output PATH', 'write result to the file') {|s|
    output = s
    format = 'tsv' if format.nil?
  }
  op.on('-f', '--format FORMAT', 'format of the result to write to the file (tsv, csv, json, msgpack, and msgpack.gz)') {|s|
    unless ['tsv', 'csv', 'json', 'msgpack', 'msgpack.gz'].include?(s)
      raise "Unknown format #{s.dump}. Supported formats are: tsv, csv, json, msgpack, and msgpack.gz"
    end
    format = s
  }
  op.on('-l', '--limit ROWS', 'limit the number of result rows shown when not outputting to file') {|s|
    unless s.to_i > 0
      raise "Invalid limit number. Must be a positive integer"
    end
    limit = s.to_i
  }
  op.on('-c', '--column-header', 'output of the columns\' header when the schema is available',
                                 '  for the table (only applies to tsv and csv formats)', TrueClass) {|b|
    render_opts[:header] = b;
  }
  op.on('-x', '--exclude', 'do not automatically retrieve the job result', TrueClass) {|b|
    exclude = b
  }

  job_id = op.cmd_parse

  # parameter concurrency validation

  if output.nil? && format
    unless ['tsv', 'csv', 'json'].include?(format)
      raise ParameterConfigurationError,
            "Supported formats are only tsv, csv and json without -o / --output option"
    end
  end

  if render_opts[:header]
    unless ['tsv', 'csv'].include?(format)
      raise ParameterConfigurationError,
            "Option -c / --column-header is only supported with tsv and csv formats"
    end
  end

  if !output.nil? && !limit.nil?
    raise ParameterConfigurationError,
          "Option -l / --limit is only valid when not outputting to file (no -o / --output option provided)"
  end

  client = get_client

  job = client.job(job_id)

  puts "JobID       : #{job.job_id}"
  #puts "URL         : #{job.url}"
  puts "Status      : #{job.status}"
  puts "Type        : #{job.type}"
  puts "Database    : #{job.db_name}"
  # exclude some fields from bulk_import_perform type jobs
  if [:hive, :pig, :impala, :presto].include?(job.type)
    puts "Priority    : #{job_priority_name_of(job.priority)}"
    puts "Retry limit : #{job.retry_limit}"
    puts "Output      : #{job.result_url}"
    puts "Query       : #{job.query}"
  elsif job.type == :bulk_import_perform
    puts "Destination : #{job.query}"
  end
  # if the job is done and is of type hive, show the Map-Reduce cumulated CPU time
  if job.finished? && [:hive].include?(job.type)
    puts "CPU time    : #{Command.humanize_time(job.cpu_time, true)}"
  end

  if wait && !job.finished?
    wait_job(job)
    if [:hive, :pig, :impala, :presto].include?(job.type) && !exclude
      puts "Result      :"
      begin
        show_result(job, output, limit, format, render_opts)
      rescue TreasureData::NotFoundError => e
        # Got 404 because result not found.
      end
    end

  else
    if [:hive, :pig, :impala, :presto].include?(job.type) && !exclude && job.finished?
      puts "Result      :"
      begin
        show_result(job, output, limit, format, render_opts)
      rescue TreasureData::NotFoundError => e
        # Got 404 because result not found.
      end
    end

    if verbose
      if !job.debug['cmdout'].nil?
        puts ""
        puts "Output:"
        job.debug['cmdout'].to_s.split("\n").each {|line|
          puts "  " + line
        }
      end
      if !job.debug['stderr'].nil?
        puts ""
        puts "Details:"
        job.debug['stderr'].to_s.split("\n").each {|line|
          puts "  " + line
        }
      end
    end
  end

  puts "Use '-v' option to show detailed messages." unless verbose
end

#job_status(op) ⇒ Object



229
230
231
232
233
234
# File 'lib/td/command/job.rb', line 229

def job_status(op)
  job_id = op.cmd_parse
  client = get_client

  puts client.job_status(job_id)
end

#password_change(op) ⇒ Object



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
# File 'lib/td/command/password.rb', line 5

def password_change(op)
  op.cmd_parse

  old_password = nil
  password = nil

  begin
    system "stty -echo"  # TODO termios
    print "Old password (typing will be hidden): "
    old_password = STDIN.gets || ""
    old_password = old_password[0..-2]  # strip \n
  rescue Interrupt
    $stderr.print "\ncanceled."
    exit 1
  ensure
    system "stty echo"   # TODO termios
    print "\n"
  end

  if old_password.empty?
    $stderr.puts "canceled."
    exit 0
  end

  3.times do
    begin
      system "stty -echo"  # TODO termios
      print "New password (typing will be hidden): "
      password = STDIN.gets || ""
      password = password[0..-2]  # strip \n
    rescue Interrupt
      $stderr.print "\ncanceled."
      exit 1
    ensure
      system "stty echo"   # TODO termios
      print "\n"
    end

    if password.empty?
      $stderr.puts "canceled."
      exit 0
    end

    begin
      system "stty -echo"  # TODO termios
      print "Retype new password: "
      password2 = STDIN.gets || ""
      password2 = password2[0..-2]  # strip \n
    rescue Interrupt
      $stderr.print "\ncanceled."
      exit 1
    ensure
      system "stty echo"   # TODO termios
      print "\n"
    end

    if password == password2
      break
    end

    puts "Doesn't match."
  end

  client = get_client(:ssl => true)

  client.change_my_password(old_password, password)

  $stderr.puts "Password changed."
end

#query(op) ⇒ Object



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
# File 'lib/td/command/query.rb', line 5

def query(op)
  db_name = nil
  wait = false
  output = nil
  format = nil
  render_opts = {:header => false}
  result_url = nil
  result_user = nil
  result_ask_password = false
  priority = nil
  retry_limit = nil
  query = nil
  type = nil
  limit = nil
  exclude = false

  op.on('-d', '--database DB_NAME', 'use the database (required)') {|s|
    db_name = s
  }
  op.on('-w', '--wait', 'wait for finishing the job', TrueClass) {|b|
    wait = b
  }
  op.on('-G', '--vertical', 'use vertical table to show results', TrueClass) {|b|
    render_opts[:vertical] = b
  }
  op.on('-o', '--output PATH', 'write result to the file') {|s|
    output = s
    format = 'tsv' if format.nil?
  }
  op.on('-f', '--format FORMAT', 'format of the result to write to the file (tsv, csv, json or msgpack)') {|s|
    unless ['tsv', 'csv', 'json', 'msgpack'].include?(s)
      raise "Unknown format #{s.dump}. Supported format: tsv, csv, json, msgpack"
    end
    format = s
  }
  op.on('-r', '--result RESULT_URL', 'write result to the URL (see also result:create subcommand)',
                                     ' It is suggested for this option to be used with the -x / --exclude option to suppress printing',
                                     ' of the query result to stdout or -o / --output to dump the query result into a file.') {|s|
    result_url = s
  }
  op.on('-u', '--user NAME', 'set user name for the result URL') {|s|
    result_user = s
  }
  op.on('-p', '--password', 'ask password for the result URL') {|s|
    result_ask_password = true
  }
  op.on('-P', '--priority PRIORITY', 'set priority') {|s|
    priority = job_priority_id_of(s)
    unless priority
      raise "unknown priority #{s.inspect} should be -2 (very-low), -1 (low), 0 (normal), 1 (high) or 2 (very-high)"
    end
  }
  op.on('-R', '--retry COUNT', 'automatic retrying count', Integer) {|i|
    retry_limit = i
  }
  op.on('-q', '--query PATH', 'use file instead of inline query') {|s|
    query = File.open(s) { |f| f.read.strip }
  }
  op.on('-T', '--type TYPE', 'set query type (hive, pig, impala, presto)') {|s|
    type = s.to_sym
  }
  op.on('--sampling DENOMINATOR', 'OBSOLETE - enable random sampling to reduce records 1/DENOMINATOR', Integer) {|i|
    puts "WARNING: the random sampling feature enabled through the '--sampling' option was removed and does no longer"
    puts "         have any effect. It is left for backwards compatibility with older scripts using 'td'."
    puts
  }
  op.on('-l', '--limit ROWS', 'limit the number of result rows shown when not outputting to file') {|s|
    unless s.to_i > 0
      raise "Invalid limit number. Must be a positive integer"
    end
    limit = s.to_i
  }
  op.on('-c', '--column-header', 'output of the columns\' header when the schema is available for the table (only applies to tsv and csv formats)', TrueClass) {|b|
    render_opts[:header] = b;
  }
  op.on('-x', '--exclude', 'do not automatically retrieve the job result', TrueClass) {|b|
    exclude = b
  }

  sql = op.cmd_parse

  # required parameters

  unless db_name
    raise ParameterConfigurationError,
          "-d / --database DB_NAME option is required."
  end

  if sql == '-'
    sql = STDIN.read
  elsif sql.nil?
    sql = query
  end
  unless sql
    raise ParameterConfigurationError,
          "<sql> argument or -q / --query PATH option is required."
  end

  # parameter concurrency validation

  if output.nil? && format
    unless ['tsv', 'csv', 'json'].include?(format)
      raise ParameterConfigurationError,
            "Supported formats are only tsv, csv and json without --output option"
    end
  end

  if render_opts[:header]
    unless ['tsv', 'csv'].include?(format)
      raise ParameterConfigurationError,
            "Option -c / --column-header is only supported with tsv and csv formats"
    end
  end

  if result_url
    require 'td/command/result'
    result_url = build_result_url(result_url, result_user, result_ask_password)
  end

  client = get_client

  # local existence check
  get_database(client, db_name)

  opts = {}
  opts['type'] = type if type
  job = client.query(db_name, sql, result_url, priority, retry_limit, opts)

  puts "Job #{job.job_id} is queued."
  puts "Use '#{$prog} " + Config.cl_options_string + "job:show #{job.job_id}' to show the status."
  #puts "See #{job.url} to see the progress."

  if wait
    wait_job(job, true)
    puts "Status     : #{job.status}"
    if job.success? && !exclude
      puts "Result     :"
      begin
        show_result(job, output, limit, format, render_opts)
      rescue TreasureData::NotFoundError => e
      end
    end
  end
end

#result_create(op) ⇒ Object



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
# File 'lib/td/command/result.rb', line 47

def result_create(op)
  result_user = nil
  result_ask_password = false

  op.on('-u', '--user NAME', 'set user name for authentication') {|s|
    result_user = s
  }
  op.on('-p', '--password', 'ask password for authentication') {|s|
    result_ask_password = true
  }

  name, url = op.cmd_parse
  API.validate_result_set_name(name)

  client = get_client

  url = build_result_url(url, result_user, result_ask_password)

  opts = {}
  begin
    client.create_result(name, url, opts)
  rescue AlreadyExistsError
    $stderr.puts "Result URL '#{name}' already exists."
    exit 1
  end

  $stderr.puts "Result URL '#{name}' is created."
end

#result_delete(op) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/td/command/result.rb', line 76

def result_delete(op)
  name = op.cmd_parse

  client = get_client

  begin
    client.delete_result(name)
  rescue NotFoundError
    $stderr.puts "Result URL '#{name}' does not exist."
    exit 1
  end

  $stderr.puts "Result URL '#{name}' is deleted."
end

#result_list(op) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/td/command/result.rb', line 22

def result_list(op)
  set_render_format_option(op)

  op.cmd_parse

  client = get_client

  rs = client.results

  rows = []
  rs.each {|r|
    rows << {:Name => r.name, :URL => r.url}
  }
  rows = rows.sort_by {|map|
    map[:Name]
  }

  puts cmd_render_table(rows, :fields => [:Name, :URL], :render_format => op.render_format)

  if rs.empty?
    $stderr.puts "There are no result URLs."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "result:create <name> <url>' to create a result URL."
  end
end

#result_show(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/td/command/result.rb', line 5

def result_show(op)
  name = op.cmd_parse
  client = get_client

  rs = client.results
  r = rs.find {|r| name == r.name }

  unless r
    $stderr.puts "Result URL '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "result:create #{name} <URL>' to create the URL."
    exit 1
  end

  puts "Name : #{r.name}"
  puts "URL  : #{r.url}"
end

#sample_apache(op) ⇒ Object



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
# File 'lib/td/command/sample.rb', line 4

def sample_apache(op)
  fname = op.cmd_parse

  require 'json'

  t = Time.now.to_i
  i = 0
  last_time = nil

  data = File.join(File.dirname(__FILE__), '../../../data/sample_apache.json')
  File.open(data) {|df|
    File.open(fname, 'w') {|of|
      df.each_line {|line|
        record = JSON.parse(line)
        record['time'] = last_time = (t - (i**1.3)).to_i
        of.puts record.to_json
        i += 1
      }
    }
  }

  $stderr.print "Created #{fname} with #{i} records whose time is "
  $stderr.puts "in the [#{Time.at(last_time)}, #{Time.at(t)}] range."

  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "table:import <db> <table> --json #{fname}' to import this file."
end

#sched_create(op) ⇒ Object



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
# File 'lib/td/command/sched.rb', line 26

def sched_create(op)
  require 'td/command/job'  # job_priority_id_of

  db_name = nil
  timezone = nil
  delay = 0
  result_url = nil
  result_user = nil
  result_ask_password = false
  priority = nil
  query = nil
  retry_limit = nil
  type = nil

  op.on('-d', '--database DB_NAME', 'use the database (required)') {|s|
    db_name = s
  }
  op.on('-t', '--timezone TZ', "name of the timezone.",
                               "  Only extended timezones like 'Asia/Tokyo', 'America/Los_Angeles' are supported,",
                               "  (no 'PST', 'PDT', etc...).",
                               "  When a timezone is specified, the cron schedule is referred to that timezone.",
                               "  Otherwise, the cron schedule is referred to the UTC timezone.",
                               "  E.g. cron schedule '0 12 * * *' will execute daily at 5 AM without timezone option",
                               "  and at 12PM with the -t / --timezone 'America/Los_Angeles' timezone option") {|s|
    timezone = s
  }
  op.on('-D', '--delay SECONDS', 'delay time of the schedule', Integer) {|i|
    delay = i
  }
  op.on('-r', '--result RESULT_URL', 'write result to the URL (see also result:create subcommand)') {|s|
    result_url = s
  }
  op.on('-u', '--user NAME', 'set user name for the result URL') {|s|
    result_user = s
  }
  op.on('-p', '--password', 'ask password for the result URL') {|s|
    result_ask_password = true
  }
  op.on('-P', '--priority PRIORITY', 'set priority') {|s|
    priority = job_priority_id_of(s)
    unless priority
      raise "unknown priority #{s.inspect} should be -2 (very-low), -1 (low), 0 (normal), 1 (high) or 2 (very-high)"
    end
  }
  op.on('-q', '--query PATH', 'use file instead of inline query') {|s|
    query = File.open(s) { |f| f.read.strip }
  }
  op.on('-R', '--retry COUNT', 'automatic retrying count', Integer) {|i|
    retry_limit = i
  }
  op.on('-T', '--type TYPE', 'set query type (hive or pig)') {|s|
    type = s
  }

  name, cron, sql = op.cmd_parse

  unless db_name
    $stderr.puts "-d, --database DB_NAME option is required."
    exit 1
  end

  if sql == '-'
    sql = STDIN.read
  elsif sql.nil?
    sql = query
  end

  unless sql
    $stderr.puts "<sql> argument or -q,--query PATH option is required."
    exit 1
  end

  if result_url
    require 'td/command/result'
    result_url = build_result_url(result_url, result_user, result_ask_password)
  end

  client = get_client

  # local existence check
  get_database(client, db_name)

  begin
    first_time = client.create_schedule(name, :cron=>cron, :query=>sql, :database=>db_name, :result=>result_url, :timezone=>timezone, :delay=>delay, :priority=>priority, :retry_limit=>retry_limit, :type=>type)
  rescue AlreadyExistsError
    cmd_debug_error $!
    $stderr.puts "Schedule '#{name}' already exists."
    exit 1
  end

  $stderr.puts "Schedule '#{name}' is created. It starts at #{first_time.localtime}."
end

#sched_delete(op) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/td/command/sched.rb', line 119

def sched_delete(op)
  name = op.cmd_parse

  client = get_client

  begin
    client.delete_schedule(name)
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Schedule '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "sched:list' to show list of the schedules."
    exit 1
  end

  $stderr.puts "Schedule '#{name}' is deleted."
end

#sched_history(op) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/td/command/sched.rb', line 219

def sched_history(op)
  require 'td/command/job'  # job_priority_name_of

  page = 0
  skip = 0

  op.on('-p', '--page PAGE', 'skip N pages', Integer) {|i|
    page = i
  }
  op.on('-s', '--skip N', 'skip N schedules', Integer) {|i|
    skip = i
  }
  set_render_format_option(op)

  name, max = op.cmd_parse

  max = (max || 20).to_i

  if page
    skip += max * page
  end

  client = get_client

  begin
    history = client.history(name, skip, skip+max-1)
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Schedule '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "sched:list' to show list of the schedules."
    exit 1
  end

  scheds = client.schedules
  if s = scheds.find {|s| s.name == name }
    puts "Name        : #{s.name}"
    puts "Cron        : #{s.cron}"
    puts "Timezone    : #{s.timezone}"
    puts "Delay       : #{s.delay} sec"
    puts "Next        : #{s.next_time}"
    puts "Result      : #{s.result_url}"
    puts "Priority    : #{job_priority_name_of(s.priority)}"
    puts "Retry limit : #{s.retry_limit}"
    puts "Database    : #{s.database}"
    puts "Query       : #{s.query}"
  end

  rows = []
  history.each {|j|
    rows << {:Time => j.scheduled_at.localtime, :JobID => j.job_id, :Status => j.status, :Priority => job_priority_name_of(j.priority), :Result=>j.result_url}
  }

  puts cmd_render_table(rows, :fields => [:JobID, :Time, :Status, :Priority, :Result], :render_format => op.render_format)
end

#sched_list(op) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/td/command/sched.rb', line 4

def sched_list(op)
  require 'td/command/job'  # job_priority_name_of

  set_render_format_option(op)

  op.cmd_parse

  client = get_client

  scheds = client.schedules

  rows = []
  scheds.each {|sched|
    rows << {:Name => sched.name, :Cron => sched.cron, :Timezone => sched.timezone, :Delay => sched.delay, :Priority => job_priority_name_of(sched.priority), :Result => sched.result_url, :Database => sched.database, :Query => sched.query, :"Next schedule" => sched.next_time ? sched.next_time.localtime : nil}
  }
  rows = rows.sort_by {|map|
    map[:Name]
  }

  puts cmd_render_table(rows, :fields => [:Name, :Cron, :Timezone, :"Next schedule", :Delay, :Priority, :Result, :Database, :Query], :max_width=>500, :render_format => op.render_format)
end

#sched_run(op) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/td/command/sched.rb', line 274

def sched_run(op)
  num = 1

  op.on('-n', '--num N', 'number of jobs to run', Integer) {|i|
    num = i
  }
  set_render_format_option(op)

  name, time = op.cmd_parse

  if time.to_i.to_s == time.to_s
    # UNIX time
    t = Time.at(time.to_i)
  else
    require 'time'
    begin
      t = Time.parse(time)
    rescue
      $stderr.puts "invalid time format: #{time}"
      exit 1
    end
  end

  client = get_client

  begin
    jobs = client.run_schedule(name, t.to_i, num)
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Schedule '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "sched:list' to show list of the schedules."
    exit 1
  end

  rows = []
  jobs.each_with_index {|job,i|
    rows << {:JobID => job.job_id, :Time => job.scheduled_at ? job.scheduled_at.localtime : nil}
  }

  $stderr.puts "Scheduled #{num} jobs from #{t}."
  puts cmd_render_table(rows, :fields => [:JobID, :Time], :max_width=>500, :render_format => op.render_format)
end

#sched_update(op) ⇒ Object



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/td/command/sched.rb', line 136

def sched_update(op)
  require 'td/command/job'  # job_priority_id_of

  cron = nil
  sql = nil
  db_name = nil
  result = nil
  timezone = nil
  delay = nil
  priority = nil
  retry_limit = nil
  type = nil

  op.on('-s', '--schedule CRON', 'change the schedule') {|s|
    cron = s
  }
  op.on('-q', '--query SQL', 'change the query') {|s|
    sql = s
  }
  op.on('-d', '--database DB_NAME', 'change the database') {|s|
    db_name = s
  }
  op.on('-r', '--result RESULT_URL', 'change the result target (see also result:create subcommand)') {|s|
    result = s
  }
  op.on('-t', '--timezone TZ', "name of the timezone.",
                               "  Only extended timezones like 'Asia/Tokyo', 'America/Los_Angeles' are supported,",
                               "  (no 'PST', 'PDT', etc...).",
                               "  When a timezone is specified, the cron schedule is referred to that timezone.",
                               "  Otherwise, the cron schedule is referred to the UTC timezone.",
                               "  E.g. cron schedule '0 12 * * *' will execute daily at 5 AM without timezone option",
                               "  and at 12PM with the -t / --timezone 'America/Los_Angeles' timezone option") {|s|
    timezone = s
  }
  op.on('-D', '--delay SECONDS', 'change the delay time of the schedule', Integer) {|i|
    delay = i
  }
  op.on('-P', '--priority PRIORITY', 'set priority') {|s|
    priority = job_priority_id_of(s)
    unless priority
      raise "unknown priority #{s.inspect} should be -2 (very-low), -1 (low), 0 (normal), 1 (high) or 2 (very-high)"
    end
  }
  op.on('-R', '--retry COUNT', 'automatic retrying count', Integer) {|i|
    retry_limit = i
  }
  op.on('-T', '--type TYPE', 'set query type (hive or pig)') {|s|
    type = s
  }


  name = op.cmd_parse

  params = {}
  params['cron'] = cron if cron
  params['query'] = sql if sql
  params['database'] = db_name if db_name
  params['result'] = result if result
  params['timezone'] = timezone if timezone
  params['delay'] = delay.to_s if delay
  params['priority'] = priority.to_s if priority
  params['retry_limit'] = retry_limit.to_s if retry_limit
  params['type'] = type.to_s if type

  if params.empty?
    $stderr.puts op.to_s
    exit 1
  end

  client = get_client

  begin
    client.update_schedule(name, params)
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Schedule '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "sched:list' to show list of the schedules."
    exit 1
  end

  $stderr.puts "Schedule '#{name}' is updated."
end

#schema_add(op) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/td/command/schema.rb', line 30

def schema_add(op)
  db_name, table_name, *columns = op.cmd_parse
  schema = parse_columns(columns)

  client = get_client
  table = get_table(client, db_name, table_name)

  schema = table.schema.merge(schema)

  client.update_schema(table.db_name, table.name, schema)

  $stderr.puts "Schema is updated on #{db_name}.#{table_name} table."
end

#schema_remove(op) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/td/command/schema.rb', line 44

def schema_remove(op)
  db_name, table_name, *columns = op.cmd_parse

  client = get_client
  table = get_table(client, db_name, table_name)

  schema = table.schema

  columns.each {|col|
    deleted = false
    schema.fields.delete_if {|f|
      f.name == col && deleted = true
    }
    unless deleted
      $stderr.puts "Column name '#{col}' does not exist."
      exit 1
    end
  }

  client.update_schema(table.db_name, table.name, schema)

  $stderr.puts "Schema is updated on #{db_name}.#{table_name} table."
end

#schema_set(op) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/td/command/schema.rb', line 18

def schema_set(op)
  db_name, table_name, *columns = op.cmd_parse
  schema = parse_columns(columns)

  client = get_client
  table = get_table(client, db_name, table_name)

  client.update_schema(table.db_name, table.name, schema)

  $stderr.puts "Schema is updated on #{db_name}.#{table_name} table."
end

#schema_show(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/td/command/schema.rb', line 5

def schema_show(op)
  db_name, table_name = op.cmd_parse

  client = get_client
  table = get_table(client, db_name, table_name)

  puts "#{db_name}.#{table_name} ("
  table.schema.fields.each {|f|
    puts "  #{f.name}:#{f.type}"
  }
  puts ")"
end

#server_endpoint(op) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/td/command/server.rb', line 12

def server_endpoint(op)
  endpoint = op.cmd_parse

  Command.validate_api_endpoint(endpoint)

  if Config.cl_endpoint and endpoint != Config.endpoint
    raise ParameterConfigurationError,
          "You specified the API server endpoint in the command options as well (-e / --endpoint option) but it does not match the value provided to the 'server:endpoint' command. Please remove the option or ensure the endpoints URLs match each other."
  end

  conf = nil
  begin
    conf = Config.read
  rescue ConfigError
    conf = Config.new
  end
  conf["account.endpoint"] = endpoint
  conf.save
end

#server_status(op) ⇒ Object



6
7
8
9
10
# File 'lib/td/command/server.rb', line 6

def server_status(op)
  op.cmd_parse

  puts Client.server_status
end

#status(op) ⇒ Object



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
# File 'lib/td/command/status.rb', line 5

def status(op)
  op.cmd_parse

  client = get_client

  # +----------------+
  # |     scheds     |
  # +----------------+
  # +----------------+
  # |      jobs      |
  # +----------------+
  # +------+ +-------+
  # |tables| |results|
  # +------+ +-------+

  scheds = []
  jobs = []
  tables = []
  results = []

  s = client.schedules
  s.each {|sched|
    scheds << {:Name => sched.name, :Cron => sched.cron, :Result => sched.result_url, :Query => sched.query}
  }
  scheds = scheds.sort_by {|map|
    map[:Name]
  }
  x1, y1 = status_render(0, 0, "[Schedules]", scheds, :fields => [:Name, :Cron, :Result, :Query])

  j = client.jobs(0, 4)
  j.each {|job|
    start = job.start_at
    elapsed = Command.humanize_elapsed_time(start, job.end_at)
    jobs << {:JobID => job.job_id, :Status => job.status, :Query => job.query.to_s, :Start => (start ? start.localtime : ''), :Elapsed => elapsed, :Result => job.result_url}
  }
  x2, y2 = status_render(0, 0, "[Jobs]", jobs, :fields => [:JobID, :Status, :Start, :Elapsed, :Result, :Query])

  dbs = client.databases
  dbs.map {|db|
    db.tables.each {|table|
      tables << {:Database => db.name, :Table => table.name, :Count => table.count.to_s, :Size => table.estimated_storage_size_string}
    }
  }
  x3, y3 = status_render(0, 0, "[Tables]", tables, :fields => [:Database, :Table, :Count, :Size])

  rs = client.results
  rs.each {|r|
    results << {:Name => r.name, :URL => r.url}
  }
  results = results.sort_by {|map|
    map[:Name]
  }
  x4, y4 = status_render(x3+2, y3, "[Results]", results, :fields => [:Name, :URL])

  (y3-y4-1).times do
    print "\eD"
  end
  print "\eE"
end

#table_create(op) ⇒ Object



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
# File 'lib/td/command/table.rb', line 15

def table_create(op)
  type = nil
  primary_key = nil
  primary_key_type = nil

  op.on('-T', '--type TYPE', 'set table type (log or item)') {|s|
    unless ['item', 'log'].include?(s)
      raise "Unknown table type #{s.dump}. Supported types: log and item"
    end
    type = s.to_sym
  }
  op.on('--primary-key PRIMARY_KEY_AND_TYPE', '[primary key]:[primary key type(int or string)]') {|s|
    unless /\A[\w]+:(string|int)\z/ =~ s
      $stderr.puts "--primary-key PRIMARY_KEY_AND_TYPE is required, and should be in the format [primary key]:[primary key type]"
      exit 1
    end

    args = s.split(':')
    if args.length != 2
      # this really shouldn't happen with the above regex
      exit 1
    end
    primary_key = args[0]
    primary_key_type = args[1]
  }

  db_name, table_name = op.cmd_parse

  API.validate_table_name(table_name)

  if HIVE_RESERVED_KEYWORDS.include?(table_name.upcase)
    $stderr.puts "* WARNING *"
    $stderr.puts "  '#{table_name}' is a reserved keyword in Hive. We recommend renaming the table."
    $stderr.puts "  For a list of all reserved keywords, see our FAQ: http://docs.treasure-data.com/articles/faq"
  end

  if type == :item && (primary_key.nil? || primary_key_type.nil?)
    $stderr.puts "for TYPE 'item', the primary-key is required"
    exit 1
  end

  client = get_client

  begin
    if type == :item
      client.create_item_table(db_name, table_name, primary_key, primary_key_type)
    else
      client.create_log_table(db_name, table_name)
    end
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Database '#{db_name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "db:create #{db_name}' to create the database."
    exit 1
  rescue AlreadyExistsError
    cmd_debug_error $!
    $stderr.puts "Table '#{db_name}.#{table_name}' already exists."
    exit 1
  end

  $stderr.puts "Table '#{db_name}.#{table_name}' is created."
end

#table_delete(op) ⇒ Object



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
# File 'lib/td/command/table.rb', line 78

def table_delete(op)
  force = false
  op.on('-f', '--force', 'never prompt', TrueClass) {|b|
    force = true
  }

  db_name, table_name = op.cmd_parse

  client = get_client

  begin
    unless force
      table = get_table(client, db_name, table_name)
      $stderr.print "Do you really delete '#{table_name}' in '#{db_name}'? [y/N]: "
      ok = nil
      while line = $stdin.gets
        line.strip!
        if line =~ /^y(?:es)?$/i
          ok = true
          break
        elsif line.empty? || line =~ /^n(?:o)?$/i
          break
        else
          $stderr.print "Type 'Y' or 'N': "
        end
      end
      unless ok
        $stderr.puts "canceled."
        exit 1
      end
    end
    client.delete_table(db_name, table_name)
  rescue NotFoundError
    cmd_debug_error $!
    $stderr.puts "Table '#{db_name}.#{table_name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "table:list #{db_name}' to show list of the tables."
    exit 1
  end

  $stderr.puts "Table '#{db_name}.#{table_name}' is deleted."
end

#table_expire(op) ⇒ Object



405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/td/command/table.rb', line 405

def table_expire(op)
  db_name, table_name, expire_days = op.cmd_parse

  expire_days = expire_days.to_i
  if expire_days <= 0
    $stderr.puts "Table expiration days must be greater than 0."
    return
  end

  client = get_client
  client.update_expire(db_name, table_name, expire_days)

  $stderr.puts "Table set to expire data older than #{expire_days} days."
end

#table_export(op) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/td/command/table.rb', line 288

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 until the job is completed', 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} " + Config.cl_options_string + "job:show #{job.job_id}' to show the status."

  if wait && !job.finished?
    wait_job(job)
    puts "Status     : #{job.status}"
  end
end

#table_import(op) ⇒ Object

TODO import-item TODO tail



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/td/command/table.rb', line 435

def table_import(op)
  op.banner << "\nsupported formats:\n"
  op.banner << "  apache\n"
  op.banner << "  syslog\n"
  op.banner << "  msgpack\n"
  op.banner << "  json\n"

  format = 'apache'
  time_key = 'time'
  auto_create = false

  op.on('--format FORMAT', "file format (default: #{format})") {|s|
    format = s
  }

  op.on('--apache', "same as --format apache; apache common log format") {
    format = 'apache'
  }

  op.on('--syslog', "same as --format syslog; syslog") {
    format = 'syslog'
  }

  op.on('--msgpack', "same as --format msgpack; msgpack stream format") {
    format = 'msgpack'
  }

  op.on('--json', "same as --format json; LF-separated json format") {
    format = 'json'
  }

  op.on('-t', '--time-key COL_NAME', "time key name for json and msgpack format (e.g. 'created_at')") {|s|
    time_key = s
  }

  op.on('--auto-create-table', "Create table and database if doesn't exist", TrueClass) { |b|
    auto_create = b
  }

  db_name, table_name, *paths = op.cmd_parse

  client = get_client

  if auto_create
    # Merge with db_create and table_create after refactoring
    API.validate_database_name(db_name)
    begin
      client.database(db_name)
    rescue NotFoundError
      begin
        client.create_database(db_name)
        $stderr.puts "Database '#{db_name}' is created."
      rescue AlreadyExistsError
      end
    end

    API.validate_table_name(table_name)
    begin
      client.create_log_table(db_name, table_name)
      $stderr.puts "Table '#{db_name}.#{table_name}' is created."
    rescue AlreadyExistsError
    end
  end

  case format
  when 'json', 'msgpack'
    #unless time_key
    #  $stderr.puts "-t, --time-key COL_NAME (e.g. '-t created_at') parameter is required for #{format} format"
    #  exit 1
    #end
    if format == 'json'
      require 'json'
      require 'time'
      parser = JsonParser.new(time_key)
    else
      parser = MessagePackParser.new(time_key)
    end

  else
    regexp, names, time_format = IMPORT_TEMPLATES[format]
    if !regexp || !names || !time_format
      $stderr.puts "Unknown format '#{format}'"
      exit 1
    end
    parser = TextParser.new(names, regexp, time_format)
  end

  get_table(client, db_name, table_name)

  require 'zlib'

  files = paths.map {|path|
    if path == '-'
      $stdin
    elsif path =~ /\.gz$/
      require 'td/compat_gzip_reader'
      Zlib::GzipReader.open(path)
    else
      File.open(path)
    end
  }

  require 'msgpack'
  require 'tempfile'
  #require 'thread'

  files.zip(paths).each {|file,path|
    import_log_file(file, path, client, db_name, table_name, parser)
  }

  puts "done."
end

#table_list(op) ⇒ Object



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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/td/command/table.rb', line 120

def table_list(op)
  require 'parallel'

  format = 'table'
  num_threads = 4
  show_size_in_bytes = false

  op.on('-n', '--num_threads VAL', 'number of threads to get list in parallel') { |i|
    num_threads = Integer(i)
  }
  op.on('--show-bytes', 'show estimated table size in bytes') {
    show_size_in_bytes = true
  }
  set_render_format_option(op)

  db_name = op.cmd_parse

  client = get_client

  if db_name
    database = get_database(client, db_name)
    databases = [database]
  else
    databases = client.databases
  end

  has_item = databases.select {|db| db.tables.select {|table| table.type == :item}.length > 0 }.length > 0

  rows = []
  ::Parallel.each(databases, :in_threads => num_threads) {|db|
    begin
      db.tables.each {}
      db.tables.each {|table|
        pschema = table.schema.fields.map {|f|
          "#{f.name}:#{f.type}"
        }.join(', ')
        new_row = {
          :Database => db.name, :Table => table.name, :Type => table.type.to_s, :Count => TreasureData::Helpers.format_with_delimiter(table.count),
          :Size => show_size_in_bytes ? TreasureData::Helpers.format_with_delimiter(table.estimated_storage_size) : table.estimated_storage_size_string,
          'Last import' => table.last_import ? table.last_import.localtime : nil,
          'Last log timestamp' => table.last_log_timestamp ? table.last_log_timestamp.localtime : nil,
          :Schema => pschema
        }
        if has_item and table.type == :item
          new_row['Primary key'] = "#{table.primary_key}:#{table.primary_key_type}"
        end
        rows << new_row
      }
    rescue APIError => e
      # ignores permission error because db:list shows all databases
      # even if the user can't access to tables in the database
      unless e.to_s =~ /not authorized/
        raise e
      end
    end
  }
  rows = rows.sort_by {|map|
    [map[:Database], map[:Type].size, map[:Table]]
  }

  fields = []
  if has_item
    fields = [:Database, :Table, :Type, :Count, :Size, 'Last import', 'Last log timestamp', 'Primary key', :Schema]
  else
    fields = [:Database, :Table, :Type, :Count, :Size, 'Last import', 'Last log timestamp', :Schema]
  end
  puts cmd_render_table(rows, :fields => fields, :max_width => 500, :render_format => op.render_format)

  if rows.empty?
    if db_name
      $stderr.puts "Database '#{db_name}' has no tables."
      $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "table:create <db> <table>' to create a table."
    elsif databases.empty?
      $stderr.puts "There are no databases."
      $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "db:create <db>' to create a database."
    else
      $stderr.puts "There are no tables."
      $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "table:create <db> <table>' to create a table."
    end
  end
end

#table_partial_delete(op) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/td/command/table.rb', line 346

def table_partial_delete(op)
  from = nil
  to = nil
  wait = false

  op.on('-t', '--to TIME', 'end time of logs to delete in Unix time multiple of 3600 (1 hour)') {|s|
    if s.to_i.to_s == s
      # UNIX time
      to = s.to_i
    else
      require 'time'
      to = Time.parse(s).to_i
    end
  }
  op.on('-f', '--from TIME', 'start time of logs to delete in Unix time multiple of 3600 (1 hour)') {|s|
    if s.to_i.to_s == s
      from = s.to_i
    else
      require 'time'
      from = Time.parse(s).to_i
    end
  }
  op.on('-w', '--wait', 'wait for the job to finish', TrueClass) {|b|
    wait = b
  }

  db_name, table_name = op.cmd_parse

  unless from
    $stderr.puts "-f, --from TIME option is required"
    exit 1
  end

  unless to
    $stderr.puts "-t, --to TIME option is required"
    exit 1
  end

  if from % 3600 != 0 || to % 3600 != 0
    $stderr.puts "Time for the -f / --from and -t / --to options must be a multiple of 3600 (1 hour)"
    exit 1
  end

  client = get_client

  table = get_table(client, db_name, table_name)

  opts = {}
  job = client.partial_delete(db_name, table_name, to, from, opts)

  $stderr.puts "Partial delete job #{job.job_id} is queued."
  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "job:show #{job.job_id}' to show the status."

  if wait && !job.finished?
    wait_job(job)
    puts "Status     : #{job.status}"
  end
end

#table_show(op) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/td/command/table.rb', line 215

def table_show(op)
  db_name, table_name = op.cmd_parse

  client = get_client

  table = get_table(client, db_name, table_name)

  puts "Name        : #{table.db_name}.#{table.name}"
  puts "Type        : #{table.type}"
  puts "Count       : #{table.count}"
  # p table.methods.each {|m| puts m}
  puts "Primary key : #{table.primary_key}:#{table.primary_key_type}" if table.type == :item
  puts "Schema      : ("
  table.schema.fields.each {|f|
    puts "    #{f.name}:#{f.type}"
  }
  puts ")"
end

#table_swap(op) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/td/command/table.rb', line 202

def table_swap(op)
  db_name, table_name1, table_name2 = op.cmd_parse

  client = get_client

  table1 = get_table(client, db_name, table_name1)
  table2 = get_table(client, db_name, table_name2)

  client.swap_table(db_name, table_name1, table_name2)

  $stderr.puts "'#{db_name}.#{table_name1}' and '#{db_name}.#{table_name2}' are swapped."
end

#table_tail(op) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/td/command/table.rb', line 234

def table_tail(op)
  from = nil
  to = nil
  count = 10
  pretty = nil

  op.on('-t', '--to TIME', 'end time of logs to get') {|s|
    if s.to_i.to_s == s
      to = s.to_i
    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.to_i
    else
      require 'time'
      from = Time.parse(s).to_i
    end
  }
  op.on('-n', '--count N', 'number of logs to get', Integer) {|i|
    count = i
  }
  op.on('-P', '--pretty', 'pretty print', TrueClass) {|b|
    pretty = b
  }

  db_name, table_name = op.cmd_parse

  client = get_client

  table = get_table(client, db_name, table_name)

  rows = table.tail(count, to, from)

  require 'json'
  if pretty
    opts = {
      :indent => ' '*2,
      :object_nl => "\n",
      :space => ' '
    }
    rows.each {|row|
      puts row.to_json(opts)
    }
  else
    rows.each {|row|
      puts row.to_json
    }
  end
end

#update(op) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/td/command/update.rb', line 6

def update(op)
  # for gem installation, this command is disallowed -
  #   it only works for the toolbelt.
  if Updater.disable?
    $stderr.puts Updater.disable_message
    exit
  end

  start_time = Time.now
  puts "Updating 'td' from #{TOOLBELT_VERSION}..."
  if new_version = Updater.update
    puts "Successfully updated to #{new_version} in #{Command.humanize_time((Time.now - start_time).to_i)}."
  else
    puts "Nothing to update."
  end
end

#user_apikey_add(op) ⇒ Object

TODO user:email:change <name> <email> def user_email_change(op) end



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/td/command/user.rb', line 140

def user_apikey_add(op)
  name = op.cmd_parse

  client = get_client

  begin
    client.add_apikey(name)
  rescue TreasureData::NotFoundError
    $stderr.puts "User '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "users' to show users."
    exit 1
  end

  $stderr.puts "Added an API key to user '#{name}'."
  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "user:apikeys #{name}' to show the API key"
end

#user_apikey_list(op) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/td/command/user.rb', line 174

def user_apikey_list(op)
  set_render_format_option(op)

  name = op.cmd_parse

  client = get_client

  keys = client.list_apikeys(name)

  rows = []
  keys.each {|key|
    rows << {:Key => key}
  }

  puts cmd_render_table(rows, :fields => [:Key], :render_format => op.render_format)
end

#user_apikey_remove(op) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/td/command/user.rb', line 157

def user_apikey_remove(op)
  name, key = op.cmd_parse

  client = get_client

  begin
    client.remove_apikey(name, key)
  rescue TreasureData::NotFoundError
    $stderr.puts "User '#{name}' or API key '#{key}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "users' to show users."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "user:apikeys '#{key}' to show API keys"
    exit 1
  end

  $stderr.puts "Removed an an API key from user '#{name}'."
end

#user_create(op) ⇒ Object



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
# File 'lib/td/command/user.rb', line 44

def user_create(op)
  email = nil
  random_password = false

  op.on('-e', '--email EMAIL', "Use this email address to identify the user") {|s|
    email = s
  }

  op.on('-R', '--random-password', "Generate random password", TrueClass) {|b|
    random_password = b
  }

  name = op.cmd_parse

  unless email
    $stderr.puts "-e, --email EMAIL option is required."
    exit 1
  end

  if random_password
    lower = ('a'..'z').to_a
    upper = ('A'..'Z').to_a
    digit = ('0'..'9').to_a
    symbol = %w[_ @ - + ;]

    r = []
    3.times { r << lower.sort_by{rand}.first }
    3.times { r << upper.sort_by{rand}.first }
    2.times { r << digit.sort_by{rand}.first }
    1.times { r << symbol.sort_by{rand}.first }
    password = r.sort_by{rand}.join

    puts "Password: #{password}"

  else
    3.times do
      begin
        system "stty -echo"  # TODO termios
        print "Password (typing will be hidden): "
        password = STDIN.gets || ""
        password = password[0..-2]  # strip \n
      rescue Interrupt
        $stderr.print "\ncanceled."
        exit 1
      ensure
        system "stty echo"   # TODO termios
        print "\n"
      end

      if password.empty?
        $stderr.puts "canceled."
        exit 0
      end

      begin
        system "stty -echo"  # TODO termios
        print "Retype password: "
        password2 = STDIN.gets || ""
        password2 = password2[0..-2]  # strip \n
      rescue Interrupt
        $stderr.print "\ncanceled."
        exit 1
      ensure
        system "stty echo"   # TODO termios
        print "\n"
      end

      if password == password2
        break
      end

      puts "Doesn't match."
    end
  end

  client = get_client(:ssl => true)
  client.add_user(name, nil, email, password)

  $stderr.puts "User '#{name}' is created."
  $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "user:apikeys #{name}' to show the API key."    
end

#user_delete(op) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/td/command/user.rb', line 126

def user_delete(op)
  name = op.cmd_parse

  client = get_client

  client.remove_user(name)

  $stderr.puts "User '#{name}' is deleted."
end

#user_list(op) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/td/command/user.rb', line 22

def user_list(op)
  set_render_format_option(op)

  op.cmd_parse

  client = get_client

  users = client.users

  rows = []
  users.each {|user|
    rows << {:Name => user.name, :Email => user.email}
  }

  puts cmd_render_table(rows, :fields => [:Name, :Email], :render_format => op.render_format)

  if rows.empty?
    $stderr.puts "There are no users."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "user:create <name>' to create an users."
  end
end

#user_password_change(op) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/td/command/user.rb', line 191

def user_password_change(op)
  name = op.cmd_parse

  password = nil

  3.times do
    begin
      system "stty -echo"  # TODO termios
      print "New password (typing will be hidden): "
      password = STDIN.gets || ""
      password = password[0..-2]  # strip \n
    rescue Interrupt
      $stderr.print "\ncanceled."
      exit 1
    ensure
      system "stty echo"   # TODO termios
      print "\n"
    end

    if password.empty?
      $stderr.puts "canceled."
      exit 0
    end

    begin
      system "stty -echo"  # TODO termios
      print "Retype new password: "
      password2 = STDIN.gets || ""
      password2 = password2[0..-2]  # strip \n
    rescue Interrupt
      $stderr.print "\ncanceled."
      exit 1
    ensure
      system "stty echo"   # TODO termios
      print "\n"
    end

    if password == password2
      break
    end

    puts "Doesn't match."
  end

  client = get_client(:ssl => true)

  client.change_password(name, password)

  $stderr.puts "Password of user '#{name}' changed."
end

#user_show(op) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/td/command/user.rb', line 5

def user_show(op)
  name = op.cmd_parse

  client = get_client

  users = client.users
  user = users.find {|user| name == user.name }
  unless user
    $stderr.puts "User '#{name}' does not exist."
    $stderr.puts "Use '#{$prog} " + Config.cl_options_string + "user:create <name>' to create an user."
    exit 1
  end

  $stderr.puts "Name  : #{user.name}"
  $stderr.puts "Email : #{user.email}"
end