赞
踩
1、Linux端通过nc -lk 开启一个服务, 监听9999端口, 然后不断输入单词;
2、编写Flink程序,连接上述服务端9999端口,实时读取单词, 并对结果进行累加
[root@master ~]# nc -lk 9999
Tips:如果没有安装nc,按如下方式安装(Centos):
[root@master ~]# yum install -y nc
- package com.wakedata.stuty;
-
- import org.apache.flink.api.common.functions.FlatMapFunction;
- import org.apache.flink.api.java.functions.FlatMapIterator;
- import org.apache.flink.api.java.functions.KeySelector;
- import org.apache.flink.api.java.tuple.Tuple2;
- import org.apache.flink.streaming.api.datastream.DataStreamSource;
- import org.apache.flink.streaming.api.datastream.KeyedStream;
- import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
- import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
- import org.apache.flink.util.Collector;
- import org.apache.flink.util.TimeUtils;
- import org.omg.PortableInterceptor.INACTIVE;
-
- import java.util.Iterator;
- import java.util.List;
-
- public class wordcount {
- public static void main(String[] args) throws Exception {
-
- //1. 获取Flink编程入口环境
- StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
-
-
- //2. 通过socketTextStream 连接服务端9999端口,获取 input DataSource
- DataStreamSource<String> inputStreaming = env.socketTextStream("master", 9999);
-
- //3.分词
- SingleOutputStreamOperator<Tuple2<String, Integer>> wordcount = inputStreaming.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
-
- @Override
- public void flatMap(String s, Collector<Tuple2<String, Integer>> out) throws Exception {
- String[] words = s.split("\\s+");
- for (String word : words) {
- out.collect(new Tuple2(word, 1));
- }
- }
- });
-
-
- //4.按单词分组
- KeyedStream<Tuple2<String, Integer>, String> wordgroup = wordcount.keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
-
-
- @Override
- public String getKey(Tuple2<String, Integer> wordcount) throws Exception {
- //按照单词分组
- return wordcount.getField(0);
- }
- });
-
-
- SingleOutputStreamOperator<Tuple2<String, Integer>> result =
- //5.累加
- wordgroup.sum(1);
-
- //打印结果
- result.print();
-
- //开启flink程序
- env.execute();
-
- }
- }

- hadoop hive spark
- jave
- hello hello
- hello
- hi
- word
- word
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。