实例使用Storm,依靠Storm的实时性以及大规模数据的特点。在Spout中随机发送内置的语句作为消息源;使用一个Bolt进行语句切分,将句子切分成单词发射出去;使用一个bolt订阅切分的单词Tuple,进行单词统计,并且选择使用按字段分组的策略,词频实时排序,把Top N实时发射出去;最后使用一个Bolt将结果打印到log中。
项目对应的实例代码可以通过【下载实例】按钮获取
开发工具: IntelliJ IDEA2017, JDK1.8, Maven 3.0.2, storm-0.9.6, zookeeper-3.4.12, python 3.6.5
【项目包含内容】(见下图):
导入工程后
下面的环境在windows下进行, zookeeper,storm皆为单机模式 (jdk1.8的安装就省略了)
1. Zookeeper的安装与启动
在页面右侧下载Zookeeper对应的版本(请保持与本工程一致), 解压下载的tar包, 将 F:\Program Files\zookeeper-3.4.12\conf(此根据自己的安装目录进行配置) 下的 zoo_sample.cfg 改为 zoo.cfg, Zookeeper会使用此配置文件, 其中clientPort为监听端口, dataDir为数据目录, dataLogDir为日志目录
zoo.cfg内容如下:
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=F:\\Program Files\\zookeeper-3.4.12\\data dataLogDir=F:\\Program Files\\zookeeper-3.4.12\\log # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
双击 F:\Program Files\zookeeper-3.4.12\bin 下的 zkServer.cmd 启动 zookeeper服务
双击 F:\Program Files\zookeeper-3.4.12\bin 下的 zkCli.cmd 启动zookeeper的客户端命令行连接, 输入 ls / 查询根路径下节点, 能正常访问说明zookeeper启动成功(见下图)
配置Zookeeper的环境变量: ZOOKEEPER_HOME: 安装目录 和 path: %ZOOKEEPER_HOME%\bin
2. Python 的安装与启动
在页面右侧下载Python对应的版本(请保持与本工程一致), 解压下载zip包并安装, 安装目录: F:\Program Files\Python
在path中配置环境变量: F:\Program Files\Python;F:\Program Files\Python\Scripts;
3. Storm 的安装与启动
在页面右侧下载Storm对应的版本(请保持与本工程一致), 解压下载zip包
配置Storm的环境变量: STORM_HOME: 安装目录 和 path:%STORM_HOME%\bin
打开cmd窗口, 输入下面三个命令
storm nimbus
storm supervisor
storm ui
在浏览器中输入 localhost:8080, 如果能访问到相应的页面,则启动成功,见下图
4. 下载工程项目代码, 打开cmd命令窗口, 切换目录到 F:\storm-example, 输入maven命令进行打包: mvn assembly:assembly,
生成jar包(此jar包包含其所依赖的其它jar包集)
进行target目录: storm jar storm-example-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.blogchong.storm.helloworld.WordCountTopology helloworld, 由下图可见,已经生成了一个名称为 helloworld的 top
另外,通过在storm中的woker日志中,可以看到bolt中打印的日志文件信息