forked from apache/rocketmq-streams
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUE apache#251]Support avg function in WindowStream
- Loading branch information
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
core/src/main/java/org/apache/rocketmq/streams/core/function/accumulator/AvgAccumulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.rocketmq.streams.core.function.accumulator; | ||
|
||
import java.util.Properties; | ||
|
||
public class AvgAccumulator<V> implements Accumulator<V, Double> { | ||
private Double avg; | ||
private Integer num; | ||
|
||
@Override | ||
public void addValue(V value) { | ||
if (value instanceof Number) { | ||
Number number = (Number) value; | ||
Double valueToDouble = number.doubleValue(); | ||
|
||
if (avg == null) { | ||
avg = valueToDouble; | ||
num = 1; | ||
} else { | ||
avg = avg + (valueToDouble - avg) / (num + 1); | ||
num++; | ||
} | ||
} else { | ||
throw new IllegalArgumentException("Calculate avg, input is not a number. value=" + value); | ||
} | ||
} | ||
|
||
@Override | ||
public void merge(Accumulator<V, Double> other) { | ||
if (other instanceof AvgAccumulator) { | ||
AvgAccumulator<V> otherAvgAccumulator = (AvgAccumulator) other; | ||
Integer numOther = otherAvgAccumulator.getNum(); | ||
Double avgOther = otherAvgAccumulator.getAvg(); | ||
|
||
avg = avg + numOther / (num + numOther) * (avgOther - avg); | ||
num = num + numOther; | ||
} else { | ||
throw new IllegalArgumentException("Merge avg, input is not a AvgAccumulator."); | ||
} | ||
} | ||
|
||
@Override | ||
public Double result(Properties context) { | ||
return avg; | ||
} | ||
|
||
@Override | ||
public Accumulator<V, Double> clone() { | ||
return new AvgAccumulator<>(); | ||
} | ||
|
||
public Double getAvg() { | ||
return avg; | ||
} | ||
|
||
public void setAvg(Double avg) { | ||
this.avg = avg; | ||
} | ||
|
||
public Integer getNum() { | ||
return num; | ||
} | ||
|
||
public void setNum(Integer num) { | ||
this.num = num; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
example/src/main/java/org/apache/rocketmq/streams/examples/window/WindowAvg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.rocketmq.streams.examples.window; | ||
|
||
import org.apache.rocketmq.common.MixAll; | ||
import org.apache.rocketmq.streams.core.RocketMQStream; | ||
import org.apache.rocketmq.streams.core.common.Constant; | ||
import org.apache.rocketmq.streams.core.rstream.StreamBuilder; | ||
import org.apache.rocketmq.streams.core.topology.TopologyBuilder; | ||
import org.apache.rocketmq.streams.core.util.Pair; | ||
import org.apache.rocketmq.streams.core.window.Time; | ||
import org.apache.rocketmq.streams.core.window.TimeType; | ||
import org.apache.rocketmq.streams.core.window.WindowBuilder; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.time.LocalTime; | ||
import java.util.Properties; | ||
|
||
/** | ||
* 1、启动RocketMQ | ||
* 2、创建topic | ||
* 3、启动本例子运行 | ||
* 4、向topic中写入数据 | ||
* 5、观察输出结果 | ||
*/ | ||
public class WindowAvg { | ||
public static void main(String[] args) { | ||
StreamBuilder builder = new StreamBuilder("WindowAvg"); | ||
builder.source("avgSource", source -> { | ||
String value = new String(source, StandardCharsets.UTF_8); | ||
Integer num = Integer.parseInt(value); | ||
return new Pair<>(null, num); | ||
}).foreach(value -> System.out.println(String.format("time:%s, input:%d", LocalTime.now(), value))) | ||
.filter(value -> value > 0) | ||
.keyBy(value -> "key") | ||
.window(WindowBuilder.tumblingWindow(Time.seconds(15))) | ||
.avg() | ||
.toRStream() | ||
.print(); | ||
|
||
TopologyBuilder topologyBuilder = builder.build(); | ||
|
||
Properties properties = new Properties(); | ||
properties.putIfAbsent(MixAll.NAMESRV_ADDR_PROPERTY, "127.0.0.1:9876"); | ||
properties.put(Constant.TIME_TYPE, TimeType.PROCESS_TIME); | ||
properties.put(Constant.ALLOW_LATENESS_MILLISECOND, 5000); | ||
|
||
RocketMQStream rocketMQStream = new RocketMQStream(topologyBuilder, properties); | ||
|
||
rocketMQStream.start(); | ||
} | ||
} |