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.
data/lib/td/client.rb ADDED
@@ -0,0 +1,337 @@
1
+ require 'time'
2
+ require 'td/api'
3
+
4
+ module TreasureData
5
+
6
+ class Client
7
+ def self.authenticate(user, password)
8
+ api = API.new(nil)
9
+ apikey = api.authenticate(user, password)
10
+ new(apikey)
11
+ end
12
+
13
+ def self.server_status
14
+ api = API.new(nil)
15
+ api.server_status
16
+ end
17
+
18
+ def initialize(apikey)
19
+ @api = API.new(apikey)
20
+ end
21
+
22
+ attr_reader :api
23
+
24
+ def apikey
25
+ @api.apikey
26
+ end
27
+
28
+ def server_status
29
+ @api.server_status
30
+ end
31
+
32
+ # => true
33
+ def create_database(db_name)
34
+ @api.create_database(db_name)
35
+ end
36
+
37
+ # => true
38
+ def delete_database(db_name)
39
+ @api.delete_database(db_name)
40
+ end
41
+
42
+ # => [Database]
43
+ def databases
44
+ names = @api.list_databases
45
+ names.map {|db_name|
46
+ Database.new(self, db_name)
47
+ }
48
+ end
49
+
50
+ # => Database
51
+ def database(db_name)
52
+ names = @api.list_databases
53
+ names.each {|n|
54
+ if n == db_name
55
+ return Database.new(self, db_name)
56
+ end
57
+ }
58
+ raise NotFoundError, "Database '#{db_name}' does not exist"
59
+ end
60
+
61
+ # => true
62
+ def create_table(db_name, table_name, type)
63
+ @api.create_table(db_name, table_name, type)
64
+ end
65
+
66
+ # => true
67
+ def create_log_table(db_name, table_name)
68
+ create_table(db_name, table_name, :log)
69
+ end
70
+
71
+ # => true
72
+ def create_item_table(db_name, table_name)
73
+ create_table(db_name, table_name, :item)
74
+ end
75
+
76
+ # => type:Symbol
77
+ def delete_table(db_name, table_name)
78
+ @api.delete_table(db_name, table_name)
79
+ end
80
+
81
+ # => [Table]
82
+ def tables(db_name)
83
+ m = @api.list_tables(db_name)
84
+ m.map {|table_name,(type,count)|
85
+ Table.new(self, db_name, table_name, type, count)
86
+ }
87
+ end
88
+
89
+ # => Table
90
+ def table(db_name, table_name)
91
+ m = @api.list_tables(db_name)
92
+ m.each_pair {|name,(type,count)|
93
+ if name == table_name
94
+ return Table.new(self, db_name, name, type, count)
95
+ end
96
+ }
97
+ raise NotFoundError, "Table '#{db_name}.#{table_name}' does not exist"
98
+ end
99
+
100
+ # => Job
101
+ def query(db_name, q)
102
+ job_id = @api.hive_query(q, db_name)
103
+ Job.new(self, job_id, :hive, q) # TODO url
104
+ end
105
+
106
+ # => [Job=]
107
+ def jobs(from=nil, to=nil)
108
+ js = @api.list_jobs(from, to)
109
+ js.map {|job_id,type,status,query,start_at,end_at|
110
+ Job.new(self, job_id, type, query, status, nil, nil, start_at, end_at)
111
+ }
112
+ end
113
+
114
+ # => Job
115
+ def job(job_id)
116
+ job_id = job_id.to_s
117
+ type, query, status, url, debug, start_at, end_at = @api.show_job(job_id)
118
+ Job.new(self, job_id, type, query, status, url, debug, start_at, end_at)
119
+ end
120
+
121
+ # => type:Symbol, url:String
122
+ def job_status(job_id)
123
+ type, query, status, url, debug, start_at, end_at = @api.show_job(job_id)
124
+ return query, status, url, debug, start_at, end_at
125
+ end
126
+
127
+ # => result:[{column:String=>value:Object]
128
+ def job_result(job_id)
129
+ @api.job_result(job_id)
130
+ end
131
+
132
+ # => result:String
133
+ def job_result_format(job_id, format)
134
+ @api.job_result_format(job_id, format)
135
+ end
136
+
137
+ # => nil
138
+ def job_result_each(job_id, &block)
139
+ @api.job_result_each(job_id, &block)
140
+ end
141
+
142
+ # => time:Flaot
143
+ def import(db_name, table_name, format, stream, stream_size=stream.lstat.size)
144
+ @api.import(db_name, table_name, format, stream, stream_size)
145
+ end
146
+ end
147
+
148
+
149
+ class Model
150
+ def initialize(client)
151
+ @client = client
152
+ end
153
+ end
154
+
155
+ class Database < Model
156
+ def initialize(client, db_name, tables=nil)
157
+ super(client)
158
+ @db_name = db_name
159
+ @tables = tables
160
+ end
161
+
162
+ def name
163
+ @db_name
164
+ end
165
+
166
+ def tables
167
+ update_tables! unless @tables
168
+ @tables
169
+ end
170
+
171
+ def create_table(name, type)
172
+ @client.create_table(@db_name, name, type)
173
+ end
174
+
175
+ def create_log_table(name)
176
+ create_table(name, :log)
177
+ end
178
+
179
+ def create_item_table(name)
180
+ create_table(name, :item)
181
+ end
182
+
183
+ def table(table_name)
184
+ @client.table(@db_name, table_name)
185
+ end
186
+
187
+ def delete
188
+ @client.delete_database(@db_name)
189
+ end
190
+
191
+ def update_tables!
192
+ @tables = @client.tables(@db_name)
193
+ end
194
+ end
195
+
196
+ class Table < Model
197
+ def initialize(client, db_name, table_name, type, count)
198
+ super(client)
199
+ @db_name = db_name
200
+ @table_name = table_name
201
+ @type = type
202
+ @count = count
203
+ end
204
+
205
+ attr_reader :type, :count
206
+
207
+ def database_name
208
+ @db_name
209
+ end
210
+
211
+ def database
212
+ @client.database(@db_name)
213
+ end
214
+
215
+ def name
216
+ @table_name
217
+ end
218
+
219
+ def identifier
220
+ "#{@db_name}.#{@table_name}"
221
+ end
222
+
223
+ def delete
224
+ @client.delete_table(@db_name, @table_name)
225
+ end
226
+ end
227
+
228
+ class Job < Model
229
+ def initialize(client, job_id, type, query, status=nil, url=nil, debug=nil, start_at=nil, end_at=nil, result=nil)
230
+ super(client)
231
+ @job_id = job_id
232
+ @type = type
233
+ @url = url
234
+ @query = query
235
+ @status = status
236
+ @debug = debug
237
+ @start_at = start_at
238
+ @end_at = end_at
239
+ @result = result
240
+ end
241
+
242
+ attr_reader :job_id, :type
243
+
244
+ def wait(timeout=nil)
245
+ # TODO
246
+ end
247
+
248
+ def query
249
+ update_status! unless @query
250
+ @query
251
+ end
252
+
253
+ def status
254
+ update_status! unless @status
255
+ @status
256
+ end
257
+
258
+ def url
259
+ update_status! unless @url
260
+ @url
261
+ end
262
+
263
+ def debug
264
+ update_status! unless @debug
265
+ @debug
266
+ end
267
+
268
+ def start_at
269
+ update_status! unless @start_at
270
+ @start_at && !@start_at.empty? ? Time.parse(@start_at) : nil
271
+ end
272
+
273
+ def end_at
274
+ update_status! unless @end_at
275
+ @end_at && !@end_at.empty? ? Time.parse(@end_at) : nil
276
+ end
277
+
278
+ def result
279
+ unless @result
280
+ return nil unless finished?
281
+ @result = @client.job_result(@job_id)
282
+ end
283
+ @result
284
+ end
285
+
286
+ def result_format(format)
287
+ return nil unless finished?
288
+ @client.job_result_format(@job_id, format)
289
+ end
290
+
291
+ def result_each(&block)
292
+ if @result
293
+ @result.each(&block)
294
+ else
295
+ @client.job_result_each(@job_id, &block)
296
+ end
297
+ nil
298
+ end
299
+
300
+ def finished?
301
+ update_status! unless @status
302
+ if @status == "success" || @status == "error"
303
+ return true
304
+ else
305
+ return false
306
+ end
307
+ end
308
+
309
+ def running?
310
+ !finished?
311
+ end
312
+
313
+ def success?
314
+ update_status! unless @status
315
+ @status == "success"
316
+ end
317
+
318
+ def error?
319
+ update_status! unless @status
320
+ @status == "error"
321
+ end
322
+
323
+ def update_status!
324
+ query, status, url, debug, start_at, end_at = @client.job_status(@job_id)
325
+ @query = query
326
+ @status = status
327
+ @url = url
328
+ @debug = debug
329
+ @start_at = start_at
330
+ @end_at = end_at
331
+ self
332
+ end
333
+ end
334
+
335
+
336
+ end
337
+
@@ -1,5 +1,5 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
 
