fluent-plugin-bigquery-storage-write 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95c405b96c77e504c30be911ef3821b2e4ff98ed817f142feb0eb927aa61b0f5
4
+ data.tar.gz: 779be744e6a644f6e257f86a9840f3ce65ddef0b88325ddd3bec4723f9985f34
5
+ SHA512:
6
+ metadata.gz: 8299958843ee11bfaaa2c06c15362173fd0c942e85e6aaa36873ac985b7669f876bcb107a72ee0ebd313b273b5da34d9f93e7e50161668fdc6bdc1fd194f35ec
7
+ data.tar.gz: 8c0c2293d91382ae521c1572c57999256407f8754ee838ec81b1b1adacf65c927589db9b0af977f7f0a54b994c3842a1c0774e2a05b4c2d32af17c78fce0cfaf
@@ -0,0 +1,27 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ branches: [ master ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby-version: ['2.7', '3.0', '3.1', '3.2']
16
+
17
+ steps:
18
+ - uses: actions/checkout@v2
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby-version }}
22
+ - name: Run tests
23
+ run: |
24
+ ruby -v
25
+ gem install bundler rake
26
+ bundle install --jobs 4 --retry 3
27
+ bundle exec rake test
data/.gitignore ADDED
@@ -0,0 +1,60 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ # Custom
59
+ .idea/
60
+ .vscode/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fluent-plugin-bigquery-storage-write (0.1.0)
5
+ fluentd (>= 0.14.10, < 2)
6
+ google-cloud-bigquery-storage (>= 1.3.0)
7
+ googleauth (>= 1.5.2)
8
+ grpc (>= 1.55)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.8.4)
14
+ public_suffix (>= 2.0.2, < 6.0)
15
+ concurrent-ruby (1.2.2)
16
+ cool.io (1.7.1)
17
+ faraday (2.7.6)
18
+ faraday-net_http (>= 2.0, < 3.1)
19
+ ruby2_keywords (>= 0.0.4)
20
+ faraday-net_http (3.0.2)
21
+ faraday-retry (2.2.0)
22
+ faraday (~> 2.0)
23
+ fluentd (1.16.1)
24
+ bundler
25
+ cool.io (>= 1.4.5, < 2.0.0)
26
+ http_parser.rb (>= 0.5.1, < 0.9.0)
27
+ msgpack (>= 1.3.1, < 2.0.0)
28
+ serverengine (>= 2.3.2, < 3.0.0)
29
+ sigdump (~> 0.2.2)
30
+ strptime (>= 0.2.4, < 1.0.0)
31
+ tzinfo (>= 1.0, < 3.0)
32
+ tzinfo-data (~> 1.0)
33
+ webrick (~> 1.4)
34
+ yajl-ruby (~> 1.0)
35
+ gapic-common (0.19.1)
36
+ faraday (>= 1.9, < 3.a)
37
+ faraday-retry (>= 1.0, < 3.a)
38
+ google-protobuf (~> 3.14)
39
+ googleapis-common-protos (>= 1.3.12, < 2.a)
40
+ googleapis-common-protos-types (>= 1.3.1, < 2.a)
41
+ googleauth (~> 1.0)
42
+ grpc (~> 1.36)
43
+ google-cloud-bigquery-storage (1.3.0)
44
+ google-cloud-bigquery-storage-v1 (>= 0.8, < 2.a)
45
+ google-cloud-core (~> 1.6)
46
+ google-cloud-bigquery-storage-v1 (0.19.1)
47
+ gapic-common (>= 0.19.1, < 2.a)
48
+ google-cloud-errors (~> 1.0)
49
+ google-cloud-core (1.6.0)
50
+ google-cloud-env (~> 1.0)
51
+ google-cloud-errors (~> 1.0)
52
+ google-cloud-env (1.6.0)
53
+ faraday (>= 0.17.3, < 3.0)
54
+ google-cloud-errors (1.3.1)
55
+ google-protobuf (3.23.2-arm64-darwin)
56
+ googleapis-common-protos (1.4.0)
57
+ google-protobuf (~> 3.14)
58
+ googleapis-common-protos-types (~> 1.2)
59
+ grpc (~> 1.27)
60
+ googleapis-common-protos-types (1.6.0)
61
+ google-protobuf (~> 3.14)
62
+ googleauth (1.5.2)
63
+ faraday (>= 0.17.3, < 3.a)
64
+ jwt (>= 1.4, < 3.0)
65
+ memoist (~> 0.16)
66
+ multi_json (~> 1.11)
67
+ os (>= 0.9, < 2.0)
68
+ signet (>= 0.16, < 2.a)
69
+ grpc (1.55.0)
70
+ google-protobuf (~> 3.23)
71
+ googleapis-common-protos-types (~> 1.0)
72
+ grpc-tools (1.55.0)
73
+ http_parser.rb (0.8.0)
74
+ jwt (2.7.1)
75
+ memoist (0.16.2)
76
+ msgpack (1.7.1)
77
+ multi_json (1.15.0)
78
+ os (1.1.4)
79
+ power_assert (1.1.7)
80
+ public_suffix (5.0.1)
81
+ rake (13.0.1)
82
+ ruby2_keywords (0.0.5)
83
+ serverengine (2.3.2)
84
+ sigdump (~> 0.2.2)
85
+ sigdump (0.2.4)
86
+ signet (0.17.0)
87
+ addressable (~> 2.8)
88
+ faraday (>= 0.17.5, < 3.a)
89
+ jwt (>= 1.5, < 3.0)
90
+ multi_json (~> 1.10)
91
+ strptime (0.2.5)
92
+ test-unit (3.3.4)
93
+ power_assert
94
+ tzinfo (2.0.6)
95
+ concurrent-ruby (~> 1.0)
96
+ tzinfo-data (1.2023.3)
97
+ tzinfo (>= 1.0.0)
98
+ webrick (1.6.1)
99
+ yajl-ruby (1.4.3)
100
+
101
+ PLATFORMS
102
+ arm64-darwin-22
103
+
104
+ DEPENDENCIES
105
+ bundler
106
+ fluent-plugin-bigquery-storage-write!
107
+ grpc-tools (>= 1.55)
108
+ rake
109
+ test-unit
110
+
111
+ BUNDLED WITH
112
+ 2.4.13
data/LICENSE ADDED
@@ -0,0 +1,177 @@
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
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # fluent-plugin-bigquery-storage-write
2
+
3
+ [Fluentd](https://fluentd.org/) output plugin to insert data into BigQuery through storage write api.
4
+
5
+ ## Overview
6
+
7
+ Google Cloud Bigquery output plugin for [Fluentd](https://fluentd.org/).
8
+ The main difference from [fluent-plugin-bigquery](https://github.com/fluent-plugins-nursery/fluent-plugin-bigquery) is that it uses BigQuery new API `Storage Write`.
9
+
10
+ Advantages of using the Storage Write API are described [here](https://cloud.google.com/bigquery/docs/write-api#advantages).
11
+
12
+ ## Installation
13
+
14
+ ### RubyGems
15
+
16
+ ```sh
17
+ gem install fluent-plugin-bigquery-storage-write
18
+ ```
19
+
20
+ ### Bundler
21
+
22
+ Add following line to your Gemfile:
23
+
24
+ ```ruby
25
+ gem "fluent-plugin-bigquery-storage-write"
26
+ ```
27
+
28
+ And then execute:
29
+
30
+ ```sh
31
+ bundle
32
+ ```
33
+
34
+ ## Configuration
35
+
36
+ ### bigquery_storage_write_insert
37
+
38
+ | name | type | required? | default | description |
39
+ |:---------------------------|:-------|:---------------|:--------------------|:-------------------------------------------------------------------------------------------------------------|
40
+ | auth_method | enum | yes | application_default | `json_key` or `compute_engine` or `application_default` |
41
+ | json_key | string | yes (json_key) | nil | GCP JSON Key file path or JSON Key string |
42
+ | project | string | yes | nil | |
43
+ | dataset | string | yes | nil | |
44
+ | table | string | yes | nil | |
45
+ | proto_schema_rb_path | string | yes | nil | Generated Protocol Buffers schema .rb file path. |
46
+ | proto_message_class_name | string | no | nil | Class name of Protocol Buffers message. If not specified, table value that converted to pascal case is used. |
47
+
48
+ ### buffer section
49
+
50
+ | name | type | required? | default | description |
51
+ |:----------------------------|:--------|:----------|:---------|:-----------------------------------|
52
+ | @type | string | no | memory | |
53
+ | chunk_limit_size | integer | no | 1MB | |
54
+ | total_limit_size | integer | no | 1GB | |
55
+ | chunk_records_limit | integer | no | 500 | |
56
+ | flush_mode | enum | no | interval | default, lazy, interval, immediate |
57
+ | flush_interval | float | no | 1.0 | |
58
+ | flush_thread_interval | float | no | 0.05 | |
59
+ | flush_thread_burst_interval | float | no | 0.05 | |
60
+
61
+ And, other params (defined by base class) are available
62
+
63
+ see. https://github.com/fluent/fluentd/blob/master/lib/fluent/plugin/output.rb
64
+
65
+ ## Examples
66
+
67
+ First, you have to generate Protocol Buffers compiled code to serialize data.
68
+ Write code `.proto` and compile it using `protoc`.
69
+ The sample code with BigQuery schema is located in the path below `proto/test_data.proto`.
70
+
71
+ ```sh
72
+ bundle exec grpc_tools_ruby_protoc -I proto --ruby_out=proto proto/test_data.proto
73
+ ```
74
+
75
+ Next, specify generated ruby code path to fluentd configuration file.
76
+
77
+ ```
78
+ <match test>
79
+ @type bigquery_storage_write_insert
80
+
81
+ auth_method application_default
82
+
83
+ project sample-project
84
+ dataset test
85
+ table data
86
+
87
+ proto_schema_rb_path /your/generated/code/path/here/test_data_pb.rb
88
+ proto_message_class_name Data
89
+ </match>
90
+ ```
91
+
92
+ ## Tips
93
+
94
+ - Can I dynamically retrieve and use the BigQuery table schema?
95
+ - No, you have to use predefined schema generated from `protoc`.
96
+ - Also, you have to create BigQuery table before using this plugin.
97
+ - Where is the type conversions between Protocol Buffers and BigQuery?
98
+ - https://cloud.google.com/bigquery/docs/write-api#data_type_conversions
99
+ - Note that some types, including google.protobuf.Timestamp, are not available due to [BigQuery limitation](https://github.com/googleapis/python-bigquery-storage/issues/257).
100
+
101
+ ## Copyright
102
+
103
+ * Copyright(c) 2023 gumigumi4f
104
+ * License
105
+ * Apache License, Version 2.0
106
+ * This plugin includes some code from [fluent-plugin-bigquery](https://github.com/fluent-plugins-nursery/fluent-plugin-bigquery) for compatibility.
data/Rakefile ADDED
@@ -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]
data/conf/fluentd.conf ADDED
@@ -0,0 +1,39 @@
1
+ <source>
2
+ @type sample
3
+ sample {"bool_test":true,"float_test":1.2,"geography_test":"POINT(1 2)","integer_test":1,"json_test":"{\"test1\":1,\"test2\":2}","numeric_test":2,"string_test":"test","repeated_field_test":[1,2,3],"record_test":{"name":"foo","value":"bar"}}
4
+ tag test
5
+ </source>
6
+
7
+ <filter test>
8
+ @type record_transformer
9
+ enable_ruby
10
+ <record>
11
+ date_test ${time.to_i / (24 * 60 * 60) rescue nil}
12
+ datetime_test ${time.strftime('%Y-%m-%d %H:%M:%S') rescue nil}
13
+ time_test ${time.strftime('%H:%M:%S') rescue nil}
14
+ timestamp_test ${(time.to_f * 1_000_000).to_i rescue nil}
15
+ </record>
16
+ </filter>
17
+
18
+ <match test>
19
+ @type bigquery_storage_write_insert
20
+
21
+ auth_method application_default
22
+
23
+ project sample-project
24
+ dataset test
25
+ table data
26
+
27
+ proto_schema_rb_path proto/test_data_pb.rb
28
+ proto_message_class_name Data
29
+
30
+ <buffer>
31
+ @type memory
32
+
33
+ flush_at_shutdown true
34
+ flush_mode interval
35
+ flush_interval 1
36
+ flush_thread_interval 1
37
+ flush_thread_burst_interval 1
38
+ </buffer>
39
+ </match>
@@ -0,0 +1,31 @@
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-bigquery-storage-write"
6
+ spec.version = "0.1.0"
7
+ spec.authors = ["gumigumi4f"]
8
+ spec.email = ["gumigumi4f@gmail.com"]
9
+
10
+ spec.summary = %q{Fluentd output plugin to insert data into BigQuery through storage write api}
11
+ spec.description = %q{Fluentd plugin to insert data into BigQuery}
12
+ spec.homepage = "https://github.com/gumigumi4f/fluent-plugin-bigquery-storage-write"
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_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "test-unit"
26
+ spec.add_development_dependency "grpc-tools", ">= 1.55"
27
+ spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"]
28
+ spec.add_runtime_dependency "grpc", ">= 1.55"
29
+ spec.add_runtime_dependency "googleauth", ">= 1.5.2"
30
+ spec.add_runtime_dependency "google-cloud-bigquery-storage", ">= 1.3.0"
31
+ end
@@ -0,0 +1,25 @@
1
+ module Fluent
2
+ module BigQuery
3
+ module Storage
4
+ module Helper
5
+ class << self
6
+ def snake_to_pascal(snake_case)
7
+ words = snake_case.split('_')
8
+ words.map(&:capitalize).join
9
+ end
10
+
11
+ def get_descriptor_data(file_path)
12
+ File.foreach(file_path) do |line|
13
+ if line.start_with?("descriptor_data = ")
14
+ data = line.chomp.gsub(/^descriptor_data = /, '')
15
+ return eval(data)
16
+ end
17
+ end
18
+
19
+ raise "No descriptor data found in file. path=#{file_path}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,79 @@
1
+ module Fluent
2
+ module BigQuery
3
+ module Storage
4
+ class Writer
5
+ def initialize(log, auth_method, project, dataset, table, proto_descriptor, **options)
6
+ @auth_method = auth_method
7
+ @scope = "https://www.googleapis.com/auth/bigquery"
8
+ @options = options
9
+ @log = log
10
+
11
+ @write_stream = "projects/#{project}/datasets/#{dataset}/tables/#{table}/streams/_default"
12
+ @write_schema = Google::Cloud::Bigquery::Storage::V1::ProtoSchema.new(
13
+ proto_descriptor: proto_descriptor
14
+ )
15
+ end
16
+
17
+ def client
18
+ @client ||= Google::Cloud::Bigquery::Storage::V1::BigQueryWrite::Client.new.tap do |cl|
19
+ cl.configure.credentials = get_auth
20
+ end
21
+ end
22
+
23
+ def insert(rows)
24
+ data = [
25
+ Google::Cloud::Bigquery::Storage::V1::AppendRowsRequest.new(
26
+ write_stream: @write_stream,
27
+ proto_rows: Google::Cloud::Bigquery::Storage::V1::AppendRowsRequest::ProtoData.new(
28
+ rows: Google::Cloud::Bigquery::Storage::V1::ProtoRows.new(
29
+ serialized_rows: rows
30
+ ),
31
+ writer_schema: @write_schema
32
+ )
33
+ )
34
+ ]
35
+ client.append_rows(data).map do |e|
36
+ @log.trace(e)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def get_auth
43
+ case @auth_method
44
+ when :compute_engine
45
+ get_auth_from_compute_engine
46
+ when :json_key
47
+ get_auth_from_json_key
48
+ when :application_default
49
+ get_auth_from_application_default
50
+ else
51
+ raise ConfigError, "Unknown auth method: #{@auth_method}"
52
+ end
53
+ end
54
+
55
+ def get_auth_from_compute_engine
56
+ Google::Auth::GCECredentials.new
57
+ end
58
+
59
+ def get_auth_from_json_key
60
+ json_key = @options[:json_key]
61
+
62
+ begin
63
+ JSON.parse(json_key)
64
+ key = StringIO.new(json_key)
65
+ Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: key, scope: @scope)
66
+ rescue JSON::ParserError
67
+ File.open(json_key) do |f|
68
+ Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: f, scope: @scope)
69
+ end
70
+ end
71
+ end
72
+
73
+ def get_auth_from_application_default
74
+ Google::Auth.get_application_default([@scope])
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,109 @@
1
+ require "fluent/plugin/output"
2
+
3
+ require "fluent/plugin/bigquery/storage/writer"
4
+ require "fluent/plugin/bigquery/storage/helper"
5
+
6
+ require 'google/protobuf'
7
+ require "google/cloud/bigquery/storage/v1"
8
+ require 'googleauth'
9
+
10
+ module Fluent
11
+ module Plugin
12
+ class BigQueryStorageWriteInsertOutput < Fluent::Plugin::Output
13
+ Fluent::Plugin.register_output("bigquery_storage_write_insert", self)
14
+
15
+ helpers :inject
16
+
17
+ config_param :auth_method, :enum, list: [:compute_engine, :json_key, :application_default], default: :application_default
18
+ config_param :json_key, default: nil, secret: true
19
+
20
+ config_param :project, :string
21
+ config_param :dataset, :string
22
+ config_param :table, :string
23
+
24
+ config_param :proto_schema_rb_path, :string
25
+ config_param :proto_message_class_name, :string, default: nil
26
+
27
+ config_section :buffer do
28
+ config_set_default :@type, "memory"
29
+ config_set_default :flush_mode, :interval
30
+ config_set_default :flush_interval, 1
31
+ config_set_default :flush_thread_interval, 0.05
32
+ config_set_default :flush_thread_burst_interval, 0.05
33
+ config_set_default :chunk_limit_size, 1 * 1024 ** 2 # 1MB
34
+ config_set_default :total_limit_size, 1 * 1024 ** 3 # 1GB
35
+ config_set_default :chunk_limit_records, 500
36
+ end
37
+
38
+ def configure(conf)
39
+ super
40
+
41
+ case @auth_method
42
+ when :compute_engine
43
+ # Do nothing
44
+ when :json_key
45
+ unless @json_key
46
+ raise Fluent::ConfigError, "'json_key' must be specified if auth_method == 'json_key'"
47
+ end
48
+ when :application_default
49
+ # Do nothing
50
+ else
51
+ raise Fluent::ConfigError, "unrecognized 'auth_method': #{@auth_method}"
52
+ end
53
+ end
54
+
55
+ def start
56
+ super
57
+
58
+ message_cls_name = @proto_message_class_name
59
+ if message_cls_name.nil?
60
+ message_cls_name = Fluent::BigQuery::Storage::Helper.snake_to_pascal(@table)
61
+ end
62
+
63
+ descriptor_data = Fluent::BigQuery::Storage::Helper.get_descriptor_data(@proto_schema_rb_path)
64
+
65
+ Google::Protobuf::DescriptorPool.generated_pool.add_serialized_file(descriptor_data)
66
+ parsed = Google::Protobuf::FileDescriptorProto.decode(descriptor_data)
67
+ @descriptor_proto = parsed.message_type.find { |msg| msg.name == message_cls_name }
68
+ if @descriptor_proto.nil?
69
+ raise "No descriptor proto found. class_name=#{message_cls_name}"
70
+ end
71
+ @klass = Google::Protobuf::DescriptorPool.generated_pool.lookup(message_cls_name).msgclass
72
+
73
+ @writer = Fluent::BigQuery::Storage::Writer.new(@log, @auth_method, @project, @dataset, @table, @descriptor_proto,
74
+ json_key: @json_key)
75
+ rescue => e
76
+ log.error("initialize error")
77
+ raise Fluent::UnrecoverableError, e
78
+ end
79
+
80
+ def format(tag, time, record)
81
+ if record.nil?
82
+ log.warn("nil record detected. corrupted chunks? tag=#{tag}, time=#{time}")
83
+ return
84
+ end
85
+
86
+ record = inject_values_to_record(tag, time, record)
87
+ record.to_json + "\n"
88
+ rescue
89
+ log.error("format error", record: record)
90
+ raise
91
+ end
92
+
93
+ def write(chunk)
94
+ rows = chunk.open do |io|
95
+ io.map do |line|
96
+ val = @klass.decode_json(line, ignore_unknown_fields: true)
97
+ @klass.encode(val)
98
+ end
99
+ end
100
+
101
+ @writer.insert(rows)
102
+ end
103
+
104
+ def multi_workers_ready?
105
+ true
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,105 @@
1
+ syntax = "proto2";
2
+
3
+ message Data {
4
+ message Record {
5
+ optional string name = 1;
6
+ optional string value = 2;
7
+ }
8
+
9
+ optional bool bool_test = 1;
10
+ optional int32 date_test = 2;
11
+ optional string datetime_test = 3;
12
+ optional double float_test = 4;
13
+ optional string geography_test = 5;
14
+ optional int64 integer_test = 6;
15
+ optional string json_test = 7;
16
+ optional int64 numeric_test = 8;
17
+ optional string string_test = 9;
18
+ optional string time_test = 10;
19
+ optional int64 timestamp_test = 11;
20
+ repeated int64 repeated_field_test = 12;
21
+ optional Record record_test = 13;
22
+ }
23
+
24
+ // Below is the schema on BigQuery
25
+ /*
26
+ [
27
+ {
28
+ "name": "bool_test",
29
+ "type": "BOOLEAN",
30
+ "mode": "NULLABLE"
31
+ },
32
+ {
33
+ "name": "date_test",
34
+ "type": "DATE",
35
+ "mode": "NULLABLE"
36
+ },
37
+ {
38
+ "name": "datetime_test",
39
+ "type": "DATETIME",
40
+ "mode": "NULLABLE"
41
+ },
42
+ {
43
+ "name": "float_test",
44
+ "type": "FLOAT",
45
+ "mode": "NULLABLE"
46
+ },
47
+ {
48
+ "name": "geography_test",
49
+ "type": "GEOGRAPHY",
50
+ "mode": "NULLABLE"
51
+ },
52
+ {
53
+ "name": "integer_test",
54
+ "type": "INTEGER",
55
+ "mode": "NULLABLE"
56
+ },
57
+ {
58
+ "name": "json_test",
59
+ "type": "JSON",
60
+ "mode": "NULLABLE"
61
+ },
62
+ {
63
+ "name": "numeric_test",
64
+ "type": "NUMERIC",
65
+ "mode": "NULLABLE"
66
+ },
67
+ {
68
+ "name": "string_test",
69
+ "type": "STRING",
70
+ "mode": "NULLABLE"
71
+ },
72
+ {
73
+ "name": "time_test",
74
+ "type": "TIME",
75
+ "mode": "NULLABLE"
76
+ },
77
+ {
78
+ "name": "timestamp_test",
79
+ "type": "TIMESTAMP",
80
+ "mode": "NULLABLE"
81
+ },
82
+ {
83
+ "name": "repeated_field_test",
84
+ "type": "INTEGER",
85
+ "mode": "REPEATED"
86
+ },
87
+ {
88
+ "name": "record_test",
89
+ "type": "RECORD",
90
+ "mode": "NULLABLE",
91
+ "fields": [
92
+ {
93
+ "name": "name",
94
+ "type": "STRING",
95
+ "mode": "NULLABLE"
96
+ },
97
+ {
98
+ "name": "value",
99
+ "type": "STRING",
100
+ "mode": "NULLABLE"
101
+ }
102
+ ]
103
+ }
104
+ ]
105
+ */
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+ # Generated by the protocol buffer compiler. DO NOT EDIT!
3
+ # source: test_data.proto
4
+
5
+ require 'google/protobuf'
6
+
7
+
8
+ descriptor_data = "\n\x0ftest_data.proto\"\xd5\x02\n\x04\x44\x61ta\x12\x11\n\tbool_test\x18\x01 \x01(\x08\x12\x11\n\tdate_test\x18\x02 \x01(\x05\x12\x15\n\rdatetime_test\x18\x03 \x01(\t\x12\x12\n\nfloat_test\x18\x04 \x01(\x01\x12\x16\n\x0egeography_test\x18\x05 \x01(\t\x12\x14\n\x0cinteger_test\x18\x06 \x01(\x03\x12\x11\n\tjson_test\x18\x07 \x01(\t\x12\x14\n\x0cnumeric_test\x18\x08 \x01(\x03\x12\x13\n\x0bstring_test\x18\t \x01(\t\x12\x11\n\ttime_test\x18\n \x01(\t\x12\x16\n\x0etimestamp_test\x18\x0b \x01(\x03\x12\x1b\n\x13repeated_field_test\x18\x0c \x03(\x03\x12!\n\x0brecord_test\x18\r \x01(\x0b\x32\x0c.Data.Record\x1a%\n\x06Record\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\r\n\x05value\x18\x02 \x01(\t"
9
+
10
+ pool = Google::Protobuf::DescriptorPool.generated_pool
11
+
12
+ begin
13
+ pool.add_serialized_file(descriptor_data)
14
+ rescue TypeError => e
15
+ # Compatibility code: will be removed in the next major version.
16
+ require 'google/protobuf/descriptor_pb'
17
+ parsed = Google::Protobuf::FileDescriptorProto.decode(descriptor_data)
18
+ parsed.clear_dependency
19
+ serialized = parsed.class.encode(parsed)
20
+ file = pool.add_serialized_file(serialized)
21
+ warn "Warning: Protobuf detected an import path issue while loading generated file #{__FILE__}"
22
+ imports = [
23
+ ]
24
+ imports.each do |type_name, expected_filename|
25
+ import_file = pool.lookup(type_name).file_descriptor
26
+ if import_file.name != expected_filename
27
+ warn "- #{file.name} imports #{expected_filename}, but that import was loaded as #{import_file.name}"
28
+ end
29
+ end
30
+ warn "Each proto file must use a consistent fully-qualified name."
31
+ warn "This will become an error in the next major version."
32
+ end
33
+
34
+ Data = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Data").msgclass
35
+ Data::Record = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("Data.Record").msgclass
data/test/helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "test-unit"
2
+ require "fluent/test"
3
+ require "fluent/test/driver/output"
4
+ require "fluent/test/helpers"
5
+
6
+ Test::Unit::TestCase.include(Fluent::Test::Helpers)
7
+ Test::Unit::TestCase.extend(Fluent::Test::Helpers)
@@ -0,0 +1,51 @@
1
+ require "helper"
2
+ require "fluent/plugin/out_bigquery_storage_write_insert.rb"
3
+
4
+ class BigQueryStorageWriteInsertOutputTest < Test::Unit::TestCase
5
+ CONFIG = %[
6
+ @type bigquery_storage_write_insert
7
+
8
+ auth_method application_default
9
+
10
+ project sample-project
11
+ dataset test
12
+ table data
13
+
14
+ proto_schema_rb_path /path/to/schema_rb
15
+ proto_message_class_name Test
16
+ ]
17
+
18
+ setup do
19
+ Fluent::Test.setup
20
+ end
21
+
22
+ sub_test_case 'configure' do
23
+ test 'all params are configured' do
24
+ d = create_driver(%[
25
+ auth_method json_key
26
+ json_key test
27
+
28
+ project sample-project
29
+ dataset test
30
+ table data
31
+
32
+ proto_schema_rb_path /path/to/schema_rb
33
+ proto_message_class_name Test
34
+ ])
35
+
36
+ assert_equal(:json_key, d.instance.auth_method)
37
+ assert_equal('test', d.instance.json_key)
38
+ assert_equal('sample-project', d.instance.project)
39
+ assert_equal('test', d.instance.dataset)
40
+ assert_equal('data', d.instance.table)
41
+ assert_equal('/path/to/schema_rb', d.instance.proto_schema_rb_path)
42
+ assert_equal('Test', d.instance.proto_message_class_name)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def create_driver(conf = CONFIG)
49
+ Fluent::Test::Driver::Output.new(Fluent::Plugin::BigQueryStorageWriteInsertOutput).configure(conf)
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,180 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent-plugin-bigquery-storage-write
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - gumigumi4f
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-15 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: '0'
20
+ type: :development
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: 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: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: grpc-tools
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.55'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.55'
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
+ - !ruby/object:Gem::Dependency
90
+ name: grpc
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '1.55'
96
+ type: :runtime
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '1.55'
103
+ - !ruby/object:Gem::Dependency
104
+ name: googleauth
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 1.5.2
110
+ type: :runtime
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: 1.5.2
117
+ - !ruby/object:Gem::Dependency
118
+ name: google-cloud-bigquery-storage
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 1.3.0
124
+ type: :runtime
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: 1.3.0
131
+ description: Fluentd plugin to insert data into BigQuery
132
+ email:
133
+ - gumigumi4f@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".github/workflows/test.yml"
139
+ - ".gitignore"
140
+ - Gemfile
141
+ - Gemfile.lock
142
+ - LICENSE
143
+ - README.md
144
+ - Rakefile
145
+ - conf/fluentd.conf
146
+ - fluent-plugin-bigquery-storage-write.gemspec
147
+ - lib/fluent/plugin/bigquery/storage/helper.rb
148
+ - lib/fluent/plugin/bigquery/storage/writer.rb
149
+ - lib/fluent/plugin/out_bigquery_storage_write_insert.rb
150
+ - proto/test_data.proto
151
+ - proto/test_data_pb.rb
152
+ - test/helper.rb
153
+ - test/plugin/test_out_bigquery_storage_write_insert.rb
154
+ homepage: https://github.com/gumigumi4f/fluent-plugin-bigquery-storage-write
155
+ licenses:
156
+ - Apache-2.0
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubygems_version: 3.1.6
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Fluentd output plugin to insert data into BigQuery through storage write
177
+ api
178
+ test_files:
179
+ - test/helper.rb
180
+ - test/plugin/test_out_bigquery_storage_write_insert.rb