本文主要是介绍Flink学习笔记1--安装NC,运行Flink实时处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NC下载地址:https://eternallybored.org/misc/netcat/
解压到自己磁盘某地。然后在CMD中运行NC
最终效果如下:
代码如下:
package com.cl;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.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;/*** @author chenli* @Description TODO* @date 2021/7/5-14:46*/
public class StreamWordCount {public static void main(String[] args) throws Exception{//流处理执行环境StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();//设置并行度env.setParallelism(6);//从程序启动参数种提取配置项ParameterTool p = ParameterTool.fromArgs(args);String host = p.get("host");int port = p.getInt("port");//从socket文本流读取数据DataStream<String> inputDataStream = env.socketTextStream(host,port);//基于数据流进行计算DataStream<Tuple2<String,Integer>> resultStream = inputDataStream.flatMap(new MyFlatMapper()).keyBy(0).sum(1);resultStream.print();env.execute();}//自定义类,实现FlatMapFunction接口public static class MyFlatMapper implements FlatMapFunction<String, Tuple2<String,Integer>> {@Overridepublic void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {//按照空格分词String[] words = value.split(" ");for (String word:words){out.collect(new Tuple2<String, Integer>(word,1));}}}
}
先启动NC中的命令
nc -L -p 7777
然后设置IDEA,设置参数
--host localhost --port 7777
这篇关于Flink学习笔记1--安装NC,运行Flink实时处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!