Class: TreasureData::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/td/client.rb,
lib/td/client/version.rb

Constant Summary collapse

VERSION =
'1.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey, opts = {}) ⇒ Client



27
28
29
# File 'lib/td/client.rb', line 27

def initialize(apikey, opts={})
  @api = API.new(apikey, opts)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



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

def api
  @api
end

Class Method Details

.authenticate(user, password, opts = {}) ⇒ Client



12
13
14
15
16
# File 'lib/td/client.rb', line 12

def self.authenticate(user, password, opts={})
  api = API.new(nil, opts)
  apikey = api.authenticate(user, password)
  new(apikey)
end

.server_status(opts = {}) ⇒ String



20
21
22
23
# File 'lib/td/client.rb', line 20

def self.server_status(opts={})
  api = API.new(nil, opts)
  api.server_status
end

Instance Method Details

#access_controlsArray<AccessControl>



523
524
525
526
527
528
# File 'lib/td/client.rb', line 523

def access_controls
  list = @api.list_access_controls
  list.map {|subject,action,scope,grant_option|
    AccessControl.new(self, subject, action, scope, grant_option)
  }
end

#accountAccount



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

def 
  , plan, storage, guaranteed_cores, maximum_cores, created_at = @api.
  return .new(self, , plan, storage, guaranteed_cores, maximum_cores, created_at)
end

#add_apikey(user) ⇒ true



497
498
499
# File 'lib/td/client.rb', line 497

def add_apikey(user)
  @api.add_apikey(user)
end

#add_user(name, org, email, password) ⇒ true



472
473
474
# File 'lib/td/client.rb', line 472

def add_user(name, org, email, password)
  @api.add_user(name, org, email, password)
end

#apikeyString



35
36
37
# File 'lib/td/client.rb', line 35

def apikey
  @api.apikey
end

#bulk_import(name) ⇒ BulkImport



326
327
328
329
# File 'lib/td/client.rb', line 326

def bulk_import(name)
  data = @api.show_bulk_import(name)
  BulkImport.new(self, data)
end

#bulk_import_delete_part(name, part_name) ⇒ nil



350
351
352
# File 'lib/td/client.rb', line 350

def bulk_import_delete_part(name, part_name)
  @api.bulk_import_delete_part(name, part_name)
end

#bulk_import_error_records(name, &block) ⇒ Hash



320
321
322
# File 'lib/td/client.rb', line 320

def bulk_import_error_records(name, &block)
  @api.bulk_import_error_records(name, &block)
end

#bulk_import_upload_part(name, part_name, stream, size) ⇒ nil



343
344
345
# File 'lib/td/client.rb', line 343

def bulk_import_upload_part(name, part_name, stream, size)
  @api.bulk_import_upload_part(name, part_name, stream, size)
end

#bulk_importsArray<BulkImport>



332
333
334
335
336
# File 'lib/td/client.rb', line 332

def bulk_imports
  @api.list_bulk_imports.map {|data|
    BulkImport.new(self, data)
  }
end

#bulk_load_create(name, database, table, job, opts = {}) ⇒ Object

name: String, database: String, table: String, job: BulkLoad -> BulkLoad



576
577
578
# File 'lib/td/client.rb', line 576

def bulk_load_create(name, database, table, job, opts = {})
  @api.bulk_load_create(name, database, table, job, opts)
end

#bulk_load_delete(name) ⇒ Object

name: String -> BulkLoad



591
592
593
# File 'lib/td/client.rb', line 591

def bulk_load_delete(name)
  @api.bulk_load_delete(name)
end

#bulk_load_guess(job) ⇒ Object

> BulkLoad::Job



556
557
558
# File 'lib/td/client.rb', line 556

def bulk_load_guess(job)
  @api.bulk_load_guess(job)
end

#bulk_load_history(name) ⇒ Object

name: String -> [Job]



596
597
598
# File 'lib/td/client.rb', line 596

def bulk_load_history(name)
  @api.bulk_load_history(name)
end

#bulk_load_issue(database, table, job) ⇒ Object

