class TreasureData::Client

Constants

VERSION

Attributes

api[R]

@!attribute [r] api

Public Class Methods

authenticate(user, password, opts={}) click to toggle source

@param [String] user TreasureData username @param [String] password TreasureData password @param [Hash] opts options for API @return [Client] instance of this class

# 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
new(apikey, opts={}) click to toggle source

@param [String] apikey TreasureData API key @param [Hash] opts options for API

# File lib/td/client.rb, line 27
def initialize(apikey, opts={})
  @api = API.new(apikey, opts)
end
server_status(opts={}) click to toggle source

@param [Hash] opts options for API @return [String] HTTP status code of server returns

# File lib/td/client.rb, line 20
def self.server_status(opts={})
  api = API.new(nil, opts)
  api.server_status
end

Public Instance Methods

account() click to toggle source

@return [Account]

# File lib/td/client.rb, line 58
def account
  account_id, plan, storage, guaranteed_cores, maximum_cores, created_at = @api.show_account
  return Account.new(self, account_id, plan, storage, guaranteed_cores, maximum_cores, created_at)
end
add_apikey(user) click to toggle source

@param [String] user @return [true]

# File lib/td/client.rb, line 520
def add_apikey(user)
  @api.add_apikey(user)
end
add_user(name, org, email, password) click to toggle source

@param [String] name @param [String] org @param [String] email @param [String] password @return [true]

# File lib/td/client.rb, line 495
def add_user(name, org, email, password)
  @api.add_user(name, org, email, password)
end
apikey() click to toggle source

@return [String] API key

# File lib/td/client.rb, line 35
def apikey
  @api.apikey
end
bulk_import(name) click to toggle source

@param [String] name @return [BulkImport]

# File lib/td/client.rb, line 349
def bulk_import(name)
  data = @api.show_bulk_import(name)
  BulkImport.new(self, data)
end
bulk_import_delete_part(name, part_name) click to toggle source

@param [String] name @param [String] part_name @return [nil]

# File lib/td/client.rb, line 373
def bulk_import_delete_part(name, part_name)
  @api.bulk_import_delete_part(name, part_name)
end
bulk_import_error_records(name, &block) click to toggle source

@param [String] name @param [Proc] block @return [Hash]

# File lib/td/client.rb, line 343
def bulk_import_error_records(name, &block)
  @api.bulk_import_error_records(name, &block)
end
bulk_import_upload_part(name, part_name, stream, size) click to toggle source

@param [String] name @param [String] part_name @param [String, StringIO] stream @param [Fixnum] size @return [nil]

# File lib/td/client.rb, line 366
def bulk_import_upload_part(name, part_name, stream, size)
  @api.bulk_import_upload_part(name, part_name, stream, size)
end
bulk_imports() click to toggle source

@return [Array<BulkImport>]

# File lib/td/client.rb, line 355
def bulk_imports
  @api.list_bulk_imports.map {|data|
    BulkImport.new(self, data)
  }
end
bulk_load_create(name, database, table, job, opts = {}) click to toggle source

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

# File lib/td/client.rb, line 566
def bulk_load_create(name, database, table, job, opts = {})
  @api.bulk_load_create(name, database, table, job, opts)
end
bulk_load_delete(name) click to toggle source

name: String -> BulkLoad

# File lib/td/client.rb, line 581
def bulk_load_delete(name)
  @api.bulk_load_delete(name)
end
bulk_load_guess(job) click to toggle source

> BulkLoad::Job

# File lib/td/client.rb, line 546
def bulk_load_guess(job)
  @api.bulk_load_guess(job)
end
bulk_load_history(name) click to toggle source

name: String -> [Job]

# File lib/td/client.rb, line 586
def bulk_load_history(name)
  @api.bulk_load_history(name)
end
bulk_load_issue(database, table, job) click to toggle source

> String

# File lib/td/client.rb, line 556
def bulk_load_issue(database, table, job)
  @api.bulk_load_issue(database, table, job)
