在Kafka Connect中使用自定义转换器?

时间:2017-10-12 14:23:02

标签: apache-kafka apache-kafka-connect

我试图在Kafka Connect上使用自定义转换器,但我似乎无法做到正确。我希望有人有这方面的经验,可以帮我搞清楚!

初始情况

会发生什么?

当连接器启动时,它们会正确装入罐子并找到自定义转换器。实际上,这就是我在日志中看到的内容:

[2017-10-10 13:06:46,274] INFO Registered loader: PluginClassLoader{pluginLocation=file:/opt/custom-connectors/custom-converter-1.0-SNAPSHOT.jar} (org.apache.kafka.connect.runtime.isolation.DelegatingClassLoader:199)
[2017-10-10 13:06:46,274] INFO Added plugin 'custom.CustomStringConverter' (org.apache.kafka.connect.runtime.isolation.DelegatingClassLoader:132)
[...]
[2017-10-10 13:07:43,454] INFO Added aliases 'CustomStringConverter' and 'CustomString' to plugin 'custom.CustomStringConverter' (org.apache.kafka.connect.runtime.isolation.DelegatingClassLoader:293)

然后我将JSON配置POST到其中一个连接器节点以创建我的连接器:

{
  "name": "hdfsSinkCustom",
  "config": {
    "topics": "yellow",
    "tasks.max": "1",
    "key.converter": "org.apache.kafka.connect.storage.StringConverter",
    "value.converter": "custom.CustomStringConverter",
    "connector.class": "io.confluent.connect.hdfs.HdfsSinkConnector",
    "hdfs.url": "hdfs://hdfs-namenode:8020/hdfs-sink",
    "topics.dir": "yellow_storage",
    "flush.size": "1",
    "rotate.interval.ms": "1000"
  }
}

并收到以下回复:

{
   "error_code": 400,
   "message": "Connector configuration is invalid and contains the following 1 error(s):\nInvalid value custom.CustomStringConverter for configuration value.converter: Class custom.CustomStringConverter could not be found.\nYou can also find the above list of errors at the endpoint `/{connectorType}/config/validate`"
}

我错过了什么?

如果我尝试运行Kafka Connect stadnalone,则错误消息是相同的。

有人有过这个吗?我错过了什么?

1 个答案:

答案 0 :(得分:4)

好的,感谢Philip Schmitt在Kafka Users邮件列表中找到了解决方案。

他提到了这个问题:https://issues.apache.org/jira/projects/KAFKA/issues/KAFKA-6007,这确实是我面临的问题。

引用他:

  

为了测试这个,我只是将我的SMT jar复制到我正在使用的连接器的文件夹中并调整了plugin.path属性。

实际上,我通过将转换器放在连接器的文件夹中来消除这个错误。

我还尝试了其他方法:创建自定义连接器并将该自定义连接器与自定义转换器一起使用,两者都作为插件加载。它也有效。

摘要:转换器由连接器加载。如果您的连接器是插件,您的转换器也应该。如果连接器不是插件(与kafka connect distrib捆绑在一起),则转换器也不应该是。