fluent-plugin-mysql-prepared-statement 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 37ea1313b1c63e4709f63a66b64872c119fbd0c0
4
+ data.tar.gz: c47c9aca3bb7cfff68215769acb2ae4a8e82117a
5
+ SHA512:
6
+ metadata.gz: 0e8e7846a2f4b3d3e798a174c99b7dbca48baf30199af91442af1ef2a109b6ce25699f061cc2825835b4c54f91a10ae95b5352faa35510cda15cff0ab95dffa5
7
+ data.tar.gz: 98d1d3520c5a4ac8241df342e6de8c925baa8307d52405a6aec07547e561aa37e91c2688a5cd35f45f30ba032e2ae2000063b72acf3eabd29fd1b1a0e573b8c3
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test.conf
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0.0
5
+ gemfile:
6
+ - Gemfile
7
+ script: "rake test"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Hiroshi Toyama
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ [![Build Status](https://secure.travis-ci.org/toyama0919/fluent-plugin-mysql-prepared-statement.png?branch=master)](http://travis-ci.org/toyama0919/fluent-plugin-mysql-prepared-statement)
2
+
3
+ # fluent-plugin-mysql-prepared-statement
4
+
5
+ fluent plugin mysql prepared statement query
6
+
7
+ ## Installation
8
+
9
+ ### td-agent(Linux)
10
+
11
+ /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-mysql-prepared-statement
12
+
13
+ ### td-agent(Mac)
14
+
15
+ sudo /usr/local/Cellar/td-agent/1.1.XX/bin/fluent-gem install fluent-plugin-mysql-prepared-statement
16
+
17
+ ### fluentd only
18
+
19
+ gem install fluent-plugin-mysql-prepared-statement
20
+
21
+
22
+ ## Parameters
23
+
24
+ param|value
25
+ --------|------
26
+ output_tag|output tag(require)
27
+ host|database host(default: 127.0.0.1)
28
+ port|database port(default: 3306)
29
+ database|database name(require)
30
+ username|user(require)
31
+ password|password(default: blank)
32
+ key_names|prepared statement value(require)
33
+ sql|running sql , prepared statement query(require)
34
+
35
+ ## Configuration Example
36
+
37
+ ```
38
+ <match mysql.input>
39
+ type mysql_prepared_statement
40
+ output_tag mysql.output
41
+ host localhost
42
+ database test_app_development
43
+ username root
44
+ password hogehoge
45
+ key_names id,user_name
46
+ sql select * from users where id = ? and user_name = ?
47
+ flush_interval 10s
48
+ </match>
49
+ ```
50
+
51
+ Assume following input is coming:
52
+
53
+ ```js
54
+ mysql.input {"id":"1", "user_name":"toyama"}
55
+ ```
56
+
57
+ then output becomes as below (indented):
58
+
59
+ ```js
60
+ mysql.output {"id":122,"user_name":"toyama","created_at":"2014-01-01 19:10:27 +0900","updated_at":"2014-01-01 19:10:27 +0900"}
61
+ ```
62
+
63
+ running query =>[select * from users where id = 1 and user_name = 'toyama']
64
+
65
+
66
+ ## spec
67
+
68
+ ```
69
+ rake test
70
+ ```
71
+
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new [Pull Request](../../pull/new/master)
80
+
81
+ ## Copyright
82
+
83
+ Copyright (c) 2013 Hiroshi Toyama. See [LICENSE](LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |gem|
3
+ gem.name = "fluent-plugin-mysql-prepared-statement"
4
+ gem.version = "0.0.1"
5
+ gem.authors = ["Toyama Hiroshi"]
6
+ gem.email = ["toyama0919@gmail.com"]
7
+ gem.description = %q{fluent plugin mysql prepared statement}
8
+ gem.summary = %q{fluent plugin mysql prepared statement}
9
+ gem.homepage = "https://github.com/toyama0919/fluent-plugin-mysql-prepared-statement"
10
+ gem.licenses = ["MIT"]
11
+ gem.has_rdoc = false
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+
18
+ gem.add_runtime_dependency "fluentd"
19
+ gem.add_runtime_dependency "mysql2-cs-bind"
20
+ gem.add_development_dependency "bundler", "~> 1.3"
21
+ gem.add_development_dependency "rake"
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "spork"
24
+ gem.add_development_dependency "pry"
25
+
26
+ end
@@ -0,0 +1,90 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Fluent
3
+ class Fluent::MysqlPreparedStatementOutput < Fluent::BufferedOutput
4
+ Fluent::Plugin.register_output('mysql_prepared_statement', self)
5
+
6
+ config_param :host, :string, :default => "127.0.0.1"
7
+ config_param :output_tag, :string
8
+ config_param :port, :integer, :default => 3306
9
+ config_param :database, :string
10
+ config_param :username, :string
11
+ config_param :password, :string, :default => ''
12
+
13
+ config_param :key_names, :string, :default => nil
14
+ config_param :sql, :string, :default => nil
15
+
16
+ attr_accessor :handler
17
+
18
+ def initialize
19
+ super
20
+ require 'mysql2-cs-bind'
21
+ end
22
+
23
+ def configure(conf)
24
+ super
25
+
26
+ if @sql.nil?
27
+ raise Fluent::ConfigError, "sql MUST be specified, but missing"
28
+ end
29
+
30
+ if @key_names.nil?
31
+ raise Fluent::ConfigError, "key_names MUST be specified, but missing"
32
+ end
33
+
34
+ @key_names = @key_names.split(',')
35
+ @format_proc = Proc.new{|tag, time, record| @key_names.map{|k| record[k]}}
36
+
37
+ begin
38
+ Mysql2::Client.pseudo_bind(@sql, @key_names.map{|n| nil})
39
+ rescue ArgumentError => e
40
+ raise Fluent::ConfigError, "mismatch between sql placeholders and key_names"
41
+ end
42
+
43
+ $log.info "sql ->[#{@sql}]"
44
+ end
45
+
46
+ def start
47
+ @handler = client
48
+ super
49
+ end
50
+
51
+ def shutdown
52
+ super
53
+ end
54
+
55
+ def format(tag, time, record)
56
+ [tag, time, @format_proc.call(tag, time, record)].to_msgpack
57
+ end
58
+
59
+ def client
60
+ Mysql2::Client.new({
61
+ :host => @host,
62
+ :port => @port,
63
+ :username => @username,
64
+ :password => @password,
65
+ :database => @database,
66
+ :flags => Mysql2::Client::MULTI_STATEMENTS
67
+ })
68
+ end
69
+
70
+ def write(chunk)
71
+ $log.info "adding mysql_query job: "
72
+ chunk.msgpack_each { |tag, time, data|
73
+ results = get_exec_result(data)
74
+ results.each{|result|
75
+ Fluent::Engine.emit(@output_tag, Fluent::Engine.now, result)
76
+ }
77
+ }
78
+ end
79
+
80
+ def get_exec_result(data)
81
+ results = Array.new
82
+ stmt = @handler.xquery(@sql, data)
83
+ return results if stmt.nil?
84
+ stmt.each do |row|
85
+ results.push(row)
86
+ end
87
+ return results
88
+ end
89
+ end
90
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
14
+ require 'fluent/test'
15
+ unless ENV.has_key?('VERBOSE')
16
+ nulllogger = Object.new
17
+ nulllogger.instance_eval {|obj|
18
+ def method_missing(method, *args)
19
+ # pass
20
+ end
21
+ }
22
+ $log = nulllogger
23
+ end
24
+
25
+ require 'fluent/plugin/out_mysql_prepared_statement'
26
+
27
+ class Test::Unit::TestCase
28
+ end
@@ -0,0 +1,107 @@
1
+ require 'helper'
2
+ require 'mysql2-cs-bind'
3
+
4
+ class MysqlPreparedStatementOutputTest < Test::Unit::TestCase
5
+ def setup
6
+ Fluent::Test.setup
7
+ end
8
+
9
+ def create_driver(conf = CONFIG, tag='test')
10
+ Fluent::Test::BufferedOutputTestDriver.new(Fluent::MysqlPreparedStatementOutput, tag).configure(conf)
11
+ end
12
+
13
+ def test_configure_error
14
+
15
+ assert_raise(Fluent::ConfigError) {
16
+ d = create_driver %[
17
+ output_tag mysql.placeholder
18
+ host localhost
19
+ username root
20
+ password hogehoge
21
+ key_names id,user_name
22
+ sql select * from users where id = ? and user_name = ?
23
+ flush_interval 10s
24
+ ]
25
+ }
26
+
27
+ # not username
28
+ assert_raise(Fluent::ConfigError) {
29
+ d = create_driver %[
30
+ output_tag mysql.placeholder
31
+ host localhost
32
+ password hogehoge
33
+ key_names id,user_name
34
+ sql select * from users where id = ? and user_name = ?
35
+ flush_interval 10s
36
+ ]
37
+ }
38
+ end
39
+
40
+ def test_configure
41
+ # not define format(default csv)
42
+ assert_nothing_raised(Fluent::ConfigError) {
43
+ d = create_driver %[
44
+ output_tag mysql.placeholder
45
+ host localhost
46
+ database test_app_development
47
+ username root
48
+ password hogehoge
49
+ key_names id,user_name
50
+ sql select * from users where id = ? and user_name = ?
51
+ flush_interval 10s
52
+ ]
53
+ }
54
+
55
+ assert_nothing_raised(Fluent::ConfigError) {
56
+ d = create_driver %[
57
+ output_tag mysql.placeholder
58
+ host localhost
59
+ database test_app_development
60
+ username root
61
+ password hogehoge
62
+ key_names id,user_name
63
+ sql select * from users where id = ? and user_name = ?
64
+ flush_interval 10s
65
+ ]
66
+ }
67
+
68
+ assert_nothing_raised(Fluent::ConfigError) {
69
+ d = create_driver %[
70
+ output_tag mysql.placeholder
71
+ database test_app_development
72
+ username root
73
+ key_names id,user_name
74
+ sql select * from users where id = ? and user_name = ?
75
+ ]
76
+ }
77
+ end
78
+
79
+ def test_query_error
80
+ assert_raise(Fluent::ConfigError) {
81
+ d = create_driver %[
82
+ output_tag mysql.placeholder
83
+ host localhost
84
+ database rails_development
85
+ username root
86
+ password hogehoge
87
+ key_names id
88
+ sql select * from users where id = ? and user_name = ?
89
+ flush_interval 10s
90
+ ]
91
+ }
92
+
93
+ assert_raise(Fluent::ConfigError) {
94
+ d = create_driver %[
95
+ output_tag mysql.placeholder
96
+ host localhost
97
+ database rails_development
98
+ username root
99
+ password hogehoge
100
+ key_names id,user_name
101
+ sql select * from users where id = ?
102
+ flush_interval 10s
103
+ ]
104
+ }
105
+ end
106
+
107
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mysql-prepared-statement
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Toyama Hiroshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fluentd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2-cs-bind
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: spork
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: fluent plugin mysql prepared statement
112
+ email:
113
+ - toyama0919@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - fluent-plugin-mysql-prepared-statement.gemspec
125
+ - lib/fluent/plugin/out_mysql_prepared_statement.rb
126
+ - test/helper.rb
127
+ - test/plugin/test_out_mysql_prepared_statement.rb
128
+ homepage: https://github.com/toyama0919/fluent-plugin-mysql-prepared-statement
129
+ licenses:
130
+ - MIT
131
+ metadata: {}
132
+ post_install_message:
133
+ rdoc_options: []
134
+ require_paths:
135
+ - lib
136
+ required_ruby_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 2.2.0
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: fluent plugin mysql prepared statement
152
+ test_files:
153
+ - test/helper.rb
154
+ - test/plugin/test_out_mysql_prepared_statement.rb