Module: TreasureData::API::Job

Included in:
TreasureData::API
Defined in:
lib/td/client/api/job.rb

Defined Under Namespace

Classes: NullInflate

Instance Method Summary collapse

Instance Method Details

#hive_query(q, db = nil, result_url = nil, priority = nil, retry_limit = nil, opts = {}) ⇒ String



254
255
256
# File 'lib/td/client/api/job.rb', line 254

def hive_query(q, db=nil, result_url=nil, priority=nil, retry_limit=nil, opts={})
  query(q, :hive, db, result_url, priority, retry_limit, opts)
end

#job_result(job_id) ⇒ Array



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/td/client/api/job.rb', line 117

def job_result(job_id)
  code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>'msgpack'})
  if code != "200"
    raise_error("Get job result failed", res)
  end
  result = []
  MessagePack::Unpacker.new.feed_each(body) {|row|
    result << row
  }
  return result
end

#job_result_each(job_id, &block) ⇒ nil

block is optional and must accept 1 argument



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/td/client/api/job.rb', line 164

def job_result_each(job_id, &block)
  upkr = MessagePack::Unpacker.new
  infl = nil

  get("/v3/job/result/#{e job_id}", {'format'=>'msgpack'}) {|res, chunk, current_total_chunk_size|
    if res.code != 200
      raise_error("Get job result failed", res)
    end

    # default to decompressing the response since format is fixed to 'msgpack'
    infl ||= create_inflate(res)

    inflated_fragment = infl.inflate(chunk)
    upkr.feed_each(inflated_fragment, &block)
  }
  nil
ensure
  infl.close if infl
end

#job_result_each_with_compr_size(job_id, &block) ⇒ nil

block is optional and must accept 1 argument



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/td/client/api/job.rb', line 189

def job_result_each_with_compr_size(job_id, &block)
  upkr = MessagePack::Unpacker.new
  infl = nil

  get("/v3/job/result/#{e job_id}", {'format'=>'msgpack'}) {|res, chunk, current_total_chunk_size|
    if res.code != 200
      raise_error("Get job result failed", res)
    end

    # default to decompressing the response since format is fixed to 'msgpack'
    infl ||= create_inflate(res)

    inflated_fragment = infl.inflate(chunk)
    upkr.feed_each(inflated_fragment) {|unpacked|
      block.call(unpacked, current_total_chunk_size) if block_given?
    }
  }
  nil
ensure
  infl.close if infl
end

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

block is optional and must accept 1 parameter



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/td/client/api/job.rb', line 136

def job_result_format(job_id, format, io=nil, &block)
  if io
    infl = nil
    code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>format}) {|res, chunk, current_total_chunk_size|
      if res.code != 200
        raise_error("Get job result failed", res)
      end

      infl ||= create_inflalte_or_null_inflate(res)

      io.write infl.inflate(chunk)
      block.call(current_total_chunk_size) if block_given?
    }
    nil
  else
    code, body, res = get("/v3/job/result/#{e job_id}", {'format'=>format})
    if code != "200"
      raise_error("Get job result failed", res)
    end
    body
  end
end

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/td/client/api/job.rb', line 214

def job_result_raw(job_id, format, io = nil, &block)
  body = nil

  get("/v3/job/result/#{e job_id}", {'format'=>format}) {|res, chunk, current_total_chunk_size|
    if res.code != 200
      raise_error("Get job result failed", res)
    end

    if io
      io.write(chunk)
      block.call(current_total_chunk_size) if block_given?
    else
      if body
        body += chunk
      else
        body = chunk
      end
    end
  }
  body
end

#job_status(job_id) ⇒ String



105
106
107
108
109
110
111
112
113
# File 'lib/td/client/api/job.rb', line 105

def job_status(job_id)
  code, body, res = get("/v3/job/status/#{e job_id}")
  if code != "200"
    raise_error("Get job status failed", res)
  end

  js = checked_json(body, %w[status])
  return js['status']
end

#kill(job_id) ⇒ String



238
239
240
241
242
243
244
245
246
# File 'lib/td/client/api/job.rb', line 238

def kill(job_id)
  code, body, res = post("/v3/job/kill/#{e job_id}")
  if code != "200"
    raise_error("Kill job failed", res)
  end
  js = checked_json(body, %w[])
  former_status = js['former_status']
  return former_status
end

