fluent-plugin-mysql-appender 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 835274ff289ba1176e9aaccf84278be8572bbd0a
4
+ data.tar.gz: 2b1eeebdff20fc94cd2a4166f70f7abe7df539bf
5
+ SHA512:
6
+ metadata.gz: 0d9e9db4e075088608f536667cc5353cb69e7a3378cac072d6a48392170c34ee7b68bd9b772b2f0f68d452c6728db844c520857cbf35f23568bc15094b20376c
7
+ data.tar.gz: 8f2e3eb258ae7e88eebb4cbdc90d5b0ab3cd26c9131f2ae9c4863c965f45c577c4a0441789505fa5b0d8d7885a700924ca49d9895cbd6e5da28610d9f935e94d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent-plugin-mysql-appender.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 tsuyoshi_terasaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Fluent::Plugin::Mysql::Appender
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fluent/plugin/mysql/appender`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fluent-plugin-mysql-appender'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fluent-plugin-mysql-appender
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fluent-plugin-mysql-appender.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "fluent-plugin-mysql-appender"
4
+ spec.version = "0.0.1"
5
+ spec.authors = ["tsuyoshi_terasaki"]
6
+ spec.email = ["tsuyoshi_terasaki@realworld.jp"]
7
+
8
+ spec.summary = %q{Fluentd input plugin to track insert event from MySQL database server.}
9
+ spec.description = %q{Simple incremental id's insert.}
10
+ spec.homepage = "https://github.com/rw-hub/fluent-plugin-mysql-appender"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files`.split($\)
14
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_runtime_dependency "fluentd"
19
+ spec.add_runtime_dependency "mysql2"
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.13"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+
25
+ end
@@ -0,0 +1,122 @@
1
+ module Fluent
2
+ class MysqlAppenderInput < Fluent::Input
3
+ Plugin.register_input('mysql_appender', self)
4
+
5
+ # Define `router` method to support v0.10.57 or earlier
6
+ unless method_defined?(:router)
7
+ define_method("router") { Engine }
8
+ end
9
+
10
+ def initialize
11
+ require 'mysql2'
12
+ super
13
+ end
14
+
15
+ config_param :host, :string, :default => 'localhost'
16
+ config_param :port, :integer, :default => 3306
17
+ config_param :username, :string, :default => 'root'
18
+ config_param :password, :string, :default => nil, :secret => true
19
+ config_param :database, :string, :default => nil
20
+ config_param :encoding, :string, :default => 'utf8'
21
+ config_param :query, :string
22
+ config_param :primary_key, :string, :default => 'id'
23
+ config_param :interval, :string, :default => '1m'
24
+ config_param :tag, :string, :default => nil
25
+
26
+ def configure(conf)
27
+ super
28
+ @interval = Config.time_value(@interval)
29
+
30
+ if @tag.nil?
31
+ raise Fluent::ConfigError, "mysql_appender: missing 'tag' parameter. Please add following line into config like 'tag replicator.mydatabase.mytable.${event}.${primary_key}'"
32
+ end
33
+
34
+ $log.info "adding mysql_appender worker. :tag=>#{tag} :query=>#{@query} :prepared_query=>#{@prepared_query} :interval=>#{@interval}sec"
35
+ end
36
+
37
+ def start
38
+ @thread = Thread.new(&method(:run))
39
+ end
40
+
41
+ def shutdown
42
+ Thread.kill(@thread)
43
+ end
44
+
45
+ def run
46
+ begin
47
+ poll
48
+ rescue StandardError => e
49
+ $log.error "mysql_appender: failed to execute query."
50
+ $log.error "error: #{e.message}"
51
+ $log.error e.backtrace.join("\n")
52
+ end
53
+ end
54
+
55
+ def poll
56
+ con = get_connection()
57
+ max_id = -1
58
+ loop do
59
+ rows_count = 0
60
+ start_time = Time.now
61
+ if max_id == -1
62
+ select_query = @query + " order by #{primary_key} asc"
63
+ else
64
+ select_query = @query + " where #{primary_key} > #{max_id} order by #{primary_key} asc"
65
+ end
66
+ rows, con = query(select_query, con)
67
+ rows.each_with_index do |row, index|
68
+ tag = format_tag(@tag, {:event => :insert})
69
+ router.emit(tag, Engine.now, row)
70
+ rows_count += 1
71
+ if index == rows.size - 1
72
+ max_id = row[@primary_key]
73
+ end
74
+ end
75
+ con.close
76
+ elapsed_time = sprintf("%0.02f", Time.now - start_time)
77
+ $log.info "mysql_appender: finished execution :tag=>#{tag} :rows_count=>#{rows_count} :elapsed_time=>#{elapsed_time} sec"
78
+ sleep @interval
79
+ end
80
+ end
81
+
82
+ def format_tag(tag, param)
83
+ pattern = {'${event}' => param[:event].to_s, '${primary_key}' => @primary_key}
84
+ tag.gsub(/(\${[a-z_]+})/) do
85
+ $log.warn "mysql_appender: missing placeholder. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
86
+ pattern[$1]
87
+ end
88
+ end
89
+
90
+ def query(query, con = nil)
91
+ begin
92
+ con = con.nil? ? get_connection : con
93
+ con = con.ping ? con : get_connection
94
+ return con.query(query), con
95
+ rescue Exception => e
96
+ $log.warn "mysql_appender: #{e}"
97
+ sleep @interval
98
+ retry
99
+ end
100
+ end
101
+
102
+ def get_connection
103
+ begin
104
+ return Mysql2::Client.new({
105
+ :host => @host,
106
+ :port => @port,
107
+ :username => @username,
108
+ :password => @password,
109
+ :database => @database,
110
+ :encoding => @encoding,
111
+ :reconnect => true,
112
+ :stream => true,
113
+ :cache_rows => false
114
+ })
115
+ rescue Exception => e
116
+ $log.warn "mysql_appender: #{e}"
117
+ sleep @interval
118
+ retry
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class MysqlAppenderInputTest < Minitest::Test
4
+
5
+ def test_it_does_something_useful
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'fluent/plugin/in_mysql_appender'
2
+
3
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-mysql-appender
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - tsuyoshi_terasaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-07 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
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.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Simple incremental id's insert.
84
+ email:
85
+ - tsuyoshi_terasaki@realworld.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - fluent-plugin-mysql-appender.gemspec
97
+ - lib/fluent/plugin/in_mysql_appender.rb
98
+ - test/fluent/plugin/in_mysql_appender_test.rb
99
+ - test/test_helper.rb
100
+ homepage: https://github.com/rw-hub/fluent-plugin-mysql-appender
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.5.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Fluentd input plugin to track insert event from MySQL database server.
124
+ test_files:
125
+ - test/fluent/plugin/in_mysql_appender_test.rb
126
+ - test/test_helper.rb