Flink入门之WordCount(一)

一、需求

1、Linux端通过nc -lk 开启一个服务, 监听9999端口, 然后不断输入单词;

2、编写Flink程序,连接上述服务端9999端口,实时读取单词, 并对结果进行累加

二、代码实现

1、服务端

[root@master ~]# nc -lk 9999

 Tips:如果没有安装nc,按如下方式安装(Centos):

[root@master ~]# yum install -y nc

2、Flink代码

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();

    }
}

3、执行结果

(1)服务端不断输入单词

hadoop hive spark
jave
hello hello
hello
hi
word
word

(2)Flink程序实时计算单词累加结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值