5
5
  def account
@@ -39,7 +39,7 @@ module Command
39
39
  exit 0
40
40
  end
41
41
 
42
- api = nil
42
+ client = nil
43
43
 
44
44
  2.times do
45
45
  begin
@@ -57,23 +57,23 @@ module Command
57
57
  exit 0
58
58
  end
59
59
 
60
- require 'td/api'
60
+ require 'td/client'
61
61
 
62
62
  begin
63
- api = API.authenticate(user_name, password)
64
- rescue TD::AuthError
63
+ client = Client.authenticate(user_name, password)
64
+ rescue TreasureData::AuthError
65
65
  $stderr.puts "User name or password mismatched."
66
66
  end
67
67
 
68
- break if api
68
+ break if client
69
69
  end
70
- return unless api
70
+ return unless client
71
71
 
72
72
  $stderr.puts "Authenticated successfully."
73
73
 
74
74
  conf ||= Config.new
75
75
  conf["account.user"] = user_name
76
- conf["account.apikey"] = api.apikey
76
+ conf["account.apikey"] = client.apikey
77
77
  conf.save
78
78
 
79
79
  $stderr.puts "Use '#{$prog} create-database <db_name>' to create a database."
@@ -1,5 +1,5 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
  private
5
5
  def cmd_opt(name, *args)
