【发布时间】:2019-08-22 06:52:58
【问题描述】:
我在 KSQL 中创建了一个流,如下所示。
create stream incident_1 (fruitName VARCHAR) WITH (KAFKA_TOPIC='test_incident',VALUE_FORMAT='JSON');
假设我有一个主题在流中有以下记录
fruitName
---------
apple
orange
banana
apple
orange
我尝试通过在 KSQL 中创建一个表来获取单个记录的计数,比如输出是
select fruitName,count(*) from incident_2 group by fruitName;
fruitName count
--------- --------
apple 2
orange 2
banana 1
我还尝试在流应用程序代码中而不是 KSQL 中编写 JAVA 逻辑。但这将有助于减少数据。将来我们会得到超过 10 万条记录,那时所有这些迭代都需要很长时间,这会影响代码的速度。所以我想不要用这个。这是代码
static HashSet<String> hash_incident = new HashSet<String>();
// Adding elements into HashSet usind add()
hash_incident.add(new_key);
System.out.println("incident_count "+hash_incident.size());
count_unique_notification+=1;
System.out.println("keyyyyyy"+new_key+
"helllllllllllllllllllllllo"+count_unique_notification);
但是,我真正想要的是,不同记录的总数为
total_distinctt_fruits_count
-----------------------------
3
那么,KSQL 中还有其他方法吗?
【问题讨论】:
标签: apache-kafka apache-kafka-streams ksqldb