https://github.com/seanmorris/ksqlc
The Asynchronous PHP KSQL Connector
https://github.com/seanmorris/ksqlc
async kafka ksql ksqldb php php7 php8 streaming
Last synced: 8 months ago
JSON representation
The Asynchronous PHP KSQL Connector
- Host: GitHub
- URL: https://github.com/seanmorris/ksqlc
- Owner: seanmorris
- License: apache-2.0
- Created: 2020-05-16T22:44:38.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-25T15:41:54.000Z (over 2 years ago)
- Last Synced: 2025-08-10T22:30:38.346Z (11 months ago)
- Topics: async, kafka, ksql, ksqldb, php, php7, php8, streaming
- Language: PHP
- Homepage: http://docs.ksqlc.seanmorr.is
- Size: 895 KB
- Stars: 13
- Watchers: 2
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# SeanMorris/Ksqlc
/*keɪ ɛs kyu ɛl si*/ • The Asynchronous PHP KSQL Connector
Ksqlc provides a PHP interface to Confluent KSQL & Apache Kafka.
Ksqlc is free for distribution, modification, and use under the [Apache-2.0 license](https://raw.githubusercontent.com/seanmorris/ksqlc/master/LICENSE).
[Docs](http://ksqlc.seanmorr.is/) | [Github](http://github.com/seanmorris/ksqlc) | [Packagist](https://packagist.org/packages/seanmorris/ksqlc)
[](http://finalbossoftheinternet.com/) [](https://github.com/seanmorris/ksqlc/blob/master/LICENSE) [](https://circleci.com/gh/seanmorris/ksqlc/) [](https://github.com/seanmorris/ksqlc) [](https://packagist.org/packages/seanmorris/ksqlc) [](https://codecov.io/gh/seanmorris/ksqlc) [](https://app.circleci.com/pipelines/github/seanmorris/ksqlc?branch=master)
### Supports PHP 7.0 - PHP 8.3!
## Installation
Install via the [composer cli](https://getcomposer.org/doc/03-cli.md#require):
```sh
$ composer require seanmorris/ksqlc
```
... or add `seanmorris/ksqlc` to your [`composer.json`](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup):
```json
"require": {
"seanmorris/ksqlc": "dev-master"
}
```
## Usage
#### Open a connection
Grab the URL to your KSQL server's REST endpoint, and use it to create a new `Ksqlc` object to begin:
```php
stream('SELECT * FROM EVENT_STREAM EMIT CHANGES');
foreach($stream as $row)
{
// $row == {"ROWKEY": "XXX", "ROWTIME": "YYY", ...}
if($row->property === 'something')
{
break;
}
}
unset($stream);
```
### Ksqlc::multiplex() - Stream Mutliple Queries
You can loop over multiple queries at once with `Ksqlc::multiplex()`. Each parameter to this method represents either a string query or a list of parameters to send to `Ksqlc::stream()`.
```php
multiplex(
[$queryOne, 'earliest'],
[$queryTwo, 'earliest']
);
foreach($stream as $row)
{
/* Stream processing... */
}
```
#### Limits
Queries with limits will terminate when the given number of rows have been iterated.
Multiplexed queries will terminate when all limits have been reached.
```php
stream('SELECT * FROM EVENT_STREAM EMIT CHANGES LIMIT 20');
foreach($stream as $row)
{
/* Stream processing... */
}
```
#### Offset Reset
Streaming queries will **ONLY** select new records by default. Use the second param to `Ksqlc::stream()` to process all records from the beginning of time.
```php
stream($queryString, 'earliest'); ## process everything
$stream = $ksqlc->stream($queryString, 'latest'); ## process new records
```
#### Full asyncronicity
Passing `TRUE` to the third parameter of `Ksqlc::stream()` allows you to turn on full asyncronous mode.
In this example, the foreach loop will spin indefinitely until the query returns 20 records and completes. If there is no data to process, a stream of `NULL`'s will be supplied. This allows you to tend to other, unrelated streams in the same loop, or even break the loop and resume processing later on.
```php
stream($queryString, 'latest', TRUE);
foreach($stream as $row)
{
var_dump($row);
}
```
### Ksqlc::run() - Run a KSQL statment
You'll do things like create or drop tables and streams with this method. Any statement that isnt a direct `SELECT` should be passed to `Ksqlc::run()`.
Ksqlc::run will return an iterable object of results with metadata properties:
```php
run('SHOW TABLES');
var_dump( $results );
// object SeanMorris\Ksqlc\Result {
// $type => "tables"
// $warnings => {}
// $statementText => "SHOW TABLES"
// }
foreach($results as $table)
{
var_dump( $table );
// object stdClass {
// $type => "TABLE"
// $name => "event_table"
// $topic => "event_table"
// $format => "JSON"
// $isWindowed => false
// }
}
```
You can also use list destructuring to get the results of multiple queries all at once:
```php
run('SHOW STREAMS', 'SHOW TABLES');
foreach($streams as $stream)
{
// ...
}
foreach($tables as $table)
{
// ...
}
```
### SeanMorris/Ksqlc
Copyright 2020 - 2024 Sean Morris
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* https://raw.githubusercontent.com/seanmorris/ksqlc/master/LICENSE
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.