@@ -56,13 +56,13 @@ EOF
56
56
  op
57
57
  end
58
58
 
59
- def cmd_api
59
+ def get_client
60
60
  apikey = Config.apikey
61
61
  unless apikey
62
62
  raise ConfigError, "Account is not configured."
63
63
  end
64
- require 'td/api'
65
- api = API.new(apikey)
64
+ require 'td/client'
65
+ Client.new(apikey)
66
66
  end
67
67
 
68
68
  def cmd_render_table(rows, *opts)
@@ -85,9 +85,9 @@ EOF
85
85
  end
86
86
  end
87
87
 
88
- def find_database(api, db_name)
88
+ def find_database(client, db_name)
89
89
  begin
90
- return api.database(db_name)
90
+ return client.database(db_name)
91
91
  rescue
92
92
  cmd_debug_error $!
93
93
  $stderr.puts $!
@@ -97,8 +97,8 @@ EOF
97
97
  db
98
98
  end
99
99
 
100
- def find_table(api, db_name, table_name, type=nil)
101
- db = find_database(api, db_name)
100
+ def find_table(client, db_name, table_name, type=nil)
101
+ db = find_database(client, db_name)
102
102
  begin
103
103
  table = db.table(table_name)
104
104
  rescue
@@ -1,17 +1,17 @@
1
1
 
2
- module TD
2
+ module TreasureData
3
3
  module Command
4
4
 
5
5
  def create_database
6
6
  op = cmd_opt 'create-database', :db_name
7
7
  db_name = op.cmd_parse
8
8
 
9
- api = cmd_api
9
+ client = get_client
10
10
 
11
11
  API.validate_database_name(db_name)
12
12
 
13
13
  begin
14
- api.create_database(db_name)
14
+ client.create_database(db_name)
15
15
  rescue AlreadyExistsError
16
16
  $stderr.puts "Database '#{db_name}' already exists."
17
17
  exit 1
@@ -33,10 +33,10 @@ module Command
33
33
 
34
34
  db_name = op.cmd_parse
35
35
 
36
- api = cmd_api
36
+ client = get_client
37
37
 
38
38
  begin
39
- db = api.database(db_name)
39
+ db = client.database(db_name)
40
40
 
41
41
  if !force && !db.tables.empty?
42
42
  $stderr.puts "Database '#{db_name}' is not empty. Use '-f' option or drop tables first."
@@ -56,9 +56,9 @@ module Command
56
56
  op = cmd_opt 'show-databases'
57
57
  op.cmd_parse
58
58
 
59
- api = cmd_api
59
+ client = get_client
60
60
 
61
- dbs = api.databases
61
+ dbs = client.databases
62
62
 
63
63
  rows = []
64
64
  dbs.each {|db|