SpecifiedPartitioner.java 928 Bytes
package com.sitech.kafka;

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

/**
 * Created with IntelliJ IDEA.
 * User: dongyj
 * Date: 15-11-8
 * Time: 上午2:18
 * To change this template use File | Settings | File Templates.
 */
public class SpecifiedPartitioner implements Partitioner<String> {

    public SpecifiedPartitioner(VerifiableProperties props){

    }

    @Override
    public int partition(String key, int numPartitions) {
        try {
            int spe_partition = 0;
            if (Integer.parseInt((String) key) < numPartitions){
                spe_partition = Math.abs(Integer.parseInt((String) key));
            }else {
                spe_partition = Math.abs(Integer.parseInt((String) key) % numPartitions);
            }
            return spe_partition;
        } catch (Exception e) {
            return Math.abs(key.hashCode() % numPartitions);
        }
    }

}