Class: TreasureData::API

Inherits:
Object
  • Object
show all
Includes:
AccessControl, Account, BulkImport, BulkLoad, Database, Export, Import, Job, PartialDelete, Result, Schedule, ServerStatus, Table, User
Defined in:
lib/td/client/api.rb,
lib/td/client/api/job.rb,
lib/td/client/api/user.rb,
lib/td/client/api/table.rb,
lib/td/client/api/export.rb,
lib/td/client/api/import.rb,
lib/td/client/api/result.rb,
lib/td/client/api/account.rb,
lib/td/client/api/database.rb,
lib/td/client/api/schedule.rb,
lib/td/client/api/bulk_load.rb,
lib/td/client/api/bulk_import.rb,
lib/td/client/api/server_status.rb,
lib/td/client/api/access_control.rb,
lib/td/client/api/partial_delete.rb

Defined Under Namespace

Modules: AccessControl, Account, BulkImport, BulkLoad, Database, Export, Import, Job, PartialDelete, Result, Schedule, ServerStatus, Table, User Classes: IncompleteError

Constant Summary collapse

DEFAULT_ENDPOINT =
'api.treasuredata.com'
DEFAULT_IMPORT_ENDPOINT =
'api-import.treasuredata.com'
NEW_DEFAULT_ENDPOINT =

Deprecated. Use DEFAULT_ENDPOINT and DEFAULT_IMPORT_ENDPOINT instead

DEFAULT_ENDPOINT
NEW_DEFAULT_IMPORT_ENDPOINT =
DEFAULT_IMPORT_ENDPOINT
OLD_ENDPOINT =
'api.treasure-data.com'
MSGPACK_INT64_MAX =
2 ** 64 - 1
MSGPACK_INT64_MIN =

it’s just same with -2**63, but for readability

-1 * (2 ** 63)

Constants included from BulkLoad

BulkLoad::JOB, BulkLoad::LIST, BulkLoad::SESSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from User

#add_apikey, #add_user, #authenticate, #change_email, #change_my_password, #change_password, #list_apikeys, #list_users, #remove_apikey, #remove_user

Methods included from Table

#create_log_table, #delete_table, #list_tables, #swap_table, #tail, #update_expire, #update_schema, #update_table

Methods included from ServerStatus

#server_status

Methods included from Schedule

#create_schedule, #delete_schedule, #history, #list_schedules, #run_schedule, #update_schedule

Methods included from Result

#create_result, #delete_result, #list_result

Methods included from PartialDelete

#partial_delete

Methods included from Job

#hive_query, #job_result, #job_result_each, #job_result_each_with_compr_size, #job_result_format, #job_result_raw, #job_status, #kill, #list_jobs, #pig_query, #query, #show_job

Methods included from Import

#import

Methods included from Export

#export, #result_export

Methods included from Database

#create_database, #delete_database, #list_databases

Methods included from BulkLoad

#bulk_load_create, #bulk_load_delete, #bulk_load_guess, #bulk_load_history, #bulk_load_issue, #bulk_load_list, #bulk_load_preview, #bulk_load_run, #bulk_load_show, #bulk_load_update

Methods included from BulkImport

#bulk_import_delete_part, #bulk_import_error_records, #bulk_import_upload_part, #commit_bulk_import, #create_bulk_import, #delete_bulk_import, #freeze_bulk_import, #list_bulk_import_parts, #list_bulk_imports, #perform_bulk_import, #show_bulk_import, #unfreeze_bulk_import

Methods included from Account

#account_core_utilization, #show_account

Methods included from AccessControl

#grant_access_control, #list_access_controls, #revoke_access_control, #test_access_control

Constructor Details

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

for backward compatibility

Parameters:

  • apikey (String)
  • opts (Hash) (defaults to: {})


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/client/api.rb', line 52

def initialize(apikey, opts={})
  require 'json'
  require 'time'
  require 'uri'
  require 'net/http'
  require 'net/https'
  require 'time'
  #require 'faraday' # faraday doesn't support streaming upload with httpclient yet so now disabled
  require 'httpclient'
  require 'zlib'
  require 'stringio'
  require 'cgi'
  require 'msgpack'

  @apikey = apikey
  @user_agent = "TD-Client-Ruby: #{TreasureData::Client::VERSION}"
  @user_agent = "#{opts[:user_agent]}; " + @user_agent if opts.has_key?(:user_agent)

  endpoint = opts[:endpoint] || ENV['TD_API_SERVER'] || DEFAULT_ENDPOINT
  uri = URI.parse(endpoint)

  @connect_timeout = opts[:connect_timeout] || 60
  @read_timeout = opts[:read_timeout] || 600
  @send_timeout = opts[:send_timeout] || 600
  @retry_post_requests = opts[:retry_post_requests] || false
  @retry_delay = opts[:retry_delay] || 5
  @max_cumul_retry_delay = opts[:max_cumul_retry_delay] || 600

  case uri.scheme
  when 'http', 'https'
    @host = uri.host
    @port = uri.port
    # the opts[:ssl] option is ignored here, it's value
    #   overridden by the scheme of the endpoint URI
    @ssl = (uri.scheme == 'https')
    @base_path = uri.path.to_s

  else
    if uri.port
      # invalid URI
      raise "Invalid endpoint: #{endpoint}"
    end

    # generic URI
    @host, @port = endpoint.split(':', 2)
    @port = @port.to_i
    if opts[:ssl] === false || @host == TreasureData::API::OLD_ENDPOINT
      # for backward compatibility, old endpoint specified without ssl option, use http
      #
      # opts[:ssl] would be nil if user doesn't specify ssl options,
      # but connecting to https is the new default behavior (since 0.9)
      # so check ssl option by `if opts[:ssl] === false` instead of `if opts[:ssl]`
      # that means if user desire to use http, give `:ssl => false` for initializer such as API.new("APIKEY", :ssl => false)
      @port = 80 if @port == 0
      @ssl = false
    else
      @port = 443 if @port == 0
      @ssl = true
    end
    @base_path = ''
  end

  @http_proxy = opts[:http_proxy] || ENV['HTTP_PROXY']
  @headers = opts[:headers] || {}
  @api = api_client("#{@ssl ? 'https' : 'http'}://#{@host}:#{@port}")
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



