Flink 奈学P6笔记

步骤一:创建

// 版本一
public class WordCount{
    public static void main(String[] args) throws Exception{
        // 1. 创建程序入口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2. 数据的输入
        DataStreamSource<String> myDataStream = env.socketTextStream("192.168.85.111", 1234);
        // 3. 数据得处理
        // 第一个参数是输入,第二个参数是输出
        // 为什么是tuple2的类型,想要把结果变为键值对的结构,key-value就是tuple2的格式。
        myDataStream.flatMap(new FlatMapFunction<String,Tuple2<String, Integer>>(){
            @Override
            public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception{
                String[] fields = line.split(",");
                for(String word:fields){
                    out.collect(new Tuple2<>(word, 1));
                    // out.collect(Tuple2.of(word, 1));
                    // 让每个单词只出现一次
                }
            }
        }).keyBy(0).sum(1)
        // keyBy进行分组,keyBy的参数0是按照哪个元素位置进行分组。根据value进行sum
        
        // 4. 数据的输出
        result.print();
        // 5. 启动应用程序
        env.execute("WordCount");
    }
}


// 启动:nc -l -p 1234
// 本地运行代码要把scope的标签注释掉;
// 发射数据
// 版本二
public class WordCount{
    public static void main(String[] args) throws Exception{
        // 1. 创建程序入口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2. 数据的输入
        DataStreamSource<String> myDataStream = env.socketTextStream("192.168.85.111", 1234);
        
        // 把输出类型看作成一个对象;
        SingleOutStreamOperator<WordAndOne> result = myDataStream.flatMap(new FlatMapFunction<String, WordAndOne>(){
            @Override
            public void flatMap(String line, Collector<WordAndOne> out) throws Exception{
                String[] fields = line.split(",");
                for(String word:fields){
                    out.collect(new WordAndOne(word, 1));
                    // out.collect(Tuple2.of(word, 1));
                    // 让每个单词只出现一次
                }
            }
        }).keyBy(0).sum(1)
        // keyBy进行分组,keyBy的参数0是按照哪个元素位置进行分组。根据value进行sum
        
        // 4. 数据的输出
        result.print();
        // 5. 启动应用程序
        env.execute("WordCount");
    }

    public static class WordAndOne{
        private String word;
        private Integer count;

        public WordAndOne(){
            
        }
        public WordAndOne(String word, Integer count){
            this.word = word;
            this.count = count;
        }
        public Stirng getWord(){
            return word;
        }    
        public void serWord(String word){
            this.word = word;
        }
        public Integer getCount(){
            return count;
        }
        public void serCount(Integer count){
            this.count = count;
        }
        @Override
        public String toString(){
            return "WordAndOne{" +
                "word='" + word + '\"' +
                ", count=" + count +
                '}';
        }
    }
}
// 版本三
// 希望我们在main方法主要写业务逻辑
public class WordCount{
    public static void main(String[] args) throws Exception{
        // 1. 创建程序入口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 2. 数据的输入
        DataStreamSource<String> myDataStream = env.socketTextStream("192.168.85.111", 1234);
        
        // 把输出类型看作成一个对象;
        SingleOutStreamOperator<WordAndOne> result = myDataStream.flatMap(new StringSplitTask()).keyBy(0).sum(1)
        // keyBy进行分组,keyBy的参数0是按照哪个元素位置进行分组。根据value进行sum
        
        // 4. 数据的输出
        result.print();
        // 5. 启动应用程序
        env.execute("WordCount");
    }

    public static class StringSplitTask implements FlatMapFunction<String,WordAndOne>{
        @Override
        public void flatMap(String line, Collector<WordAndOne> out) throws Exception{
            String[] fields = line.split(",");
            for(String word:fields){
                out.collect(new WordAndOne(word, 1));
                // out.collect(Tuple2.of(word, 1));
                // 让每个单词只出现一次
            }
        }
    }

    public static class WordAndOne{
        private String word;
        private Integer count;

        public WordAndOne(){
            
        }
        public WordAndOne(String word, Integer count){
            this.word = word;
            this.count = count;
        }
        public Stirng getWord(){
            return word;
        }    
        public void serWord(String word){
            this.word = word;
        }
        public Integer getCount(){
            return count;
        }
        public void serCount(Integer count){
            this.count = count;
        }
        @Override
        public String toString(){
            return "WordAndOne{" +
                "word='" + word + '\"' +
                ", count=" + count +
                '}';
        }
    }
}

```java
// 版本4
// socket数据源写死了,传参
public class WordCount{
    public static void main(String[] args) throws Exception{
        // 1. 创建程序入口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        ParameterTool parameterTool = ParameterTool.fromArgs(args);
        String hostname = parameter.Tool.get("hostname");
        int port = parameterTool.getInt("port");
        // 2. 数据的输入
        DataStreamSource<String> myDataStream = env.socketTextStream(hostname, port);
        
        // 把输出类型看作成一个对象;
        SingleOutStreamOperator<WordAndOne> result = myDataStream.flatMap(new StringSplitTask()).keyBy(0).sum(1)
        // keyBy进行分组,keyBy的参数0是按照哪个元素位置进行分组。根据value进行sum
        
        // 4. 数据的输出
        result.print();
        // 5. 启动应用程序
        env.execute("WordCount");
    }

    public static class StringSplitTask implements FlatMapFunction<String,WordAndOne>{
        @Override
        public void flatMap(String line, Collector<WordAndOne> out) throws Exception{
            String[] fields = line.split(",");
            for(String word:fields){
                out.collect(new WordAndOne(word, 1));
                // out.collect(Tuple2.of(word, 1));
                // 让每个单词只出现一次
            }
        }
    }

    public static class WordAndOne{
        private String word;
        private Integer count;

        public WordAndOne(){
            
        }
        public WordAndOne(String word, Integer count){
            this.word = word;
            this.count = count;
        }
        public Stirng getWord(){
            return word;
        }    
        public void serWord(String word){
            this.word = word;
        }
        public Integer getCount(){
            return count;
        }
        public void serCount(Integer count){
            this.count = count;
        }
        @Override
        public String toString(){
            return "WordAndOne{" +
                "word='" + word + '\"' +
                ", count=" + count +
                '}';
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

精神抖擞王大鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值