Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
doujutun3207
flink
提交
98967ea7
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,发现更多精彩内容 >>
提交
98967ea7
编写于
7月 14, 2014
作者:
Y
Yingjun Wu
提交者:
Stephan Ewen
8月 18, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[streaming] user-defined window operator
上级
6f8f384b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
149 addition
and
15 deletion
+149
-15
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/examples/window/wordcount/WindowWordCountCounter.java
...ing/examples/window/wordcount/WindowWordCountCounter.java
+50
-15
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/state/WindowInternalState.java
.../eu/stratosphere/streaming/state/WindowInternalState.java
+94
-0
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/state/WindowStateIterator.java
.../eu/stratosphere/streaming/state/WindowStateIterator.java
+5
-0
未找到文件。
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/examples/window/wordcount/WindowWordCountCounter.java
浏览文件 @
98967ea7
...
...
@@ -15,29 +15,35 @@
package
eu.stratosphere.streaming.examples.window.wordcount
;
import
java.util.HashMap
;
import
java.util.Map
;
import
eu.stratosphere.api.java.tuple.Tuple3
;
import
eu.stratosphere.streaming.api.invokable.UserTaskInvokable
;
import
eu.stratosphere.streaming.api.streamrecord.StreamRecord
;
import
eu.stratosphere.streaming.state.MutableInternalState
;
import
eu.stratosphere.streaming.state.WindowInternalState
;
public
class
WindowWordCountCounter
extends
UserTaskInvokable
{
private
int
windowSize
=
100
;
private
int
slidingStep
=
20
;
private
int
windowSize
;
private
int
slidingStep
;
private
WindowInternalState
<
Integer
>
window
;
private
MutableInternalState
<
String
,
Integer
>
wordCounts
;
private
Map
<
String
,
Integer
>
wordCounts
=
new
HashMap
<
String
,
Integer
>();
private
String
word
=
""
;
private
Integer
count
=
0
;
private
Long
timestamp
=
0L
;
private
StreamRecord
outRecord
=
new
StreamRecord
(
new
Tuple3
<
String
,
Integer
,
Long
>());
private
StreamRecord
outRecord
=
new
StreamRecord
(
new
Tuple3
<
String
,
Integer
,
Long
>());
@Override
public
void
invoke
(
StreamRecord
record
)
throws
Exception
{
word
=
record
.
getString
(
0
);
timestamp
=
record
.
getLong
(
1
);
public
WindowWordCountCounter
()
{
windowSize
=
100
;
slidingStep
=
20
;
window
=
new
WindowInternalState
<
Integer
>(
windowSize
,
slidingStep
);
wordCounts
=
new
MutableInternalState
<
String
,
Integer
>();
}
private
void
incrementCompute
(
StreamRecord
record
)
{
word
=
record
.
getString
(
0
);
if
(
wordCounts
.
containsKey
(
word
))
{
count
=
wordCounts
.
get
(
word
)
+
1
;
wordCounts
.
put
(
word
,
count
);
...
...
@@ -45,12 +51,41 @@ public class WindowWordCountCounter extends UserTaskInvokable {
count
=
1
;
wordCounts
.
put
(
word
,
1
);
}
}
outRecord
.
setString
(
0
,
word
);
outRecord
.
setInteger
(
1
,
count
);
outRecord
.
setLong
(
2
,
timestamp
);
private
void
decrementCompute
(
StreamRecord
record
)
{
word
=
record
.
getString
(
0
);
count
=
wordCounts
.
get
(
word
)
-
1
;
if
(
count
==
0
)
{
wordCounts
.
delete
(
word
);
}
else
{
wordCounts
.
put
(
word
,
count
);
}
}
emit
(
outRecord
);
@Override
public
void
invoke
(
StreamRecord
record
)
throws
Exception
{
if
(
window
.
isFull
())
{
StreamRecord
expiredRecord
=
window
.
popFront
();
incrementCompute
(
record
);
decrementCompute
(
expiredRecord
);
window
.
pushBack
(
record
);
if
(
window
.
isComputable
())
{
outRecord
.
setString
(
0
,
word
);
outRecord
.
setInteger
(
1
,
count
);
outRecord
.
setLong
(
2
,
timestamp
);
emit
(
outRecord
);
}
}
else
{
incrementCompute
(
record
);
window
.
pushBack
(
record
);
if
(
window
.
isFull
()){
outRecord
.
setString
(
0
,
word
);
outRecord
.
setInteger
(
1
,
count
);
outRecord
.
setLong
(
2
,
timestamp
);
emit
(
outRecord
);
}
}
}
}
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/
examples/window/wordcount/IncrementalWindow
.java
→
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/
state/WindowInternalState
.java
浏览文件 @
98967ea7
...
...
@@ -13,20 +13,82 @@
*
**********************************************************************************************************************/
package
eu.stratosphere.streaming.
examples.window.wordcount
;
package
eu.stratosphere.streaming.
state
;
public
class
IncrementalWindow
{
import
org.apache.commons.collections.buffer.CircularFifoBuffer
;
private
int
currentTupleNum
;
private
int
fullTupleNum
;
private
int
slideTupleNum
;
import
eu.stratosphere.streaming.api.streamrecord.StreamRecord
;
public
IncrementalWindow
(
int
batchRange
,
int
windowSize
,
int
slidingStep
)
{
/**
* The window state for window operator. To be general enough, this class
* implements a count based window operator. It is possible for the user to
* compose time based window operator by extending this class by splitting the
* stream into multiple mini batches.
*/
public
class
WindowInternalState
<
K
>
implements
InternalState
<
K
,
StreamRecord
>
{
private
int
currentRecordNum
;
private
int
fullRecordNum
;
private
int
slideRecordNum
;
CircularFifoBuffer
buffer
;
public
WindowInternalState
(
int
windowSize
,
int
slidingStep
)
{
currentRecordNum
=
0
;
fullRecordNum
=
windowSize
;
slideRecordNum
=
slidingStep
;
buffer
=
new
CircularFifoBuffer
(
windowSize
);
}
public
void
pushBack
(
StreamRecord
records
)
{
buffer
.
add
(
records
);
currentRecordNum
+=
1
;
}
public
StreamRecord
popFront
()
{
StreamRecord
frontRecord
=(
StreamRecord
)
buffer
.
get
();
buffer
.
remove
();
return
frontRecord
;
}
public
boolean
isFull
()
{
return
currentRecordNum
>=
fullRecordNum
;
}
public
boolean
isComputable
()
{
if
(
currentRecordNum
==
fullRecordNum
+
slideRecordNum
)
{
currentRecordNum
-=
slideRecordNum
;
return
true
;
}
return
false
;
}
void
pushBack
()
{
@Override
public
void
put
(
K
key
,
StreamRecord
value
)
{
// TODO Auto-generated method stub
}
void
popFront
()
{
@Override
public
StreamRecord
get
(
K
key
)
{
// TODO Auto-generated method stub
return
null
;
}
@Override
public
void
delete
(
K
key
)
{
// TODO Auto-generated method stub
}
@Override
public
boolean
containsKey
(
K
key
)
{
// TODO Auto-generated method stub
return
false
;
}
@Override
public
StateIterator
<
K
,
StreamRecord
>
getIterator
()
{
// TODO Auto-generated method stub
return
null
;
}
}
flink-addons/flink-streaming/src/main/java/eu/stratosphere/streaming/state/WindowStateIterator.java
0 → 100644
浏览文件 @
98967ea7
package
eu.stratosphere.streaming.state
;
public
class
WindowStateIterator
{
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录