Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
F
flinktest
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
Close sidebar
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
liugen.wen
flinktest
提交
1c76e00b
提交
1c76e00b
编写于
1月 10, 2019
作者:
liugen.wen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
flink
上级
f79458cd
变更
1
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
74 行增加
和
0 行删除
+74
-0
SocketWindowWordCount.java
src/main/java/demo/SocketWindowWordCount.java
+74
-0
未找到文件。
src/main/java/demo/SocketWindowWordCount.java
0 → 100644
浏览文件 @
1c76e00b
package
demo
;
import
org.apache.flink.api.common.functions.FlatMapFunction
;
import
org.apache.flink.api.common.functions.ReduceFunction
;
import
org.apache.flink.api.java.utils.ParameterTool
;
import
org.apache.flink.streaming.api.datastream.DataStream
;
import
org.apache.flink.streaming.api.environment.StreamExecutionEnvironment
;
import
org.apache.flink.streaming.api.windowing.time.Time
;
import
org.apache.flink.util.Collector
;
public
class
SocketWindowWordCount
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
// the port to connect to
final
int
port
;
try
{
final
ParameterTool
params
=
ParameterTool
.
fromArgs
(
args
);
port
=
params
.
getInt
(
"port"
);
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"No port specified. Please run 'SocketWindowWordCount --port <port>'"
);
return
;
}
// get the execution environment
final
StreamExecutionEnvironment
env
=
StreamExecutionEnvironment
.
getExecutionEnvironment
();
// get input data by connecting to the socket
DataStream
<
String
>
text
=
env
.
socketTextStream
(
"localhost"
,
port
,
"\n"
);
// parse the data, group it, window it, and aggregate the counts
DataStream
<
WordWithCount
>
windowCounts
=
text
.
flatMap
(
new
FlatMapFunction
<
String
,
WordWithCount
>()
{
@Override
public
void
flatMap
(
String
value
,
Collector
<
WordWithCount
>
out
)
{
for
(
String
word
:
value
.
split
(
"\\s"
))
{
out
.
collect
(
new
WordWithCount
(
word
,
1L
));
}
}
})
.
keyBy
(
"word"
)
.
timeWindow
(
Time
.
seconds
(
5
),
Time
.
seconds
(
1
))
.
reduce
(
new
ReduceFunction
<
WordWithCount
>()
{
@Override
public
WordWithCount
reduce
(
WordWithCount
a
,
WordWithCount
b
)
{
return
new
WordWithCount
(
a
.
word
,
a
.
count
+
b
.
count
);
}
});
// print the results with a single thread, rather than in parallel
windowCounts
.
print
().
setParallelism
(
1
);
env
.
execute
(
"Socket Window WordCount"
);
}
// Data type for words with count
public
static
class
WordWithCount
{
public
String
word
;
public
long
count
;
public
WordWithCount
()
{}
public
WordWithCount
(
String
word
,
long
count
)
{
this
.
word
=
word
;
this
.
count
=
count
;
}
@Override
public
String
toString
()
{
return
word
+
" : "
+
count
;
}
}
}
写
预览
Markdown
格式
0%
请重试
or
附加一个文件
附加文件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录