【发布时间】:2022-07-04 17:47:11
【问题描述】:
我正在编写一个 kafka 流应用程序,在其中我正在为网页生成统计信息。 我有一个关于网页的信息流,其中包括结构中的页面类型(新闻、游戏、博客等)和页面语言(en、fr、ru 等)。
我已将此流过滤为第二个流,其中包含特定页面类型的所有语言。 对于这个例子,我们可以假设过滤后的流包括“新闻”页面的所有事件。
我现在想将每种语言的页面数量除以相同类型的页面总数的值 a 输出到主题。
我使用 .count() 创建了一个 KTable 来计算每种语言的事件。 我还使用 .count() 创建了一个包含所有相同类型事件的 KTable。
为了产生除法,我计划在流之间使用连接,它将取左值并将其除以右值。 不幸的是,这似乎不起作用,因为左值的键是语言,而右值的键是页面类型。
我的代码如下:
ValueJoiner<Long, Long, Float> valueJoiner = (leftVal, rightVal) -> {
if ((rightVal != null) && (leftVal != null))
{
return leftVal.floatValue()/rightVal;
}
return 0f;
};
// the per language table for news pages
KTable<String, Long> langTable = newsStream.selectKey((ignored, value) -> value.getLang()).groupByKey().count();
// the table which counts all events of news pages
KTable<String, Long> allTable = newsStream.groupBy((ignored, value) -> value.getType()).count();
// this is the join that doesn't produce values (as there are no common keys?)
KTable<String, Float> joinedLangs = langTable.join(allTable, valueJoiner);
使此代码工作并产生相对数量值的最佳方法是什么?
【问题讨论】: