上一节介绍了config的相关信息,这一节说下,这些參数各自是什么。在zookeeper中的存放路径是如何的,之前QQ群里有非常多不知道该怎么传入正确的參数来new 一个kafkaSpout,其主要还是參数传递正确就可。

看SpoutConfig的构造函数

public SpoutConfig(BrokerHosts hosts, String topic, String zkRoot, String id) {
super(hosts, topic);
this.zkRoot = zkRoot;
this.id = id;
}

须要一个BrokerHosts,看代码:

public class ZkHosts implements BrokerHosts {
private static final String DEFAULT_ZK_PATH = "/brokers"; public String brokerZkStr = null;
public String brokerZkPath = null; // e.g., /kafka/brokers
public int refreshFreqSecs = 60; public ZkHosts(String brokerZkStr, String brokerZkPath) {
this.brokerZkStr = brokerZkStr;
this.brokerZkPath = brokerZkPath;
} public ZkHosts(String brokerZkStr) {
this(brokerZkStr, DEFAULT_ZK_PATH);
}
}

须要brokerZKStr,这个事实上就是hosts列表,多个host以逗号隔开。由于zookeeper解析string时是以逗号分隔的。这里附上zookeeper的解析代码

public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,
boolean canBeReadOnly)
throws IOException
{
LOG.info("Initiating client connection, connectString=" + connectString
+ " sessionTimeout=" + sessionTimeout + " watcher=" + watcher); watchManager.defaultWatcher = watcher; ConnectStringParser connectStringParser = new ConnectStringParser(
connectString);
HostProvider hostProvider = new StaticHostProvider(
connectStringParser.getServerAddresses());
cnxn = new ClientCnxn(connectStringParser.getChrootPath(),
hostProvider, sessionTimeout, this, watchManager,
getClientCnxnSocket(), canBeReadOnly);
cnxn.start();
}

当中主要StringParser做解析的,看俺怎样解析的就知道了

public ConnectStringParser(String connectString) {
// parse out chroot, if any
int off = connectString.indexOf('/');
if (off >= 0) {
String chrootPath = connectString.substring(off);
// ignore "/" chroot spec, same as null
if (chrootPath.length() == 1) {
this.chrootPath = null;
} else {
PathUtils.validatePath(chrootPath);
this.chrootPath = chrootPath;
}
connectString = connectString.substring(0, off);
} else {
this.chrootPath = null;
} String hostsList[] = connectString.split(",");
for (String host : hostsList) {
int port = DEFAULT_PORT;
int pidx = host.lastIndexOf(':');
if (pidx >= 0) {
// otherwise : is at the end of the string, ignore
if (pidx < host.length() - 1) {
port = Integer.parseInt(host.substring(pidx + 1));
}
host = host.substring(0, pidx);
}
serverAddresses.add(InetSocketAddress.createUnresolved(host, port));
}
}

好了,这里就讲到这了。

刚才说到brokerZKStr须要。另一个參数就是zkpath,这个能够自己定,也有个默认值 “/brokers”

SpoutConfig还有个zkroot,这个zkroot事实上就是Consumer端消费的信息存放地方,好了给个样例:

String topic = “test”;  //
String zkRoot = “/kafkastorm”; //
String spoutId = “id”; //读取的status会被存在,/kafkastorm/id以下,所以id相似consumer group BrokerHosts brokerHosts = new ZkHosts("10.1.110.24:2181,10.1.110.22:2181"); // 这里使用默认的/brokers SpoutConfig spoutConfig = new SpoutConfig(brokerHosts, topic, zkRoot, spoutId);
spoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme()); // 下一节介绍 scheme /*spoutConfig.zkServers = new ArrayList<String>(){{ // 仅仅有在local模式下须要记录读取状态时。才须要设置
add("10.118.136.107");
}};
spoutConfig.zkPort = 2181;*/
spoutConfig.forceFromStart = true;
spoutConfig.startOffsetTime = -1;//从最新的開始消费
spoutConfig.metricsTimeBucketSizeInSecs = 6;
builder.setSpout(SqlCollectorTopologyDef.KAFKA_SPOUT_NAME, new KafkaSpout(spoutConfig), 1);

By default, the offsets will be stored in the same Zookeeper cluster that Storm uses. You can override
this via your spout config like this:

spoutConfig.zkServers = ImmutableList.of("otherserver.com");
spoutConfig.zkPort = 2191;

这里就成功建了一个KafkaSpout。假设项目执行成功的话,

能够到zk master上看下相关信息,

./bin/zkCli.sh -server 10.1.110.24:2181

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3poZzA1MDg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

而对于StaticHosts来说,看官方解释:

StaticHosts

This is an alternative implementation where broker -> partition information is static. In order to construct an instance of this class you need to first construct an instance of GlobalPartitionInformation.

    Broker brokerForPartition0 = new Broker("localhost");//localhost:9092
Broker brokerForPartition1 = new Broker("localhost", 9092);//localhost:9092 but we specified the port explicitly
Broker brokerForPartition2 = new Broker("localhost:9092");//localhost:9092 specified as one string.
GlobalPartitionInformation partitionInfo = new GlobalPartitionInformation();
partitionInfo.addPartition(0, brokerForPartition0);//mapping form partition 0 to brokerForPartition0
partitionInfo.addPartition(1, brokerForPartition1);//mapping form partition 1 to brokerForPartition1
partitionInfo.addPartition(2, brokerForPartition2);//mapping form partition 2 to brokerForPartition2
StaticHosts hosts = new StaticHosts(partitionInfo);

