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 <hostname> --port <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 <port>' and " +
                    "type the input text into the command line");
            return;
        }

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream<String> str=env.socketTextStream(hostname,port);
        DataStream<WordCount> wordcounts=str.flatMap(new FlatMapFunction<String, WordCount>() {
            public void flatMap(String s, Collector<WordCount> collector) throws Exception {
                for(String word:s.split("\\s")){
                    collector.collect(new WordCount(word,1));
                }
            }
        }).keyBy("word").timeWindow(Time.seconds(5)).reduce(new ReduceFunction<WordCount>() {
            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

<dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-java</artifactId>
        <version>1.9.0</version>

    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.flink/flink-streaming-java -->
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-streaming-java_2.12</artifactId>
        <version>1.9.0</version>
        <scope>provided</scope>
    </dependency>

异常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.<init>(PackagedProgram.java:206)
    at org.apache.flink.client.program.PackagedProgram.<init>(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<com.test.pojp.WordCount>) cannot be used as key.
    org.apache.flink.api.common.operators.Keys$ExpressionKeys.<init>(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 +
                '}';
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 拍戏那三个月,我真的是像海绵吸水,现场有太多的东西要学,这是在学校课堂上完全没有的。仙剑摄制组是一个很高效的团队,...
    漫漫阅读 161评论 1 0
  • 你要让你自己有目标别忘记你的一切,现在你已经没有时间了,所以,你只有让自己变得更好,才会实现你的梦想。今天在开课的...
    赵公子的树洞阅读 77评论 0 0
  • 今天是什么日子 起床:5:00 就寝:22:30 天气:晴 心情:一般 纪念日:不开心的事 任务清单 昨日完成的任...
    桔子_e93c阅读 91评论 0 0
  • 我们收到一口老蜂箱,用原木挖出来的圆桶状蜂箱,临时放在墙边,今天下午,有蜜蜂自己来安家。 早上,我们去看了大池塘边...
    尚生耕本阅读 508评论 0 3
  • 1.编程的艺术就是处理复杂性的艺术。 2.简单是可靠的先决条件。 3.优秀的程序员很清楚自己的能力是有限的,所以他...
    337b94dc718f阅读 502评论 0 0