flink之wordcount异常记录

pojo代码

package com.test.pojo;

public class WordCount {
    public  String word;
    public  int count;

    public WordCount(String word, int count) {
        this.word = word;
        this.count = count;
    }

    @Override
    public String toString() {
        return "WordCount{" +
                "word='" + word + '\'' +
                ", count=" + count +
                '}';
    }
}
package com.test;

import com.test.pojp.WordCount;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;
@SuppressWarnings("serial")
public class Flink01 {
    public static void main(String[] args) {
        final  String hostname;
        final  int port;
        System.out.println("-----------aaaaaaaaaaaaaaaaaaa--");
        try {
            final  ParameterTool parms = ParameterTool.fromArgs(args);
            hostname=parms.has("hostname")?parms.get("hostname"):"localhost";
            port=parms.getInt("port");
            System.out.println(port+"-------------"+hostname);
        } catch (Exception e) {
            System.err.println("No port specified. Please run 'SocketWindowWordCount " +
                    "--hostname  --port ', where hostname (localhost by default) " +
                    "and port is the address of the text server");
            System.err.println("To start a simple text server, run 'netcat -l ' and " +
                    "type the input text into the command line");
            return;
        }

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream str=env.socketTextStream(hostname,port);
        DataStream wordcounts=str.flatMap(new FlatMapFunction() {
            public void flatMap(String s, Collector collector) throws Exception {
                for(String word:s.split("\\s")){
                    collector.collect(new WordCount(word,1));
                }
            }
        }).keyBy("word").timeWindow(Time.seconds(5)).reduce(new ReduceFunction() {
            public WordCount reduce(WordCount wordCount, WordCount t1) throws Exception {
                return new WordCount(wordCount.word,wordCount.count+t1.count);
            }
        });
        wordcounts.print().setParallelism(1);
        try {
            env.execute("socket wordcount window");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

pom.xml


        org.apache.flink
        flink-java
        1.9.0

    
    
    
        org.apache.flink
        flink-streaming-java_2.12
        1.9.0
        provided
    

异常log_1

Setting HADOOP_CONF_DIR=/etc/hadoop/conf because no HADOOP_CONF_DIR was set.

------------------------------------------------------------
 The program finished with the following exception:

org.apache.flink.client.program.ProgramInvocationException: Neither a 'Main-Class', nor a 'program-class' entry was found in the jar file.
    at org.apache.flink.client.program.PackagedProgram.getEntryPointClassNameFromJar(PackagedProgram.java:643)
    at org.apache.flink.client.program.PackagedProgram.(PackagedProgram.java:206)
    at org.apache.flink.client.program.PackagedProgram.(PackagedProgram.java:140)
    at org.apache.flink.client.cli.CliFrontend.buildProgram(CliFrontend.java:799)
    at org.apache.flink.client.cli.CliFrontend.run(CliFrontend.java:196)
    at org.apache.flink.client.cli.CliFrontend.parseParameters(CliFrontend.java:1010)
    at org.apache.flink.client.cli.CliFrontend.lambda$main$10(CliFrontend.java:1083)
    at org.apache.flink.runtime.security.NoOpSecurityContext.runSecured(NoOpSecurityContext.java:30)
    at org.apache.flink.client.cli.CliFrontend.main(CliFrontend.java:1083)

解决方法如下,打包方式要调整,去掉图中标黄部分


1.png

异常记录2

 The program finished with the following exception:

This type (GenericType) cannot be used as key.
    org.apache.flink.api.common.operators.Keys$ExpressionKeys.(Keys.java:330)
    org.apache.flink.streaming.api.datastream.DataStream.keyBy(DataStream.java:337)
    com.test.Flink01.main(Flink01.java:40)

上面的error原因是pojo对象没有添加无参构造
添加这行代码重新编包即可

package com.test.pojp;

public class WordCount {
    public  String word;
    public  int count;

    public WordCount(String word, int count) {
        this.word = word;
        this.count = count;
    }
    public WordCount(){};//就是这行
    @Override
    public String toString() {
        return "WordCount{" +
                "word='" + word + '\'' +
                ", count=" + count +
                '}';
    }
}

你可能感兴趣的:(flink之wordcount异常记录)