Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
doujutun3207
flink
提交
2f8bcf53
F
flink
项目概览
doujutun3207
/
flink
与 Fork 源项目一致
从无法访问的项目Fork
通知
24
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
flink
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2f8bcf53
编写于
9月 06, 2015
作者:
H
HuangWHWHW
提交者:
Stephan Ewen
9月 07, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[FLINK-2626] [core] Add an average accumulator
This closes #1096
上级
99105109
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
202 addition
and
0 deletion
+202
-0
flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
...che/flink/api/common/accumulators/AverageAccumulator.java
+88
-0
flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
...flink/api/common/accumulators/AverageAccumulatorTest.java
+114
-0
未找到文件。
flink-core/src/main/java/org/apache/flink/api/common/accumulators/AverageAccumulator.java
0 → 100644
浏览文件 @
2f8bcf53
/*
* 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.flink.api.common.accumulators
;
/**
* An accumulator that get the average values.
* Input can be {@code long}, {@code integer}, {@code double} as the result is {@code double}.
*/
public
class
AverageAccumulator
implements
SimpleAccumulator
<
Double
>
{
private
double
localValue
=
0
;
private
long
count
=
0
;
@Override
public
void
add
(
Double
value
)
{
this
.
count
++;
this
.
localValue
+=
value
;
}
public
void
add
(
double
value
)
{
this
.
count
++;
this
.
localValue
+=
value
;
}
public
void
add
(
long
value
)
{
this
.
count
++;
this
.
localValue
+=
value
;
}
public
void
add
(
int
value
)
{
this
.
count
++;
this
.
localValue
+=
value
;
}
@Override
public
Double
getLocalValue
()
{
if
(
this
.
count
==
0
)
{
return
0.0
;
}
return
this
.
localValue
/
(
double
)
this
.
count
;
}
@Override
public
void
resetLocal
()
{
this
.
count
=
0
;
this
.
localValue
=
0
;
}
@Override
public
void
merge
(
Accumulator
<
Double
,
Double
>
other
)
{
if
(
other
instanceof
AverageAccumulator
)
{
AverageAccumulator
temp
=
(
AverageAccumulator
)
other
;
this
.
count
+=
temp
.
count
;
this
.
localValue
+=
other
.
getLocalValue
();
}
else
{
throw
new
RuntimeException
(
"The merged accumulator must be AverageAccumulator."
);
}
}
@Override
public
AverageAccumulator
clone
()
{
AverageAccumulator
average
=
new
AverageAccumulator
();
average
.
localValue
=
this
.
localValue
;
average
.
count
=
this
.
count
;
return
average
;
}
@Override
public
String
toString
()
{
return
"AverageAccumulator "
+
this
.
localValue
+
" count "
+
this
.
count
;
}
}
flink-core/src/test/java/org/apache/flink/api/common/accumulators/AverageAccumulatorTest.java
0 → 100644
浏览文件 @
2f8bcf53
/*
* 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.flink.api.common.accumulators
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
public
class
AverageAccumulatorTest
{
@Test
public
void
testGet
()
{
AverageAccumulator
average
=
new
AverageAccumulator
();
assertEquals
(
Double
.
valueOf
(
0
),
average
.
getLocalValue
());
}
@Test
public
void
testAdd
()
{
AverageAccumulator
average
=
new
AverageAccumulator
();
int
i1
;
for
(
i1
=
0
;
i1
<
10
;
i1
++)
{
average
.
add
(
i1
);
}
assertEquals
(
Double
.
valueOf
(
4.5
),
average
.
getLocalValue
());
average
.
resetLocal
();
Integer
i2
;
for
(
i2
=
0
;
i2
<
10
;
i2
++)
{
average
.
add
(
i2
);
}
assertEquals
(
Double
.
valueOf
(
4.5
),
average
.
getLocalValue
());
average
.
resetLocal
();
long
i3
;
for
(
i3
=
0
;
i3
<
10
;
i3
++)
{
average
.
add
(
i3
);
}
assertEquals
(
Double
.
valueOf
(
4.5
),
average
.
getLocalValue
());
average
.
resetLocal
();
Long
i4
;
for
(
i4
=
0L
;
i4
<
10
;
i4
++)
{
average
.
add
(
i4
);
}
assertEquals
(
Double
.
valueOf
(
4.5
),
average
.
getLocalValue
());
average
.
resetLocal
();
double
i5
;
for
(
i5
=
0
;
i5
<
10
;
i5
++)
{
average
.
add
(
i5
);
}
assertEquals
(
Double
.
valueOf
(
4.5
),
average
.
getLocalValue
());
average
.
resetLocal
();
Double
i6
;
for
(
i6
=
0.0
;
i6
<
10
;
i6
++)
{
average
.
add
(
i6
);
}
assertEquals
(
Double
.
valueOf
(
4.5
),
average
.
getLocalValue
());
average
.
resetLocal
();
assertEquals
(
Double
.
valueOf
(
0
),
average
.
getLocalValue
());
}
@Test
public
void
testMergeSuccess
()
{
AverageAccumulator
average
=
new
AverageAccumulator
();
AverageAccumulator
averageNew
=
new
AverageAccumulator
();
average
.
add
(
1
);
averageNew
.
add
(
2
);
average
.
merge
(
averageNew
);
assertEquals
(
Double
.
valueOf
(
1.5
),
average
.
getLocalValue
());
}
@Test
public
void
testMergeFailed
()
{
AverageAccumulator
average
=
new
AverageAccumulator
();
Accumulator
<
Double
,
Double
>
averageNew
=
null
;
average
.
add
(
1
);
try
{
average
.
merge
(
averageNew
);
}
catch
(
Exception
e
)
{
assertTrue
(
e
.
toString
().
indexOf
(
"The merged accumulator must be AverageAccumulator."
)
!=
-
1
);
return
;
}
fail
();
}
@Test
public
void
testClone
()
{
AverageAccumulator
average
=
new
AverageAccumulator
();
average
.
add
(
1
);
AverageAccumulator
averageNew
=
average
.
clone
();
assertEquals
(
Double
.
valueOf
(
1
),
averageNew
.
getLocalValue
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录