end
bulk_load_list() click to toggle source

nil -> [BulkLoad]

# File lib/td/client.rb, line 561
def bulk_load_list
  @api.bulk_load_list
end
bulk_load_preview(job) click to toggle source

> BulkLoad::Job

# File lib/td/client.rb, line 551
def bulk_load_preview(job)
  @api.bulk_load_preview(job)
end
bulk_load_run(name, scheduled_time = nil) click to toggle source
# File lib/td/client.rb, line 590
def bulk_load_run(name, scheduled_time = nil)
  @api.bulk_load_run(name, scheduled_time)
end
bulk_load_show(name) click to toggle source

name: String -> BulkLoad

# File lib/td/client.rb, line 571
def bulk_load_show(name)
  @api.bulk_load_show(name)
end
bulk_load_update(name, settings) click to toggle source

name: String, settings: Hash -> BulkLoad

# File lib/td/client.rb, line 576
def bulk_load_update(name, settings)
  @api.bulk_load_update(name, settings)
end
change_database(db_name, table_name, new_db_name) click to toggle source

@param [String] db_name @param [String] table_name @param [String] new_db_name @return [true]

# File lib/td/client.rb, line 180
def change_database(db_name, table_name, new_db_name)
  @api.change_database(db_name, table_name, new_db_name)
end
change_email(user, email) click to toggle source

@param [String] user @param [String] email @return [true]

# File lib/td/client.rb, line 508
def change_email(user, email)
  @api.change_email(user, email)
end
change_my_password(old_password, password) click to toggle source

@param [String] old_password @param [String] password @return [true]

# File lib/td/client.rb, line 541
def change_my_password(old_password, password)
  @api.change_my_password(old_password, password)
end
change_password(user, password) click to toggle source

@param [String] user @param [String] password @return [true]

# File lib/td/client.rb, line 534
def change_password(user, password)
  @api.change_password(user, password)
end
commit_bulk_import(name) click to toggle source

@param [String] name @return [nil]

# File lib/td/client.rb, line 336
def commit_bulk_import(name)
  @api.commit_bulk_import(name)
end
core_utilization(from, to) click to toggle source

@param [Fixnum] from @param [Fixnum] to @return [Array] from, to, interval, history

# File lib/td/client.rb, line 66
def core_utilization(from, to)
  from, to, interval, history = @api.account_core_utilization(from, to)
  return from, to, interval, history
end
create_bulk_import(name, database, table, opts={}) click to toggle source

@param [String] name @param [String] database @param [String] table @param [Hash] opts @return [nil]

# File lib/td/client.rb, line 304
def create_bulk_import(name, database, table, opts={})
  @api.create_bulk_import(name, database, table, opts)
end
create_database(db_name, opts={}) click to toggle source

@param [String] db_name @param [Hash] opts @return [true]

# 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, params={}) click to toggle source

@param [String] db @param [String] table @option params [Fixnum] :expire_days days to expire table @option params [Boolean] :include_v (true) include v column on Hive @option params [Boolean] :detect_schema (true) detect schema on import @return [true]

# File lib/td/client.rb, line 97
def create_log_table(db_name, table_name, params={})
  @api.create_log_table(db_name, table_name, params)
end
create_result(name, url, opts={}) click to toggle source

@param [String] name @param [String] url @param [Hash] opts @return [true]

# File lib/td/client.rb, line 472
def create_result(name, url, opts={})
  @api.create_result(name, url, opts)
end
create_schedule(name, opts) click to toggle source

@param [String] name @param [Hash] opts @return [Time]

# File lib/td/client.rb, line 386
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) click to toggle source

@param [String] db_name @return [Database]

# 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
databases() click to toggle source

@return [Array] databases

# 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) click to toggle source

@param [String] name @return [nil]

# File lib/td/client.rb, line 310
def delete_bulk_import(name)
  @api.delete_bulk_import(name)
end
delete_database(db_name) click to toggle source

@param [String] db_name @return [Symbol]

