ksql 0.1.0.beta
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +175 -0
- data/CHANGELOG.md +23 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +88 -0
- data/LICENSE.txt +21 -0
- data/README.md +319 -0
- data/Rakefile +12 -0
- data/examples/quickstart.rb +58 -0
- data/lib/generators/ksql.rb +19 -0
- data/lib/generators/templates/initializer.rb +6 -0
- data/lib/ksql/api/close_query.rb +26 -0
- data/lib/ksql/api/ksql.rb +34 -0
- data/lib/ksql/api/query.rb +32 -0
- data/lib/ksql/api/stream.rb +11 -0
- data/lib/ksql/client.rb +70 -0
- data/lib/ksql/collection.rb +48 -0
- data/lib/ksql/configuration.rb +3 -0
- data/lib/ksql/connection/client.rb +47 -0
- data/lib/ksql/connection/request.rb +27 -0
- data/lib/ksql/connection/response.rb +25 -0
- data/lib/ksql/error.rb +7 -0
- data/lib/ksql/handlers/collection.rb +22 -0
- data/lib/ksql/handlers/raw.rb +20 -0
- data/lib/ksql/handlers/stream.rb +19 -0
- data/lib/ksql/handlers/typed_row.rb +24 -0
- data/lib/ksql/stream.rb +89 -0
- data/lib/ksql/version.rb +5 -0
- data/lib/ksql.rb +38 -0
- data/sig/ksql.rbs +4 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2cd9a1d71cc892172f660cee8406014672b19bc7a510cf6897194201ab9fbf76
|
4
|
+
data.tar.gz: acef9c5aca159a91667816ef3b6c53c54bfa432e141bdff755cbad7ea7a3e986
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 603741e0d00a8bc9c8193473600b2314c9cebb4fbbf0de321127ce52d91d78cfe70710a45d6ec1721e80109811645f207186b2c3aedab967c5485771e822bac6
|
7
|
+
data.tar.gz: 3e50792e2df0bca4ccf27fb8991ff727099d761284bff71303390fdaa23233ea12756dc92fda170d72ddcdfb4bb3112360c88658ca90a8a4f8f97ddbda58b5ea
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
# Allow longer lines and methods
|
2
|
+
Layout/LineLength:
|
3
|
+
Max: 240
|
4
|
+
|
5
|
+
Metrics/MethodLength:
|
6
|
+
Max: 30
|
7
|
+
|
8
|
+
# Tests are declarative, no block length test
|
9
|
+
Metrics/BlockLength:
|
10
|
+
IgnoredMethods: ['describe', 'context', 'namespace']
|
11
|
+
|
12
|
+
AllCops:
|
13
|
+
TargetRubyVersion: 2.5
|
14
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
15
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
16
|
+
DisabledByDefault: true
|
17
|
+
Exclude:
|
18
|
+
|
19
|
+
# Prefer &&/|| over and/or.
|
20
|
+
Style/AndOr:
|
21
|
+
Enabled: true
|
22
|
+
|
23
|
+
# Align `when` with `case`.
|
24
|
+
Layout/CaseIndentation:
|
25
|
+
Enabled: true
|
26
|
+
|
27
|
+
# Align comments with method definitions.
|
28
|
+
Layout/CommentIndentation:
|
29
|
+
Enabled: true
|
30
|
+
|
31
|
+
Layout/ElseAlignment:
|
32
|
+
Enabled: true
|
33
|
+
|
34
|
+
# Align `end` with the matching keyword or starting expression except for
|
35
|
+
# assignments, where it should be aligned with the LHS.
|
36
|
+
Layout/EndAlignment:
|
37
|
+
Enabled: true
|
38
|
+
EnforcedStyleAlignWith: variable
|
39
|
+
AutoCorrect: true
|
40
|
+
|
41
|
+
Layout/EmptyLineAfterMagicComment:
|
42
|
+
Enabled: true
|
43
|
+
|
44
|
+
# In a regular class definition, no empty lines around the body.
|
45
|
+
Layout/EmptyLinesAroundClassBody:
|
46
|
+
Enabled: true
|
47
|
+
|
48
|
+
# In a regular method definition, no empty lines around the body.
|
49
|
+
Layout/EmptyLinesAroundMethodBody:
|
50
|
+
Enabled: true
|
51
|
+
|
52
|
+
# In a regular module definition, no empty lines around the body.
|
53
|
+
Layout/EmptyLinesAroundModuleBody:
|
54
|
+
Enabled: true
|
55
|
+
|
56
|
+
Layout/FirstArgumentIndentation:
|
57
|
+
Enabled: true
|
58
|
+
|
59
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
60
|
+
Style/HashSyntax:
|
61
|
+
Enabled: true
|
62
|
+
|
63
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
64
|
+
# extra level of indentation.
|
65
|
+
Layout/IndentationConsistency:
|
66
|
+
Enabled: true
|
67
|
+
EnforcedStyle: indented_internal_methods
|
68
|
+
|
69
|
+
# Two spaces, no tabs (for indentation).
|
70
|
+
Layout/IndentationWidth:
|
71
|
+
Enabled: true
|
72
|
+
|
73
|
+
Layout/LeadingCommentSpace:
|
74
|
+
Enabled: true
|
75
|
+
|
76
|
+
Layout/SpaceAfterColon:
|
77
|
+
Enabled: true
|
78
|
+
|
79
|
+
Layout/SpaceAfterComma:
|
80
|
+
Enabled: true
|
81
|
+
|
82
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
83
|
+
Enabled: true
|
84
|
+
|
85
|
+
Layout/SpaceAroundKeyword:
|
86
|
+
Enabled: true
|
87
|
+
|
88
|
+
Layout/SpaceAroundOperators:
|
89
|
+
Enabled: true
|
90
|
+
|
91
|
+
Layout/SpaceBeforeComma:
|
92
|
+
Enabled: true
|
93
|
+
|
94
|
+
Layout/SpaceBeforeFirstArg:
|
95
|
+
Enabled: true
|
96
|
+
|
97
|
+
Style/DefWithParentheses:
|
98
|
+
Enabled: true
|
99
|
+
|
100
|
+
# Defining a method with parameters needs parentheses.
|
101
|
+
Style/MethodDefParentheses:
|
102
|
+
Enabled: true
|
103
|
+
|
104
|
+
# Style/FrozenStringLiteralComment:
|
105
|
+
# Enabled: true
|
106
|
+
# EnforcedStyle: always
|
107
|
+
# Exclude:
|
108
|
+
# - 'actionview/test/**/*.builder'
|
109
|
+
# - 'actionview/test/**/*.ruby'
|
110
|
+
# - 'actionpack/test/**/*.builder'
|
111
|
+
# - 'actionpack/test/**/*.ruby'
|
112
|
+
# - 'activestorage/db/migrate/**/*.rb'
|
113
|
+
# - 'db/migrate/**/*.rb'
|
114
|
+
# - 'db/*.rb'
|
115
|
+
|
116
|
+
# Use `foo {}` not `foo{}`.
|
117
|
+
Layout/SpaceBeforeBlockBraces:
|
118
|
+
Enabled: true
|
119
|
+
|
120
|
+
# Use `foo { bar }` not `foo {bar}`.
|
121
|
+
Layout/SpaceInsideBlockBraces:
|
122
|
+
Enabled: true
|
123
|
+
|
124
|
+
# Use `{ a: 1 }` not `{a:1}`.
|
125
|
+
Layout/SpaceInsideHashLiteralBraces:
|
126
|
+
Enabled: true
|
127
|
+
|
128
|
+
Layout/SpaceInsideParens:
|
129
|
+
Enabled: true
|
130
|
+
|
131
|
+
# Check quotes usage according to lint rule below.
|
132
|
+
Style/StringLiterals:
|
133
|
+
Enabled: true
|
134
|
+
EnforcedStyle: single_quotes
|
135
|
+
|
136
|
+
# Detect hard tabs, no hard tabs.
|
137
|
+
Layout/IndentationStyle:
|
138
|
+
Enabled: true
|
139
|
+
|
140
|
+
# Blank lines should not have any spaces.
|
141
|
+
Layout/TrailingEmptyLines:
|
142
|
+
Enabled: true
|
143
|
+
|
144
|
+
# No trailing whitespace.
|
145
|
+
Layout/TrailingWhitespace:
|
146
|
+
Enabled: true
|
147
|
+
|
148
|
+
# Use quotes for string literals when they are enough.
|
149
|
+
Style/RedundantPercentQ:
|
150
|
+
Enabled: true
|
151
|
+
|
152
|
+
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
153
|
+
Lint/RequireParentheses:
|
154
|
+
Enabled: true
|
155
|
+
|
156
|
+
Lint/RedundantStringCoercion:
|
157
|
+
Enabled: true
|
158
|
+
|
159
|
+
# Supports --auto-correct
|
160
|
+
Style/RedundantSelf:
|
161
|
+
Description: Don't use self where it's not needed.
|
162
|
+
StyleGuide: https://github.com/rubocop-hq/ruby-style-guide#no-self-unless-required
|
163
|
+
Enabled: true
|
164
|
+
|
165
|
+
Style/RedundantReturn:
|
166
|
+
Enabled: true
|
167
|
+
AllowMultipleReturnValues: true
|
168
|
+
|
169
|
+
Style/Semicolon:
|
170
|
+
Enabled: true
|
171
|
+
AllowAsExpressionSeparator: true
|
172
|
+
|
173
|
+
# Prefer Foo.method over Foo::method
|
174
|
+
Style/ColonMethodCall:
|
175
|
+
Enabled: true
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
## [Releases]
|
2
|
+
|
3
|
+
## [0.1.0.beta] - 2022-02-24
|
4
|
+
|
5
|
+
- Beta release
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Authentication header in config
|
10
|
+
- `/close-query` endpoint support
|
11
|
+
- `/ksql` endpoint support
|
12
|
+
- `/query-stream` endpoint support
|
13
|
+
|
14
|
+
## [0.1.0] - 2022-02-21 (Yanked)
|
15
|
+
|
16
|
+
- First release
|
17
|
+
|
18
|
+
### Added
|
19
|
+
|
20
|
+
- Statements
|
21
|
+
- Push Queries
|
22
|
+
- Pull Queries
|
23
|
+
- Close Queries
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ksql (0.1.0)
|
5
|
+
activesupport (>= 5)
|
6
|
+
hashie (~> 4)
|
7
|
+
net-http2 (~> 0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (7.0.1)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 1.6, < 2)
|
15
|
+
minitest (>= 5.1)
|
16
|
+
tzinfo (~> 2.0)
|
17
|
+
addressable (2.8.0)
|
18
|
+
public_suffix (>= 2.0.2, < 5.0)
|
19
|
+
ast (2.4.2)
|
20
|
+
byebug (11.1.3)
|
21
|
+
concurrent-ruby (1.1.9)
|
22
|
+
crack (0.4.5)
|
23
|
+
rexml
|
24
|
+
diff-lcs (1.5.0)
|
25
|
+
hashdiff (1.0.1)
|
26
|
+
hashie (4.1.0)
|
27
|
+
http-2 (0.11.0)
|
28
|
+
i18n (1.9.1)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
minitest (5.15.0)
|
31
|
+
net-http2 (0.18.4)
|
32
|
+
http-2 (~> 0.11)
|
33
|
+
parallel (1.21.0)
|
34
|
+
parser (3.0.3.2)
|
35
|
+
ast (~> 2.4.1)
|
36
|
+
public_suffix (4.0.6)
|
37
|
+
rainbow (3.0.0)
|
38
|
+
rake (13.0.6)
|
39
|
+
regexp_parser (2.2.0)
|
40
|
+
rexml (3.2.5)
|
41
|
+
rspec (3.10.0)
|
42
|
+
rspec-core (~> 3.10.0)
|
43
|
+
rspec-expectations (~> 3.10.0)
|
44
|
+
rspec-mocks (~> 3.10.0)
|
45
|
+
rspec-core (3.10.2)
|
46
|
+
rspec-support (~> 3.10.0)
|
47
|
+
rspec-expectations (3.10.2)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.10.0)
|
50
|
+
rspec-mocks (3.10.2)
|
51
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
+
rspec-support (~> 3.10.0)
|
53
|
+
rspec-support (3.10.3)
|
54
|
+
rubocop (1.24.0)
|
55
|
+
parallel (~> 1.10)
|
56
|
+
parser (>= 3.0.0.0)
|
57
|
+
rainbow (>= 2.2.2, < 4.0)
|
58
|
+
regexp_parser (>= 1.8, < 3.0)
|
59
|
+
rexml
|
60
|
+
rubocop-ast (>= 1.15.0, < 2.0)
|
61
|
+
ruby-progressbar (~> 1.7)
|
62
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
63
|
+
rubocop-ast (1.15.0)
|
64
|
+
parser (>= 3.0.1.1)
|
65
|
+
ruby-progressbar (1.11.0)
|
66
|
+
tzinfo (2.0.4)
|
67
|
+
concurrent-ruby (~> 1.0)
|
68
|
+
unicode-display_width (2.1.0)
|
69
|
+
vcr (6.0.0)
|
70
|
+
webmock (3.14.0)
|
71
|
+
addressable (>= 2.8.0)
|
72
|
+
crack (>= 0.3.2)
|
73
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
74
|
+
|
75
|
+
PLATFORMS
|
76
|
+
x86_64-darwin-20
|
77
|
+
|
78
|
+
DEPENDENCIES
|
79
|
+
byebug (~> 11)
|
80
|
+
ksql!
|
81
|
+
rake (~> 13)
|
82
|
+
rspec (~> 3)
|
83
|
+
rubocop (~> 1)
|
84
|
+
vcr (~> 6)
|
85
|
+
webmock (~> 3)
|
86
|
+
|
87
|
+
BUNDLED WITH
|
88
|
+
2.3.7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Lapo Elisacci
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,319 @@
|
|
1
|
+
![][ruby-shield]
|
2
|
+
|
3
|
+
#  Ruby ksqlDB Client (Under QA)
|
4
|
+
|
5
|
+

