Flink入门连接kafka异常处理Caused by: org.apache.flink.api.common.functions.InvalidTypesException: The types

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'Custom Source' could not be determined automatically, due to type erasure. You can give type information hints by using the returns(...) method on the result of the transformation call, or by letting your function implement the 'ResultTypeQueryable' interface.
    at org.apache.flink.streaming.api.transformations.StreamTransformation.getOutputType(StreamTransformation.java:420)
    at org.apache.flink.streaming.api.datastream.DataStream.addSink(DataStream.java:1184)
    at Streaming.WriteToKafka.main(WriteToKafka.java:29)
Caused by: org.apache.flink.api.common.functions.InvalidTypesException: The types of the interface org.apache.flink.streaming.api.functions.source.SourceFunction could not be inferred. Support for synthetic interfaces, lambdas, and generic or raw types is limited at this point
    at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterType(TypeExtractor.java:1239)
    at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterTypeFromGenericType(TypeExtractor.java:1263)
    at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterType(TypeExtractor.java:1226)
    at org.apache.flink.api.java.typeutils.TypeExtractor.privateCreateTypeInfo(TypeExtractor.java:789)
    at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfo(TypeExtractor.java:769)
    at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1461)
    at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1416)
    at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1398)
    at Streaming.WriteToKafka.main(WriteToKafka.java:27)

Process finished with exit code 1

 

1.在上一个工程里面直接创建新的主函数就ok了

这里面还是有个坑。注意下

【尖叫提示】:

自己写的类实现SourceFunction这里的泛型一定要加上!如果不加则会报一下错误。

publicstaticclassSimpleStringGeneratorimplementsSourceFunction<String>{

 

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Exception in thread "main" org.apache.flink.api.common.functions.InvalidTypesException: The return type of function 'Custom Source' could not be determined automatically, due to type erasure. You can give type information hints by using the returns(...) method on the result of the transformation call, or by letting your function implement the 'ResultTypeQueryable' interface.

at org.apache.flink.streaming.api.transformations.StreamTransformation.getOutputType(StreamTransformation.java:420)

at org.apache.flink.streaming.api.datastream.DataStream.addSink(DataStream.java:1184)

at Streaming.WriteToKafka.main(WriteToKafka.java:29)

Caused by: org.apache.flink.api.common.functions.InvalidTypesException: The types of the interface org.apache.flink.streaming.api.functions.source.SourceFunction could not be inferred. Support for synthetic interfaces, lambdas, and generic or raw types is limited at this point

at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterType(TypeExtractor.java:1239)

at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterTypeFromGenericType(TypeExtractor.java:1263)

at org.apache.flink.api.java.typeutils.TypeExtractor.getParameterType(TypeExtractor.java:1226)

at org.apache.flink.api.java.typeutils.TypeExtractor.privateCreateTypeInfo(TypeExtractor.java:789)

at org.apache.flink.api.java.typeutils.TypeExtractor.createTypeInfo(TypeExtractor.java:769)

at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1461)

at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1416)

at org.apache.flink.streaming.api.environment.StreamExecutionEnvironment.addSource(StreamExecutionEnvironment.java:1398)

at Streaming.WriteToKafka.main(WriteToKafka.java:27)

主要代码如下:

package Streaming;



import org.apache.flink.api.common.serialization.SimpleStringSchema;

import org.apache.flink.streaming.api.datastream.DataStreamSource;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer09;



import java.util.Properties;



/**

 * Created with IntelliJ IDEA.

 * User: @ziyu  freedomziyua@gmail.com

 * Date: 2018-09-10

 * Time: 11:31

 * Description: Streaming.WriteToKafka

 */

public class WriteToKafka {





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



        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        Properties properties = new Properties();

        properties.setProperty("bootstrap.servers", "192.168.2.41:9092");



        DataStreamSource stream = env.addSource(new SimpleStringGenerator());



        stream.addSink(new FlinkKafkaProducer09("flink-demo", new SimpleStringSchema(), properties));

        try {

            env.execute();

        }catch (Exception e ){

            e.printStackTrace();

        }

    }

    /**

     * 一个简单的类去生产数据

     */

    public static class SimpleStringGenerator implements SourceFunction<String>{

        private static final long serialVersionUID = 119007289730474249L;

        boolean running = true;

        long i = 0;





        public void run(SourceContext ctx) throws Exception {

            while (running){

                ctx.collect("Flink----"+(i++));

                Thread.sleep(100);

            }

        }

        public void cancel() {

            running = false;

        }

    }



}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值