kafka_etl_base 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzdmYzA5ZGIxMjU0MDYxZTVhM2FmZmY5N2RiYjkzYzEzNDk5ZjJjOA==
5
+ data.tar.gz: !binary |-
6
+ ZTRkOWNiOGExY2IwNmNhNzM3OGQwZTNmMDE2MmQyZDhkZDg2NTcwNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YTljYzA5NGM3YWRmNjNmOGY3YTg3MjIyNGY1YjQ2ZTU3NmI3ZGVmMWNiOWU5
10
+ MjczNTQzNjMzMzU5YTQ4YjRmNmY1ZTY2YWJiY2JkY2FhMmIxNDAzMGJkZDc0
11
+ ZDJhYmI3MjIyYjM5ZDFlMmQ4MmMyZWM2YmY3ZmQ1NmVjMWZiZWQ=
12
+ data.tar.gz: !binary |-
13
+ OGMyYzM1Yzg5OWVkMGIyMDk3OTAwODJiZTQ5ZDJiYzFkMTczZjkyNDAwOWY4
14
+ MmQ4ZTNlZjg5MzM5OWJjODQ5NWE2MGEwNDUwMTZkMzIxZDRjYjY0ZmJkMGZm
15
+ OGQyMjZkMzIwOGM5ZjI1ZWZiNmFjZmJhZTQ4YzlkNGRkODU5MmE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kafka_etl_base.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Shinji Ikeda
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,29 @@
1
+ # KafkaEtlBase
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'kafka_etl_base'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install kafka_etl_base
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/kafka_etl_base/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kafka_etl_base/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kafka_etl_base"
8
+ spec.version = KafkaETLBase::VERSION
9
+ spec.authors = ["Shinji Ikeda"]
10
+ spec.email = ["gm.ikeda@gmail.com"]
11
+ spec.summary = %q{Kafa ETL Base}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency "poseidon"
25
+ spec.add_dependency "zk"
26
+ spec.add_dependency "parallel"
27
+ end
@@ -0,0 +1,169 @@
1
+
2
+ require 'poseidon'
3
+ require 'zk'
4
+ require 'parallel'
5
+ require 'logger'
6
+
7
+ module KafkaETLBase
8
+ class BackendError < StandardError; end
9
+ class Base
10
+ def initialize(zookeeper, kafka_brokers, kafka_topic, opts={})
11
+
12
+ @num_threads = opts[:num_threads] ? opts[:num_threads] : 2
13
+ @max_fetch_bytes = opts[:max_fetch_bytes] ? opts[:max_fetch_bytes] : 5_000_000
14
+ @min_fetch_bytes = opts[:min_fetch_bytes] ? opts[:min_fetch_bytes] : 0
15
+ @kafka_clint_id = opts[:kafka_client_id] ? opts[:kafka_client_id] : "my_consumer"
16
+ @kafka_part_num = opts[:kafka_topic_part_num] ? opts[:kafka_topic_part_num] : 2
17
+
18
+ @zookeeper = zookeeper
19
+ @kafka_brokers = kafka_brokers
20
+ @kafka_topic = kafka_topic
21
+
22
+ $log = Logger.new(STDOUT) if $log.nil?
23
+
24
+ $log.debug("zookeeper: #{@zookeeper}")
25
+ $log.debug("kafka_brokers: #{@kafka_brokers}")
26
+ $log.debug("kafka_topic: #{@kafka_topic}")
27
+
28
+ @partition_shuffle = true
29
+
30
+ @mutex = Mutex.new
31
+
32
+ @total_procs = 0
33
+ end
34
+
35
+ def process()
36
+
37
+ @total_procs = 0
38
+
39
+ zk = ZK.new(@zookeeper)
40
+ zk.create("/", ignore: :node_exists)
41
+
42
+ begin
43
+ seq = [ * 0 ... @kafka_part_num ]
44
+ seq.shuffle! if @partition_shuffle == true
45
+
46
+ r = Parallel.each(seq, :in_threads => @num_threads) do |part_no|
47
+ zk_lock = "lock_hdfs_part_#{part_no}"
48
+ 3.times.each do | n |
49
+ locker = zk.locker(zk_lock)
50
+ begin
51
+ if locker.lock!
52
+ remain = proccess_thread(zk, part_no)
53
+ break if remain < 1000
54
+ else
55
+ $log.info("part: #{part_no} is aleady locked skip")
56
+ break
57
+ end
58
+ ensure
59
+ locker.unlock!
60
+ end
61
+ end
62
+ end
63
+ ensure
64
+ zk.close
65
+ end
66
+ $log.info "total procs: #{@total_procs}"
67
+ end
68
+
69
+ def process_partition(part_no)
70
+ 3.times.each do | n |
71
+ zk = ZK.new(@zookeeper)
72
+ begin
73
+ zk_lock = "lock_hdfs_part_#{part_no}"
74
+ locker = zk.locker(zk_lock)
75
+ begin
76
+ if locker.lock!
77
+ remain = proccess_thread(zk, part_no)
78
+ break if remain < 1000
79
+ else
80
+ $log.info("part: #{part_no} is aleady locked skip")
81
+ break
82
+ end
83
+ ensure
84
+ locker.unlock!
85
+ end
86
+ ensure
87
+ zk.close
88
+ end
89
+ end
90
+ end
91
+
92
+ def proccess_thread(zk, part_no)
93
+ zk_part_node = "/part_offset_#{part_no}"
94
+
95
+ num_cur_part_procs = 0
96
+
97
+ if ! zk.exists?(zk_part_node)
98
+ zk.create(zk_part_node, "0")
99
+ end
100
+
101
+ offset = nil
102
+ begin
103
+ value, stat = zk.get(zk_part_node)
104
+ offset = value.to_i
105
+ if offset == 0
106
+ part_offset = :earliest_offset
107
+ #part_offset = :latest_offset
108
+ else
109
+ part_offset = offset
110
+ end
111
+ rescue ZK::Exceptions::NoNode => e
112
+ part_offset = :earliest_offset
113
+ #part_offset = :latest_offset
114
+ end
115
+ $log.debug "part: #{part_no}, offset: #{part_offset}"
116
+
117
+ cons = Poseidon::PartitionConsumer.consumer_for_partition(@kafka_client_id,
118
+ @kafka_brokers,
119
+ @kafka_topic,
120
+ part_no,
121
+ part_offset,
122
+ :max_wait_ms => 0,
123
+ :max_bytes => @max_fetch_bytes,
124
+ :min_bytes => @min_fetch_bytes
125
+ )
126
+ begin
127
+ num_cur_part_procs += process_messages(cons, part_no)
128
+
129
+ next_offset = cons.next_offset
130
+ last_offset = cons.highwater_mark
131
+ $log.info "part: #{part_no}, next_offset: #{next_offset}, last_offset: #{last_offset}, proc: #{num_cur_part_procs}"
132
+ @mutex.synchronize do
133
+ @total_procs += num_cur_part_procs
134
+ end
135
+ # set next offset to zookeper
136
+ zk.set(zk_part_node, next_offset.to_s) if next_offset >= offset
137
+ return last_offset - next_offset
138
+ rescue Poseidon::Errors::NotLeaderForPartition => e
139
+ $log.error "Skip: Not Leader For Partition"
140
+ return 0
141
+ rescue Poseidon::Errors::OffsetOutOfRange => e
142
+ $log.error e.to_s
143
+ zk.set(zk_part_node, "0")
144
+ return 0
145
+ rescue Poseidon::Connection::ConnectionFailedError
146
+ $log.error "kafka connection failed"
147
+ return 0
148
+ rescue BackendError
149
+ $log.debug "backend error"
150
+ return 0
151
+ ensure
152
+ cons.close if ! cons.nil?
153
+ end
154
+ rescue Poseidon::Errors::OffsetOutOfRange => e
155
+ $log.error e.to_s
156
+ zk.set(zk_part_node, "0")
157
+ end
158
+
159
+ def process_messages(cons, part_no)
160
+ messages = cons.fetch
161
+ messages.each do |m|
162
+ key = m.key
163
+ val = m.value
164
+ puts "part: #{part_no}, key: #{key}, val: #{val}"
165
+ end
166
+ messages.size
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,3 @@
1
+ module KafkaETLBase
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "kafka_etl_base/version"
2
+
3
+ module KafkaETLBase
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kafka_etl_base
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shinji Ikeda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: poseidon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: parallel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - gm.ikeda@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - kafka_etl_base.gemspec
96
+ - lib/kafka_etl_base.rb
97
+ - lib/kafka_etl_base/base.rb
98
+ - lib/kafka_etl_base/version.rb
99
+ homepage: ''
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.2.2
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Kafa ETL Base
123
+ test_files: []