• Flink入门使用


    完全参考:Flink1.3QuickStart

    启动本地运行

    首先找一台安装了hadoop的linux。
    将安装包解压,到bin目录启动local模式的脚本。

    tar -zxvf flink-1.3.1-bin-hadoop26-scala_2.11.tgz
    ./start-local.sh
    

    运行wordCount例子

    这个例子从sokect端口中每隔5秒读取其中的输入并进行记数。

    //执行完nc输入单词,程序会开始记数。
    nc -l 9001
    //开另一个xshell,执行运行程序的命令
    ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9001
    //到log目录下可以看到输出了记数的文件
    

    运行的jar中的源码如下:

    package org.apache.flink.streaming.examples.socket;
    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;
    
    @SuppressWarnings("serial")
    public class SocketWindowWordCount {
    
    	public static void main(String[] args) throws Exception {
    
    		// the host and the port to connect to
    		final String hostname;
    		final int port;
    		try {
    			final ParameterTool params = ParameterTool.fromArgs(args);
    			hostname = params.has("hostname") ? params.get("hostname") : "localhost";
    			port = params.getInt("port");
    		} catch (Exception e) {
    			System.err.println("No port specified. Please run 'SocketWindowWordCount " +
    				"--hostname <hostname> --port <port>', where hostname (localhost by default) " +
    				"and port is the address of the text server");
    			System.err.println("To start a simple text server, run 'netcat -l <port>' and " +
    				"type the input text into the command line");
    			return;
    		}
    
    		// get the execution environment
    		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    
    		// get input data by connecting to the socket
    		DataStream<String> text = env.socketTextStream(hostname, 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))
    
    				.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;
    		}
    	}
    }
    

    创建flink项目

    window的命令行执行以下命令即可下载一个模板项目,导入IDE中就可以愉快地撸了。

    mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-java -DarchetypeVersion=1.3.0
    
  • 相关阅读:
    (转) CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)
    服务器修改密码后,发布的网站报“500内部服务器错误”
    关于“/”应用程序中的服务器错误 之解决方案
    (转)根据IP返回对应的位置信息
    (转)C# DateTime格式化大全
    线包字效果
    (转)VS2012网站发布详细步骤
    HTML5 为什么这么火?
    百度地图下拉框搜索建议,并自动添加标注点
    js中检查时间段列表是否有交叉
  • 原文地址:https://www.cnblogs.com/stillcoolme/p/7258933.html
Copyright © 2020-2023  润新知