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

三、使用 nc -lk 监听socketTextStream

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

三、使用 nc -lk 监听socketTextStream

一、所需命令及代码

 linux命令

[root@localhost ~]# nc -lk 7777
-bash: nc: command not found

 yum install -y nc

[root@localhost ~]# nc -lk 7777      
hello world nihao are you

代码

package com.robert.flink.wcTest;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FlinStreamSocketWordCountPrintTest {

    public static void main(String[] args) throws Exception {

        //get runtime environment
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //read content into dataset from file
        DataStream stringDataSet = env.socketTextStream("192.168.56.10",7777);


        //count word appeared times
        DataStream> sum = stringDataSet.flatMap((FlatMapFunction>) (value, collector) -> {

            String[] words = value.split(" ");
            for (String word : words) {
                collector.collect(new Tuple2<>(word, 1));
            }
        }).returns(Types.TUPLE(Types.STRING, Types.INT)).keyBy(0).sum(1);

        //print content of dataset
        sum.print();

        env.execute();

    }

}





二、流程及效果

流程:1.先运行以上linux命令 中的nc -lk 7777  如果没有nc则yum安装

          2.然后运行java代码开启flink的socket流监听

         然后输入数据

效果:输入单词 以空格分开 一次换行打印一次 分组统计出现次数如下

 

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

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

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