gpt4 book ai didi

java - Apache Kafka + Apache Camel 集成 + POC 问题 + java.util.Hashtable.put 处的 java.lang.NullPointerException(Hashtable.java :459)

转载 作者:行者123 更新时间:2023-11-30 06:22:18 36 4
gpt4 key购买 nike

这是我的路线构建器。在这里,我尝试将文件中的数据插入主题。稍后,我将传递我的主要方法并使用 Camel 上下文运行它。我尝试了几个代码,但没有一个对我有帮助。我正在研究 Apache kafka - Camel 的 POC。

public class SimpleRouteBuilder extends RouteBuilder {

@Override
public void configure() throws Exception {

String topicName = "test120";
String kafkaServer = "kafka:localhost:9092";
String zooKeeperHost = "zookeeperHost=localhost&zookeeperPort=2181";
String serializerClass = "serializerClass=kafka.serializer.StringEncoder";

String toKafka = "kafka:localhost:9092?topic=test120;zookeeperHost=localhost;zookeeperPort=2181;groupId=group1";


// toKafka = new StringBuilder().append("&").append(serializerClass).toString();

/*new StringBuilder().append(kafkaServer).append("?").append(topicName).append("&")
.append(zooKeeperHost).append("&").append(serializerClass).toString();*/

from("file:C:/inbox?noop=true").to(toKafka);


}
}

这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelloWorld</groupId>
<artifactId>Pallavi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.20.1</version>
</dependency>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-kafka</artifactId>
<version>2.20.1</version>
</dependency>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>

</project>

这是我的主课:

import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;

public class MainApp {

public static void main(String[] args) {
SimpleRouteBuilder routeBuilder = new SimpleRouteBuilder();
CamelContext ctx = new DefaultCamelContext();
try {
ctx.addRoutes(routeBuilder);
ctx.start();
Thread.sleep(5 * 60 * 1000);
ctx.stop();
System.out.println("hi i am working");
}
catch (Exception e) {
e.printStackTrace();
}

}
}

错误是:

java.lang.NullPointerException
at java.util.Hashtable.put(Hashtable.java:459)
at org.apache.camel.component.kafka.KafkaProducer.getProps(KafkaProducer.java:63)
at org.apache.camel.component.kafka.KafkaProducer.doStart(KafkaProducer.java:89)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.util.ServiceHelper.startService(ServiceHelper.java:75)
at org.apache.camel.impl.DeferServiceStartupListener.onCamelContextStarted(DeferServiceStartupListener.java:49)
at org.apache.camel.impl.DefaultCamelContext.safelyStartRouteServices(DefaultCamelContext.java:3859)
at org.apache.camel.impl.DefaultCamelContext.doStartOrResumeRoutes(DefaultCamelContext.java:3638)
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3490)
at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:208)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3249)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3245)
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3268)
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3245)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3168)
at demo.MainApp.main(MainApp.java:13)
Picked up _JAVA_OPTIONS: -Xmx512M -Xms512M

最佳答案

我遵循了相同的教程。但最初,我无法成功运行(使用最新版本的kafka服务器)。我得到以下异常而不是空点异常:

org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> Split[tokenize{body() using token: 
} -> [To[kafka:localhost:9092?topic=test&zookeeperHost=localhost&zookeeperPort=2181&serializerClass=kafka.serializer.StringEncoder]]] <<< in route: Route(route1)[[From[file:C:/input?noop=true]] -> [Split[toke... because of Failed to resolve endpoint: kafka://localhost:9092?serializerClass=kafka.serializer.StringEncoder&topic=test&zookeeperHost=localhost&zookeeperPort=2181 due to: Failed to resolve endpoint: kafka://localhost:9092?serializerClass=kafka.serializer.StringEncoder&topic=test&zookeeperHost=localhost&zookeeperPort=2181 due to: There are 2 parameters that couldn't be set on the endpoint. Check the uri if the parameters are spelt correctly and that they are properties of the endpoint. Unknown parameters=[{zookeeperHost=localhost, zookeeperPort=2181}]
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1303)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:204)
at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:1143)
at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3729)
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3443)
at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:209)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3251)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:3247)
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:3270)
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:3247)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:3163)
at com.parane.kafka.demo.DemoApplication.main(DemoApplication.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: kafka://localhost:9092?serializerClass=kafka.serializer.StringEncoder&topic=test&zookeeperHost=localhost&zookeeperPort=2181 due to: Failed to resolve endpoint: kafka://localhost:9092?serializerClass=kafka.serializer.StringEncoder&topic=test&zookeeperHost=localhost&zookeeperPort=2181 due to: There are 2 parameters that couldn't be set on the endpoint. Check the uri if the parameters are spelt correctly and that they are properties of the endpoint. Unknown parameters=[{zookeeperHost=localhost, zookeeperPort=2181}]
at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:758)
at org.apache.camel.util.CamelContextHelper.getMandatoryEndpoint(CamelContextHelper.java:80)
at org.apache.camel.model.RouteDefinition.resolveEndpoint(RouteDefinition.java:219)
at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:115)
at org.apache.camel.impl.DefaultRouteContext.resolveEndpoint(DefaultRouteContext.java:121)
at org.apache.camel.model.SendDefinition.resolveEndpoint(SendDefinition.java:62)
at org.apache.camel.model.SendDefinition.createProcessor(SendDefinition.java:56)
at org.apache.camel.model.ProcessorDefinition.createProcessor(ProcessorDefinition.java:511)
at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:474)
at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:441)
at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:185)
at org.apache.camel.model.ProcessorDefinition.createChildProcessor(ProcessorDefinition.java:204)
at org.apache.camel.model.SplitDefinition.createProcessor(SplitDefinition.java:101)
at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:562)
at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:523)
at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:239)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1300)
... 17 more
Caused by: org.apache.camel.ResolveEndpointFailedException: Failed to resolve endpoint: kafka://localhost:9092?serializerClass=kafka.serializer.StringEncoder&topic=test&zookeeperHost=localhost&zookeeperPort=2181 due to: There are 2 parameters that couldn't be set on the endpoint. Check the uri if the parameters are spelt correctly and that they are properties of the endpoint. Unknown parameters=[{zookeeperHost=localhost, zookeeperPort=2181}]
at org.apache.camel.impl.DefaultComponent.validateParameters(DefaultComponent.java:215)
at org.apache.camel.impl.DefaultComponent.createEndpoint(DefaultComponent.java:139)
at org.apache.camel.impl.DefaultCamelContext.getEndpoint(DefaultCamelContext.java:711)
... 33 more

正如克劳斯·易卜生所提到的,您需要添加

brokers

代码示例

toKafka="kafka://localhost:9092?topic=test&brokers=localhost:9092";

我希望这对遵循教程的人有所帮助。

关于java - Apache Kafka + Apache Camel 集成 + POC 问题 + java.util.Hashtable.put 处的 java.lang.NullPointerException(Hashtable.java :459),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47866344/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com