producer怎样发送消息到指定的partitions

http://www.aboutyun.com/thread-9906-1-1.html

http://my.oschina.net/u/591402/blog/152837

https://github.com/bkimminich/apache-kafka-book-examples/blob/master/src/test/kafka/SimplePartitioner.java

https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example

http://stackoverflow.com/questions/18202986/kafka-partitioner-class-assign-message-to-partition-within-topic-using-key

 

kafka分片类正确的写法如下:

package com.*.epp.kafka;

import java.util.Random;

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


public class CustomPartition implements Partitioner, scala.ScalaObject {

    public CustomPartition(VerifiableProperties props) {

    }

    /*
     * (non-Javadoc)
     * @see kafka.producer.Partitioner#partition(java.lang.Object, int)
     */
    @Override
    public int partition(Object key, int numPartitions) {
        if (key == null) {
            Random random = new Random();
            return random.nextInt(numPartitions);
        } else {
            int result = Math.abs(key.hashCode()) % numPartitions;
            return result;
        }
    }

}

 

posted @ 2015-06-30 14:11  唾手可得的树  阅读(351)  评论(0编辑  收藏  举报