# File lib/td/client.rb, line 53
def delete_database(db_name)
  @api.delete_database(db_name)
end
delete_result(name) click to toggle source

@param [String] name @return [true]

# File lib/td/client.rb, line 478
def delete_result(name)
  @api.delete_result(name)
end
delete_schedule(name) click to toggle source

@param [String] name @return [Array]

# File lib/td/client.rb, line 395
def delete_schedule(name)
  @api.delete_schedule(name)
end
delete_table(db_name, table_name) click to toggle source

@param [String] db_name @param [String] table_name @return [Symbol]

# File lib/td/client.rb, line 140
def delete_table(db_name, table_name)
  @api.delete_table(db_name, table_name)
end
export(db_name, table_name, storage_type, opts={}) click to toggle source

@param [String] db_name @param [String] table_name @param [String] storage_type @param [Hash] opts @return [Job]

# File lib/td/client.rb, line 275
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) click to toggle source

@param [String] name @return [nil]

# File lib/td/client.rb, line 316
def freeze_bulk_import(name)
  @api.freeze_bulk_import(name)
end
history(name, from=nil, to=nil) click to toggle source

@param [String] name @param [Fixnum] from @param [Fixnum] to @return [Array<ScheduledJob>]

# File lib/td/client.rb, line 419
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) click to toggle source

@param [String] db_name @param [String] table_name @param [String] format @param [String, StringIO] stream @param [Fixnum] size @param [String] unique_id @return [Float]

# File lib/td/client.rb, line 455
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) click to toggle source

@param [String] job_id @return [Job]

# File lib/td/client.rb, line 217
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) click to toggle source

@param [String] job_id @return [Object]

# File lib/td/client.rb, line 233
def job_result(job_id)
  @api.job_result(job_id)
end
job_result_each(job_id, &block) click to toggle source

@param [String] job_id @param [Proc] block @return [nil]

# File lib/td/client.rb, line 253
def job_result_each(job_id, &block)
  @api.job_result_each(job_id, &block)
end
job_result_each_with_compr_size(job_id, &block) click to toggle source

@param [String] job_id @param [Proc] block @return [nil]

# File lib/td/client.rb, line 260
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) click to toggle source

@param [String] job_id @param [String] format @param [IO] io @param [Proc] block @return [String]

# File lib/td/client.rb, line 242
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) click to toggle source
# File lib/td/client.rb, line 246
def job_result_raw(job_id, format, io=nil, &block)
  @api.job_result_raw(job_id, format, io, &block)
end
job_status(job_id) click to toggle source

@param [String] job_id @return [String] HTTP status code

# File lib/td/client.rb, line 227
def job_status(job_id)
  return @api.job_status(job_id)
end
jobs(from=nil, to=nil, status=nil, conditions=nil) click to toggle source

@param [Fixnum] from @param [Fixnum] to @param [String] status @param [Hash] conditions @return [Job]

# File lib/td/client.rb, line 204
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) click to toggle source

@param [String] job_id @return [String] former_status

# File lib/td/client.rb, line 266
def kill(job_id)
  @api.kill(job_id)
end
list_apikeys(user) click to toggle source

@param [String] user @return [Array<String>]

# File lib/td/client.rb, line 514
def list_apikeys(user)
  @api.list_apikeys(user)
end
list_bulk_import_parts(name) click to toggle source

@param [String] name @return [Array]

# File lib/td/client.rb, line 379
def list_bulk_import_parts(name)
  @api.list_bulk_import_parts(name)
end
partial_delete(db_name, table_name, to, from, opts={}) click to toggle source

@param [String] db_name @param [String] table_name @param [Fixnum] to @param [Fixnum] from @param [Hash] opts @return [Job]

# File lib/td/client.rb, line 294
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={}) click to toggle source

@param [String] name @param [Hash] opts options for API @return [Job]

# File lib/td/client.rb, line 329
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={}) click to toggle source

@param [String] db_name @param [String] q @param [String] result_url @param [Fixnum] priority @param [Fixnum] retry_limit @param [Hash] opts @return [Job]

