重磅开源 KSQL:用于 Apache Kafka 的流数据 SQL 引擎 2017.8.29
Kafka 的作者 Neha Narkhede 在 Confluent 上發表了一篇博文,介紹了Kafka 新引入的KSQL 引擎——一個基于流的SQL。推出KSQL 是為了降低流式處理的門檻,為處理Kafka 數據提供簡單而完整的可交互式SQL 接口。KSQL 目前可以支持多種流式操作,包括聚合(aggregate)、連接(join)、時間窗口(window)、會話(session),等等。
與傳統 SQL 的主要區別
KSQL 與關系型數據庫中的 SQL 還是有很大不同的。傳統的 SQL 都是即時的一次性操作,不管是查詢還是更新都是在當前的數據集上進行。而 KSQL 則不同,KSQL 的查詢和更新是持續進行的,而且數據集可以源源不斷地增加。KSQL 所做的其實是轉換操作,也就是流式處理。
KSQL 的適用場景
1. 實時監控
一方面,可以通過 KSQL 自定義業務層面的度量指標,這些指標可以實時獲得。底層的度量指標無法告訴我們應用程序的實際行為,所以基于應用程序生成的原始事件來自定義度量指標可以更好地了解應用程序的運行狀況。另一方面,可以通過 KSQL 為應用程序定義某種標準,用于檢查應用程序在生產環境中的行為是否達到預期。
2. 安全檢測
KSQL 把事件流轉換成包含數值的時間序列數據,然后通過可視化工具把這些數據展示在 UI 上,這樣就可以檢測到很多威脅安全的行為,比如欺詐、入侵,等等。KSQL 為此提供了一種實時、簡單而完備的方案。
3. 在線數據集成
大部分的數據處理都會經歷 ETL(Extract——Transform——Load)這樣的過程,而這樣的系統通常都是通過定時的批次作業來完成數據處理的,但批次作業所帶來的延時在很多時候是無法被接受的。而通過使用 KSQL 和 Kafka 連接器,可以將批次數據集成轉變成在線數據集成。比如,通過流與表的連接,可以用存儲在數據表里的元數據來填充事件流里的數據,或者在將數據傳輸到其他系統之前過濾掉數據里的敏感信息。
4. 應用開發
對于復雜的應用來說,使用 Kafka 的原生 Streams API 或許會更合適。不過,對于簡單的應用來說,或者對于不喜歡 Java 編程的人來說,KSQL 會是更好的選擇。
KSQL 的核心抽象
KSQL 是基于 Kafka 的?Streams API?進行構建的,所以它的兩個核心概念是流(Stream)和表(Table)。流是沒有邊界的結構化數據,數據可以被源源不斷地添加到流當中,但流中已有的數據是不會發生變化的,即不會被修改也不會被刪除。表就是流的視圖,或者說它代表了可變數據的集合。它與傳統的數據庫表類似,只不過具備了一些流式語義,比如時間窗口,而且表中的數據是可變的。KSQL 將流和表集成在一起,允許將代表當前狀態的表與代表當前發生事件的流連接在一起。
KSQL 架構
KSQL 是一個獨立運行的服務器,多個 KSQL 服務器可以組成集群,可以動態地添加服務器實例。集群具有容錯機制,如果一個服務器失效,其他服務器就會接管它的工作。KSQL 命令行客戶端通過 REST API 向集群發起查詢操作,可以查看流和表的信息、查詢數據以及查看查詢狀態。因為是基于 Streams API 構建的,所以 KSQL 也沿襲了 Streams API 的彈性、狀態管理和容錯能力,同時也具備了僅一次(exactly once)語義。KSQL 服務器內嵌了這些特性,并增加了一個分布式SQL 引擎、用于提升查詢性能的自動字節碼生成機制,以及用于執行查詢和管理的REST API。
Kafka+KSQL 要顛覆傳統數據庫
傳統關系型數據庫以表為核心,日志只不過是實現手段。而在以事件為中心的世界里,情況卻恰好相反。日志成為了核心,而表幾乎是以日志為基礎,新的事件不斷被添加到日志里,表的狀態也因此發生變化。將 Kafka 作為中心日志,配置 KSQL 這個引擎,我們就可以創建出我們想要的物化視圖,而且視圖也會持續不斷地得到更新。
KSQL 的未來
KSQL 目前還處于開發者預覽階段,作者還在收集社區的反饋。未來計劃增加更多的特性,包括支持更豐富的SQL 語法,讓KSQL 成為生產就緒的系統。
這里有 KSQL 的快速入門指南和一個演示程序。可以在Slack 的#KSQL 頻道上向作者提供反饋信息,或者如果發現Bug,可以在?GitHub?上提出來。
?
?KSQL - Streaming SQL for Apache Kafka
KSQL is now GA and officially supported by Confluent Inc.?Get started with KSQL today.
KSQL is the streaming SQL engine for Apache Kafka. It provides a simple and completely interactive SQL interface for stream processing on Kafka; no need to write code in a programming language such as Java or Python. KSQL is distributed, scalable, reliable, and real-time. It supports a wide range of powerful stream processing operations including aggregations, joins, windowing, sessionization, and much more. You can find?more KSQL tutorials and resources here?if you are interested.
Click here to watch a screencast of the KSQL demo on YouTube.?
?
Getting Started and Download
Stable Releases
Stable releases are published every four months and are officially supported by?Confluent.
Preview Releases
In addition to supported?stable KSQL releases, we also provide preview releases. We encourage you to try them in development and testing environments and to take advantage of?Confluent Community resources?to get help and share feedback.
- Download latest KSQL Preview.
Documentation
See?KSQL documentation?for the latest stable release.
Use Cases and Examples
Streaming ETL
Apache Kafka is a popular choice for powering data pipelines. KSQL makes it simple to transform data within the pipeline, readying messages to cleanly land in another system.
CREATE STREAM vip_actions ASSELECT userid, page, actionFROM clickstream cLEFT JOIN users u ON c.userid = u.user_id WHERE u.level = 'Platinum';Anomaly Detection
KSQL 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 ASSELECT card_number, count(*) FROM authorization_attempts WINDOW TUMBLING (SIZE 5 SECONDS) GROUP BY card_number HAVING count(*) > 3;Monitoring
Kafka's ability to provide scalable ordered messages with stream processing make it a common solution for log data monitoring and alerting. KSQL lends a familiar syntax for tracking, understanding, and managing alerts.
CREATE TABLE error_counts ASSELECT error_code, count(*) FROM monitoring_stream WINDOW TUMBLING (SIZE 1 MINUTE) WHERE type = 'ERROR' GROUP BY error_code;Join the Community
You can get help, learn how to contribute to KSQL, and find the latest news by?connecting with the Confluent community.
- Ask a question in the #ksql channel in our public?Confluent Community Slack. Account registration is free and self-service.
- Join the?Confluent Google group.
Contributing
Contributions to the code, examples, documentation, etc. are very much appreciated.
- Report issues and bugs directly in?this GitHub project.
- Learn how to work with the KSQL source code, including building and testing KSQL as well as contributing code changes to KSQL by reading our?Development and Contribution guidelines.
- One good way to get started is by tackling a?newbie issue.
License
The project is licensed under the?Confluent Community License.
Apache, Apache Kafka, Kafka, and associated open source project names are trademarks of the?Apache Software Foundation.
轉載于:https://www.cnblogs.com/felixzh/p/11362527.html
總結
以上是生活随笔為你收集整理的重磅开源 KSQL:用于 Apache Kafka 的流数据 SQL 引擎 2017.8.29的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Delphi 10.X 不用联接真机或模
- 下一篇: Sqoop import导入表时报错ja