根据类查找缺少的jar包,在已有jar包内查找类

背景

提交Flink作业的时候报错:
NoClassDefFoundError:org.apache.kafka.connect.source.SourceRecord

java.lang.NoClassDefFoundError: org/apache/kafka/connect/source/SourceRecord
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
	at java.lang.Class.getDeclaredMethod(Class.java:2128)

	at com.slankka.rtc.flinkplatform.sql.SqlJob.executeStatement(SqlJob.java:220)
	at com.slankka.rtc.flinkplatform.sql.SqlJob.start(SqlJob.java:138)
	at com.slankka.rtc.flinkplatform.sql.SqlJobDriver.main(SqlJobDriver.java:14)

Caused by: java.lang.ClassNotFoundException: org.apache.kafka.connect.source.SourceRecord
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at org.apache.flink.util.FlinkUserCodeClassLoader.loadClassWithoutExceptionHandling(FlinkUserCodeClassLoader.java:64)
	at org.apache.flink.util.ChildFirstClassLoader.loadClassWithoutExceptionHandling(ChildFirstClassLoader.java:74)
	at org.apache.flink.util.FlinkUserCodeClassLoader.loadClass(FlinkUserCodeClassLoader.java:48)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 60 more

找出这个类在哪个jar包,哪个artifact

根据类查找jar包

方法

访问search.maven.org
看包名应该是 kafka的包,那么Kafka的包 groupId:org.apache.kafka

输入 c:SourceRecord g:org.apache.kafka即可定位

c:SourceRecord g: org.apache.kafka

这个包是connect-api。

于是下载 connect-api 即可解决。

在jar包内查找类

分析某一个class在哪个jar包内

for i in `ls`;do echo $i && jar -tf $i | grep SourceRecord.class; done

通过执行此命令,可以找到对应的jar包。

Flink CDC2.2兼容性问题探究

经过查找发现这里有一个Shade的类

flink-sql-connector-mysql-cdc-2.1.1.jar
com/ververica/cdc/connectors/shaded/org/apache/kafka/connect/source/SourceRecord.class

于是猜测是这个flink-sql-connector-mysql-cdc 使用了Kafka的connect-api。

继续分析发现这个库的引用来自:
io.debezium:debezium-embedded:1.5.4.FINAL

点击查看代码
<artifactId>debezium-embedded</artifactId>
<name>Debezium Embedded</name>
<packaging>jar</packaging>
<dependencies>
    <dependency>
        <groupId>io.debezium</groupId>
        <artifactId>debezium-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>connect-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>connect-runtime</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>connect-json</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>connect-file</artifactId>
    </dependency>
...
posted @ 2022-11-30 15:47  一杯半盏  阅读(935)  评论(0编辑  收藏  举报