栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

【flink学习笔记】【2】本地模式-流处理wordcount

【flink学习笔记】【2】本地模式-流处理wordcount

文章目录

一、有界流wordcount二、无界流wordcount

一、有界流wordcount
package com.shinho.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple;
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;


public class BoundryWordCount {
    public static void main(String[] args) throws Exception {
        //1创建流式执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource lineDS = env.readTextFile("input/words.txt");

        SingleOutputStreamOperator> wordAndOne = lineDS.flatMap((String line, Collector> out) -> {
            String[] words = line.split(" ");
            for (String word : words) {
                out.collect(Tuple2.of(word, 1L));
            }
        }).returns(Types.TUPLE(Types.STRING, Types.LONG));

        //分组
        KeyedStream, Tuple> keyBy = wordAndOne.keyBy(0);

        //求和
        SingleOutputStreamOperator> sum = keyBy.sum(1);

        //
        sum.print();

        //启动执行
        env.execute();
    }
}

控制台输出结果

1> (xx,1)
7> (kaikai,1)
3> (hello,1)
6> (word,1)
2> (gez,1)
7> (count,1)
3> (hello,2)
3> (hello,3)
3> (hello,4)
6> (word,2)

前面是并行子任务的编码,子任务个数取决于并行度(电脑CPU核数)。同一个任务上才能进行词频叠加。

二、无界流wordcount

监听事件

yum install -y nc
nc -lk 7777
package com.shinho.wc;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple;
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;

public class NoBoundryWordCount {
    public static void main(String[] args) throws Exception {
        // 创建流式环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //2.读取文本流
        DataStreamSource lineDS = env.socketTextStream("192.168.10.132", 7777);

        SingleOutputStreamOperator> wordAndOne = lineDS.flatMap((String line, Collector> out) -> {
            String[] words = line.split(" ");
            for (String word : words) {
                out.collect(Tuple2.of(word, 1L));
            }
        }).returns(Types.TUPLE(Types.STRING, Types.LONG));

        //分组
        KeyedStream, Tuple> keyBy = wordAndOne.keyBy(0);

        //求和
        SingleOutputStreamOperator> sum = keyBy.sum(1);

        //
        sum.print();

        //启动执行
        env.execute();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775368.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号