fluent-plugin-postgres-replicator 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ca7d5dc0cc16563d50a913c50da4d89f130ceb3
4
- data.tar.gz: a12eaccf0624403b32359673935b6614ed367363
3
+ metadata.gz: e0800a5552e7c84208560aa4e71ae507af0c826a
4
+ data.tar.gz: 0b17c898e047944522ef5f97fb6f10c3f4631701
5
5
  SHA512:
6
- metadata.gz: c5d817d1e75134379f26443e0106b28a1afc2e03de7b94467cc8a21660169365a6295da941e6bad9cf25373e066acfd3ec7628ce09c13a4c1d1f3fe749fd846e
7
- data.tar.gz: 13db91bfb686ed5eeec844d50a68eb55f72f7e82495223191f27b2d2f9ee2b92e65ac7bcaeaf3923f20b65f9416069a69eaf825c77677c8840e6ad869c0c6b4d
6
+ metadata.gz: ce731ae495459c72229c84760df4c76963b08618f0d55300156b7d8303a838b0b8609d2b6d2ece8e27e0d01273eedd9b195bef6c74c215311b9cbf3149cf3d22
7
+ data.tar.gz: 3c98a0b8018703aa6e748c5b62eae1ddca8f4fe951c0c430e7594e5624d81b726ac1307766550d27b3b724eb274f0ce7ac0d838d11cee04a5803c9f89ffc7379
data/.travis.yml CHANGED
@@ -1,4 +1,19 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
- - 2.2.3
4
- before_install: gem install bundler -v 1.10.6
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - 2.2
8
+
9
+ addons:
10
+ postgresql: "9.4"
11
+
12
+ services:
13
+ - postgresql
14
+
15
+ before_script:
16
+ - psql -c 'create database pg_repli_test_db;' -U postgres
17
+ - psql -c 'create table pg_repli_test_table (id int8 primary key, total int8) ;' -U postgres -d pg_repli_test_db
18
+ - psql -c 'insert into pg_repli_test_table values (1, 10) ;' -U postgres -d pg_repli_test_db
19
+ - psql -c 'insert into pg_repli_test_table values (2, 20) ;' -U postgres -d pg_repli_test_db
data/README.md CHANGED
@@ -1,34 +1,37 @@
1
- # Fluent::Plugin::Postgres::Replicator
1
+ # Fluent::Plugin::PostgresReplicator, a plugin for [Fluentd](http://www.fluentd.org)
2
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/postgres/replicator`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/fluent-plugin-postgres-replicator.svg)](https://badge.fury.io/rb/fluent-plugin-postgres-replicator)
4
+ [![Build Status](https://travis-ci.org/innossh/fluent-plugin-postgres-replicator.svg?branch=master)](https://travis-ci.org/innossh/fluent-plugin-postgres-replicator)
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Fluentd input plugin to track insert/update event from PostgreSQL.
6
7
 
7
8
  ## Installation
8
9
 
9
- Add this line to your application's Gemfile:
10
+ ```sh
11
+ # requirements
12
+ $ apt-get install libpq-dev
10
13
 
11
- ```ruby
12
- gem 'fluent-plugin-postgres-replicator'
14
+ $ gem install fluent-plugin-postgres-replicator
13
15
  ```
14
16
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install fluent-plugin-postgres-replicator
22
-
23
17
  ## Usage
24
18
 
