线程“main”中的异常java.lang.IllegalArgumentException:运行kafka生成器时参数的数量错误

时间:2015-11-26 09:50:43

标签: java apache-kafka

我正在尝试运行Kafka制作人https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example

这是Kafka Producer类:

package com.kafka.demo;
import java.util.*;
import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
/* A Simple producer using API. This producer is creating log messages and sending to a topic called vulab123 on a node with name vulab-build-system Please make sure vulab-build-system is configured in the /etc/hosts file in your unix or linux environment */
public class KafkaProducer
{
public static void main(String[] args)
{

    //long events = Long.parseLong(args[0]);

    long events =100;

    Random rnd = new Random();
    Properties props = new Properties();
    props.put("metadata.broker.list", "10.25.3.207:6667");
    //props.put("producer.type", "sync");
    props.put("zk.connect","10.25.3.207:2181");
    props.put("serializer.class", "kafka.serializer.StringEncoder");
    props.put("partitioner.class", "com.kafka.demo.KafkaPartitioner");
    props.put("request.required.acks", "1");
    props.put("producer.type","async");

    ProducerConfig config = new ProducerConfig(props);

    Producer<String, String> producer = new Producer<String, String>(config);

    for (long nEvents = 0; nEvents<events; nEvents++)
    {
        System.out.println("creating event "+nEvents);
        long runtime = new Date().getTime();
        String ip = "192.168.2."+ rnd.nextInt(255);
        String msg = runtime + ",www.vulab.com," + ip;
        KeyedMessage<String, String> data = new KeyedMessage<String, String>("vulab123", ip, msg);
        producer.send(data);

    //System.out.println(data);
    }
    producer.close();
}

}

这是Kafka Partitioner类:

package com.kafka.demo;
import kafka.producer.Partitioner;
import kafka.utils.VerifiableProperties;
public class KafkaPartitioner implements Partitioner
{
public KafkaPartitioner (VerifiableProperties props)
{

}
public int partition(Object key, int a_numPartitions)
{
    int partition = 0;
    String stringKey = (String) key;
    int offset = stringKey.lastIndexOf('.');
    if (offset > 0)
    {
        partition = Integer.parseInt( stringKey.substring(offset+1)) % a_numPartitions;
    }
    return partition;
}

}

我收到以下错误:

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at kafka.utils.Utils$.getObject(Utils.scala:567)
at kafka.javaapi.producer.Producer.<init>(Producer.scala:39)
at com.kafka.demo.KafkaProducer.main(KafkaProducer.java:34)

有关如何解决此问题的任何想法或建议。

1 个答案:

答案 0 :(得分:0)

这只是直觉,但尝试使用:

KafkaProducer<String, String> producer = new KafkaProducer<String, String>(config);

而不是

Producer<String, String> producer = new Producer<String, String>(config);

还要确保使用正确版本的Apache Kafka库。