Class: Embulk::Output::Bigobject

Inherits:
OutputPlugin
  • Object
show all
Defined in:
lib/embulk/output/bigobject.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, schema, index) ⇒ Bigobject

def self.resume(task, schema, count, &control)

task_reports = yield(task)

next_config_diff = {}
return next_config_diff

end



88
89
90
91
92
93
94
95
# File 'lib/embulk/output/bigobject.rb', line 88

def initialize(task, schema, index)
    super

  # Embulk.logger.debug { "Initialize #{index}" }
  # initialization code:
  @table = task["table"]
  @counter = 0 
end

Class Method Details

.column_options_map(column_options) ⇒ Object



27
28
29
30
31
# File 'lib/embulk/output/bigobject.rb', line 27

def self.column_options_map(column_options) 
    (column_options || {}).map do |column_option| 
 [column_option['name'], column_option] 
    end.to_h 
end

.configure(config, schema, count) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/embulk/output/bigobject.rb', line 13

def self.configure(config, schema, count)
     # configuration code:
     task = {
       "host"           => config.param("host",           :string,  :default => "localhost"), # string, optional
       "restport"       => config.param("restport",       :integer, :default => 9090),  # integer, optional
       "ncport"         => config.param("ncport",         :integer, :default => 9091),  # integer, optional
       "table"          => config.param("table",          :string),        # string, required
 "column_options" => config.param("column_options", :array,   :default => []),   
       "payload_column_index" => config.param("payload_column_index", :integer,  :default => nil),   
     }
     
    task
end

.create_botable_stmt(tbl, schema, cos) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/embulk/output/bigobject.rb', line 171

def self.create_botable_stmt(tbl,schema, cos)
    val_array = Array.new
    schema.each do |c|
      co = cos[c.index] || {}
      Embulk.logger.debug {"#{c.index}, #{c.name}, #{co}"}
      val_array.push "#{co["name"] || c.name} #{to_bigobject_column_type(c.type.to_s, c.format.to_s, co)}" 
    end
      bo_table_schema = val_array.join(',')
    Embulk.logger.debug {"schema (#{schema.class}): #{schema}"}
    Embulk.logger.debug {"schema: #{bo_table_schema}"}
    keys = Array.new
    cos.each do |co|
      keys.push co["name"] if co["is_key"] 
    end
    if keys.length == 0
    "CREATE TABLE #{tbl} (#{bo_table_schema})"
    else
    "CREATE TABLE #{tbl} (#{bo_table_schema} KEY(#{keys.join(',')}))"
    end
end

.rest_exec(uri, stmt) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/embulk/output/bigobject.rb', line 153

def self.rest_exec(uri, stmt)
  begin
    response = RestClient.post uri, { "Stmt" => "#{stmt}" }.to_json, :content_type => :json, :accept => :json, :timeout => 2
    JSON.parse(response.body)
  rescue RestClient::Exception => e
    #Embulk.logger.error { "RestClient: #{e.http_code}, #{e.message}, response: #{e.response}" }
    Embulk.logger.warn { "Timeout: statement: #{stmt}" }
    begin
      response = RestClient.post uri, { "Stmt" => "#{stmt}" }.to_json, :content_type => :json, :accept => :json, :timeout => 4
      JSON.parse(response.body)
    rescue RestClient::Exception => e2
      Embulk.logger.error { "RestClient: #{e2.http_code}, #{e2.message}, response: #{e2.response}" }
    end
      rescue JSON::Exception => e
    Embulk.logger.error { "JSON: #{e.message}" }
  end
end

.to_bigobject_column_type(type, format, co) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/embulk/output/bigobject.rb', line 192

def self.to_bigobject_column_type(type, format, co)
    co = co || {}
    Embulk.logger.debug {"type: #{type}, format #{format}, option #{co}"}
    return co["type"] if co["type"]

  case type
  when 'long'
    botype = :INT64
  when 'boolean'
    botype = :INT8
  when 'string'
    botype = :STRING
  when 'double'
    botype = :DOUBLE
  when 'timestamp'
    if format.include? "%H" 
      botype = :DATETIME32
    else
      botype = :DATE32
    end
  end
  botype
end

.transaction(config, schema, count, &control) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/embulk/output/bigobject.rb', line 33

def self.transaction(config, schema, count, &control)
    task = self.configure(config, schema, count)
    task['co_map'] = self.column_options_map task["column_options"]
  task['rest_uri'] = "http://#{task['host']}:#{task['restport']}/cmd".freeze
    task['ttl_counter'] = 0
  
  Embulk.logger.debug { "Transaction #{count}" }
  # resumable output:
  # resume(task, schema, count, &control)

  # Create-Table if it does not exist
  response = rest_exec(task['rest_uri'], "desc #{task['table']}") # check table
  if response["Status"] == 0 then # the table exists
    Embulk.logger.debug { "#{response}" }
  elsif response["Status"] == -11 then # the table does not exist
    response = rest_exec(task['rest_uri'], "#{create_botable_stmt("#{task['table']}",schema, task["column_options"])}")
    if response["Status"] != 0 then 
      Embulk.logger.error { "#{response}" }
      raise "Create table #{task['table']} in BigObject Failed"
    end
    Embulk.logger.info { "embulk-output-bigobject: Create table #{task['table']}" }
  else # should not be here
    Embulk.logger.error { "#{response}" }
    raise "Please check table #{task['table']} in BigObject First"  
  end

  # non-resumable output:
  task_reports = yield(task)
  next_config_diff = {}
  return next_config_diff
end

Instance Method Details

#abortObject



142
143
144
# File 'lib/embulk/output/bigobject.rb', line 142

def abort
  raise "Please Check BigObject"
end

#add(page) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/embulk/output/bigobject.rb', line 131

def add(page)
 if (@task['payload_column_index'])
  add_payload(page)
 else
  add_csv(page)
 end
end

#add_csv(page) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/embulk/output/bigobject.rb', line 100

def add_csv(page)
  data = Array.new
  
  page.each do |record|
    values = []
    record.each do |row| values << "\"#{row.to_s.gsub(/\"/,"\"\"")}\"" end
      data.push "#{values.join(",")}\n"
  end

    safe_io_write "#{data.join}"

  @counter += data.length
  @task['ttl_counter'] += data.length

end

#add_payload(page) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/embulk/output/bigobject.rb', line 116

def add_payload(page)
  data = Array.new
    pindex = @task['payload_column_index']

  page.each do |record|
      data.push "#{record[pindex]}\n"
  end

    safe_io_write "#{data.join}"

  @counter += data.length
  @task['ttl_counter'] += data.length

end

#closeObject



97
98
# File 'lib/embulk/output/bigobject.rb', line 97

def close
end

#commitObject



146
147
148
149
150
151
# File 'lib/embulk/output/bigobject.rb', line 146

def commit
  task_report = {
    "records" => @counter
  }
  return task_report
end

#create_shared_ioObject



73
74
75
76
77
78
79
# File 'lib/embulk/output/bigobject.rb', line 73

def create_shared_io
    io = TCPSocket.new @task['host'], @task['ncport']
    #io = File.new "out.dump", "w"
    io.write "csv\x01"
    io.puts @task['table']
    io
end

#finishObject



139
140
# File 'lib/embulk/output/bigobject.rb', line 139

def finish
end

#safe_io_write(buff) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/embulk/output/bigobject.rb', line 65

def safe_io_write(buff)
    @@io ||= create_shared_io
    @@mutext ||= Mutex.new
     @@mutext.synchronize do 
 @@io.write buff
    end
end