> String



566
567
568
# File 'lib/td/client.rb', line 566

def bulk_load_issue(database, table, job)
  @api.bulk_load_issue(database, table, job)
end

#bulk_load_listObject

nil -> [BulkLoad]



571
572
573
# File 'lib/td/client.rb', line 571

def bulk_load_list
  @api.bulk_load_list
end

#bulk_load_preview(job) ⇒ Object

> BulkLoad::Job



561
562
563
# File 'lib/td/client.rb', line 561

def bulk_load_preview(job)
  @api.bulk_load_preview(job)
end

#bulk_load_run(name, scheduled_time = nil) ⇒ Object



600
601
602
# File 'lib/td/client.rb', line 600

def bulk_load_run(name, scheduled_time = nil)
  @api.bulk_load_run(name, scheduled_time)
end

#bulk_load_show(name) ⇒ Object

name: String -> BulkLoad



581
582
583
# File 'lib/td/client.rb', line 581

def bulk_load_show(name)
  @api.bulk_load_show(name)
end

#bulk_load_update(name, settings) ⇒ Object

name: String, settings: Hash -> BulkLoad



586
587
588
# File 'lib/td/client.rb', line 586

def bulk_load_update(name, settings)
  @api.bulk_load_update(name, settings)
end

#change_email(user, email) ⇒ true



485
486
487
# File 'lib/td/client.rb', line 485

def change_email(user, email)
  @api.change_email(user, email)
end

#change_my_password(old_password, password) ⇒ true



518
519
520
# File 'lib/td/client.rb', line 518

def change_my_password(old_password, password)
  @api.change_my_password(old_password, password)
end

#change_password(user, password) ⇒ true



511
512
513
# File 'lib/td/client.rb', line 511

def change_password(user, password)
  @api.change_password(user, password)
end

#commit_bulk_import(name) ⇒ nil



313
314
315
# File 'lib/td/client.rb', line 313

def commit_bulk_import(name)
  @api.commit_bulk_import(name)
end

#core_utilization(from, to) ⇒ Array



66
67
68
69
# File 'lib/td/client.rb', line 66

def core_utilization(from, to)
  from, to, interval, history = @api.(from, to)
  return from, to, interval, history
end

#create_bulk_import(name, database, table, opts = {}) ⇒ nil



281
282
283
# File 'lib/td/client.rb', line 281

def create_bulk_import(name, database, table, opts={})
  @api.create_bulk_import(name, database, table, opts)
end

#create_database(db_name, opts = {}) ⇒ true



47
48
49
# File 'lib/td/client.rb', line 47

def create_database(db_name, opts={})
  @api.create_database(db_name, opts)
end

#create_log_table(db_name, table_name) ⇒ true



92
93
94
# File 'lib/td/client.rb', line 92

def create_log_table(db_name, table_name)
  @api.create_log_table(db_name, table_name)
end

#create_result(name, url, opts = {}) ⇒ true



449
450
451
# File 'lib/td/client.rb', line 449

def create_result(name, url, opts={})
  @api.create_result(name, url, opts)
end

#create_schedule(name, opts) ⇒ Time

Raises:

  • (ArgumentError)


363
364
365
366
367
368
# File 'lib/td/client.rb', line 363

def create_schedule(name, opts)
  raise ArgumentError, "'cron' option is required" unless opts[:cron] || opts['cron']
  raise ArgumentError, "'query' option is required" unless opts[:query] || opts['query']
  start = @api.create_schedule(name, opts)
  return start && Time.parse(start)
end

#database(db_name) ⇒ Database

Raises:



81
82
83
84
85
86
87
88
89
# File 'lib/td/client.rb', line 81

def database(db_name)
  m = @api.list_databases
  m.each {|name,(count, created_at, updated_at, org, permission)|
    if name == db_name
      return Database.new(self, name, nil, count, created_at, updated_at, org, permission)
    end
  }
  raise NotFoundError, "Database '#{db_name}' does not exist"
end

#databasesArray



