接上篇文章 【kafka KSQL】遊戲日誌統計分析(2),本文主要經過實例展現KSQL的鏈接查詢功能。json
bin/kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 4 --topic propnew-normalized
bin/kafka-console-producer --broker-list localhost:9092 --topic propnew-normalized > {"user__name":"lzb", "prop__id":"id1"}
CREATE STREAM PROP_USE_EVENT \ (user__name VARCHAR, \ prop__id VARCHAR ) \ WITH (KAFKA_TOPIC='propnew-normalized', \ VALUE_FORMAT='json');
user__name
CREATE STREAM PROP_USE_EVENT_REKEY AS \ SELECT * FROM PROP_USE_EVENT \ PARTITION BY user__name;
USER_SCORE_TABLE
(前面已經建立過了):CREATE TABLE USER_SCORE_TABLE AS \ SELECT username, COUNT(*) AS game_count, SUM(delta) AS delta_sum, SUM(tax) AS tax_sum \ FROM USER_SCORE_EVENT_REKEY \ WHERE reason = 'game' \ GROUP BY username;
USER_PROP_TABLE
:CREATE TABLE USER_PROP_TABLE AS \ SELECT user__name, COUNT(*) AS use_count \ FROM PROP_USE_EVENT_REKEY \ GROUP BY user__name;
CREATE TABLE USER_SCORE_AND_PROP AS \ SELECT s.username AS username, s.game_count, s.tax_sum, s.delta_sum, p.use_count \ FROM USER_SCORE_TABLE s \ LEFT JOIN USER_PROP_TABLE p \ ON s.username = p.user__name;
SELECT username FROM USER_SCORE_AND_PROP \ WHERE game_count >= 3 AND use_count IS NULL;
SELECT username FROM USER_SCORE_AND_PROP \ WHERE game_count >= 3 AND use_count >= 2;