#list_jobs(from = 0, to = nil, status = nil, conditions = nil) ⇒ Array



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/td/client/api/job.rb', line 13

def list_jobs(from=0, to=nil, status=nil, conditions=nil)
  params = {}
  params['from'] = from.to_s if from
  params['to'] = to.to_s if to
  params['status'] = status.to_s if status
  params.merge!(conditions) if conditions
  code, body, res = get("/v3/job/list", params)
  if code != "200"
    raise_error("List jobs failed", res)
  end
  js = checked_json(body, %w[jobs])
  result = []
  js['jobs'].each {|m|
    job_id = m['job_id']
    type = (m['type'] || '?').to_sym
    database = m['database']
    status = m['status']
    query = m['query']
    start_at = m['start_at']
    end_at = m['end_at']
    cpu_time = m['cpu_time']
    result_size = m['result_size'] # compressed result size in msgpack.gz format
    result_url = m['result']
    priority = m['priority']
    retry_limit = m['retry_limit']
    duration = m['duration']
    result << [job_id, type, status, query, start_at, end_at, cpu_time,
               result_size, result_url, priority, retry_limit, nil, database,
               duration]
  }
  return result
end

#pig_query(q, db = nil, result_url = nil, priority = nil, retry_limit = nil, opts = {}) ⇒ String



264
265
266
# File 'lib/td/client/api/job.rb', line 264

def pig_query(q, db=nil, result_url=nil, priority=nil, retry_limit=nil, opts={})
  query(q, :pig, db, result_url, priority, retry_limit, opts)
end

#query(q, type = :hive, db = nil, result_url = nil, priority = nil, retry_limit = nil, opts = {}) ⇒ String



275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/td/client/api/job.rb', line 275

def query(q, type=:hive, db=nil, result_url=nil, priority=nil, retry_limit=nil, opts={})
  params = {'query' => q}.merge(opts)
  params['result'] = result_url if result_url
  params['priority'] = priority if priority
  params['retry_limit'] = retry_limit if retry_limit
  code, body, res = post("/v3/job/issue/#{type}/#{e db}", params)
  if code != "200"
    raise_error("Query failed", res)
  end
  js = checked_json(body, %w[job_id])
  return js['job_id'].to_s
end

#show_job(job_id) ⇒ Array



48
49
50
51
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
# File 'lib/td/client/api/job.rb', line 48

def show_job(job_id)
  # use v3/job/status instead of v3/job/show to poll finish of a job
  code, body, res = get("/v3/job/show/#{e job_id}")
  if code != "200"
    raise_error("Show job failed", res)
  end
  js = checked_json(body, %w[status])
  # TODO debug
  type = (js['type'] || '?').to_sym  # TODO
  database = js['database']
  query = js['query']
  status = js['status']
  debug = js['debug']
  url = js['url']
  start_at = js['start_at']
  end_at = js['end_at']
  cpu_time = js['cpu_time']
  result_size = js['result_size'] # compressed result size in msgpack.gz format
  result = js['result'] # result target URL
  hive_result_schema = (js['hive_result_schema'] || '')
  if hive_result_schema.empty?
    hive_result_schema = nil
  else
    begin
      hive_result_schema = JSON.parse(hive_result_schema)
    rescue JSON::ParserError => e
      # this is a workaround for a Known Limitation in the Pig Engine which does not set a default, auto-generated
      #   column name for anonymous columns (such as the ones that are generated from UDF like COUNT or SUM).
      # The schema will contain 'nil' for the name of those columns and that breaks the JSON parser since it violates
      #   the JSON syntax standard.
      if type == :pig and hive_result_schema !~ /[\{\}]/
        begin
          # NOTE: this works because a JSON 2 dimensional array is the same as a Ruby one.
          #   Any change in the format for the hive_result_schema output may cause a syntax error, in which case
          #   this lame attempt at fixing the problem will fail and we will be raising the original JSON exception
          hive_result_schema = eval(hive_result_schema)
        rescue SyntaxError => ignored_e
          raise e
        end
        hive_result_schema.each_with_index {|col_schema, idx|
          if col_schema[0].nil?
            col_schema[0] = "_col#{idx}"
          end
        }
      else
        raise e
      end
    end
  end
  priority = js['priority']
  retry_limit = js['retry_limit']
  return [type, query, status, url, debug, start_at, end_at, cpu_time,
          result_size, result, hive_result_schema, priority, retry_limit, nil, database]
end