72
73
74
75
76
77
# File 'lib/td/client.rb', line 72

def databases
  m = @api.list_databases
  m.map {|db_name,(count, created_at, updated_at, org, permission)|
    Database.new(self, db_name, nil, count, created_at, updated_at, org, permission)
  }
end

#delete_bulk_import(name) ⇒ nil



287
288
289
# File 'lib/td/client.rb', line 287

def delete_bulk_import(name)
  @api.delete_bulk_import(name)
end

#delete_database(db_name) ⇒ Symbol



53
54
55
# File 'lib/td/client.rb', line 53

def delete_database(db_name)
  @api.delete_database(db_name)
end

#delete_result(name) ⇒ true



455
456
457
# File 'lib/td/client.rb', line 455

def delete_result(name)
  @api.delete_result(name)
end

#delete_schedule(name) ⇒ Array



372
373
374
# File 'lib/td/client.rb', line 372

def delete_schedule(name)
  @api.delete_schedule(name)
end

#delete_table(db_name, table_name) ⇒ Symbol



125
126
127
# File 'lib/td/client.rb', line 125

def delete_table(db_name, table_name)
  @api.delete_table(db_name, table_name)
end

#export(db_name, table_name, storage_type, opts = {}) ⇒ Job



252
253
254
255
# File 'lib/td/client.rb', line 252

def export(db_name, table_name, storage_type, opts={})
  job_id = @api.export(db_name, table_name, storage_type, opts)
  Job.new(self, job_id, :export, nil)
end

#freeze_bulk_import(name) ⇒ nil



293
294
295
# File 'lib/td/client.rb', line 293

def freeze_bulk_import(name)
  @api.freeze_bulk_import(name)
end

#grant_access_control(subject, action, scope, grant_option) ⇒ true



535
536
537
# File 'lib/td/client.rb', line 535

def grant_access_control(subject, action, scope, grant_option)
  @api.grant_access_control(subject, action, scope, grant_option)
end

#history(name, from = nil, to = nil) ⇒ Array<ScheduledJob>



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/td/client.rb', line 396

def history(name, from=nil, to=nil)
  result = @api.history(name, from, to)
  result.map {|scheduled_at,job_id,type,status,query,start_at,end_at,result_url,priority,database|
    job_param = [job_id, type, query, status,
      nil, nil, # url, debug
      start_at, end_at,
      nil, # cpu_time
      nil, nil, # result_size, result
      result_url,
      nil, # hive_result_schema
      priority,
      nil, # retry_limit
      nil, # TODO org_name
      database]
    ScheduledJob.new(self, scheduled_at, *job_param)
  }
end

#import(db_name, table_name, format, stream, size, unique_id = nil) ⇒ Float



432
433
434
# File 'lib/td/client.rb', line 432

def import(db_name, table_name, format, stream, size, unique_id=nil)
  @api.import(db_name, table_name, format, stream, size, unique_id)
end

#job(job_id) ⇒ Job



194
195
196
197
198
199
200
# File 'lib/td/client.rb', line 194

def job(job_id)
  job_id = job_id.to_s
  type, query, status, url, debug, start_at, end_at, cpu_time,
    result_size, result_url, hive_result_schema, priority, retry_limit, org, db, duration, num_records = @api.show_job(job_id)
  Job.new(self, job_id, type, query, status, url, debug, start_at, end_at, cpu_time,
          result_size, nil, result_url, hive_result_schema, priority, retry_limit, org, db, duration, num_records)
end

#job_result(job_id) ⇒ Object



210
211
212
# File 'lib/td/client.rb', line 210

def job_result(job_id)
  @api.job_result(job_id)
end

#job_result_each(job_id, &block) ⇒ nil



230
231
232
# File 'lib/td/client.rb', line 230

def job_result_each(job_id, &block)
  @api.job_result_each(job_id, &block)
end

#job_result_each_with_compr_size(job_id, &block) ⇒ nil



237
238
239
# File 'lib/td/client.rb', line 237

