Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于过滤器和聚合的批量数据处理的Kafka流问题_Java_Apache Kafka_Apache Kafka Streams - Fatal编程技术网

Java 基于过滤器和聚合的批量数据处理的Kafka流问题

Java 基于过滤器和聚合的批量数据处理的Kafka流问题,java,apache-kafka,apache-kafka-streams,Java,Apache Kafka,Apache Kafka Streams,我的拓扑结构有问题。我需要在某些条件下处理我的事件,当条件已满时,将向主题发送消息,如果再次满足此条件,则不会处理。我以前做过数据聚合,因为一个条件可能包含多条消息。例如: 我的活动: 键1:{“k0”:“v0”,“k1”:“v1”} 键1:{“k1”:“v1”,“k2”:“v2”} 我的情况: 聚合数据必须有:“k0”和“k2” 如果我一对一地发送事件,所有这些都可以正常工作,但是,如果我成批发送事件,我会在输出主题上收到重复的消息 这是我的代码: KStream<String, JSO

我的拓扑结构有问题。我需要在某些条件下处理我的事件,当条件已满时,将向主题发送消息,如果再次满足此条件,则不会处理。我以前做过数据聚合,因为一个条件可能包含多条消息。例如:

我的活动: 键1:{“k0”:“v0”,“k1”:“v1”} 键1:{“k1”:“v1”,“k2”:“v2”}

我的情况: 聚合数据必须有:“k0”和“k2”

如果我一对一地发送事件,所有这些都可以正常工作,但是,如果我成批发送事件,我会在输出主题上收到重复的消息

这是我的代码:

KStream<String, JSONObject> source0 = builder.stream("streams-plaintext-kafka-input", Consumed.with(Serdes.String(), serdeJson));
        KTable<String, JSONObject> source1 = builder.stream("streams-plaintext-kafka-output", Consumed.with(Serdes.String(), serdeJson)).toTable();

        KTable<String, JSONObject> t1 = source0.leftJoin(source1, (leftValue, rightValue) ->
        {
                System.out.println("JOIN - LeftValue: " + leftValue);
                System.out.println("JOIN - RightValue: " + rightValue);

                
                if (rightValue == null || rightValue.length() == 0 || !rightValue.has("c1"))
                {
                        JSONObject jsonData = new JSONObject();

                        jsonData.put("left", leftValue);
                        jsonData.put("right", rightValue);

                        return jsonData;
                }else
                    return null;
        })
                //.toStream()
                .filter((key, value) -> value != null)
                .filter((key, value) -> !value.has("right") || !value.getJSONObject("right").has("c1"))
                .groupByKey()
                .aggregate(
                    () -> new JSONObject(),
                    (aggKey, newValue, aggValue) ->
                    {

                        JSONObject leftValue = newValue.getJSONObject("left");
                        Iterator<String> leftKeys = leftValue.keys();

                        JSONObject lv = null;

                        if (aggValue.has("left"))
                            lv = aggValue.getJSONObject("left");
                        else
                            lv = new JSONObject();

                        while(leftKeys.hasNext())
                        {
                            String key = leftKeys.next();

                            if (leftValue.get(key) != null) {

                                // do something with jsonObject here
                                lv.put(key, leftValue.get(key));
                            }
                        }

                        aggValue.put("left", lv);

                        JSONObject rightValue = null;

                        if (newValue.has("right"))
                            rightValue = newValue.getJSONObject("right");
                        else
                            rightValue = new JSONObject();

                        Iterator<String> rightKeys = rightValue.keys();

                        JSONObject rv = null;

                        if (aggValue.has("right"))
                            rv = aggValue.getJSONObject("right");
                        else
                            rv = new JSONObject();

                        while(rightKeys.hasNext())
                        {
                            String key = rightKeys.next();

                            if (rightValue.get(key) != null) {
                                // do something with jsonObject here
                                rv.put(key, rightValue.get(key));
                            }
                        }

                        aggValue.put("right", rv);

                        System.out.println("JSON - AGGREGATE: " + aggValue.toString());

                        return aggValue;
                    },
                    Materialized.<String, JSONObject, KeyValueStore<String, JSONObject>>as("store-result-aggregate").withKeySerde(Serdes.String()).withValueSerde(serdeJson)
                );

                KTable<String, JSONObject> t2 = t1.filter((key, value) -> value.getJSONObject("left").has("some") && value.getJSONObject("left").has("some1"))
                        .toStream()
                        .groupByKey()
                        .aggregate(
                            () -> new JSONObject(),
                            (aggKey, newValue, aggValue) ->
                            {

                                if (aggValue.has("right"))
                                {
                                    aggValue.getJSONObject("right").put("c1", true);

                                } else
                                {
                                    JSONObject rightValue = new JSONObject();
                                    rightValue.put("c1", true);
                                    aggValue.put("right", rightValue);
                                }

                                return aggValue;
                            },
                                Materialized.<String, JSONObject, KeyValueStore<String, JSONObject>>as("store-result-aggregate-total").withKeySerde(Serdes.String()).withValueSerde(serdeJson)
                        );


                KTable <String, JSONObject> t3 = t2.mapValues((value) ->
                {
                    JSONObject rightValue = value.getJSONObject("right");
                    System.out.println("Final: " + rightValue);
                    return rightValue;
                });

                t3.toStream().to("streams-plaintext-kafka-output", Produced.keySerde(Serdes.String()).withValueSerde(serdeJson));
