ksql 使用
注意执行命令要在ksqldb-server 而不是 ksqldb-cli 的容器下面
stream 和 table 的区别是,table必须定义primary key,而且执行了query后下次还可以拿到数据,而stream是没有的
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233008125-407396933.png)
ksql> print bear from beginning;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233314615-738962310.png)
CREATE STREAM pageviews_original (viewtime bigint, userid varchar, pageid varchar) WITH (kafka_topic='pageviews', value_format='DELIMITED');
CREATE TABLE users_original (registertime bigint primary key, gender varchar, regionid varchar, userid varchar) WITH (kafka_topic='users', value_format='JSON');
ksql> select * from USERS_ORIGINAL emit changes limit 5;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233342773-523188537.png)
ksql> select * from users_original where rowtime >='2022-03-18' emit changes;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233357549-688671471.png)
ksql> show queries;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233414670-1371926350.png)
stream 和 table都可以insert data
ksql> insert into stream_test values('1',1,1,1);
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233434457-659782867.png)
ksql> insert into users_original(REGISTERTIME,GENDER,REGIONID,USERID) values(1,'G1','G1','G1');
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220319233449351-1718379994.png)
想count的话只能通过group by 的操作,数据量大的话会是个灾难
ksql> select key,count(*) from stream_test group by key emit changes;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220320014316942-473055558.png)
ksql> select count(*) from stream_test group by 1 emit changes;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220320015723184-1410326232.png)
可以用describe extended 命令查询
ksql> describe stream_test extended;
![](https://img2022.cnblogs.com/blog/512990/202203/512990-20220320020152774-14085492.png)
Oftentimes—and particularly in testing and development—you’ll want to read the data that already exists in a topic. To tell KSQL to do this, change the offset property:
ksql> SET 'auto.offset.reset'='earliest';
update table 的话可以通过join语法创建个新表来实现