fluent-plugin-parser-avro 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: accbc87b882c2e1e0999a9992da794fbde6be745f240e043e77e92d9964406ac
4
+ data.tar.gz: 817d393998e57f8ba2352f405e565b0c59de34e3c92b7cbc38653453a621fd4a
5
+ SHA512:
6
+ metadata.gz: 2dda0e434a73d6e3d1380c3e96e5cfe1595c3041bfe4afa9b99b125bcc8b19c7177139860ea108c7523f418d5fd589bc5edcd673d3025f628c0b47f2ca664c31
7
+ data.tar.gz: 4277e90990b38c9d72e5982f817d02c39f57592af64f2b03c54af9ec708b57908f3a13bd41de3ad2a5e82794b4612b555888ace4e10ec197d728e84b89d4718e
@@ -0,0 +1,26 @@
1
+ name: Testing on Ubuntu
2
+ on:
3
+ - push
4
+ - pull_request
5
+ jobs:
6
+ build:
7
+ runs-on: ${{ matrix.os }}
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: [ '2.4', '2.5', '2.6', '2.7' ]
12
+ os:
13
+ - ubuntu-latest
14
+ name: Ruby ${{ matrix.ruby }} unit testing on ${{ matrix.os }}
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ - name: unit testing
21
+ env:
22
+ CI: true
23
+ run: |
24
+ gem install bundler rake
25
+ bundle install --jobs 4 --retry 3
26
+ bundle exec rake test
@@ -0,0 +1,26 @@
1
+ name: Testing on Windows
2
+ on:
3
+ - push
4
+ - pull_request
5
+ jobs:
6
+ build:
7
+ runs-on: ${{ matrix.os }}
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: [ '2.4', '2.5', '2.6', '2.7' ]
12
+ os:
13
+ - windows-latest
14
+ name: Ruby ${{ matrix.ruby }} unit testing on ${{ matrix.os }}
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+ - name: unit testing
21
+ env:
22
+ CI: true
23
+ run: |
24
+ gem install bundler rake
25
+ bundle install --jobs 4 --retry 3
26
+ bundle exec rake test
@@ -0,0 +1,3 @@
1
+ /vendor/
2
+ .DS_Store
3
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2020- Hiroshi Hatake
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,82 @@
1
+ # fluent-plugin-parser-avro
2
+
3
+ ![Testing on Ubuntu](https://github.com/fluent-plugins-nursery/fluent-plugin-parser-avro/workflows/Testing%20on%20Ubuntu/badge.svg?branch=master)
4
+ ![Testing on Windows](https://github.com/fluent-plugins-nursery/fluent-plugin-parser-avro/workflows/Testing%20on%20Windows/badge.svg?branch=master)
5
+
6
+ [Fluentd](https://fluentd.org/) parser plugin to parse avro formatted data.
7
+
8
+ ## Installation
9
+
10
+ ### RubyGems
11
+
12
+ ```
13
+ $ gem install fluent-plugin-avro
14
+ ```
15
+
16
+ ### Bundler
17
+
18
+ Add following line to your Gemfile:
19
+
20
+ ```ruby
21
+ gem "fluent-plugin-avro"
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```
27
+ $ bundle
28
+ ```
29
+
30
+ ## Configuration
31
+
32
+ * **schema_file** (string) (optional): avro schema file path.
33
+ * **schema_json** (string) (optional): avro schema definition hash.
34
+ * **schema_url** (string) (optional): avro schema remote URL.
35
+ * **schema_registery_with_subject_url** (string) (optional): avro schema registry URL.
36
+ * **schema_url_key** (string) (optional): avro schema registry or something's response schema key.
37
+ * **writers_schema_file** (string) (optional): avro schema file path for writers definition.
38
+ * **writers_schema_json** (string) (optional): avro schema definition hash for writers definition.
39
+ * **readers_schema_file** (string) (optional): avro schema file path for readers definition.
40
+ * **readers_schema_json** (string) (optional): avro schema definition hash for readers definition.
41
+
42
+ ### Configuration Example
43
+
44
+ ```aconf
45
+ <parse>
46
+ @type avro
47
+ # schema_file /path/to/file
48
+ # schema_json { "namespace": "org.fluentd.parser.avro", "type": "record", "name": "User", "fields" : [{"name": "username", "type": "string"}, {"name": "age", "type": "int"}, {"name": "verified", "type": ["boolean", "null"], "default": false}]}
49
+ # schema_url http(s)://[server fqdn]:[port]/subjects/[a great user's subject]/[the latest schema version]
50
+ # schema_key schema
51
+ # schema_registery_with_subject_url http(s)://[server fqdn]:[port]/subjects/[a great user's subject]/
52
+ </parse>
53
+ ```
54
+
55
+ ## AVRO schema registry support
56
+
57
+ Confluent AVRO schema registry should respond with REST API.
58
+
59
+ This plugin uses the following API:
60
+
61
+ * [`GET /subjects/(string: subject)/versions`](https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects-(string-%20subject)-versions)
62
+ * [`GET /subjects/(string: subject)/versions/(versionId: version)`](https://docs.confluent.io/current/schema-registry/develop/api.html#get--subjects-(string-%20subject)-versions)
63
+
64
+ Users can specify a URL for retrieving the latest schemna information:
65
+
66
+ e.g.) `http(s)://[server fqdn]:[port]/subjects/[a great user's subject]/`
67
+
68
+ For example, when specifying the following configuration:
69
+
70
+ ```
71
+ <parse>
72
+ @type avro
73
+ schema_registery_with_subject_url http://localhost:8081/subjects/persons-avro-value/
74
+ ```
75
+
76
+ Then the parser plugin calls `GET http://localhost:8081/subjects/persons-avro-value/versions/` to retrive the registered schema versions and then calls `GET GET http://localhost:8081/subjects/persons-avro-value/versions/<the latest schema version>`.
77
+
78
+ ## Copyright
79
+
80
+ * Copyright(c) 2020- Hiroshi Hatake
81
+ * License
82
+ * Apache License, Version 2.0
@@ -0,0 +1,13 @@
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs.push("lib", "test")
8
+ t.test_files = FileList["test/**/test_*.rb"]
9
+ t.verbose = true
10
+ t.warning = true
11
+ end
12
+
13
+ task default: [:test]
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "fluent-plugin-parser-avro"
6
+ spec.version = "0.1.0"
7
+ spec.authors = ["Hiroshi Hatake"]
8
+ spec.email = ["cosmo0920.wp@gmail.com"]
9
+
10
+ spec.summary = %q{Avro parser plugin for Fluentd}
11
+ spec.description = spec.summary
12
+ spec.homepage = "https://github.com/fluent-plugins-nerysery/fluent-plugin-parser-avro"
13
+ spec.license = "Apache-2.0"
14
+
15
+ test_files, files = `git ls-files -z`.split("\x0").partition do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.files = files
19
+ spec.executables = files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = test_files
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "avro"
24
+ spec.add_development_dependency "bundler", "~> 2.1"
25
+ spec.add_development_dependency "rake", "~> 13.0"
26
+ spec.add_development_dependency "test-unit", "~> 3.3"
27
+ spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
28
+ end
@@ -0,0 +1,151 @@
1
+ #
2
+ # Copyright 2020- Hiroshi Hatake
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "avro"
17
+ require "net/http"
18
+ require "stringio"
19
+ require "uri"
20
+ require "fluent/plugin/parser"
21
+
22
+ module Fluent
23
+ module Plugin
24
+ class AvroParser < Fluent::Plugin::Parser
25
+ Fluent::Plugin.register_parser("avro", self)
26
+
27
+ config_param :schema_file, :string, :default => nil
28
+ config_param :schema_json, :string, :default => nil
29
+ config_param :schema_url, :string, :default => nil
30
+ config_param :schema_registery_with_subject_url, :string, :default => nil
31
+ config_param :schema_url_key, :string, :default => nil
32
+ config_param :writers_schema_file, :string, :default => nil
33
+ config_param :writers_schema_json, :string, :default => nil
34
+ config_param :readers_schema_file, :string, :default => nil
35
+ config_param :readers_schema_json, :string, :default => nil
36
+
37
+ def configure(conf)
38
+ super
39
+
40
+ if (!@writers_schema_file.nil? || !@writers_schema_json.nil?) &&
41
+ (!@readers_schema_file.nil? || !@readers_schema_json.nil?)
42
+ unless [@writers_schema_json, @writers_schema_file].compact.size == 1
43
+ raise Fluent::ConfigError, "writers_schema_json, writers_schema_file is required, but they cannot specify at the same time!"
44
+ end
45
+ unless [@readers_schema_json, @readers_schema_file].compact.size == 1
46
+ raise Fluent::ConfigError, "readers_schema_json, readers_schema_file is required, but they cannot specify at the same time!"
47
+ end
48
+
49
+ @writers_raw_schema = if @writers_schema_file
50
+ File.read(@writers_schema_file)
51
+ elsif @writers_schema_json
52
+ @writers_schema_json
53
+ end
54
+ @readers_raw_schema = if @readers_schema_file
55
+ File.read(@readers_schema_file)
56
+ elsif @readers_schema_json
57
+ @readers_schema_json
58
+ end
59
+
60
+ @writers_schema = Avro::Schema.parse(@writers_raw_schema)
61
+ @readers_schema = Avro::Schema.parse(@readers_raw_schema)
62
+ @reader = Avro::IO::DatumReader.new(@writers_schema, @readers_schema)
63
+ else
64
+ unless [@schema_json, @schema_file, @schema_url, @schema_registery_with_subject_url].compact.size == 1
65
+ raise Fluent::ConfigError, "schema_json, schema_file, or schema_url is required, but they cannot specify at the same time!"
66
+ end
67
+ if @schema_registery_with_subject_url && !@schema_registery_with_subject_url.end_with?("/")
68
+ raise Fluent::ConfigError, "schema_registery_with_subject_url must contain the trailing slash('/')."
69
+ end
70
+
71
+ @raw_schema = if @schema_file
72
+ File.read(@schema_file)
73
+ elsif @schema_registery_with_subject_url
74
+ fetch_latest_schema(@schema_registery_with_subject_url, @schema_url_key)
75
+ elsif @schema_url
76
+ fetch_schema(@schema_url, @schema_url_key)
77
+ elsif @schema_json
78
+ @schema_json
79
+ end
80
+
81
+ @schema = Avro::Schema.parse(@raw_schema)
82
+ @reader = Avro::IO::DatumReader.new(@schema)
83
+ end
84
+ end
85
+
86
+ def parser_type
87
+ :binary
88
+ end
89
+
90
+ def parse(data)
91
+ buffer = StringIO.new(data)
92
+ decoder = Avro::IO::BinaryDecoder.new(buffer)
93
+ begin
94
+ decoded_data = @reader.read(decoder)
95
+ time, record = convert_values(parse_time(decoded_data), decoded_data)
96
+ yield time, record
97
+ rescue => e
98
+ raise e if @schema_url.nil? or @schema_registery_with_subject_url.nil?
99
+ begin
100
+ new_raw_schema = if @schema_url
101
+ fetch_schema(@schema_url, @schema_url_key)
102
+ elsif @schema_registery_with_subject_url
103
+ fetch_latest_schema(@schema_registery_with_subject_url, @schema_url_key)
104
+ end
105
+ new_schema = Avro::Schema.parse(new_raw_schema)
106
+ is_changed = (new_raw_schena_== @raw_schema)
107
+ @raw_schema = new_raw_schema
108
+ @schame = new_schema
109
+ rescue
110
+ # Do nothing.
111
+ end
112
+ if is_changed
113
+ decoded_data = @reader.read(decoder)
114
+ time, record = convert_values(parse_time(decoded_data), decoded_data)
115
+ yield time, record
116
+ else
117
+ raise e
118
+ end
119
+ end
120
+ end
121
+
122
+ def fetch_schema_versions(base_uri_with_versions)
123
+ versions_response = Net::HTTP.get_response(base_uri_with_versions)
124
+ Yajl.load(versions_response.body)
125
+ end
126
+
127
+ def fetch_latest_schema(base_url, schema_key)
128
+ base_uri = URI.parse(base_url)
129
+ base_uri_with_versions = URI.join(base_uri, "versions/")
130
+ versions = fetch_schema_versions(base_uri_with_versions)
131
+ uri = URI.join(base_uri_with_versions, versions.last.to_s)
132
+ response = Net::HTTP.get_response(uri)
133
+ if schema_key.nil?
134
+ response.body
135
+ else
136
+ Yajl.load(response.body)[schema_key]
137
+ end
138
+ end
139
+
140
+ def fetch_schema(url, schema_key)
141
+ uri = URI.parse(url)
142
+ response = Net::HTTP.get_response(uri)
143
+ if schema_key.nil?
144
+ response.body
145
+ else
146
+ Yajl.load(response.body)[schema_key]
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1 @@
1
+ {"subject":"persons-avro-value","version":1,"id":1,"schema":"{\"type\":\"record\",\"name\":\"Person\",\"namespace\":\"com.ippontech.kafkatutorials\",\"fields\":[{\"name\":\"firstName\",\"type\":\"string\"},{\"name\":\"lastName\",\"type\":\"string\"},{\"name\":\"birthDate\",\"type\":\"long\"}]}"}
@@ -0,0 +1 @@
1
+ {"subject":"persons-avro-value","version":2,"id":21,"schema":"{\"type\":\"record\",\"name\":\"Person\",\"namespace\":\"com.ippontech.kafkatutorials\",\"fields\":[{\"name\":\"firstName\",\"type\":\"string\"},{\"name\":\"lastName\",\"type\":\"string\"},{\"name\":\"birthDate\",\"type\":\"long\"},{\"name\":\"verified\",\"type\":\"boolean\",\"default\":false}]}"}
@@ -0,0 +1 @@
1
+ {"subject":"persons-avro-value","version":3,"id":41,"schema":"{\"type\":\"record\",\"name\":\"Person\",\"namespace\":\"com.ippontech.kafkatutorials\",\"fields\":[{\"name\":\"firstName\",\"type\":\"string\"},{\"name\":\"lastName\",\"type\":\"string\"},{\"name\":\"birthDate\",\"type\":\"long\"},{\"name\":\"verified\",\"type\":\"boolean\"}]}"}
@@ -0,0 +1 @@
1
+ {"subject":"persons-avro-value","version":4,"id":42,"schema":"{\"type\":\"record\",\"name\":\"Person\",\"namespace\":\"com.ippontech.kafkatutorials\",\"fields\":[{\"name\":\"firstName\",\"type\":\"string\"},{\"name\":\"lastName\",\"type\":\"string\"},{\"name\":\"birthDate\",\"type\":\"long\"},{\"name\":\"verified\",\"type\":[\"boolean\",\"null\"],\"default\":false}]}"}
@@ -0,0 +1,7 @@
1
+ { "namespace": "org.fluentd.parser.avro",
2
+ "type": "record",
3
+ "name": "User",
4
+ "fields" : [
5
+ {"name": "username", "type": "string"},
6
+ {"name": "age", "type": "int"}
7
+ ]}
@@ -0,0 +1,8 @@
1
+ { "namespace": "org.fluentd.parser.avro",
2
+ "type": "record",
3
+ "name": "User",
4
+ "fields" : [
5
+ {"name": "username", "type": "string"},
6
+ {"name": "age", "type": "int"},
7
+ {"name": "verified", "type": ["boolean", "null"], "default": false}
8
+ ]}
@@ -0,0 +1,8 @@
1
+ { "namespace": "org.fluentd.parser.avro",
2
+ "type": "record",
3
+ "name": "User",
4
+ "fields" : [
5
+ {"name": "username", "type": "string"},
6
+ {"name": "age", "type": "int"},
7
+ {"name": "verified", "type": ["boolean", "null"], "default": false}
8
+ ]}
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.expand_path("../../", __FILE__))
2
+ require "test-unit"
3
+ require "fluent/test"
4
+ require "fluent/test/driver/parser"
5
+ require "fluent/test/helpers"
6
+ require "webrick"
7
+
8
+ Test::Unit::TestCase.include(Fluent::Test::Helpers)
9
+ Test::Unit::TestCase.extend(Fluent::Test::Helpers)
@@ -0,0 +1,388 @@
1
+ require "helper"
2
+ require "fluent/plugin/parser_avro.rb"
3
+
4
+ class AvroParserTest < Test::Unit::TestCase
5
+ AVRO_REGISTRY_PORT = 8081
6
+
7
+ setup do
8
+ Fluent::Test.setup
9
+ end
10
+
11
+ SCHEMA = <<-JSON
12
+ { "namespace": "org.fluentd.parser.avro",
13
+ "type": "record",
14
+ "name": "User",
15
+ "fields" : [
16
+ {"name": "username", "type": "string"},
17
+ {"name": "age", "type": "int"},
18
+ {"name": "verified", "type": ["boolean", "null"], "default": false}
19
+ ]}
20
+ JSON
21
+
22
+ READERS_SCHEMA = <<-JSON
23
+ { "namespace": "org.fluentd.parser.avro",
24
+ "type": "record",
25
+ "name": "User",
26
+ "fields" : [
27
+ {"name": "username", "type": "string"},
28
+ {"name": "age", "type": "int"}
29
+ ]}
30
+ JSON
31
+
32
+ COMPLEX_SCHEMA = <<-EOC
33
+ {
34
+ "type" : "record",
35
+ "name" : "ComplexClass",
36
+ "namespace" : "org.fluentd.parser.avro.complex.example",
37
+ "fields" : [ {
38
+ "name" : "time",
39
+ "type" : "string"
40
+ }, {
41
+ "name" : "image",
42
+ "type" : {
43
+ "type" : "record",
44
+ "name" : "image",
45
+ "fields" : [ {
46
+ "name" : "src",
47
+ "type" : "string"
48
+ }, {
49
+ "name" : "mime_type",
50
+ "type" : "string"
51
+ }, {
52
+ "name" : "height",
53
+ "type" : "long"
54
+ }, {
55
+ "name" : "width",
56
+ "type" : "long"
57
+ }, {
58
+ "name" : "alignment",
59
+ "type" : "string"
60
+ } ]
61
+ }
62
+ }, {
63
+ "name" : "data",
64
+ "type" : {
65
+ "type" : "record",
66
+ "name" : "data",
67
+ "fields" : [ {
68
+ "name" : "size",
69
+ "type" : "long"
70
+ }, {
71
+ "name" : "hidden",
72
+ "type" : "boolean"
73
+ } ]
74
+ }
75
+ } ]
76
+ }
77
+ EOC
78
+
79
+ def test_parse
80
+ conf = {
81
+ 'schema_json' => SCHEMA
82
+ }
83
+ d = create_driver(conf)
84
+ datum = {"username" => "foo", "age" => 42, "verified" => true}
85
+ encoded = encode_datum(datum, SCHEMA)
86
+ d.instance.parse(encoded) do |_time, record|
87
+ assert_equal datum, record
88
+ end
89
+
90
+ datum = {"username" => "baz", "age" => 34}
91
+ encoded = encode_datum(datum, SCHEMA)
92
+ d.instance.parse(encoded) do |_time, record|
93
+ assert_equal datum.merge("verified" => nil), record
94
+ end
95
+ end
96
+
97
+ def test_parse_with_avro_schema
98
+ conf = {
99
+ 'schema_file' => File.join(__dir__, "..", "data", "user.avsc")
100
+ }
101
+ d = create_driver(conf)
102
+ datum = {"username" => "foo", "age" => 42, "verified" => true}
103
+ encoded = encode_datum(datum, SCHEMA)
104
+ d.instance.parse(encoded) do |_time, record|
105
+ assert_equal datum, record
106
+ end
107
+
108
+ datum = {"username" => "baz", "age" => 34}
109
+ encoded = encode_datum(datum, SCHEMA)
110
+ d.instance.parse(encoded) do |_time, record|
111
+ assert_equal datum.merge("verified" => nil), record
112
+ end
113
+ end
114
+
115
+ def test_parse_with_readers_and_writers_schema
116
+ conf = {
117
+ 'writers_schema_json' => SCHEMA,
118
+ 'readers_schema_json' => READERS_SCHEMA,
119
+ }
120
+ d = create_driver(conf)
121
+ datum = {"username" => "foo", "age" => 42, "verified" => true}
122
+ encoded = encode_datum(datum, SCHEMA)
123
+ d.instance.parse(encoded) do |_time, record|
124
+ datum.delete("verified")
125
+ assert_equal datum, record
126
+ end
127
+ end
128
+
129
+ def test_parse_with_readers_and_writers_schema_files
130
+ conf = {
131
+ 'writers_schema_file' => File.join(__dir__, "..", "data", "writer_user.avsc"),
132
+ 'readers_schema_file' => File.join(__dir__, "..", "data", "reader_user.avsc"),
133
+ }
134
+ d = create_driver(conf)
135
+ datum = {"username" => "foo", "age" => 42, "verified" => true}
136
+ encoded = encode_datum(datum, SCHEMA)
137
+ d.instance.parse(encoded) do |_time, record|
138
+ datum.delete("verified")
139
+ assert_equal datum, record
140
+ end
141
+ end
142
+
143
+ def test_parse_with_complex_schema
144
+ conf = {
145
+ 'schema_json' => COMPLEX_SCHEMA,
146
+ 'time_key' => 'time'
147
+ }
148
+ d = create_driver(conf)
149
+ time_str = "2020-09-25 15:08:09.082113 +0900"
150
+ datum = {
151
+ "time" => time_str,
152
+ "image" => {
153
+ "src" => "images/avroexam.png",
154
+ "mime_type"=> "image/png",
155
+ "height" => 320,
156
+ "width" => 280,
157
+ "alignment" => "center"
158
+ },
159
+ "data" => {
160
+ "size" => 36,
161
+ "hidden" => false
162
+ }
163
+ }
164
+
165
+ encoded = encode_datum(datum, COMPLEX_SCHEMA)
166
+ d.instance.parse(encoded) do |time, record|
167
+ assert_equal Time.parse(time_str).to_r, time.to_r
168
+ datum.delete("time")
169
+ assert_equal datum, record
170
+ end
171
+ end
172
+
173
+ class SchemaURLTest < self
174
+ teardown do
175
+ @dummy_server_thread.kill
176
+ @dummy_server_thread.join
177
+ end
178
+
179
+ setup do
180
+ @got = []
181
+ @dummy_server_thread = Thread.new do
182
+ server = WEBrick::HTTPServer.new({:BindAddress => '127.0.0.1', :Port => AVRO_REGISTRY_PORT})
183
+ begin
184
+ server.mount_proc('/') do |req,res|
185
+ res.status = 200
186
+ res.body = 'running'
187
+ end
188
+ server.mount_proc("/subjects") do |req, res|
189
+ req.path =~ /^\/subjects\/([^\/]*)\/([^\/]*)\/(.*)$/
190
+ avro_registered_name = $1
191
+ version = $3
192
+ @got.push({
193
+ registered_name: avro_registered_name,
194
+ version: version,
195
+ })
196
+ res.status = 200
197
+ if version == ""
198
+ res.body = '[1,2,3,4]'
199
+ elsif version == "1"
200
+ res.body = File.read(File.join(__dir__, "..", "data", "persons-avro-value.avsc"))
201
+ elsif version == "2"
202
+ res.body = File.read(File.join(__dir__, "..", "data", "persons-avro-value2.avsc"))
203
+ elsif version == "3"
204
+ res.body = File.read(File.join(__dir__, "..", "data", "persons-avro-value3.avsc"))
205
+ elsif version == "4"
206
+ res.body = File.read(File.join(__dir__, "..", "data", "persons-avro-value4.avsc"))
207
+ end
208
+ end
209
+ server.start
210
+ ensure
211
+ server.shutdown
212
+ end
213
+ end
214
+
215
+ # to wait completion of dummy server.start()
216
+ require 'thread'
217
+ condv = ConditionVariable.new
218
+ _watcher = Thread.new {
219
+ connected = false
220
+ while not connected
221
+ begin
222
+ Net::HTTP.start('localhost', AVRO_REGISTRY_PORT){|http|
223
+ http.get("/", {}).body
224
+ }
225
+ connected = true
226
+ rescue Errno::ECONNREFUSED
227
+ sleep 0.1
228
+ rescue StandardError => e
229
+ p e
230
+ sleep 0.1
231
+ end
232
+ end
233
+ condv.signal
234
+ }
235
+ mutex = Mutex.new
236
+ mutex.synchronize {
237
+ condv.wait(mutex)
238
+ }
239
+ end
240
+
241
+ REMOTE_SCHEMA = <<-EOC
242
+ {
243
+ "type": "record",
244
+ "name": "Person",
245
+ "namespace": "com.ippontech.kafkatutorials",
246
+ "fields": [
247
+ {
248
+ "name": "firstName",
249
+ "type": "string"
250
+ },
251
+ {
252
+ "name": "lastName",
253
+ "type": "string"
254
+ },
255
+ {
256
+ "name": "birthDate",
257
+ "type": "long"
258
+ }
259
+ ]
260
+ }
261
+ EOC
262
+ REMOTE_SCHEMA2 = <<-EOC
263
+ {
264
+ "type": "record",
265
+ "name": "Person",
266
+ "namespace": "com.ippontech.kafkatutorials",
267
+ "fields": [
268
+ {
269
+ "name": "firstName",
270
+ "type": "string"
271
+ },
272
+ {
273
+ "name": "lastName",
274
+ "type": "string"
275
+ },
276
+ {
277
+ "name": "birthDate",
278
+ "type": "long"
279
+ },
280
+ {
281
+ "name": "verified",
282
+ "type": [
283
+ "boolean",
284
+ "null"
285
+ ],
286
+ "default": false
287
+ }
288
+ ]
289
+ }
290
+ EOC
291
+
292
+ def test_dummy_server
293
+ conf = {
294
+ 'schema_url' => "http://localhost:8081/subjects/persons-avro-value/versions/1",
295
+ 'schema_url_key' => 'schema'
296
+ }
297
+ d = create_driver(conf)
298
+ d.instance.schema_url =~ /^http:\/\/([.:a-z0-9]+)\//
299
+ server = $1
300
+ host = server.split(':')[0]
301
+ port = server.split(':')[1].to_i
302
+ client = Net::HTTP.start(host, port)
303
+
304
+ assert_equal '200', client.request_get('/').code
305
+
306
+ assert_equal '200', client.request_get('/subjects/persons-avro-value/versions/1').code
307
+ # The first GET request is in #configure.
308
+ assert_equal 2, @got.size
309
+ assert_equal 'persons-avro-value', @got[1][:registered_name]
310
+ assert_equal '1', @got[1][:version]
311
+
312
+ assert_equal '200', client.request_get('/subjects/persons-avro-value/versions/').code
313
+ assert_equal 3, @got.size
314
+ assert_equal 'persons-avro-value', @got[2][:registered_name]
315
+ assert_equal '', @got[2][:version]
316
+
317
+ assert_equal '200', client.request_get('/subjects/persons-avro-value/versions/3').code
318
+ assert_equal 4, @got.size
319
+ assert_equal 'persons-avro-value', @got[3][:registered_name]
320
+ assert_equal '3', @got[3][:version]
321
+ end
322
+
323
+ def test_schema_url
324
+ conf = {
325
+ 'schema_url' => "http://localhost:8081/subjects/persons-avro-value/versions/1",
326
+ 'schema_url_key' => 'schema'
327
+ }
328
+ d = create_driver(conf)
329
+ datum = {"firstName" => "Aleen","lastName" => "Terry","birthDate" => 159202477258}
330
+ encoded = encode_datum(datum, REMOTE_SCHEMA)
331
+ d.instance.parse(encoded) do |_time, record|
332
+ assert_equal datum, record
333
+ end
334
+ end
335
+
336
+ def test_schema_url_with_version2
337
+ conf = {
338
+ 'schema_url' => "http://localhost:8081/subjects/persons-avro-value/versions/2",
339
+ 'schema_url_key' => 'schema'
340
+ }
341
+ d = create_driver(conf)
342
+ datum = {"firstName" => "Aleen","lastName" => "Terry","birthDate" => 159202477258}
343
+ encoded = encode_datum(datum, REMOTE_SCHEMA2)
344
+ d.instance.parse(encoded) do |_time, record|
345
+ assert_equal datum.merge("verified" => false), record
346
+ end
347
+ end
348
+
349
+ def test_schema_registery_with_subject_url
350
+ conf = {
351
+ 'schema_registery_with_subject_url' => "http://localhost:8081/subjects/persons-avro-value/",
352
+ 'schema_url_key' => 'schema'
353
+ }
354
+ d = create_driver(conf)
355
+ datum = {"firstName" => "Aleen","lastName" => "Terry","birthDate" => 159202477258}
356
+ encoded = encode_datum(datum, REMOTE_SCHEMA2)
357
+ d.instance.parse(encoded) do |_time, record|
358
+ assert_equal datum.merge("verified" => nil), record
359
+ end
360
+ end
361
+
362
+ def test_schema_registery_with_invalid_subject_url
363
+ conf = {
364
+ 'schema_registery_with_subject_url' => "http://localhost:8081/subjects/persons-avro-value",
365
+ 'schema_url_key' => 'schema'
366
+ }
367
+ assert_raise(Fluent::ConfigError) do
368
+ create_driver(conf)
369
+ end
370
+ end
371
+ end
372
+
373
+ private
374
+
375
+ def encode_datum(datum, string_schema)
376
+ buffer = StringIO.new
377
+ encoder = Avro::IO::BinaryEncoder.new(buffer)
378
+ schema = Avro::Schema.parse(string_schema)
379
+ writer = Avro::IO::DatumWriter.new(schema)
380
+ writer.write(datum, encoder)
381
+ buffer.rewind
382
+ buffer.read
383
+ end
384
+
385
+ def create_driver(conf)
386
+ Fluent::Test::Driver::Parser.new(Fluent::Plugin::AvroParser).configure(conf)
387
+ end
388
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-parser-avro
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroshi Hatake
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: avro
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: test-unit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: fluentd
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.14.10
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '2'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 0.14.10
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '2'
89
+ description: Avro parser plugin for Fluentd
90
+ email:
91
+ - cosmo0920.wp@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".github/workflows/linux.yml"
97
+ - ".github/workflows/windows.yml"
98
+ - ".gitignore"
99
+ - Gemfile
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - fluent-plugin-avro.gemspec
104
+ - lib/fluent/plugin/parser_avro.rb
105
+ - test/data/persons-avro-value.avsc
106
+ - test/data/persons-avro-value2.avsc
107
+ - test/data/persons-avro-value3.avsc
108
+ - test/data/persons-avro-value4.avsc
109
+ - test/data/reader_user.avsc
110
+ - test/data/user.avsc
111
+ - test/data/writer_user.avsc
112
+ - test/helper.rb
113
+ - test/plugin/test_parser_avro.rb
114
+ homepage: https://github.com/fluent-plugins-nerysery/fluent-plugin-parser-avro
115
+ licenses:
116
+ - Apache-2.0
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.1.2
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Avro parser plugin for Fluentd
137
+ test_files:
138
+ - test/data/persons-avro-value.avsc
139
+ - test/data/persons-avro-value2.avsc
140
+ - test/data/persons-avro-value3.avsc
141
+ - test/data/persons-avro-value4.avsc
142
+ - test/data/reader_user.avsc
143
+ - test/data/user.avsc
144
+ - test/data/writer_user.avsc
145
+ - test/helper.rb
146
+ - test/plugin/test_parser_avro.rb