Apache Kafka默认编码器不工作

lau*_*man 5 apache-kafka

我正在使用Kafka 0.8测试版,我只是试图发送不同的对象,使用我自己的编码器序列化它们,并将它们发送到现有的代理配置.现在我试图让DefaultEncoder工作.

我有代理和一切设置并为StringEncoder工作,但我无法获得任何其他数据类型,包括纯字节[],由代理发送和接收.

我的制作人代码是:

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

import java.util.Date;
import java.util.Properties;
import java.util.Random;


public class ProducerTest {
    public static void main(String[] args) {
        long events = 5;
        Random rnd = new Random();
        rnd.setSeed(new Date().getTime());
        Properties props = new Properties();
        props.setProperty("metadata.broker.list", "localhost:9093,localhost:9094");
        props.setProperty("serializer.class", "kafka.serializer.DefaultEncoder");
        props.setProperty("partitioner.class", "example.producer.SimplePartitioner");
        props.setProperty("request.required.acks", "1");
        props.setProperty("producer.type", "async");
        props.setProperty("batch.num.messages", "4");

        ProducerConfig config = new ProducerConfig(props);
        Producer<byte[], byte[]> producer = new Producer<byte[], byte[]>(config);
        for (long nEvents = 0; nEvents < events; nEvents++) {
            byte[] a = "Hello".getBytes();
            byte[] b = "There".getBytes();

            KeyedMessage<byte[], byte[]> data = new KeyedMessage<byte[], byte[]>("page_visits", a, b);
            producer.send(data);
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        producer.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

我用同样的SimplePartitioner中给出的例子在这里,并通过字符串替换所有的字节数组和改变序列化kafka.serializer.StringEncoder完美的作品.

供参考,SimplePartitioner:

import kafka.producer.Partitioner;
import kafka.utils.VerifiableProperties;

public class SimplePartitioner implements Partitioner<String> {
    public SimplePartitioner (VerifiableProperties props) {

    }

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

}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

lau*_*man 6

答案是分区类SimplePartitioner仅适用于字符串.当我尝试异步运行Producer时,它会创建一个单独的线程来处理编码和分区,然后再发送给代理.当这个线程意识到SimplePartitioner仅适用于Strings时会遇到障碍,但因为它是一个单独的线程,所以不会抛出异常,因此线程只是退出而没有任何错误指示.

如果我们将SimplePartitioner更改为接受byte [],例如:

import kafka.producer.Partitioner;
import kafka.utils.VerifiableProperties;

public class SimplePartitioner implements Partitioner<byte[]> {
    public SimplePartitioner (VerifiableProperties props) {

    }

    public int partition(byte[] key, int a_numPartitions) {
        int partition = 0;
        return partition;
    }

}
Run Code Online (Sandbox Code Playgroud)

这现在完美地运作.