def job_result_each_with_compr_size(job_id, &block)
  @api.job_result_each_with_compr_size(job_id, &block)
end

#job_result_format(job_id, format, io = nil, &block) ⇒ String



219
220
221
# File 'lib/td/client.rb', line 219

def job_result_format(job_id, format, io=nil, &block)
  @api.job_result_format(job_id, format, io, &block)
end

#job_result_raw(job_id, format, io = nil, &block) ⇒ Object



223
224
225
# File 'lib/td/client.rb', line 223

def job_result_raw(job_id, format, io=nil, &block)
  @api.job_result_raw(job_id, format, io, &block)
end

#job_status(job_id) ⇒ String



204
205
206
# File 'lib/td/client.rb', line 204

def job_status(job_id)
  return @api.job_status(job_id)
end

#jobs(from = nil, to = nil, status = nil, conditions = nil) ⇒ Job



181
182
183
184
185
186
187
188
189
190
# File 'lib/td/client.rb', line 181

def jobs(from=nil, to=nil, status=nil, conditions=nil)
  results = @api.list_jobs(from, to, status, conditions)
  results.map {|job_id, type, status, query, start_at, end_at, cpu_time,
               result_size, result_url, priority, retry_limit, org, db,
               duration, num_records|
    Job.new(self, job_id, type, query, status, nil, nil, start_at, end_at, cpu_time,
            result_size, nil, result_url, nil, priority, retry_limit, org, db,
            duration, num_records)
  }
end

#kill(job_id) ⇒ String



243
244
245
# File 'lib/td/client.rb', line 243

def kill(job_id)
  @api.kill(job_id)
end

#list_apikeys(user) ⇒ Array<String>



491
492
493
# File 'lib/td/client.rb', line 491

def list_apikeys(user)
  @api.list_apikeys(user)
end

#list_bulk_import_parts(name) ⇒ Array



356
357
358
# File 'lib/td/client.rb', line 356

def list_bulk_import_parts(name)
  @api.list_bulk_import_parts(name)
end

#partial_delete(db_name, table_name, to, from, opts = {}) ⇒ Job



271
272
273
274
# File 'lib/td/client.rb', line 271

def partial_delete(db_name, table_name, to, from, opts={})
  job_id = @api.partial_delete(db_name, table_name, to, from, opts)
  Job.new(self, job_id, :partialdelete, nil)
end

#perform_bulk_import(name, opts = {}) ⇒ Job



306
307
308
309
# File 'lib/td/client.rb', line 306

def perform_bulk_import(name, opts={})
  job_id = @api.perform_bulk_import(name, opts)
  Job.new(self, job_id, :bulk_import, nil)
end

#query(db_name, q, result_url = nil, priority = nil, retry_limit = nil, opts = {}) ⇒ Job

Raises:

  • (ArgumentError)


168
169
170
171
172
173
174
# File 'lib/td/client.rb', line 168

def query(db_name, q, result_url=nil, priority=nil, retry_limit=nil, opts={})
  # for compatibility, assume type is hive unless specifically specified
  type = opts[:type] || opts['type'] || :hive
  raise ArgumentError, "The specified query type is not supported: #{type}" unless [:hive, :pig, :impala, :presto].include?(type)
  job_id = @api.query(q, type, db_name, result_url, priority, retry_limit, opts)
  Job.new(self, job_id, type, q)
end

#remove_apikey(user, apikey) ⇒ true



504
505
506
# File 'lib/td/client.rb', line 504

def remove_apikey(user, apikey)
  @api.remove_apikey(user, apikey)
end

#remove_user(user) ⇒ true



478
479
480
# File 'lib/td/client.rb', line 478

def remove_user(user)
  @api.remove_user(user)
end

#result_export(target_job_id, opts = {}) ⇒ Job



260
261
262
263
# File 'lib/td/client.rb', line 260

def result_export(target_job_id, opts={})
  job_id = @api.result_export(target_job_id, opts)
  Job.new(self, job_id, :result_export, nil)
end

#resultsArray<Result>



437
438
439
440
441
442
443
# File 'lib/td/client.rb', line 437

def results
  results = @api.list_result
  rs = results.map {|name,url,organizations|
    Result.new(self, name, url, organizations)
  }
  return rs
end

#revoke_access_control(subject, action, scope) ⇒ true



543
544
545
# File 'lib/td/client.rb', line 543

def revoke_access_control(subject, action, scope)
  @api.revoke_access_control(subject, action, scope)
end

#run_schedule(name, time, num) ⇒ Array<ScheduledJob>



418
419
420
421
422
423
# File 'lib/td/client.rb', line 418

def run_schedule(name, time, num)
  results = @api.run_schedule(name, time, num)
  results.map {|job_id,type,scheduled_at|
    ScheduledJob.new(self, scheduled_at, job_id, type, nil)
  }
end

#schedulesArray<Schedule>



377
378
379
380
381
382
# File 'lib/td/client.rb', line 377

def schedules
  result = @api.list_schedules
  result.map {|name,cron,query,database,result_url,timezone,delay,next_time,priority,retry_limit,org_name|
    Schedule.new(self, name, cron, query, database, result_url, timezone, delay, next_time, priority, retry_limit, org_name)
  }
end

#server_statusString



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

def server_status
  @api.server_status
end

#swap_table(db_name, table_name1, table_name2) ⇒ true

Swap table names



102
103
104
# File 'lib/td/client.rb', line 102

def swap_table(db_name, table_name1, table_name2)
  @api.swap_table(db_name, table_name1, table_name2)
end

#table(db_name, table_name) ⇒ Table

Raises:



143
144
145
146
147
148
149
150
# File 'lib/td/client.rb', line 143

def table(db_name, table_name)
  tables(db_name).each {|t|
    if t.name == table_name
      return t
    end
  }
  raise NotFoundError, "Table '#{db_name}.#{table_name}' does not exist"
end

#tables(db_name) ⇒ Array



131
132
133
134
135
136
137
138
# File 'lib/td/client.rb', line 131

def tables(db_name)
  m = @api.list_tables(db_name)
  m.map {|table_name, (type, schema, count, created_at, updated_at, estimated_storage_size, last_import, last_log_timestamp, expire_days)|
    schema = Schema.new.from_json(schema)
    Table.new(self, db_name, table_name, type, schema, count, created_at, updated_at,
      estimated_storage_size, last_import, last_log_timestamp, expire_days)
  }
end

#tail(db_name, table_name, count, to = nil, from = nil, &block) ⇒ Array?



157
158
159
# File 'lib/td/client.rb', line 157

def tail(db_name, table_name, count, to = nil, from = nil, &block)
  @api.tail(db_name, table_name, count, to, from, &block)
end

#test_access_control(user, action, scope) ⇒ Array



551
552
553
# File 'lib/td/client.rb', line 551

def test_access_control(user, action, scope)
  @api.test_access_control(user, action, scope)
end

#unfreeze_bulk_import(name) ⇒ nil



299
300
301
# File 'lib/td/client.rb', line 299

def unfreeze_bulk_import(name)
  @api.unfreeze_bulk_import(name)
end

#update_expire(db_name, table_name, expire_days) ⇒ true



118
119
120
# File 'lib/td/client.rb', line 118

def update_expire(db_name, table_name, expire_days)
  @api.update_expire(db_name, table_name, expire_days)
end

#update_schedule(name, params) ⇒ nil



387
388
389
390
# File 'lib/td/client.rb', line 387

def update_schedule(name, params)
  @api.update_schedule(name, params)
  nil
end

#update_schema(db_name, table_name, schema) ⇒ true



110
111
112
# File 'lib/td/client.rb', line 110

def update_schema(db_name, table_name, schema)
  @api.update_schema(db_name, table_name, schema.to_json)
end

#usersArray<User>



460
461
462
463
464
465
# File 'lib/td/client.rb', line 460

def users
  list = @api.list_users
  list.map {|name,org,roles,email|
    User.new(self, name, org, roles, email)
  }
end