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



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

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
# 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 => []),   
     }
     
		task
end

.create_botable_stmt(tbl, schema, cos) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/embulk/output/bigobject.rb', line 148

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/embulk/output/bigobject.rb', line 130

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



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

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



32
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 32

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['co_map'])}")
    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



119
120
121
# File 'lib/embulk/output/bigobject.rb', line 119

def abort
  raise "Please Check BigObject"
end

#add(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(page)
  data = Array.new
  
  page.each do |records|
    values = []
    records.each do |row| values << "\"#{row.to_s.gsub(/\"/,"\"\"")}\"" end
		  data.push "#{values.join(",")}"
  end

		safe_io_puts "#{data.join("\n")}"

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

end

#closeObject



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

def close
end

#commitObject



123
124
125
126
127
128
# File 'lib/embulk/output/bigobject.rb', line 123

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



116
117
# File 'lib/embulk/output/bigobject.rb', line 116

def finish
end

#safe_io_puts(buff) ⇒ Object



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

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