td 0.7.2 → 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
 
5
5
  IMPORT_TEMPLATES = {
@@ -56,7 +56,7 @@ module Command
56
56
 
57
57
  db_name, table_name, *paths = op.cmd_parse
58
58
 
59
- api = cmd_api
59
+ client = get_client
60
60
 
61
61
  case format
62
62
  when 'json', 'msgpack'
@@ -81,7 +81,7 @@ module Command
81
81
  parser = TextParser.new(names, regexp, time_format)
82
82
  end
83
83
 
84
- find_table(api, db_name, table_name, :log)
84
+ find_table(client, db_name, table_name, :log)
85
85
 
86
86
  require 'zlib'
87
87
 
@@ -100,14 +100,14 @@ module Command
100
100
  #require 'thread'
101
101
 
102
102
  files.zip(paths).each {|file,path|
103
- import_log_file(file, path, api, db_name, table_name, parser)
103
+ import_log_file(file, path, client, db_name, table_name, parser)
104
104
  }
105
105
 
106
106
  puts "done."
107
107
  end
108
108
 
109
109
  private
110
- def import_log_file(file, path, api, db_name, table_name, parser)
110
+ def import_log_file(file, path, client, db_name, table_name, parser)
111
111
  puts "importing #{path}..."
112
112
 
113
113
  out = Tempfile.new('td-import')
@@ -131,7 +131,7 @@ module Command
131
131
  out.pos = 0
132
132
 
133
133
  puts " uploading #{size} bytes..."
134
- api.import(db_name, table_name, "msgpack.gz", out, size)
134
+ client.import(db_name, table_name, "msgpack.gz", out, size)
135
135
 
136
136
  out.truncate(0)
137
137
  out.pos = 0
@@ -151,7 +151,7 @@ module Command
151
151
 
152
152
  puts " uploading #{size} bytes..."
153
153
  # TODO upload on background thread
154
- api.import(db_name, table_name, "msgpack.gz", out, size)
154
+ client.import(db_name, table_name, "msgpack.gz", out, size)
155
155
  end
156
156
 
157
157
  puts " imported #{n} entries from #{path}."
@@ -1,5 +1,5 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
  module List
5
5
 
@@ -107,7 +107,7 @@ end
107
107
 
108
108
  def version
109
109
  require 'td/version'
110
- puts "td-#{TD::VERSION}"
110
+ puts "td-#{TreasureData::VERSION}"
111
111
  end
112
112
 
113
113
  end
@@ -1,5 +1,5 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
 
5
5
  def query
@@ -30,16 +30,16 @@ module Command
30
30
 
31
31
  sql = op.cmd_parse
32
32
 
33
- api = cmd_api
33
+ client = get_client
34
34
 
35
35
  unless db_name
36
36
  $stderr.puts "-d, --database DB_NAME option is required."
37
37
  exit 1
38
38
  end
39
39
 
40
- find_database(api, db_name)
40
+ find_database(client, db_name)
41
41
 
42
- job = api.query(db_name, sql)
42
+ job = client.query(db_name, sql)
43
43
 
44
44
  $stderr.puts "Job #{job.job_id} is started."
45
45
  $stderr.puts "Use '#{$prog} job #{job.job_id}' to show the status."
@@ -62,8 +62,6 @@ module Command
62
62
 
63
63
  page = 0
64
64
  skip = 0
65
- from = nil
66
- around = nil
67
65
 
68
66
  op.on('-p', '--page PAGE', 'skip N pages', Integer) {|i|
69
67
  page = i
@@ -71,33 +69,17 @@ module Command
71
69
  op.on('-s', '--skip N', 'skip N jobs', Integer) {|i|
72
70
  skip = i
73
71
  }
74
- op.on('-f', '--from JOB_ID', 'show jobs from the id', Integer) {|i|
75
- from = i
76
- }
77
- op.on('-a', '--around JOB_ID', 'show jobs around the id', Integer) {|i|
78
- around = i
79
- }
80
72
 
81
73
  max = op.cmd_parse
82
74
 
83
75
  max = (max || 20).to_i
84
76
 
85
- api = cmd_api
77
+ client = get_client
86
78
 
87
- if from || around
88
- jobs = api.jobs(0, 1)
89
- if last = jobs[0]
90
- if from
91
- skip += last.job_id.to_i - from - (max-1)
92
- else
93
- skip += last.job_id.to_i - around - (max-1) + (max-1)/2
94
- end
95
- end
96
- end
97
79
  if page
98
80
  skip += max * page
99
81
  end
100
- jobs = api.jobs(skip, skip+max-1)
82
+ jobs = client.jobs(skip, skip+max-1)
101
83
 
102
84
  rows = []
103
85
  jobs.each {|job|
@@ -161,9 +143,9 @@ module Command
161
143
 
162
144
  job_id = op.cmd_parse
163
145
 
164
- api = cmd_api
146
+ client = get_client
165
147
 
166
- job = api.job(job_id)
148
+ job = client.job(job_id)
167
149
 
168
150
  puts "JobID : #{job.job_id}"
169
151
  puts "URL : #{job.url}"
@@ -1,13 +1,13 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
 
5
5
  def server_status
6
6
  op = cmd_opt 'server-status'
7
7
  op.cmd_parse
8
8
 
9
- require 'td/api'
10
- puts API.server_status
9
+ require 'td/client'
10
+ puts Client.server_status
11
11
  end
12
12
 
13
13
  end
@@ -1,14 +1,14 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
 
5
5
  def create_table_type(type, db_name, table_name)
6
- api = cmd_api
6
+ client = get_client
7
7
 
8
8
  API.validate_table_name(table_name)
9
9
 
10
10
  begin
11
- api.create_table(db_name, table_name, type)
11
+ client.create_table(db_name, table_name, type)
12
12
  rescue NotFoundError
13
13
  cmd_debug_error $!
14
14
  $stderr.puts "Database '#{db_name}' does not exist."
@@ -42,10 +42,10 @@ module Command
42
42
  op = cmd_opt 'drop-table', :db_name, :table_name
43
43
  db_name, table_name = op.cmd_parse
44
44
 
45
- api = cmd_api
45
+ client = get_client
46
46
 
47
47
  begin
48
- api.delete_table(db_name, table_name)
48
+ client.delete_table(db_name, table_name)
49
49
  rescue NotFoundError
50
50
  cmd_debug_error $!
51
51
  $stderr.puts "Table '#{db_name}.#{table_name}' does not exist."
@@ -60,13 +60,13 @@ module Command
60
60
  op = cmd_opt 'show-tables', :db_name?
61
61
  db_name = op.cmd_parse
62
62
 
63
- api = cmd_api
63
+ client = get_client
64
64
 
65
65
  if db_name
66
- db = find_database(api, db_name)
66
+ db = find_database(client, db_name)
67
67
  dbs = [db]
68
68
  else
69
- dbs = api.databases
69
+ dbs = client.databases
70
70
  end
71
71
 
72
72
  rows = []
data/lib/td/command/td.rb CHANGED
@@ -18,7 +18,7 @@ op.summary_indent = " "
18
18
  puts op.to_s
19
19
  puts ""
20
20
  puts "commands:"
21
- puts TD::Command::List.help(op.summary_indent)
21
+ puts TreasureData::Command::List.help(op.summary_indent)
22
22
  puts ""
23
23
  puts "Type 'td help COMMAND' for more information on a specific command."
24
24
  if errmsg
@@ -58,10 +58,10 @@ begin
58
58
 
59
59
  require 'td/config'
60
60
  if config_path
61
- TD::Config.path = config_path
61
+ TreasureData::Config.path = config_path
62
62
  end
63
63
  if apikey
64
- TD::Config.apikey = apikey
64
+ TreasureData::Config.apikey = apikey
65
65
  end
66
66
  rescue
67
67
  usage $!.to_s
@@ -69,18 +69,16 @@ end
69
69
 
70
70
  require 'td/command/list'
71
71
 
72
- method = TD::Command::List.get_method(cmd)
72
+ method = TreasureData::Command::List.get_method(cmd)
73
73
  unless method
74
74
  $stderr.puts "'#{cmd}' is not a td command. Run '#{$prog}' to show the list."
75
- TD::Command::List.show_guess(cmd)
75
+ TreasureData::Command::List.show_guess(cmd)
76
76
  exit 1
77
77
  end
78
78
 
79
- require 'td/error'
80
-
81
79
  begin
82
80
  method.call
83
- rescue TD::ConfigError
81
+ rescue TreasureData::ConfigError
84
82
  $stderr.puts "TreasureData account is not configured yet."
85
83
  $stderr.puts "Run '#{$prog} account' first."
86
84
  rescue
data/lib/td/config.rb CHANGED
@@ -1,6 +1,15 @@
1
- require 'td/error'
2
1
 
3
- module TD
2
+ module TreasureData
3
+
4
+
5
+ class ConfigError < StandardError
6
+ end
7
+
8
+ class ConfigNotFoundError < ConfigError
9
+ end
10
+
11
+ class ConfigParseError < ConfigError
12
+ end
4
13
 
5
14
 
6
15
  class Config
data/lib/td/version.rb CHANGED
@@ -1,4 +1,4 @@
1
- module TD
1
+ module TreasureData
2
2
 
3
3
  VERSION = '0.7.2'
4
4
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: td
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 2
10
- version: 0.7.2
9
+ - 3
10
+ version: 0.7.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sadayuki Furuhashi
@@ -77,7 +77,7 @@ extra_rdoc_files:
77
77
  - README.rdoc
78
78
  files:
79
79
  - lib/td/api.rb
80
- - lib/td/api_iface.rb
80
+ - lib/td/client.rb
81
81
  - lib/td/command/account.rb
82
82
  - lib/td/command/common.rb
83
83
  - lib/td/command/database.rb
@@ -88,7 +88,6 @@ files:
88
88
  - lib/td/command/table.rb
89
89
  - lib/td/command/td.rb
90
90
  - lib/td/config.rb
91
- - lib/td/error.rb
92
91
  - lib/td/version.rb
93
92
  - ChangeLog
94
93
  - README.rdoc
data/lib/td/api_iface.rb DELETED
@@ -1,374 +0,0 @@
1
-
2
- module TD
3
-
4
-
5
- class APIInterface
6
- def initialize(apikey)
7
- require 'json'
8
- @apikey = apikey
9
- end
10
-
11
- # TODO error check & raise appropriate errors
12
-
13
- attr_reader :apikey
14
-
15
- ####
16
- ## Database API
17
- ##
18
-
19
- # => [name:String]
20
- def list_databases
21
- code, body, res = get("/v3/database/list")
22
- if code != "200"
23
- raise_error("List databases failed", res)
24
- end
25
- # TODO format check
26
- js = JSON.load(body)
27
- names = js["databases"].map {|dbinfo| dbinfo['name'] }
28
- return names
29
- end
30
-
31
- # => true
32
- def delete_database(db)
33
- code, body, res = post("/v3/database/delete/#{e db}")
34
- if code != "200"
35
- raise_error("Delete database failed", res)
36
- end
37
- return true
38
- end
39
-
40
- # => true
41
- def create_database(db)
42
- code, body, res = post("/v3/database/create/#{e db}")
43
- if code != "200"
44
- raise_error("Create database failed", res)
45
- end
46
- return true
47
- end
48
-
49
-
50
- ####
51
- ## Table API
52
- ##
53
-
54
- # => {name:String => [type:Symbol, count:Integer]}
55
- def list_tables(db)
56
- code, body, res = get("/v3/table/list/#{e db}")
57
- if code != "200"
58
- raise_error("List tables failed", res)
59
- end
60
- # TODO format check
61
- js = JSON.load(body)
62
- result = {}
63
- js["tables"].map {|m|
64
- name = m['name']
65
- type = (m['type'] || '?').to_sym
66
- count = (m['count'] || 0).to_i # TODO?
67
- result[name] = [type, count]
68
- }
69
- return result
70
- end
71
-
72
- # => true
73
- def create_table(db, table, type)
74
- code, body, res = post("/v3/table/create/#{e db}/#{e table}/#{type}")
75
- if code != "200"
76
- raise_error("Create #{type} table failed", res)
77
- end
78
- return true
79
- end
80
-
81
- # => true
82
- def create_log_table(db, table)
83
- create_table(db, table, :log)
84
- end
85
-
86
- # => true
87
- def create_item_table(db, table)
88
- create_table(db, table, :item)
89
- end
90
-
91
- # => type:Symbol
92
- def delete_table(db, table)
93
- code, body, res = post("/v3/table/delete/#{e db}/#{e table}")
94
- if code != "200"
95
- raise_error("Drop table failed", res)
96
- end
97
- # TODO format check
98
- js = JSON.load(body)
99
- type = (js['type'] || '?').to_sym
100
- return type
101
- end
102
-
103
-
104
- ####
105
- ## Job API
106
- ##
107
-
108
- # => [(jobId:String, type:Symbol, status:String, start_at:String, end_at:String)]
109
- def list_jobs(from=0, to=nil)
110
- params = {}
111
- params['from'] = from.to_s if from
112
- params['to'] = to.to_s if to
113
- code, body, res = get("/v3/job/list", params)
114
- if code != "200"
115
- raise_error("List jobs failed", res)
116
- end
117
- # TODO format check
118
- js = JSON.load(body)
119
- result = []
120
- js['jobs'].each {|m|
121
- job_id = m['job_id']
122
- type = (m['type'] || '?').to_sym
123
- status = m['status']
124
- query = m['query']
125
- start_at = m['start_at']
126
- end_at = m['end_at']
127
- result << [job_id, type, status, query, start_at, end_at]
128
- }
129
- return result
130
- end
131
-
132
- # => (type:Symbol, status:String, result:String, url:String)
133
- def show_job(job_id)
134
- code, body, res = get("/v3/job/show/#{e job_id}")
135
- if code != "200"
136
- raise_error("Show job failed", res)
137
- end
138
- # TODO format check
139
- js = JSON.load(body)
140
- # TODO debug
141
- type = (js['type'] || '?').to_sym # TODO
142
- query = js['query']
143
- status = js['status']
144
- debug = js['debug']
145
- url = js['url']
146
- start_at = js['start_at']
147
- end_at = js['end_at']
148
- return [type, query, status, url, debug, start_at, end_at]
149
- end
150
-
151
- def job_result(job_id)
152
- require 'msgpack'
153
- code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>'msgpack'})
154
- if code != "200"
155
- raise_error("Get job result failed", res)
156
- end
157
- result = []
158
- MessagePack::Unpacker.new.feed_each(body) {|row|
159
- result << row
160
- }
161
- return result
162
- end
163
-
164
- def job_result_format(job_id, format)
165
- # TODO chunked encoding
166
- code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>format})
167
- if code != "200"
168
- raise_error("Get job result failed", res)
169
- end
170
- return body
171
- end
172
-
173
- def job_result_each(job_id, &block)
174
- # TODO chunked encoding
175
- require 'msgpack'
176
- code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>'msgpack'})
177
- if code != "200"
178
- raise_error("Get job result failed", res)
179
- end
180
- result = []
181
- MessagePack::Unpacker.new.feed_each(body) {|row|
182
- yield row
183
- }
184
- nil
185
- end
186
-
187
- def job_result_raw(job_id, format)
188
- code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>format})
189
- if code != "200"
190
- raise_error("Get job result failed", res)
191
- end
192
- return body
193
- end
194
-
195
- # => jobId:String
196
- def hive_query(q, db=nil)
197
- code, body, res = post("/v3/job/issue/hive/#{e db}", {'query'=>q})
198
- if code != "200"
199
- raise_error("Query failed", res)
200
- end
201
- # TODO format check
202
- js = JSON.load(body)
203
- return js['job_id'].to_s
204
- end
205
-
206
-
207
- ####
208
- ## Import API
209
- ##
210
-
211
- # => time:Float
212
- def import(db, table, format, stream, stream_size=stream.lstat.size)
213
- code, body, res = put("/v3/table/import/#{e db}/#{e table}/#{format}", stream, stream_size)
214
- if code[0] != ?2
215
- raise_error("Import failed", res)
216
- end
217
- # TODO format check
218
- js = JSON.load(body)
219
- time = js['time'].to_f
220
- return time
221
- end
222
-
223
-
224
- ####
225
- ## User API
226
- ##
227
-
228
- # apikey:String
229
- def authenticate(user, password)
230
- code, body, res = post("/v3/user/authenticate", {'user'=>user, 'password'=>password})
231
- if code != "200"
232
- raise_error("Authentication failed", res)
233
- end
234
- # TODO format check
235
- js = JSON.load(body)
236
- apikey = js['apikey']
237
- return apikey
238
- end
239
-
240
- ####
241
- ## Server Status API
242
- ##
243
-
244
- # => status:String
245
- def server_status
246
- code, body, res = get('/v3/system/server_status')
247
- if code != "200"
248
- return "Server is down (#{code})"
249
- end
250
- # TODO format check
251
- js = JSON.load(body)
252
- status = js['status']
253
- return status
254
- end
255
-
256
- private
257
- host = 'api.treasure-data.com'
258
- port = 80
259
- if e = ENV['TD_API_SERVER']
260
- host, port_ = e.split(':',2)
261
- port_ = port_.to_i
262
- port = port_ if port_ != 0
263
- end
264
-
265
- HOST = host
266
- PORT = port
267
- USE_SSL = false
268
- BASE_URL = ''
269
-
270
- def get(url, params=nil)
271
- http, header = new_http
272
-
273
- path = BASE_URL + url
274
- if params && !params.empty?
275
- path << "?"+params.map {|k,v|
276
- "#{k}=#{e v}"
277
- }.join('&')
278
- end
279
-
280
- request = Net::HTTP::Get.new(path, header)
281
-
282
- response = http.request(request)
283
- return [response.code, response.body, response]
284
- end
285
-
286
- def post(url, params=nil)
287
- http, header = new_http
288
-
289
- path = BASE_URL + url
290
-
291
- request = Net::HTTP::Post.new(path, header)
292
- request.set_form_data(params) if params
293
-
294
- response = http.request(request)
295
- return [response.code, response.body, response]
296
- end
297
-
298
- def put(url, stream, stream_size)
299
- http, header = new_http
300
-
301
- path = BASE_URL + url
302
-
303
- header['Content-Type'] = 'application/octet-stream'
304
- header['Content-Length'] = stream_size.to_s
305
-
306
- request = Net::HTTP::Put.new(url, header)
307
- if request.respond_to?(:body_stream=)
308
- request.body_stream = stream
309
- else # Ruby 1.8
310
- request.body = stream.read
311
- end
312
-
313
- response = http.request(request)
314
- return [response.code, response.body, response]
315
- end
316
-
317
- def new_http
318
- require 'net/http'
319
- require 'time'
320
-
321
- http = Net::HTTP.new(HOST, PORT)
322
- if USE_SSL
323
- http.use_ssl = true
324
- http.verify_mode = OpenSSL::SSL::VERIFY_PEER
325
- store = OpenSSL::X509::Store.new
326
- http.cert_store = store
327
- end
328
-
329
- #http.read_timeout = options[:read_timeout]
330
-
331
- header = {}
332
- if @apikey
333
- header['Authorization'] = "TD1 #{apikey}"
334
- end
335
- header['Date'] = Time.now.rfc2822
336
-
337
- return http, header
338
- end
339
-
340
- def raise_error(msg, res)
341
- begin
342
- js = JSON.load(res.body)
343
- msg = js['message']
344
- error_code = js['error_code']
345
-
346
- if res.code == "404"
347
- raise NotFoundError, "#{error_code}: #{msg}"
348
- elsif res.code == "409"
349
- raise AlreadyExistsError, "#{error_code}: #{msg}"
350
- else
351
- raise APIError, "#{error_code}: #{msg}"
352
- end
353
-
354
- rescue
355
- if res.code == "404"
356
- raise NotFoundError, "#{msg}: #{res.body}"
357
- elsif res.code == "409"
358
- raise AlreadyExistsError, "#{msg}: #{res.body}"
359
- else
360
- raise APIError, "#{msg}: #{res.body}"
361
- end
362
- end
363
- # TODO error
364
- end
365
-
366
- def e(s)
367
- require 'cgi'
368
- CGI.escape(s.to_s)
369
- end
370
- end
371
-
372
-
373
- end
374
-