25
- TODO: Write usage instructions here
19
+ In your Fluentd configuration, use `type postgres_replicator`.
20
+ Default values would look like this:
26
21
 
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` 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).
22
+ ```
23
+ <source>
24
+ type postgres_replicator
25
+ host localhost
26
+ username pipeline
27
+ password pipeline
28
+ database pipeline
29
+ query SELECT hour, project, total_pages from wiki_stats;
30
+ primary_keys hour,project
31
+ interval 1m
32
+ tag replicator.pipeline.wiki_stats.${event}.${primary_keys}
33
+ </source>
34
+ ```
32
35
 
33
36
  ## Contributing
34
37
 
@@ -36,5 +39,5 @@ Bug reports and pull requests are welcome.
36
39
 
37
40
  ## License
38
41
 
39
- - Copyright (c) 2015 innossh
42
+ - Copyright (c) 2016 innossh
40
43
  - [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "fluent-plugin-postgres-replicator"
7
- gem.version = "0.0.1"
7
+ gem.version = "0.0.2"
8
8
  gem.authors = ["innossh"]
9
9
  gem.email = ["innossh@users.noreply.github.com"]
10
10
 
@@ -6,7 +6,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
6
6
  config_param :username, :string, :default => 'root'
7
7
  config_param :password, :string, :default => nil, :secret => true
8
8
  config_param :database, :string, :default => nil
9
- config_param :query, :string, :default => nil
9
+ config_param :sql, :string, :default => nil
10
10
  config_param :primary_keys, :string, :default => nil
11
11
  config_param :interval, :string, :default => '10s'
12
12
  config_param :tag, :string, :default => nil
@@ -26,7 +26,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
26
26
  if @tag.nil?
27
27
  raise Fluent::ConfigError, "tag MUST be specified"
28
28
  end
29
- @primary_keys = @primary_keys.split(',')
29
+ @primary_keys = @primary_keys.split(/\s*,\s*/)
30
30
  end
31
31
 
32
32
  def start
@@ -41,8 +41,8 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
41
41
  begin
42
42
  poll
43
43
  rescue StandardError => e
44
- $log.error "failed to execute query. error: #{e.message}"
45
- $log.error e.backtrace.join("\n")
44
+ log.error "failed to execute query. error: #{e.message}"
45
+ log.error e.backtrace.join("\n")
46
46
  end
47
47
  end
48
48
 
@@ -52,15 +52,17 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
52
52
  loop do
53
53
  rows_count = 0
54
54
  start_time = Time.now
55
- rows, conn = query(@query, conn)
55
+ rows, conn = query(@sql, conn)
56
56
  rows.each do |row|
57
57
  row_ids = Array.new
58
58
  @primary_keys.each do |primary_key|
59
- if row[primary_key].nil?
60
- $log.error "primary_key value is not found. :tag=>#{@tag} :primary_key=>#{primary_key}"
61
- break
59
+ if !row[primary_key].nil?
60
+ row_ids << row[primary_key]
62
61
  end
63
- row_ids << row[primary_key]
62
+ end
63
+ if row_ids.size != @primary_keys.size
64
+ log.error "primary_keys column value is something wrong. :tag=>#{@tag} :primary_keys=>#{@primary_keys}"
65
+ break
64
66
  end
65
67
 
66
68
  hash_value_id = row_ids.join('_')
@@ -77,18 +79,18 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
77
79
  end
78
80
  conn.close
79
81
  elapsed_time = sprintf('%0.02f', Time.now - start_time)
80
- $log.info "success to execute replicator. :tag=>#{@tag} :rows_count=>#{rows_count} :elapsed_time=>#{elapsed_time} sec"
82
+ log.info "success to execute replicator. :tag=>#{@tag} :rows_count=>#{rows_count} :elapsed_time=>#{elapsed_time} sec"
81
83
  sleep @interval
82
84
  end
83
85
 
84
86
  end
85
87
 
86
- def query(query, conn = nil)
88
+ def query(sql, conn = nil)
87
89
  begin
88
90
  conn = (conn.nil? || conn.finished?) ? get_connection : conn
89
- return conn.query(query), conn
91
+ return conn.query(sql), conn
90
92
  rescue Exception => e
91
- $log.warn "failed to execute query and will retry. error: #{e}"
93
+ log.warn "failed to execute query and will retry. error: #{e}"
92
94
  sleep @interval
93
95
  retry
94
96
  end
@@ -97,7 +99,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
97
99
  def format_tag(tag, param)
98
100
  pattern = {'${event}' => param[:event].to_s, '${primary_keys}' => @primary_keys.join('_')}
99
101
  tag.gsub(/(\${[a-z_]+})/) do
100
- $log.warn "placeholder value is not found. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
102
+ log.warn "placeholder value is not found. :tag=>#{tag} :placeholder=>#{$1}" unless pattern.include?($1)
101
103
  pattern[$1]
102
104
  end
103
105
  end
@@ -116,7 +118,7 @@ class Fluent::PostgresReplicatorInput < Fluent::Input
116
118
  :dbname => @database
117
119
  })
118
120
  rescue Exception => e
119
- $log.warn "failed to get connection and will retry. error: #{e}"
121
+ log.warn "failed to get connection and will retry. error: #{e}"
120
122
  sleep @interval
121
123
  retry
122
124
  end
@@ -5,13 +5,18 @@ require 'fluent/plugin/in_postgres_replicator'
5
5
  class PostgresReplicatorTest < Test::Unit::TestCase
6
6
  def setup
7
7
  Fluent::Test.setup
8
+ @time = Fluent::Engine.now
8
9
  end
9
10
 
10
11
  CONFIG = %[
11
12
  host 127.0.0.1
12
13
  port 5432
14
+ username postgres
15
+ database pg_repli_test_db
16
+ sql SELECT id, total from pg_repli_test_table;
17
+ interval 1s
13
18
  primary_keys id
14
- tag pgreplicator.db.table.${event}.${primary_keys}
19
+ tag pgreplicator.pg_repli_test_db.pg_repli_test_table.${event}.${primary_keys}
15
20
  ]
16
21
 
17
22
  def create_driver(conf = CONFIG)
@@ -26,8 +31,26 @@ class PostgresReplicatorTest < Test::Unit::TestCase
26
31
  d = create_driver
27
32
  assert_equal '127.0.0.1', d.instance.host
28
33
  assert_equal 5432, d.instance.port
29
- assert_equal [ 'id' ], d.instance.primary_keys
30
- assert_equal 'pgreplicator.db.table.${event}.${primary_keys}', d.instance.tag
34
+ assert_equal 'postgres', d.instance.username
35
+ assert_equal nil, d.instance.password
36
+ assert_equal 'pg_repli_test_db', d.instance.database
37
+ assert_equal 'SELECT id, total from pg_repli_test_table;', d.instance.sql
38
+ assert_equal 1, d.instance.interval
39
+ assert_equal ['id'], d.instance.primary_keys
40
+ assert_equal 'pgreplicator.pg_repli_test_db.pg_repli_test_table.${event}.${primary_keys}', d.instance.tag
41
+ end
42
+
43
+ def test_emit
44
+ d = create_driver
45
+
46
+ d.run do
47
+ sleep 2
48
+ end
49
+
50
+ emits = d.emits
51
+ assert_equal true, emits.length > 0
52
+ assert_equal ['pgreplicator.pg_repli_test_db.pg_repli_test_table.insert.id', @time, {'id' => '1', 'total' => '10'}], emits[0]
53
+ assert_equal ['pgreplicator.pg_repli_test_db.pg_repli_test_table.insert.id', @time, {'id' => '2', 'total' => '20'}], emits[1]
31
54
  end
32
55
 
33
56
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-postgres-replicator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - innossh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-06 00:00:00.000000000 Z
11
+ date: 2016-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fluentd