简体   繁体   English

Java 中的 Kafka 消费者

[英]Kafka Consumer in Java

So I am learning Kafka currently and have attempted to duplicate the examples provided from Apache here .因此,我目前正在学习 Kafka,并尝试复制 Apache 此处提供的示例。 This is example code for the consumer and I have written it in java just as shown.这是消费者的示例代码,我已经用 java 编写了它,如图所示。 When I attempt to execute the file however I run into some issues.但是,当我尝试执行该文件时,我遇到了一些问题。 I am able to get the file to compile but it will not run properly.我能够编译该文件,但它无法正常运行。

I am executing the program with the following line without the quotations, "java TestConsumer localhost:2181 group1 test 4" This passes the 4 arguments necessary in the example code.我正在使用以下不带引号的行执行程序,“java TestConsumer localhost:2181 group1 test 4”这传递了示例代码中必需的 4 个参数。 I am provided with the following error though when I execute this command.但是,当我执行此命令时,我收到了以下错误。

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Category
    at kafka.utils.VerifiableProperties.<init>(Unknown Source)
    at kafka.consumer.ConsumerConfig.<init>(Unknown Source)
    at TestConsumer.ConsumerProps(TestConsumer.java:69)
    at TestConsumer.<init>(TestConsumer.java:31)
    at TestConsumer.main(TestConsumer.java:97)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Category
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 5 more

I have tried going in an manually replacing the arguments with the necessary values and attempting to execute that way but I am given a different issue.我试过用必要的值手动替换参数并尝试以这种方式执行,但我遇到了一个不同的问题。 Below is the error message along with the code I'm using just in case I screwed something up from the example provided.下面是错误消息以及我正在使用的代码,以防万一我从提供的示例中搞砸了一些东西。 If anyone can help me out I would be incredibly appreciative since I am attempting to write my own consumer to test with parsing given information, etc. Thanks如果有人可以帮助我,我将非常感激,因为我正在尝试编写自己的消费者来测试解析给定的信息等。谢谢

log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NoClassDefFoundError: org/I0Itec/zkclient/IZkStateListener
at kafka.javaapi.consumer.ZookeeperConsumerConnector.<init>(Unknown Source)
at kafka.javaapi.consumer.ZookeeperConsumerConnector.<init>(Unknown Source)
at kafka.consumer.Consumer$.createJavaConsumerConnector(Unknown Source)
at kafka.consumer.Consumer.createJavaConsumerConnector(Unknown Source)
at TestConsumer.<init>(TestConsumer.java:31)
at TestConsumer.main(TestConsumer.java:97)
Caused by: java.lang.ClassNotFoundException: org.I0Itec.zkclient.IZkStateListener
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more

 /* * Test Consumer to gather input from * a Producer. Attempt to perform functions * from the produced data */ // Kafka API import kafka.consumer.ConsumerConfig; import kafka.consumer.KafkaStream; import kafka.javaapi.consumer.ConsumerConnector; import java.util.Map; import java.util.HashMap; import java.util.Properties; import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; public class TestConsumer{ private final ConsumerConnector consumer; private final String topic; private ExecutorService executor; // CONSTRUCTOR public TestConsumer(String zookeeper, String groupid, String aTopic){ consumer = kafka.consumer.Consumer.createJavaConsumerConnector(ConsumerProps(zookeeper, groupid)); this.topic = aTopic; } // END CONSTRUCTOR // RUN FUNCTION public void run(int threads){ Map<String, Integer> topicMap = new HashMap<String, Integer>(); topicMap.put(topic, new Integer(threads)); Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicMap); List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic); executor = Executors.newFixedThreadPool(threads); // process threads int numThread = 0; // thread counter for consumption // consumer all messages for(final KafkaStream stream : streams){ executor.submit(new TestConsumerRun(stream, numThread)); numThread ++; } } // END RUN FUNCTION // CREATE PROPERTIES FUNCTION private static ConsumerConfig ConsumerProps(String zookeeper, String groupid){ Properties properties = new Properties(); // config properties file properties.put("zookeeper.connect", zookeeper); properties.put("group.id", groupid); properties.put("zookeeper.session.timeout.ms", "400"); properties.put("zookeeper.sync.time.ms", "200"); properties.put("auto.commit.interval.ms", "1000"); properties.put("auto.offset.reset", "smallest"); return new ConsumerConfig(properties); } // END CREATE PROPERTIES FUNCTION // SHUTDOWN FUNCTION public void shutdown(){ if (consumer != null) consumer.shutdown(); if (executor != null) executor.shutdown(); try{ if (!executor.awaitTermination(5000, TimeUnit.MILLISECONDS)){ System.out.println("Timed out waiting for consumer threads to shut down, exiting uncleanly"); } } catch (InterruptedException e){ System.out.println("Interrupted during shutdown, exiting uncleanly"); } } // END SHUTDOWN FUNCTION // MAIN FUNCTION public static void main(String[] args){ String zookeeper = args[0]; String groupid = args[1]; String topic = args[2]; int threads = Integer.parseInt(args[3]); TestConsumer test = new TestConsumer(zookeeper, groupid, topic); // send information to constructor test.run(threads); // pass threads for iteration try{ Thread.sleep(10000); } catch (InterruptedException ie){ } test.shutdown(); // close program } // END MAIN FUNCTION }

 /* * Test Consumer to gather input from * a Producer. Attempt to perform functions * from the produced data */ // Kafka API import kafka.consumer.ConsumerIterator; import kafka.consumer.KafkaStream; public class TestConsumerRun implements Runnable{ private KafkaStream aStream; private int aThread; // CONSTRUCTOR public TestConsumerRun(KafkaStream stream, int thread){ aStream = stream; // set stream from main read aThread = thread; // set thread from main read } // END CONSTRUCTOR // RUN FUNCTION public void run(){ ConsumerIterator<byte[], byte[]> iterator = aStream.iterator(); // used to check throughout the list continiously while(iterator.hasNext()) System.out.println("Thread " + aThread + ": " + new String(iterator.next().message())); System.out.println("Shutting down Thread: " + aThread); } // END RUN FUNCTION }

Try adding BasicConfigurator.configure();尝试添加BasicConfigurator.configure(); in the main method and it will work fine.在主要方法中,它会正常工作。

I had the same problem.我有同样的问题。 You need to add log4j jar to your classpath.您需要将log4j jar 添加到类路径中。 Also you might need to add slf4j and commons-logging .此外,您可能需要添加slf4jcommons-logging

java.lang.NoClassDefFoundError occurs when JVM can't find the class at runtime. java.lang.NoClassDefFoundError当 JVM 在运行时找不到类时发生。 (But it was there during compile.) Happens when a jar is missing during runtime and also for many other reasons. (但它在编译期间就在那里。)在运行时缺少 jar 以及许多其他原因时会发生这种情况。 Your classpath during the compile and runtime needs to be the same.您在编译和运行时的类路径需要相同。 Sometimes you might have the same jar with different versions, so at runtime JVM might find the different version rather than the one used in compile.有时您可能有不同版本的相同 jar,因此在运行时 JVM 可能会找到不同的版本,而不是编译中使用的版本。

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

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