Flink-WordCount

本文基于Flink1.14

pom.xml

<properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <flink.version>1.14.3flink.version>
    <log4j.version>2.19.0log4j.version>
  properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.flinkgroupId>
      <artifactId>flink-javaartifactId>
      <version>${flink.version}version>
    dependency>

    <dependency>
      <groupId>org.apache.flinkgroupId>
      <artifactId>flink-streaming-java_2.12artifactId>
      <version>${flink.version}version>
    dependency>

    <dependency>
      <groupId>org.apache.flinkgroupId>
      <artifactId>flink-clients_2.12artifactId>
      <version>${flink.version}version>
    dependency>

    <dependency>
      <groupId>org.apache.flinkgroupId>
      <artifactId>flink-table-api-javaartifactId>
      <version>${flink.version}version>
    dependency>

    <dependency>
      <groupId>org.apache.flinkgroupId>
      <artifactId>flink-table-api-java-bridge_2.12artifactId>
      <version>${flink.version}version>
    dependency>
    
  dependencies>

Flink程序

package org.example.datastream.wordcount;

import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SocketTextStreamFunction;
import org.apache.flink.util.Collector;


public class Main {
    public static void main( String[] args ) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // nc -lk 9901
        DataStreamSource<String> source = env.addSource(new SocketTextStreamFunction("127.0.0.1", 9901, "\n", 5));
        env.setParallelism(1);

        source.flatMap((String line, Collector<Tuple2<String, Integer>> coc) -> {
            String[] split = line.split("\\s+");
            for (String s : split) {
                coc.collect(new Tuple2<>(s, 1));
            }
        }).returns(new TypeHint<Tuple2<String, Integer>>() {})
                .keyBy(d -> d.f0).sum(1).print();

        env.execute();
    }
}

启动一个socket进程

# linux系统自带nc
$ nc -lk 9901

# windows需要自行下载一个,执行 $ nc -lp 9901

你可能感兴趣的:(flink,java,大数据)