zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    
    }
  • 相关阅读:
    Python 模块,数据类型,元组
    Python条件循环判断
    Python简介
    File对象的基本操作学习
    File对象的基本操作学习
    sublime学习心得
    sublime学习心得
    IO学习
    IO学习
    Emacs学习
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/4608510.html
Copyright © 2011-2022 走看看