KStream source0=builder.stream(“streams纯文本卡夫卡输入”,consumered.with(Serdes.String(),serdeJson));
KTable source1=builder.stream(“streams明文卡夫卡输出”,consumered.with(Serdes.String(),serdeJson)).toTable();
KTable t1=source0.leftJoin(source1,(leftValue,rightValue)->
{
System.out.println(“JOIN-LeftValue:+LeftValue”);
System.out.println(“JOIN-RightValue:+RightValue”);
如果(rightValue==null | | rightValue.length()==0 | |!rightValue.has(“c1”))
{
JSONObject jsonData=新的JSONObject();
put(“left”,leftValue);
jsonData.put(“right”,rightValue);
返回jsonData;
}否则
返回null;
})
//.toStream()
.filter((键,值)->value!=null)
.filter((key,value)->!value.has(“right”)| |!value.getJSONObject(“right”).has(“c1”))
.groupByKey()
.合计(
()->新的JSONObject(),
(aggKey、newValue、aggValue)->
{
JSONObject leftValue=newValue.getJSONObject(“左”);
迭代器leftKeys=leftValue.keys();
JSONObject lv=null;
if(aggValue.has(“左”))
lv=aggValue.getJSONObject(“左”);
其他的
lv=新的JSONObject();
while(leftKeys.hasNext())
{
String key=leftKeys.next();
if(leftValue.get(key)!=null){
//在这里对jsonObject执行一些操作
lv.put(key,leftValue.get(key));
}
}
加总值。放置(“左”,lv);
JSONObject rightValue=null;
if(newValue.has(“right”))
rightValue=newValue.getJSONObject(“右”);
其他的
rightValue=新的JSONObject();
迭代器rightKeys=rightValue.keys();
JSONObject rv=null;
如果(aggValue.has(“right”))
rv=aggValue.getJSONObject(“右”);
其他的
rv=新的JSONObject();
while(rightKeys.hasNext())
{
String key=rightKeys.next();
if(rightValue.get(key)!=null){
//在这里对jsonObject执行一些操作
rv.put(key,rightValue.get(key));
}
}
总价值。放置(“右”,rv);
System.out.println(“JSON-AGGREGATE:+aggValue.toString());
返回值;
},
具体化.as(“存储结果聚合”).withKeySerde(Serdes.String()).withValueSerde(serdeJson)
);
KTable t2=t1.filter((key,value)->value.getJSONObject(“left”).has(“some”)和&value.getJSONObject(“left”).has(“some1”))
.toStream()
.groupByKey()
.合计(
()->新的JSONObject(),
(aggKey、newValue、aggValue)->
{
如果(aggValue.has(“right”))
{
aggValue.getJSONObject(“右”).put(“c1”,true);
}否则
{
JSONObject rightValue=新的JSONObject();
rightValue.put(“c1”,真);
aggValue.put(“right”,rightValue);
}
返回值;
},
物化.as(“存储结果聚合总数”).withKeySerde(Serdes.String()).withValueSerde(serdeJson)
);
KTable t3=t2。映射值((值)->
{
JSONObject rightValue=value.getJSONObject(“右”);
System.out.println(“最终:+rightValue”);
返回正确值;
});
t3.toStream().to(“streams纯文本卡夫卡输出”,生成.keySerde(Serdes.String()).withValueSerde(serdeJson));
有什么建议吗

提前谢谢