flink入门例子,经典wordCount

步骤:运行socket服务,再运行flink计算服务即可

这是提供数据源的服务

package wikiedits;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {

	public static void main(String[] args) {
		try {
			ServerSocket ss = new ServerSocket(9000);
			System.out.println("启动服务器....");
			Socket s = ss.accept();
			System.out.println("客户端:" + s.getInetAddress().getLocalHost() + "已连接到服务器");

			BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			// 读取客户端发送来的消息
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
			while (true) {
				bw.write(((int) (Math.random() * 100)) % 10 + "\t");
				bw.write(((int) (Math.random() * 100)) % 10 + "\n");
				bw.flush();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

这是flink计算wordcount的代码

package wikiedits;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.FoldFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditEvent;
import org.apache.flink.streaming.connectors.wikiedits.WikipediaEditsSource;
import org.apache.flink.util.Collector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FlinkDemoApplication {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(FlinkDemoApplication.class, args);
		wordCountDemo();
		// officialDemo();
	}

	private static void wordCountDemo() throws Exception {
		// 创建环境
		StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
		// 设置数据源,这里相当于socket客户端从上面的server取数据
		DataStream text = env.socketTextStream("localhost", 9000, "\n");
		// 数据源数据处理,将文本串解析成(单词-数量)元组
		DataStream> wordCounts = text
				.flatMap(new FlatMapFunction>() {
					@Override
					public void flatMap(String value, Collector> out) {
//						System.out.println(value);
						for (String word : value.split("\\s")) {
							out.collect(Tuple2.of(word, 1));
						}
					}
				});
		DataStream> windowCounts = wordCounts//
				.keyBy(0)// 设置key为元组的第一个字段
				.timeWindow(Time.seconds(5))// 设置时间窗口
				.sum(1);// 对元组的第二个字段做求和操作
		// 打印结果
		windowCounts.print().setParallelism(1);
		// 启动计算
		env.execute("Socket Window WordCount");

	}
}


你可能感兴趣的:(大数据)