The database purpose-built for stream processing applications.
ksqlDBksqlDB is a database for building stream processing applications on top of Apache Kafka. It is distributed, scalable, reliable, and real-time. ksqlDB combines the power of real-time stream processing with the approachable feel of a relational database through a familiar, lightweight SQL syntax. ksqlDB offers these core primitives:
Composing these powerful primitives enables you to build a complete streaming app with just SQL statements, minimizing complexity and operational overhead. ksqlDB supports a wide range of operations including aggregations, joins, windowing, sessionization, and much more. You can find more ksqlDB tutorials and resources here.
See the ksqlDB documentation for the latest stable release.
ksqlDB allows you to define materialized views over your streams and tables. Materialized views are defined by what is known as a "persistent query". These queries are known as persistent because they maintain their incrementally updated results using a table.
CREATE TABLE hourly_metrics AS
SELECT url, COUNT(*)
FROM page_views
WINDOW TUMBLING (SIZE 1 HOUR)
GROUP BY url EMIT CHANGES;
Results may be "pulled" from materialized views on demand via SELECT queries. The following query will return a single row:
SELECT * FROM hourly_metrics
WHERE url = 'http://myurl.com' AND WINDOWSTART = '2019-11-20T19:00';
Results may also be continuously "pushed" to clients via streaming SELECT queries. The following streaming query will push to the client all incremental changes made to the materialized view:
SELECT * FROM hourly_metrics EMIT CHANGES;
Streaming queries will run perpetually until they are explicitly terminated.
Apache Kafka is a popular choice for powering data pipelines. ksqlDB makes it simple to transform data within the pipeline, readying messages to cleanly land in another system.
CREATE STREAM vip_actions AS
SELECT userid, page, action
FROM clickstream c
LEFT JOIN users u ON c.userid = u.user_id
WHERE u.level = 'Platinum' EMIT CHANGES;
ksqlDB is a good fit for identifying patterns or anomalies on real-time data. By processing the stream as data arrives you can identify and properly surface out of the ordinary events with millisecond latency.
CREATE TABLE possible_fraud AS
SELECT card_number, count(*)
FROM authorization_attempts
WINDOW TUMBLING (SIZE 5 SECONDS)
GROUP BY card_number
HAVING count(*) > 3 EMIT CHANGES;
Kafka's ability to provide scalable ordered records with stream processing make it a common solution for log data monitoring and alerting. ksqlDB lends a familiar syntax for tracking, understanding, and managing alerts.
CREATE TABLE error_counts AS
SELECT error_code, count(*)
FROM monitoring_stream
WINDOW TUMBLING (SIZE 1 MINUTE)
WHERE type = 'ERROR'
GROUP BY error_code EMIT CHANGES;
ksqlDB includes native integration with Kafka Connect data sources and sinks, effectively providing a unified SQL interface over a broad variety of external systems.
The following query is a simple persistent streaming query that will produce all of its output into a topic named clicks_transformed:
CREATE STREAM clicks_transformed AS
SELECT userid, page, action
FROM clickstream c
LEFT JOIN users u ON c.userid = u.user_id EMIT CHANGES;
Rather than simply send all continuous query output into a Kafka topic, it is often very useful to route the output into another datastore. ksqlDB's Kafka Connect integration makes this pattern very easy.
The following statement will create a Kafka Connect sink connector that continuously sends all output from the above streaming ETL query directly into Elasticsearch:
CREATE SINK CONNECTOR es_sink WITH (
'connector.class' = 'io.confluent.connect.elasticsearch.ElasticsearchSinkConnector',
'key.converter' = 'org.apache.kafka.connect.storage.StringConverter',
'topics' = 'clicks_transformed',
'key.ignore' = 'true',
'schema.ignore' = 'true',
'type.name' = '',
'connection.url' = 'http://elasticsearch:9200');
For user help, questions or queries about ksqlDB please use our [user Google Group](https://groups.google.com/for
good first issue 또는 help wanted 이슈가 없습니다.
We are no longer accepting contributions from the community. The guidelines below are retained for historical reference only.
If you have questions, please create a GH issue.
Development versions of ksqlDB use version ranges for dependencies on other Confluent projects, and due to a bug in Apache Maven, you may find that both Maven and your IDE download hundreds or thousands of pom files while resolving dependencies. They get cached, so it will be most apparent on fresh forks or switching to branches that you haven't used in a while.
Until we are able to get a fix in and released officially, we need to work
around the bug. We have added a Maven wrapper script and configured
it to download a patched version of Maven. You can use this wrapper simply by
substituting ./mvnw instead of mvn for every Maven invocation.
The patch is experimental, so if it causes you some trouble, you can switch
back to the official release just by using mvn again. Please let us know
if you do have any trouble with the wrapper.
You can also configure IntelliJ IDEA to use the patched version of Maven. First, get the path to the patched Maven home:
$ ./mvnw -v
...
Maven home: /home/john/.m2/wrapper/dists/apache-maven-3.9.4.1-bin/<hash>/apache-maven-3.9.4.1
...
Then, update your IDEA Maven home in Settings > Build, Execution, Deployment > Build Tools > Maven
and set the value of Maven home path to the same directory listed as the "Maven home" above.
In the above example, it is /home/john/.m2/wrapper/dists/apache-maven-3.9.4.1-bin/<hash>/apache-maven-3.9.4.1,
but it may be different on your machine.
Finally, you may need to restart the IDE to get it to reload the Maven binary.
There is likely a similar option available for other IDEs.
To build ksqlDB locally, run the following commands:
$ ./mvnw clean package -DskipTests
To run ksqldb locally, you can start the ksqlDB server and CLI with the following commands. Ensure you have dependent services like Kafka and Schema Registry running before starting ksqlDB.
$ ./bin/ksql-server-start -daemon config/ksql-server.properties
$ ./bin/ksql
This will start the ksqlDB server in the background and the ksqlDB CLI in the
foreground. Check the logs folder for the log files that the server writes
including any errors.
If you would rather have the ksqlDB server logs spool to the console, then
drop the -daemon switch, and start the CLI in a second console.
It is recommended to instruct maven to use the native git client on your system by setting
export MAVEN_OPTS="-Dmaven.gitcommitid.nativegit=true $MAVEN_OPTS". This will drastically
reduce build times.
The source for standalone ksqlDB versions is tagged with the pattern vN.NN.N. For example, the tag for the 0.28.2 standalone release is v0.28.2.
If you wish to build a released version, run the following commands, replacing 0.28.2
with the desired version:
$ git fetch origin --tags
$ git checkout v0.28.2
$ ./mvnw --settings maven-settings.xml clean package -DskipTests
Note that the --settings maven-settings.xml argument is necessary to ensure all dependencies
can be pulled respective to the target version.
You should configure your IDE to use the Maven wrapper (see "About the Apache Maven wrapper", above).
You can create a run configuration in your IDE for the main class:
io.confluent.ksql.rest.server.KsqlServerMain, using the classpath
of the ksqldb-rest-app module, and specifying the path to a config
file as the program argument. There is a basic config available in
config/ksql-server.properties.
NOTE: At least for IDEA (and maybe for other IDEs), building the
project doesn't automatically run the generate-sources Maven phase,
so our ANTLR and Avro classes will be missing (unless you happen to
have built with Maven lately). You can generate them by running:
$ ./mvnw --projects ksqldb-parser,ksqldb-version-metrics-client generate-sources
If you want to use the CLI with your server, you can either build
it ahead of time using the package command above, or you can compile
and run the class from a terminal using maven:
$ ./mvnw compile exec:java --projects ksqldb-cli -Dexec.mainClass="io.confluent.ksql.Ksql"
To build and test changes locally, run the following commands:
$ ./mvnw verify -T 1.5C
This example showcases the new parallel build feature of Maven 3.
If it causes problems for you, you can change the parallelism argument, or
drop the -T ... option altogether.
See comments at the top of the docker compose file in the root of the project for instructions on how to build and run docker images.
Report bugs and issues by creating a new GitHub issue. Prior to creating an issue, please search through existing issues so that you are not creating duplicate ones.
Any change to the public API of ksqlDB, especially the SQL syntax, requires a KsqlDB improvement proposal (KLIP) to be raised and approved. See the KLIP Readme for more info.
Code changes are submitted via a pull request (PR). When submitting a PR use the following guidelines: