fluent-plugin-vertica 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +19 -0
  2. data/README.md +21 -0
  3. data/lib/fluent/plugin/out_vertica.rb +136 -0
  4. metadata +80 -0
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Erik Selin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ Fluentd output plugin for Vertica
2
+ =================================
3
+
4
+ Simple batched output plugin for getting events into vertica.
5
+
6
+ Example config
7
+ ==============
8
+
9
+ <match vertica.public.test>
10
+ type vertica
11
+
12
+ database mydb
13
+ schema public
14
+ table test
15
+
16
+ username dbadmin
17
+ password mypass
18
+
19
+ host 127.0.0.1
20
+ port 5433
21
+ </match>
@@ -0,0 +1,136 @@
1
+ module Fluent
2
+ class VerticaOutput < Fluent::BufferedOutput
3
+ Fluent::Plugin.register_output('vertica', self)
4
+
5
+ config_param :host, :string, :default => '127.0.0.1'
6
+ config_param :port, :integer, :default => 5433
7
+ config_param :username, :string, :default => 'dbadmin'
8
+ config_param :password, :string, :default => nil
9
+ config_param :database, :string, :default => nil
10
+ config_param :schema, :string, :default => nil
11
+ config_param :table, :string, :default => nil
12
+ config_param :ssl, :bool, :default => false
13
+
14
+ def initialize
15
+ super
16
+
17
+ require 'vertica'
18
+ require 'csv'
19
+ end
20
+
21
+ def format(tag, time, record)
22
+ values = columns.map { |col| record[col] }
23
+ CSV.generate_line(values, { :col_sep => "\t" })
24
+ end
25
+
26
+ def write(chunk)
27
+ chunk.open do |file|
28
+
29
+ reset
30
+
31
+ temp_table = "temp_#{@table}"
32
+ perm_table = "#{@schema}.#{@table}"
33
+
34
+ vertica.query(<<-SQL)
35
+ CREATE LOCAL TEMPORARY TABLE #{temp_table}
36
+ ON COMMIT DELETE ROWS
37
+ AS SELECT * FROM #{perm_table} LIMIT 0
38
+ SQL
39
+
40
+ vertica.copy(<<-SQL) { |handle| handle.write(file.read) }
41
+ COPY #{temp_table} (#{columns.join(",")})
42
+ FROM STDIN DELIMITER E'\t'
43
+ RECORD TERMINATOR E'\n' NULL AS '__NULL__'
44
+ ENFORCELENGTH
45
+ NO COMMIT
46
+ SQL
47
+
48
+ if primary_keys.empty?
49
+ vertica.query(<<-SQL)
50
+ INSERT INTO #{perm_table}
51
+ (#{columns.join(",")})
52
+ SELECT
53
+ #{columns.join(",")}
54
+ FROM #{temp_table}
55
+ SQL
56
+ else
57
+ condition = primary_keys.map do |key|
58
+ "#{perm_table}.#{key} = #{temp_table}.#{key}"
59
+ end
60
+
61
+ unless empty_table?(perm_table)
62
+ vertica.query(<<-SQL)
63
+ DELETE FROM #{perm_table}
64
+ WHERE EXISTS (
65
+ SELECT 1
66
+ FROM #{temp_table}
67
+ WHERE #{condition.join(" AND ")}
68
+ )
69
+ SQL
70
+ end
71
+
72
+ vertica.query(<<-SQL)
73
+ INSERT INTO #{perm_table}
74
+ (#{columns.join(",")})
75
+ SELECT
76
+ #{columns.join(",")}
77
+ FROM (
78
+ SELECT
79
+ #{columns.join(",")},
80
+ row_number() OVER (partition by #{primary_keys.join(",")}) AS r
81
+ FROM #{temp_table}
82
+ ) AS temp
83
+ WHERE r = 1
84
+ SQL
85
+ end
86
+
87
+ vertica.query("COMMIT")
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def reset
94
+ @vertica = nil
95
+ @columns = nil
96
+ @primary_keys = nil
97
+ @empty = nil
98
+ end
99
+
100
+ def vertica
101
+ @vertica ||= Vertica.connect({
102
+ :host => @host,
103
+ :user => @username,
104
+ :password => @password,
105
+ :ssl => @ssl,
106
+ :port => @port,
107
+ :database => @database
108
+ })
109
+ end
110
+
111
+ def columns
112
+ @columns ||= vertica.query(<<-SQL).map { |column| column[:column_name] }
113
+ SELECT column_name
114
+ FROM columns
115
+ WHERE table_schema ='#{@schema}'
116
+ AND table_name='#{@table}'
117
+ ORDER BY ordinal_position
118
+ SQL
119
+ end
120
+
121
+ def primary_keys
122
+ @primary_keys ||= vertica.query(<<-SQL).map { |column| column[:column_name] }
123
+ SELECT column_name
124
+ FROM v_catalog.primary_keys
125
+ WHERE table_schema = '#{@schema}'
126
+ AND table_name = '#{@table}'
127
+ SQL
128
+ end
129
+
130
+ def empty_table?(table)
131
+ @empty ||= vertica.query(<<-SQL).first[:count] == 0
132
+ SELECT count(1) FROM #{table}
133
+ SQL
134
+ end
135
+ end
136
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-vertica
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erik Selin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluentd
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.10.35
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.10.35
30
+ - !ruby/object:Gem::Dependency
31
+ name: vertica
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description:
47
+ email:
48
+ - erik.selin@ifelsestudio.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - LICENSE
54
+ - README.md
55
+ - lib/fluent/plugin/out_vertica.rb
56
+ homepage: http://tyro89.github.com/fluent-plugin-vertica
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.9.1
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.23
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Fluentd output plugin for Vertica.
80
+ test_files: []