Class: Fluent::RedshiftOutput
- Inherits:
-
BufferedOutput
- Object
- BufferedOutput
- Fluent::RedshiftOutput
show all
- Defined in:
- lib/fluent/plugin/out_redshift.rb
Defined Under Namespace
Classes: MaintenanceError, MaintenanceMonitor, RedshiftConnection, RedshiftError
Constant Summary
collapse
- NULL_CHAR_FOR_COPY =
"\\N"
- IGNORE_REDSHIFT_ERROR_REGEXP =
ignore load table error. (invalid data format)
/^ERROR: Load into table '[^']+' failed\./
Instance Method Summary
collapse
Constructor Details
Returns a new instance of RedshiftOutput.
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/fluent/plugin/out_redshift.rb', line 12
def initialize
super
require 'aws-sdk-v1'
require 'zlib'
require 'time'
require 'tempfile'
require 'pg'
require 'json'
require 'csv'
end
|
Instance Method Details
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/fluent/plugin/out_redshift.rb', line 95
def configure(conf)
super
if !check_credentials
raise ConfigError, "aws_key_id and aws_sec_key is required. or, use aws_iam_role instead."
end
@path = "#{@path}/" unless @path.end_with?('/')
@path = @path[1..-1] if @path.start_with?('/')
@utc = true if conf['utc']
@db_conf = {
host:@redshift_host,
port:@redshift_port,
dbname:@redshift_dbname,
user:@redshift_user,
password:@redshift_password,
connect_timeout: @redshift_connect_timeout
}
@delimiter = determine_delimiter(@file_type) if @delimiter.nil? or @delimiter.empty?
$log.debug format_log("redshift file_type:#{@file_type} delimiter:'#{@delimiter}'")
@table_name_with_schema = [@redshift_schemaname, @redshift_tablename].compact.join('.')
@redshift_copy_columns = if !@redshift_copy_columns.to_s.empty?
@redshift_copy_columns.split(/[,\s]+/)
else
nil
end
@copy_sql_template = build_redshift_copy_sql_template
@maintenance_monitor = MaintenanceMonitor.new(@maintenance_file_path)
@s3_server_side_encryption = @s3_server_side_encryption.to_sym if s3_server_side_encryption
end
|
140
141
142
143
144
145
146
147
148
|
# File 'lib/fluent/plugin/out_redshift.rb', line 140
def format(tag, time, record)
if json?
record.to_msgpack
elsif msgpack?
{ @record_log_tag => record }.to_msgpack
else
"#{record[@record_log_tag]}\n"
end
end
|
#start ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/fluent/plugin/out_redshift.rb', line 124
def start
super
options = {}
if @aws_key_id && @aws_sec_key
options = {
:access_key_id => @aws_key_id,
:secret_access_key => @aws_sec_key
}
end
options[:s3_endpoint] = @s3_endpoint if @s3_endpoint
@s3 = AWS::S3.new(options)
@bucket = @s3.buckets[@s3_bucket]
@redshift_connection = RedshiftConnection.new(@db_conf)
end
|
#write(chunk) ⇒ Object
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/fluent/plugin/out_redshift.rb', line 150
def write(chunk)
$log.debug format_log("start creating gz.")
@maintenance_monitor.check_maintenance!
tmp = Tempfile.new("s3-")
tmp =
if json? || msgpack?
create_gz_file_from_structured_data(tmp, chunk, @delimiter)
else
create_gz_file_from_flat_data(tmp, chunk)
end
unless tmp
$log.debug format_log("received no valid data. ")
return false
end
s3path = create_s3path(@bucket, @path)
@bucket.objects[s3path].write(Pathname.new(tmp.path),
:acl => :bucket_owner_full_control,
:server_side_encryption => @s3_server_side_encryption)
tmp.close!
s3_uri = "s3://#{@s3_bucket}/#{s3path}"
sql = @copy_sql_template % s3_uri
$log.debug format_log("start copying. s3_uri=#{s3_uri}")
begin
@redshift_connection.exec(sql)
$log.info format_log("completed copying to redshift. s3_uri=#{s3_uri}")
rescue RedshiftError => e
if e.to_s =~ IGNORE_REDSHIFT_ERROR_REGEXP
$log.error format_log("failed to copy data into redshift due to load error. s3_uri=#{s3_uri}"), :error=>e.to_s
return false
end
raise e
end
true
end
|