简体   繁体   English

如何在kafka中加入2个流?

[英]How to join 2 streams in kafka?

Learning Kafka Streams, trying to join two streams(Json value) on a 5-minute window. 学习Kafka Streams,尝试在5分钟的窗口中加入两个流(Json值)。 My understanding is to have same key for values to match join criteria. 我的理解是为值具有相同的键以匹配联接条件。 If my understanding is correct, like keys only get to join, right? 如果我的理解是正确的,就像只有钥匙才能加入,对吗? If so, how do I join the json values. 如果是这样,我如何加入json值。 Ie Stream1: Key=a, value={a,b,c}. Stream2: Key=a, value={x} and key=a, value={y}. Expected o/p: {a,b,c,x} and {a,b,c,y}. Stream1: Key=a, value={a,b,c}. Stream2: Key=a, value={x} and key=a, value={y}. Expected o/p: {a,b,c,x} and {a,b,c,y}. Stream1: Key=a, value={a,b,c}. Stream2: Key=a, value={x} and key=a, value={y}. Expected o/p: {a,b,c,x} and {a,b,c,y}.

To achieve this, how should my ValueJoiner look like. 为了实现这一目标,我的ValueJoiner应该是什么样子。 Help me with this. 帮帮我 My sample code: 我的示例代码:

KStream<String, JsonNode> resultStream = stream1.leftJoin(stream2,
                new ValueJoiner<JsonNode, JsonNode, JsonNode>() {
                    @Override
                    public JsonNode apply(JsonNode value1, JsonNode value2) {
                        if (value1 != null && value2 != null) {


                            return value1;
                        }
                        return null;
                    }
                }, JoinWindows.of(TimeUnit.SECONDS.toMillis(20)), Joined.with(Serdes.String(), /* key */
                        jsonSerde, /* left value */
                        jsonSerde) /* right value */
        );

Your understanding on how the joins work is correct (assuming that the record timestamp different is smaller than the join-window size). 您对联接如何工作的理解是正确的(假设记录时间戳不同小于联接窗口的大小)。

For manipulating JsonNodes, just search the internet: How to modify JsonNode in Java? 要操作JsonNode,只需在互联网上搜索: 如何在Java中修改JsonNode?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM