flink结合Java_flink基础wordcount demo-java版

该博客介绍了一个使用Flink进行流处理的Java示例,通过socketTextStream读取数据,实现WordCount功能。文章详细展示了如何定义FlatMapFunction进行数据拆分,按关键词分组并设置时间窗口进行计数,最后打印结果并执行任务。
摘要由CSDN通过智能技术生成

import org.apache.flink.api.common.functions.FlatMapFunction;

import org.apache.flink.api.java.tuple.Tuple2;

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 FlinkFirstApp2 {

public static void main(String[] args)throws Exception{

final int port;

final String hostname;

try{

final ParameterTool params=ParameterTool.fromArgs(args);

port=params.getInt("port",9999);

hostname=params.get("hostname","192.168.21.138");

}catch (Exception e){

System.err.println("No port or hostname specified.Please run check your parameters");

return;

}

final StreamExecutionEnvironment env=StreamExecutionEnvironment.getExecutionEnvironment();

DataStream lines=env.socketTextStream(hostname,port);

DataStream> result=lines.flatMap(new FlatMapFunction>() {

public void flatMap(String s, Collector> collector){

String[] datas=s.split(" ");

for (String data:datas){

collector.collect(new Tuple2(data,1));

}

}

}).keyBy(0)//以Tuple的第一个字段分组

.timeWindow(Time.seconds(4),Time.seconds(2))//每隔2秒算4秒

.sum(1);//对Tuple的第二个字段求和

result.print().setParallelism(1);

env.execute("FlinkFirstApp");

}

}

/**

* recognized as POJO:lombok plugin

* 1)public

* 2)without arguments constructor

* 3)getter/setter

* 4)serialize(有些东西需要序列化)

*

*/

public class WC {

private Stringword;

private long count;

public WC(String word, long count) {

this.word = word;

this.count = count;

}

public WC() {

}

public StringgetWord() {

return word;

}

public long getCount() {

return count;

}

public void setWord(String word) {

this.word = word;

}

public void setCount(long count) {

this.count = count;

}

@Override

public StringtoString() {

return "WC{" +

"word='" +word +'\'' +

", count=" +count +

'}';

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值