# File lib/td/client.rb, line 191
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) click to toggle source

@param [String] user @param [String] apikey @return [true]

# File lib/td/client.rb, line 527
def remove_apikey(user, apikey)
  @api.remove_apikey(user, apikey)
end
remove_user(user) click to toggle source

@param [String] user @return [true]

# File lib/td/client.rb, line 501
def remove_user(user)
  @api.remove_user(user)
end
result_export(target_job_id, opts={}) click to toggle source

@param [String] target_job_id @param [Hash] opts @return [Job]

# File lib/td/client.rb, line 283
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
results() click to toggle source

@return [Array<Result>]

# File lib/td/client.rb, line 460
def results
  results = @api.list_result
  rs = results.map {|name,url,organizations|
    Result.new(self, name, url, organizations)
  }
  return rs
end
run_schedule(name, time, num) click to toggle source

@param [String] name @param [Fixnum] time UNIX timestamp @param [Fixnum] num @return [Array<ScheduledJob>]

# File lib/td/client.rb, line 441
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
schedules() click to toggle source

@return [Array<Schedule>]

# File lib/td/client.rb, line 400
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_status() click to toggle source

@return [String] HTTP status code of server returns

# File lib/td/client.rb, line 40
def server_status
  @api.server_status
end
swap_table(db_name, table_name1, table_name2) click to toggle source

Swap table names

@param [String] db_name @param [String] table_name1 @param [String] table_name2 @return [true]

# File lib/td/client.rb, line 107
def swap_table(db_name, table_name1, table_name2)
  @api.swap_table(db_name, table_name1, table_name2)
end
table(db_name, table_name) click to toggle source

@param [String] db_name @param [String] table_name @return [Table]

# File lib/td/client.rb, line 158
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) click to toggle source

@param [String] db_name @return [Array] Tables

# File lib/td/client.rb, line 146
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, include_v)|
    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, include_v)
  }
end
tail(db_name, table_name, count, to = nil, from = nil, &block) click to toggle source

@param [String] db_name @param [String] table_name @param [Fixnum] count @param [Proc] block @return [Array, nil]

# File lib/td/client.rb, line 172
def tail(db_name, table_name, count, to = nil, from = nil, &block)
  @api.tail(db_name, table_name, count, to, from, &block)
end
unfreeze_bulk_import(name) click to toggle source

@param [String] name @return [nil]

# File lib/td/client.rb, line 322
def unfreeze_bulk_import(name)
  @api.unfreeze_bulk_import(name)
end
update_expire(db_name, table_name, expire_days) click to toggle source

@param [String] db_name @param [String] table_name @param [Fixnum] expire_days @return [true]

# File lib/td/client.rb, line 133
def update_expire(db_name, table_name, expire_days)
  @api.update_expire(db_name, table_name, expire_days)
end
update_schedule(name, params) click to toggle source

@param [String] name @param [Hash] params @return [nil]

# File lib/td/client.rb, line 410
def update_schedule(name, params)
  @api.update_schedule(name, params)
  nil
end
update_schema(db_name, table_name, schema) click to toggle source

@param [String] db_name @param [String] table_name @param [String] schema @return [true]

# File lib/td/client.rb, line 115
def update_schema(db_name, table_name, schema)
  @api.update_schema(db_name, table_name, schema.to_json)
end
update_table(db_name, table_name, params={}) click to toggle source

@param [String] db @param [String] table @option params [Fixnum] :expire_days days to expire table @option params [Boolean] :include_v (true) include v column on Hive @option params [Boolean] :detect_schema (true) detect schema on import @return [true]

# File lib/td/client.rb, line 125
def update_table(db_name, table_name, params={})
  @api.update_table(db_name, table_name, params)
end
users() click to toggle source

@return [Array<User>]

# File lib/td/client.rb, line 483
def users
  list = @api.list_users
  list.map {|name,org,roles,email|
    User.new(self, name, org, roles, email)
  }
end