【问题标题】:Error while creating topic in kafka在kafka中创建主题时出错
【发布时间】:2019-09-20 17:24:16
【问题描述】:

我在 Cygwin 的窗口上使用 kafka 并尝试创建一个主题并收到以下错误

log4j:ERROR Could not read configuration file from URL [file:/cygdrive/d/kafka/bin/../config/tools-log4j.properties].
java.io.FileNotFoundException: \cygdrive\d\kafka\bin\..\config\tools-log4j.properties (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:524)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
    at org.apache.log4j.Logger.getLogger(Logger.java:117)
    at org.I0Itec.zkclient.ZkClient.<clinit>(ZkClient.java:57)
    at kafka.admin.TopicCommand$.main(TopicCommand.scala:51)
    at kafka.admin.TopicCommand.main(TopicCommand.scala)
log4j:ERROR Ignoring configuration file [file:/cygdrive/d/kafka/bin/../config/tools-log4j.properties].
log4j:WARN No appenders could be found for logger (org.I0Itec.zkclient.ZkEventThread).
log4j:WARN No appenders could be found for logger (org.I0Itec.zkclient.ZkConnection).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

但主题正在创建中。 你能帮我解决这个问题吗

【问题讨论】:

    标签: apache-kafka


    【解决方案1】:

    您是否在Kafka documentation 中使用此命令行创建主题?

    bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

    我在 Cygwin 中运行该脚本时遇到了同样的错误,我在 bin/kafka-run-class.sh 中进行了此更改以解决问题(类似于中引用的 Cygwin 类路径解决方案第3步第一个答案here):

    原文:

    # Log4j settings if [ -z "$KAFKA_LOG4J_OPTS" ]; then KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:$base_dir/config/tools-log4j.properties" fi

    收件人:

    # Log4j settings if [ -z "$KAFKA_LOG4J_OPTS" ]; then KAFKA_LOG4J_OPTS="-Dlog4j.configuration=file:$(cygpath -wp $base_dir/config/tools-log4j.properties)" fi

    【讨论】:

    • 似乎他们更新了该脚本以查找 Cygwin,但如果您尝试像我一样使用 Git Bash,您仍然会收到错误:P
    • 我收到与 git bash 相同的错误。像魅力一样工作!
    【解决方案2】:

    我运行了.bat 脚本而不是.sh 脚本并且它工作正常。我不需要改变任何东西。

    最初我在 git bash 中运行这个脚本;

    bin/kafka-console-consumer.sh --boostrap-server localhost:9092 --topic kafkaTopic
    

    并得到以下错误:

    $ bin/kafka-console-consumer.sh --boostrap-server localhost:9092 --topic kafkaTopic
    log4j:ERROR Could not read configuration file from URL
    [file:/c/Users/admin/Documents/kafka/bin/../config/tools-log4j.properties].
    java.io.FileNotFoundException: 
    
    \c\Users\admin\Documents\kafka\bin\..\config\tools-log4j.properties
    (The system cannot find the path specified)
          at java.io.FileInputStream.open0(Native Method)
          at java.io.FileInputStream.open(FileInputStream.java:195)
          at java.io.FileInputStream.<init>(FileInputStream.java:138)
          at java.io.FileInputStream.<init>(FileInputStream.java:93)
          at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
          at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
          at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:557)
          at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:526)
          at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
          at org.slf4j.impl.Log4jLoggerFactory.<init>(Log4jLoggerFactory.java:66)
          at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
          at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45)
          at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
          at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
          at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
          at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
          at com.typesafe.scalalogging.Logger$.apply(Logger.scala:48)
          at kafka.utils.Log4jControllerRegistration$.<init>(Logging.scala:25)
          at kafka.utils.Log4jControllerRegistration$.<clinit>(Logging.scala)
          at kafka.utils.Logging.$init$(Logging.scala:47)
          at kafka.tools.ConsoleConsumer$.<init>(ConsoleConsumer.scala:45)
          at kafka.tools.ConsoleConsumer$.<clinit>(ConsoleConsumer.scala)
          at kafka.tools.ConsoleConsumer.main(ConsoleConsumer.scala)
    

    我打开cmd 并运行以下命令:

    C:\Users\admin\Documents\kafka\bin\windows>kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic kafkaTopic
    

    输出:

    {"message":"Spring Kafka is cool","sender":"Sydney"}

    工作得很好:)

    【讨论】:

      【解决方案3】:

      我认为你提到的路径不正确。正如消息所说,找不到文件异常。 下面是用 Java 向 kafka 发送消息的示例代码。假设 Kafka 服务器在本地运行。

      `public static void main(String[] args){
          long events = 100000;
          Random rnd  = new Random();
      
          Properties props = new Properties();
          props.put("metadata.broker.list", "localhost:9092");
          props.put("serializer.class", "kafka.serializer.StringEncoder");
          props.put("partitioner.class", "com.pranjal.kafkatest.SimplePartitioner");
          props.put("request.required.acks", "1");
      
          ProducerConfig config = new ProducerConfig(props);
      
          Producer<String, String> producer = new Producer<String, String>(config);
      
          for(int i=0;i<events;++i){
      
              long runtime = new Date().getTime();
              String ip    = "192.168.2." + rnd.nextInt(255);
      
              String msg= "Hello";
      
              KeyedMessage<String, String> data = new KeyedMessage<String, String>("sentences", ip, msg);
              producer.send(data);
          }
          producer.close();
      }`
      

      【讨论】:

      • 我没有做任何路径设置你能帮我在哪里做吗
      • 顺便说一句,此消息没有问题,因为它仅与日志记录实用程序 \cygdrive\d\kafka\bin..\config\tools-log4j.properties 有关。这就是为什么尽管出现此错误消息,但仍能正确创建您的主题。不用担心。
      • 谢谢。能帮我看看用java向kafka发送消息的示例代码吗
      • 非常感谢 sahu。无论是否发送,我如何查看主题中的消息
      • 使用控制台消费者。它存在于 kafka 的 bin 目录中。
      猜你喜欢
      • 2017-07-21
      • 2017-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多