|
| 1 | +# ksqlDB-JS |
| 2 | + |
| 3 | +<div align="center"> |
| 4 | + |
| 5 | +<a href="https://github.com/oslabs-beta/ksqljs"><img src="https://img.shields.io/badge/license-MIT-blue"/></a> |
| 6 | +<a href="https://github.com/oslabs-beta/ksqljs/stargazers"><img alt="GitHub stars" src="https://img.shields.io/github/stars/oslabs-beta/ksqljs"></a> |
| 7 | +<a href="https://github.com/oslabs-beta/ksqljs/issues"><img alt="GitHub issues" src="https://img.shields.io/github/issues/oslabs-beta/ksqljs"></a> |
| 8 | +<img alt="GitHub last commit" src="https://img.shields.io/github/last-commit/oslabs-beta/ksqljs"> |
| 9 | + |
| 10 | + <p align="center"> <strong>A native Node.js client for ksqlDB</strong></p> |
| 11 | + </div> |
| 12 | + |
| 13 | +## Table of Contents |
| 14 | + |
| 15 | +- [About the project](#about) |
| 16 | +- [Getting Started](#getting-started) |
| 17 | + - [Usage](#usage) |
| 18 | +- [Features](#features) |
| 19 | +- [Developers](#developers) |
| 20 | +- [Contributions](#contributions) |
| 21 | +- [License](#license) |
| 22 | + |
| 23 | +## <a name="about"></a> About the Project |
| 24 | + |
| 25 | +ksqlDB-JS - a ksqlDB client for Node.js |
| 26 | + |
| 27 | +### Prerequisites |
| 28 | + |
| 29 | +> Node.js - https://nodejs.org/en/ |
| 30 | +> |
| 31 | +> ksqlDB - https://ksqldb.io/ |
| 32 | +> |
| 33 | +> Docker (for tests) -https://www.docker.com/ |
| 34 | +
|
| 35 | +## <a name="getting-started"></a> Getting Started |
| 36 | + |
| 37 | +Install package from Node package manager |
| 38 | + |
| 39 | +``` |
| 40 | +npm install ksqldb-js |
| 41 | +``` |
| 42 | + |
| 43 | +### <a name="usage"></a> Usage |
| 44 | + |
| 45 | +Create a client in the application file: |
| 46 | + |
| 47 | +``` |
| 48 | +const ksqldb = require('ksqldb-js'); |
| 49 | +const client = new ksqldb({ksqldbURL: '<url to ksqlDB server>'}) |
| 50 | +``` |
| 51 | + |
| 52 | +To run tests initiate Docker containers included in yaml file: |
| 53 | + |
| 54 | +``` |
| 55 | +docker-compose up |
| 56 | +npm test |
| 57 | +``` |
| 58 | + |
| 59 | +## <a name="features"></a> Features |
| 60 | + |
| 61 | +- #### Create a pull query |
| 62 | + |
| 63 | +``` |
| 64 | +client.pull("SELECT * FROM myTable;"); |
| 65 | +``` |
| 66 | + |
| 67 | +- #### Create a push query |
| 68 | + |
| 69 | +``` |
| 70 | +client.push('SELECT * FROM myTable EMIT CHANGES;', |
| 71 | + (data) => { |
| 72 | + console.log(data); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +- #### Terminate persistent query |
| 77 | + e.g. a push query |
| 78 | + |
| 79 | +``` |
| 80 | +client.terminate(queryId); |
| 81 | +``` |
| 82 | + |
| 83 | +- #### Insert rows of data into a stream |
| 84 | + |
| 85 | +``` |
| 86 | +client.insertStream('myTable', [ |
| 87 | + { "name": "jack", "email": "123@mail.com", "age": 25 }, |
| 88 | + { "name": "john", "email": "456@mail.com", "age": 20 } |
| 89 | +]); |
| 90 | +``` |
| 91 | + |
| 92 | +- #### List streams/queries |
| 93 | + |
| 94 | +``` |
| 95 | +client.ksql('LIST STREAMS;'); |
| 96 | +``` |
| 97 | + |
| 98 | +- #### Create table/streams |
| 99 | + |
| 100 | +``` |
| 101 | +client.createStream('testStream', |
| 102 | + columnsType = ['name VARCHAR', 'email varchar', 'age INTEGER'], |
| 103 | + topic = 'testTopic', |
| 104 | + value_format = 'json', |
| 105 | + partitions = 1); |
| 106 | +``` |
| 107 | + |
| 108 | +- #### For custom SQL statements including complex joins use the .ksql method |
| 109 | + |
| 110 | +``` |
| 111 | +client.ksql('DROP STREAM IF EXISTS testStream;'); |
| 112 | +``` |
| 113 | + |
| 114 | +- #### SQL Query builder |
| 115 | + |
| 116 | +Please use the built-in query builder to parametrize any SQL query to avoid SQL injection. |
| 117 | + |
| 118 | +``` |
| 119 | +const builder = new queryBuilder(); |
| 120 | +const query = 'SELECT * FROM table WHERE id = ? AND size = ?'; |
| 121 | +const finishedQuery = builder.build(query, 123, "middle"); |
| 122 | +
|
| 123 | +client.ksql(finishedQuery); |
| 124 | +``` |
| 125 | + |
| 126 | +- #### Create table as |
| 127 | + |
| 128 | +Generating a materialized view that can be |
| 129 | + |
| 130 | +``` |
| 131 | +client.createTableAs('testTable', 'sourceStream', selectArray = ['name', 'LATEST_BY_OFFSET(age) AS recentAge'], |
| 132 | + propertiesObj = {topic:'newTestTopic'}, |
| 133 | + conditionsObj = {WHERE: 'age >= 21', GROUP_BY: 'name'}); |
| 134 | +``` |
| 135 | + |
| 136 | +- #### Create stream as |
| 137 | + |
| 138 | +``` |
| 139 | +client.createStreamAs('testStream', selectColumns = ['name', 'age'], 'sourceStream', |
| 140 | + propertiesObj = { |
| 141 | + kafka_topic: 'testTopic', |
| 142 | + value_format: 'json', |
| 143 | + partitions: 1 |
| 144 | + }, |
| 145 | + conditions = 'age > 50'); |
| 146 | +``` |
| 147 | + |
| 148 | +- #### Pull from to |
| 149 | + |
| 150 | +Pull stream data between two time points |
| 151 | + |
| 152 | +``` |
| 153 | +client.pullFromTo('TESTSTREAM', 'America/Los_Angeles', |
| 154 | + from = ['2022-01-01', '00', '00', '00'], |
| 155 | + to = ['2022-01-01', '00', '00', '00']); |
| 156 | +``` |
| 157 | + |
| 158 | +- #### Troubleshooting methods (.inspectServerStatus, .inspectQueryStatus, .inspectClusterStatus ) |
| 159 | + |
| 160 | +## <a name="developers"></a> Developers |
| 161 | + |
| 162 | +- [Javan Ang](https://github.com/javanang) |
| 163 | +- [Gerry Bong](https://github.com/ggbong734) |
| 164 | +- [Jonathan Luu](https://github.com/jonathanluu17) |
| 165 | +- [Michael Snyder](https://github.com/MichaelCSnyder) |
| 166 | +- [Matthew Xing](https://github.com/Aengil) |
| 167 | + |
| 168 | +## <a name="contributions"></a> Contributions |
| 169 | + |
| 170 | +Contributions to the code, examples, documentation, etc. are very much appreciated. |
| 171 | + |
| 172 | +- Please report issues and bugs directly in this [GitHub project](https://github.com/oslabs-beta/ksqljs/issues). |
| 173 | + |
| 174 | +## <a name="license"></a> License |
| 175 | + |
| 176 | +This product is licensed under the MIT License - see the LICENSE.md file for details. |
| 177 | + |
| 178 | +This is an open source product. |
| 179 | + |
| 180 | +This product is accelerated by OS Labs. |
| 181 | + |
| 182 | +ksqlDB is licensed under the [Confluent Community License](https://github.com/confluentinc/ksql/blob/master/LICENSE). |
| 183 | + |
| 184 | +_Apache, Apache Kafka, Kafka, and associated open source project names are trademarks of the [Apache Software Foundation](https://www.apache.org/)_. |
0 commit comments