|
6
|
+

|
7
|
+

|
8
|
+

|
9
|
+
|
10
|
+
KSQL is a [ksqlDB](https://ksqldb.io/) Ruby client that focuses on ease of use. Supports all recent ksqlDB features and does not have any heavyweight dependencies.
|
11
|
+
|
12
|
+
## What is ksqlDB?
|
13
|
+
|
14
|
+
ksqlDB is a database purpose-built for [Apache Kafka®](https://kafka.apache.org/) streams processing applications, more details [here](https://ksqldb.io/).
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'ksql'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle install
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install ksql
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
The gem allows you to perform requests to ksqlDB REST API.
|
35
|
+
Checkout the ksqlDB official documentation [here](https://docs.ksqldb.io/en/latest/developer-guide/api/).
|
36
|
+
|
37
|
+
### Configuration
|
38
|
+
|
39
|
+
The gem requires a minimum configuration to connect to ksqlDB, it is shipped with a built-in generator to create a Rails initializer.
|
40
|
+
|
41
|
+
|
42
|
+
$ rails generate ksql
|
43
|
+
|
44
|
+
```Ruby
|
45
|
+
Ksql.configure do |config|
|
46
|
+
config.host = 'http://localhost:8088' # Required
|
47
|
+
# config.auth = 'user:password' # optional
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
**The Client supports Basic Authentication only**
|
52
|
+
|
53
|
+
### [Statements](https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-rest-api/ksql-endpoint/)
|
54
|
+
|
55
|
+
All statements, except those starting with `SELECT` can be run with the `ksql` method:
|
56
|
+
|
57
|
+
```Ruby
|
58
|
+
Ksql::Client.ksql("SHOW TABLES;")
|
59
|
+
|
60
|
+
# Or
|
61
|
+
|
62
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude) VALUES ('18f4ea86', 37.3903, -122.0643);")
|
63
|
+
```
|
64
|
+
|
65
|
+
You can also pass optional `properties` to the method (Check the official documentation if needed).
|
66
|
+
|
67
|
+
```Ruby
|
68
|
+
Ksql::Client.ksql("INSERT INTO ...", command_sequence_number: 42, streams_properties: { ... }, session_variables: { ... })
|
69
|
+
```
|
70
|
+
|
71
|
+
As well as custom `Headers`.
|
72
|
+
|
73
|
+
```Ruby
|
74
|
+
Ksql::Client.ksql("INSERT INTO ...", headers: { ... })
|
75
|
+
```
|
76
|
+
|
77
|
+
## [Queries](https://docs.ksqldb.io/en/latest/concepts/queries)
|
78
|
+
|
79
|
+
ksqlDB has three kinds of Queries:
|
80
|
+
|
81
|
+
### [Persistent Query](https://docs.ksqldb.io/en/latest/concepts/queries/#persistent)
|
82
|
+
|
83
|
+
`Persistent queries` are server-side queries and there's nothing much to say here.
|
84
|
+
|
85
|
+
In case you want to close a `Persistent` Query you can do so with the `close_query` method, passing the Query ID:
|
86
|
+
|
87
|
+
```Ruby
|
88
|
+
Ksql::Client.close_query("CTAS_RIDERSNEARMOUNTAINVIEW_5")
|
89
|
+
```
|
90
|
+
|
91
|
+
**WARNING:** There's a known issue here, read [below](#ksqldb-close-query).
|
92
|
+
|
93
|
+
### [Push Query](https://docs.ksqldb.io/en/latest/concepts/queries/#push)
|
94
|
+
|
95
|
+
`Push` queries enable you to query a stream or materialized table with a subscription to the results.
|
96
|
+
They run asynchronously and you can spot them as they include the `EMIT` keyword at the end of the SQL statement.
|
97
|
+
|
98
|
+
To define a `Push` query connection simply call the `stream` method:
|
99
|
+
|
100
|
+
```Ruby
|
101
|
+
stream = Ksql::Client.stream("SELECT * FROM riderLocations EMIT CHANGES;")
|
102
|
+
```
|
103
|
+
|
104
|
+
You can specify what action to take when an exception gets raised:
|
105
|
+
|
106
|
+
```Ruby
|
107
|
+
stream.on_error do |e|
|
108
|
+
puts e.message
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
or what to do when the connection gets closed:
|
113
|
+
|
114
|
+
```Ruby
|
115
|
+
stream.on_close do
|
116
|
+
puts 'Session closed!'
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
To start the stream, call the `start` method, It accepts a block to execute each time a message gets recieved:
|
121
|
+
|
122
|
+
```Ruby
|
123
|
+
stream.start do |location|
|
124
|
+
# The streaming events get wrapped inside an ORM-like Class
|
125
|
+
puts location.latitude
|
126
|
+
puts location.longitude
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
**CAREFUL:** The block gets executed inside a separated Thread, make sure your code is thread safe!
|
131
|
+
|
132
|
+
You can close the connection by calling the `close` method:
|
133
|
+
|
134
|
+
```Ruby
|
135
|
+
stream.close
|
136
|
+
```
|
137
|
+
|
138
|
+
Example:
|
139
|
+
|
140
|
+
```Ruby
|
141
|
+
# Define the Stream
|
142
|
+
stream = Ksql::Client.stream("SELECT * FROM riderLocations EMIT CHANGES;")
|
143
|
+
|
144
|
+
# Start the connection
|
145
|
+
stream.start do |location|
|
146
|
+
# This code will get executed inside a separated Thread
|
147
|
+
puts location.latitude
|
148
|
+
puts location.longitude
|
149
|
+
end
|
150
|
+
|
151
|
+
# Do something
|
152
|
+
sleep(10)
|
153
|
+
|
154
|
+
# Close the connection
|
155
|
+
stream.close
|
156
|
+
```
|
157
|
+
|
158
|
+
You can also specify custom `Properties` as well as `Headers` and `Session Variables`:
|
159
|
+
|
160
|
+
```Ruby
|
161
|
+
stream = Ksql::Client.stream("SELECT * FROM riderLocations EMIT CHANGES;", headers: { ... }, properties: { ... }, session_variables: { ... })
|
162
|
+
# ...
|
163
|
+
```
|
164
|
+
|
165
|
+
### [Pull Query](https://docs.ksqldb.io/en/latest/concepts/queries/#push)
|
166
|
+
|
167
|
+
`Pull` queries are the most "traditional" ones, they run synchronously and they can be executed with the `query` method"
|
168
|
+
|
169
|
+
```Ruby
|
170
|
+
locations = Ksql::Client.query("SELECT * FROM riderLocations;")
|
171
|
+
```
|
172
|
+
|
173
|
+
An Enumerable collection of ORM-like Objects is returned.
|
174
|
+
Iteration methods are available on the collection:
|
175
|
+
|
176
|
+
```Ruby
|
177
|
+
locations.each do |location|
|
178
|
+
# do something
|
179
|
+
end
|
180
|
+
|
181
|
+
locations.map do |location|
|
182
|
+
# do something
|
183
|
+
end
|
184
|
+
|
185
|
+
locations.count
|
186
|
+
```
|
187
|
+
|
188
|
+
## Example
|
189
|
+
|
190
|
+
The following example is from the official ksqlDB [Quickstart](https://ksqldb.io/quickstart.html):
|
191
|
+
|
192
|
+
```Ruby
|
193
|
+
require 'ksql'
|
194
|
+
require 'logger'
|
195
|
+
|
196
|
+
logger = Logger.new(STDOUT)
|
197
|
+
logger.level = Logger::INFO
|
198
|
+
|
199
|
+
# Create a stream
|
200
|
+
|
201
|
+
Ksql::Client.ksql("CREATE STREAM riderLocations (profileId VARCHAR, latitude DOUBLE, longitude DOUBLE)
|
202
|
+
WITH (kafka_topic='locations', value_format='json', partitions=1);")
|
203
|
+
|
204
|
+
# Create materialized views
|
205
|
+
|
206
|
+
Ksql::Client.ksql("CREATE TABLE currentLocation AS
|
207
|
+
SELECT profileId,
|
208
|
+
LATEST_BY_OFFSET(latitude) AS la,
|
209
|
+
LATEST_BY_OFFSET(longitude) AS lo
|
210
|
+
FROM riderlocations
|
211
|
+
GROUP BY profileId
|
212
|
+
EMIT CHANGES;")
|
213
|
+
|
214
|
+
# Create materialized views
|
215
|
+
|
216
|
+
Ksql::Client.ksql("CREATE TABLE ridersNearMountainView AS
|
217
|
+
SELECT ROUND(GEO_DISTANCE(la, lo, 37.4133, -122.1162), -1) AS distanceInMiles,
|
218
|
+
COLLECT_LIST(profileId) AS riders,
|
219
|
+
COUNT(*) AS count
|
220
|
+
FROM currentLocation
|
221
|
+
GROUP BY ROUND(GEO_DISTANCE(la, lo, 37.4133, -122.1162), -1);")
|
222
|
+
|
223
|
+
# Run a push query over the stream
|
224
|
+
|
225
|
+
stream = Ksql::Client.stream("SELECT * FROM riderLocations
|
226
|
+
WHERE GEO_DISTANCE(latitude, longitude, 37.4133, -122.1162) <= 5 EMIT CHANGES;")
|
227
|
+
|
228
|
+
# Handle exceptions
|
229
|
+
|
230
|
+
stream.on_error do |e|
|
231
|
+
logger.error(e.message)
|
232
|
+
end
|
233
|
+
|
234
|
+
# Handle stream close
|
235
|
+
|
236
|
+
stream.on_close do
|
237
|
+
logger.info("Stream closed!")
|
238
|
+
end
|
239
|
+
|
240
|
+
# Start the stream
|
241
|
+
|
242
|
+
stream.start do |location|
|
243
|
+
# Print the result into a file
|
244
|
+
File.open('output.log', 'a') do |f|
|
245
|
+
f.puts "Latitude: #{location.latitude}, Longitude: #{location.longitude}"
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
# Populate the stream with events
|
250
|
+
|
251
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude)
|
252
|
+
VALUES ('c2309eec', 37.7877, -122.4205);")
|
253
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude)
|
254
|
+
VALUES ('18f4ea86', 37.3903, -122.0643);")
|
255
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude)
|
256
|
+
VALUES ('4ab5cbad', 37.3952, -122.0813);")
|
257
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude)
|
258
|
+
VALUES ('8b6eae59', 37.3944, -122.0813);")
|
259
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude)
|
260
|
+
VALUES ('4a7c7b41', 37.4049, -122.0822);")
|
261
|
+
Ksql::Client.ksql("INSERT INTO riderLocations (profileId, latitude, longitude)
|
262
|
+
VALUES ('4ddad000', 37.7857, -122.4011);")
|
263
|
+
|
264
|
+
# Run a Pull query against the materialized view
|
265
|
+
|
266
|
+
locations = Ksql::Client.query("SELECT * from ridersNearMountainView
|
267
|
+
WHERE distanceInMiles <= 10;")
|
268
|
+
|
269
|
+
# Close the stream
|
270
|
+
|
271
|
+
stream.close
|
272
|
+
|
273
|
+
# Drop
|
274
|
+
|
275
|
+
Ksql::Client.ksql('DROP TABLE IF EXISTS ridersNearMountainView;')
|
276
|
+
Ksql::Client.ksql('DROP TABLE IF EXISTS currentLocation;')
|
277
|
+
Ksql::Client.ksql('DROP STREAM IF EXISTS riderLocations;')
|
278
|
+
|
279
|
+
```
|
280
|
+
|
281
|
+
## Supported ksqlDB versions
|
282
|
+
|
283
|
+
| Version | |
|
284
|
+
| ------ | -- |
|
285
|
+
| 0.23.1 | :heavy_check_mark: Supported |
|
286
|
+
| 0.22.0 | :heavy_check_mark: Supported |
|
287
|
+
| 0.21.0 | :heavy_check_mark: Supported |
|
288
|
+
| 0.20.0 | :heavy_check_mark: Supported |
|
289
|
+
| 0.19.0 | :heavy_check_mark: Supported |
|
290
|
+
| 0.18.0 | :heavy_check_mark: Supported |
|
291
|
+
| 0.17.0 | :x: Untested |
|
292
|
+
| Older | :x: Untested |
|
293
|
+
|
294
|
+
## Known issues
|
295
|
+
|
296
|
+
### ksqlDB close-query
|
297
|
+
|
298
|
+
Although it actually works, at the moment, the latest release of ksqlDB returns an error each time you request the `/close-query` endpoint.
|
299
|
+
Therefore the query will correctly get closed but an error will get returned anyways.
|
300
|
+
|
301
|
+
Official issue [here](https://github.com/confluentinc/ksql/issues/8251).
|
302
|
+
|
303
|
+
## Development
|
304
|
+
|
305
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rspec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
306
|
+
|
307
|
+
Make sure you have a working instance of ksqlDB, you can find the official quickstart [here](https://ksqldb.io/quickstart.html).
|
308
|
+
|
309
|
+
## Contributing
|
310
|
+
|
311
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/LapoElisacci/ksql.
|
312
|
+
|
313
|
+
## License
|
314
|
+
|
315
|
+
[](https://opensource.org/licenses/MIT)
|
316
|
+
|
317
|
+
<!--- MARKDOWN LINKS --->
|
318
|
+
|
319
|
+
[ruby-shield]: https://img.shields.io/badge/Ruby-CC342D?style=for-the-badge&logo=ruby&logoColor=white
|