标签:
先说说partition的好处:Partition的好处是可以并发的获取同类数据,提高效率。
第一步需要实现Partitioner对象。
public
class
ProducerPartitioner
implements
Partitioner<String> {
public
static
final
Logger LOG=LoggerFactory.getLogger(UserInfo.
class
);
@Override
public
int
partition(String key,
int
numPartitions) {
LOG.info(
"ProducerPartitioner key:"
+key+
" partitions:"
+numPartitions);
return
key.length() % numPartitions;
}
}
/**选择用哪个类来进行设置partition*/
props.put(
"partitioner.class"
,
"org.kafka.partitioner.ProducerPartitioner"
);
ProducerData<String,UserInfo> data=
new
ProducerData<String, UserInfo
>(
"test"
,
"developerTest"
, msg);
标签:
原文地址:http://www.cnblogs.com/xubiao/p/5361773.html