122
123
124
# File 'lib/td/client/api.rb', line 122

def apikey
  @apikey
end

Class Method Details

.create_empty_gz_dataString

for fluent-plugin-td / td command to check table existence with import onlt user

Returns:

  • (String)


239
240
241
242
243
# File 'lib/td/client/api.rb', line 239

def self.create_empty_gz_data
  io = StringIO.new
  Zlib::GzipWriter.new(io).close
  io.string
end

.normalize_database_name(name) ⇒ Object

Parameters:

  • name (String)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/td/client/api.rb', line 216

def self.normalize_database_name(name)
  name = name.to_s
  if name.empty?
    raise "Empty name is not allowed"
  end
  if name.length < 3
    name += "_" * (3 - name.length)
  end
  if 255 < name.length
    name = name[0, 253] + "__"
  end
  name = name.downcase
  name = name.gsub(/[^a-z0-9_]/, '_')
  name
end

.normalize_table_name(name) ⇒ Object

Parameters:

  • name (String)


233
234
235
# File 'lib/td/client/api.rb', line 233

def self.normalize_table_name(name)
  normalize_database_name(name)
end

.normalized_msgpack(record, out = nil) ⇒ Object

Parameters:

  • record (Hash)
  • out (IO) (defaults to: nil)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/td/client/api.rb', line 129

def self.normalized_msgpack(record, out = nil)
  record.keys.each { |k|
    v = record[k]
    if v.kind_of?(Integer) && (v > MSGPACK_INT64_MAX || v < MSGPACK_INT64_MIN)
      record[k] = v.to_s
    end
  }
  if out
    out << record.to_msgpack
    out
  else
    record.to_msgpack
  end
end

.validate_column_name(name) ⇒ Object

Parameters:

  • name (String)


200
201
202
203
204
205
206
207
208
# File 'lib/td/client/api.rb', line 200

def self.validate_column_name(name)
  target = 'column'
  name = name.to_s
  if name.empty?
    raise ParameterValidationError,
          "Empty #{target} name is not allowed"
  end
  name
end

.validate_database_name(name) ⇒ Object

Parameters:

  • name (String)


185
186
187
# File 'lib/td/client/api.rb', line 185

def self.validate_database_name(name)
  validate_name("database", 3, 255, name)
end

.validate_name(target, min_len, max_len, name) ⇒ Object

Parameters:

  • target (String)
  • min_len (Fixnum)
  • max_len (Fixnum)
  • name (String)


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
# File 'lib/td/client/api.rb', line 148

def self.validate_name(target, min_len, max_len, name)
  if !target.instance_of?(String) || target.empty?
    raise ParameterValidationError,
          "A valid target name is required"
  end

  name = name.to_s
  if max_len
    if name.length < min_len || name.length > max_len
      raise ParameterValidationError,
        "#{target.capitalize} name must be between #{min_len} and #{max_len} characters long. Got #{name.length} " +
        (name.length == 1 ? "character" : "characters") + "."
    end
  else
    if min_len == 1
      if name.empty?
        raise ParameterValidationError,
          "Empty #{target} name is not allowed"
      end
    else
      if name.length < min_len
        raise ParameterValidationError,
          "#{target.capitalize} name must be longer than #{min_len} characters. Got #{name.length} " +
          (name.length == 1 ? "character" : "characters") + "."
      end
    end
  end

  unless name =~ /^([a-z0-9_]+)$/
    raise ParameterValidationError,
          "#{target.capitalize} name must only consist of lower-case alpha-numeric characters and '_'."
  end

  name
end

.validate_result_set_name(name) ⇒ Object

Parameters:

  • name (String)


195
196
197
# File 'lib/td/client/api.rb', line 195

def self.validate_result_set_name(name)
  validate_name("result set", 3, 255, name)
end

.validate_sql_alias_name(name) ⇒ Object

Parameters:

  • name (String)


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

def self.validate_sql_alias_name(name)
  validate_name("sql_alias", 1, nil, name)
end

.validate_table_name(name) ⇒ Object

Parameters:

  • name (String)


190
191
192
# File 'lib/td/client/api.rb', line 190

def self.validate_table_name(name)
  validate_name("table", 3, 255, name)
end

Instance Method Details

#ssl_ca_file=(ssl_ca_file) ⇒ Object

Parameters:

  • ssl_ca_file (String)


246
247
248
# File 'lib/td/client/api.rb', line 246

def ssl_ca_file=(ssl_ca_file)
  @ssl_ca_file = ssl_ca_file
end