个人觉得是须要开发者,自己知道partition与broker之间的相应关系,正确关联起来。而storm-kafka 0.9.0.1的版本号是。不须要指定,我仅仅须要传入zkServer list,partition总数,由kafkautil利用两个for(遍历全部broker和partition)循环,暂时生成Consumer去连接消费一下试试,假设有数据,那么就把partitionId和brokerHost关系存到Map中去。可想而知0.9.3-rc1为什么要改成这样了。

假设该broker没有该partition信息,后果会如何???笔者没有測试过,有測试过的请留言,说一下情况。

Reference

http://www.cnblogs.com/fxjwind/p/3808346.html

(三)storm-kafka源代码走读之怎样构建一个KafkaSpout的更多相关文章

  1. 【原创】Windows平台搭建Kafka源代码开发环境(Eclipse版本)

    最近在研究Kafka源代码,需要自己搭建一个开发环境.官网上给出的提示略显简单,照着做了一遍也碰到了一些问题.特此记录下来. 开发环境: Oracle Java 1.7_u71 + Eclipse 4 ...

  2. storm笔记:Storm+Kafka简单应用

    storm笔记:Storm+Kafka简单应用 这几天工作须要使用storm+kafka,基本场景是应用出现错误,发送日志到kafka的某个topic.storm订阅该topic.然后进行兴许处理.场 ...

  3. Storm+kafka的HelloWorld初体验

    从16年4月5号开始学习kafka,后来由于项目需要又涉及到了storm. 经过几天的扫盲,到今天16年4月13日,磕磕碰碰的总算是写了一个kafka+storm的HelloWorld的例子. 为了达 ...

  4. hadoop+yarn+hbase+storm+kafka+spark+zookeeper)高可用集群详细配置

    配置 hadoop+yarn+hbase+storm+kafka+spark+zookeeper 高可用集群,同时安装相关组建:JDK,MySQL,Hive,Flume 文章目录 环境介绍 节点介绍 ...

  5. (六)storm-kafka源代码走读之PartitionManager

    PartitionManager算是storm-kafka的核心类了,如今開始简单分析一下.还是先声明一下,metric部分这里不做分析. PartitionManager主要负责的是消息的发送.容错 ...

  6. kafka学习笔记(三)kafka的使用技巧

    概述 上一篇随笔主要介绍了kafka的基本使用包括集群参数,生产者基本使用,consumer基本使用,现在来介绍一下kafka的使用技巧. 分区机制 我们在使用 Apache Kafka 生产和消费消 ...

  7. Kafka 源代码分析.

    这里记录kafka源代码笔记.(代码版本是0.8.2.1) kafka的源代码如何下载.这里简单说一下. git clone https://git-wip-us.apache.org/repos/a ...

  8. kafka学习(三)kafka生产者,消费者详解

    文章更新时间:2020/06/14 一.生产者 当我们发送消息之前,先问几个问题:每条消息都是很关键且不能容忍丢失么?偶尔重复消息可以么?我们关注的是消息延迟还是写入消息的吞吐量? 举个例子,有一个信 ...

  9. 《Linux内核分析》第三周 构建一个简单的Linux系统MenuOS

    [刘蔚然 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK THREE ...

随机推荐

  1. xen 保存快照的实现之 —— device model 状态保存

    xen 保存快照的实现之 —— device model 状态保存 实现要点: 设备状态保存在 /var/lib/xen/qemu-save.x 文件这个文件由 qemu-dm 产生,也由 qemu- ...

  2. Linux操作系统的权限代码分析【转】

    转自:http://blog.csdn.net/lixuyuan/article/details/6217502 现在关于内核的书很少涉及到Linux内核的安全,内核安全大概包括了密码学实现(cryp ...

  3. django中的类视图

    # 原创,转载请留言联系 当我们在开发一个注册模块时.浏览器会通过get请求让注册表单弹出来,然后用户输完注册信息后,通过post请求向服务端提交信息.这时候我们后端有两个视图函数,一个处理get请求 ...

  4. UAF学习之Adobe reader CVE-2013-3346

    学习了UAF,分析了几个漏洞,同时,也熟悉了windbg的用法,收获挺大. 基本的UAF分析流程如下: i:找有漏洞的函数 ii:找到被释放对象的类型,以及被释放对象在内存中的位置 iii:理解对象的 ...

  5. StringBuffer 清空

    几种方法: 方法1: 1 2 3 4 StringBuffer my_StringBuffer = new StringBuffer(); my_StringBuffer.append('hellow ...

  6. elasticsearch5.3.0 安装

    公司有项目打算用elasticsearch,所以研究了下,目前最新版本5.3.0 安装 1.下载包  https://artifacts.elastic.co/downloads/elasticsea ...

  7. Tiny6410下的第一个Linux驱动程序

    Linux系统环境是照着友善之臂的教程搭建的 //Hello  World驱动程序源文件 #include <linux/miscdevice.h> #include <linux/ ...

  8. SQL 存储过程入门(事务)

    本篇我们来讲一下事务处理技术. 为什么要使用事务呢,事务有什么用呢,举个例子. 假设我们现在有个业务,当做成功某件事情的时候要向2张表中插入数据,A表,B表,我们插入的顺序是先插入A,再插入B表,如果 ...

  9. linux命令详解:basename命令

    转:http://www.cnblogs.com/lwgdream/archive/2013/11/05/3407768.html 前言 bashname命令用于获取路径中的文件名或路径名(获取的时候 ...

  10. Java实现中文算数验证码(算数运算+-*/)

    原文:http://blog.csdn.net/typa01_kk/article/details/45050091 /** * creat verification code * */ @Actio ...