简体   繁体   English

Kafka Connect 无法将自定义存储接收器分区器投射到分区器接口

[英]Kafka Connect cannot cast custom storage sink partitioner to Partitioner interface

I need to create a custom partitioner for the kafka connect S3 sink plugin .我需要为 kafka 连接S3 接收器插件创建一个自定义分区 I've extended the HourlyPartitioner in a custom class using kotlin:我已经使用kotlin在自定义类中扩展了 HourlyPartitioner:

class RawDumpHourlyPartitioner<T> : HourlyPartitioner<T>() {
...
}

and changed my connector config accordingly to use the custom class:并相应地更改了我的连接器配置以使用自定义类:

"partitioner.class": "co.myapp.RawDumpHourlyPartitioner",

I've then created our jar (we use shadow ) and included it into a custom docker image based on the kafka connect image (the image version is the same as the dependencies we use in the project):然后我创建了我们的 jar(我们使用了shadow )并将它包含在一个基于 kafka 连接镜像的自定义 docker 镜像中(镜像版本与我们在项目中使用的依赖项相同):

FROM gradle:6.0-jdk8 as builder
WORKDIR /app
ADD . .
RUN gradle clean shadowJar

FROM confluentinc/cp-kafka-connect:5.3.2

COPY --from=builder /app/build/libs/kafka-processor-0.1-all.jar /usr/share/java/kafka/kafka-processor.jar

When the connector starts I get this error:当连接器启动时,我收到此错误:

ERROR WorkerSinkTask{id=staging-raw-dump-0} Task threw an uncaught and unrecoverable exception (org.apache.kafka.connect.runtime.WorkerTask)
java.lang.ClassCastException: co.myapp.RawDumpHourlyPartitioner cannot be cast to io.confluent.connect.storage.partitioner.Partitioner

To double check I've created a java file that tries to instantiate the class and it didn't throw any error:为了仔细检查,我创建了一个尝试实例化类的 java 文件,但它没有抛出任何错误:

import io.confluent.connect.storage.partitioner.Partitioner;

public class InstantiateTest {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<? extends Partitioner<?>> partitionerClass =
                (Class<? extends Partitioner<?>>) Class.forName("co.myapp.RawDumpHourlyPartitioner");

        Partitioner<?> partitioner = partitionerClass.newInstance();
    }
}

Looking at the kafka connect guide it says:查看 kafka 连接指南,它说:

A Kafka Connect plugin is simply a set of JAR files where Kafka Connect can find an implementation of one or more connectors, transforms, and/or converters. Kafka Connect 插件只是一组 JAR 文件,Kafka Connect 可以在其中找到一个或多个连接器、转换和/或转换器的实现。 Kafka Connect isolates each plugin from one another so that libraries in one plugin are not affected by the libraries in any other plugins. Kafka Connect 将每个插件彼此隔离,以便一个插件中的库不受任何其他插件中的库的影响。 This is very important when mixing and matching connectors from multiple providers.这在混合和匹配来自多个提供商的连接器时非常重要。

This means that since I'm using the S3 sink connector, I have to put my jar with the custom partitioner in the directory of the S3 plugin.这意味着,由于我使用的是 S3 接收器连接器,因此我必须将带有自定义分区器的 jar 放在 S3 插件的目录中。

Moving the jar file to /usr/share/java/kafka-connect-s3 solved the issue将 jar 文件移动到/usr/share/java/kafka-connect-s3解决了这个问题

In the comments I've mentioned that my jar also includes a custom subject name strategy that we use in the main kafka-connect config (the env variables), in that case the jar needs to be in the /usr/share/java/kafka folder在我提到的评论中,我的 jar 还包含我们在主 kafka-connect 配置(环境变量)中使用的自定义主题名称策略,在这种情况下,jar 需要位于/usr/share/java/kafka文件夹

Update : as cricket_007 mentioned it's better to put the custom partitioner jar into the /usr/share/java/kafka-connect-storage-common folder which is where all the other partitioners are更新:正如cricket_007提到的,最好将自定义分区器 jar 放入/usr/share/java/kafka-connect-storage-common文件夹中,这是所有其他分区器所在的位置

Depending on which Sink you use, We need to push partitioner class there, as in our case when we were using Confluent Kafka 5.5 , and connector class Azure Gen2 Storage.根据您使用的 Sink,我们需要将分区器类推送到那里,就像我们使用 Confluent Kafka 5.5 和连接器类 Azure Gen2 存储一样。

For that we need to write custom partitioner similar to following Repo in Github .为此,我们需要编写类似于Github 中的 Repo 的自定义分区器。

Then We place the custom JAR in following path:然后我们将自定义 JAR 放在以下路径中:

/usr/share/confluent-hub-components/confluentinc-kafka-connect-azure-data-lake-gen2-storage/lib/ 

After which our connector class working successfully!之后我们的连接器类成功运行!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM