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



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

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



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

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
26
# 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
 "rowcolumn"	   => config.param("rowcolumn",      :string,  :default => "ROW"), # string, optional
 "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, rowcol, schema, cos) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/embulk/output/bigobject.rb', line 158

def self.create_botable_stmt(tbl,rowcol,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} #{rowcol} (#{bo_table_schema})"
		else
    "CREATE TABLE #{tbl} #{rowcol} (#{bo_table_schema} KEY(#{keys.join(',')}))"
		end
end

.rest_exec(uri, stmt) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/embulk/output/bigobject.rb', line 140

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



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/embulk/output/bigobject.rb', line 179

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



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
64
# File 'lib/embulk/output/bigobject.rb', line 34

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']}", "#{task['rowcolumn']}", 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



129
130
131
# File 'lib/embulk/output/bigobject.rb', line 129

def abort
  raise "Please Check BigObject"
end

#add(page) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/embulk/output/bigobject.rb', line 101

def add(page)
  data = Array.new
		pindex = @task['payload_column_index']
		Embulk.logger.debug "#{pindex}"

		if pindex
		  page.each do |record|
data.push "#{record[pindex]}\n"
			Embulk.logger.debug "#{record[pindex]}\n"
		  end
		else
    page.each do |record|
      values = []
      record.each do |row| values << "\"#{row.to_s.gsub(/\"/,"\"\"")}\"" end
data.push "#{values.join(",")}\n"
    end
		end

		safe_io_write "#{data.join}"

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

end

#closeObject



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

def close
end

#commitObject



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

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

#create_shared_ioObject



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

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



126
127
# File 'lib/embulk/output/bigobject.rb', line 126

def finish
end

#safe_io_write(buff) ⇒ Object



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

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