前提

安装Kafka前需要先安装zookeeper集群,集体安装方法请参照我的另一篇文档

Storm安装

下载
 wget https://mirrors.tuna.tsinghua.edu.cn/apache/storm/apache-storm-1.1.0/apache-storm-1.1.0.tar.gz
解压
tar -zxvf apache-storm-1.1.0.tar.gz
移动文件夹
mv apache-storm-1.1.0 /usr/local/hadoop/
vim storm.yaml
storm.zookeeper.servers:
- "192.168.174.200"
- "192.168.174.201" nimbus.seeds: ["192.168.174.200"] storm.local.dir: "/usr/local/hadoop/apache-storm-1.1.0/data"
  • storm.zookeeper.servers:表示zookeeper的集群地址,如果Zookeeper集群使用的不是默认端口,那么还需要配置storm.zookeeper.port
  • storm.zookeeper.port: Zookeeper集群的端口号
  • storm.local.dir: 用于配置Storm存储少量文件的路径
  • nimbus.seeds: 用于配置主控节点的地址,可以配置多个
拷贝文件到其余工作节点
scp apache-storm-1.1.0 salver1:/usr/local/hadoop/

Storm操作

启动主控节点
./storm nimbus 1>/dev/null 2>&1 &
启动主控节点管理界面
./storm ui 1>/dev/null 2>&1 &  
启动工作节点
./storm supervisor 1>/dev/null 2>&1 &
访问地址

http://127.0.0.1:8080

运行拓扑
./storm jar storm-book.jar com.TopologyMain /usr/words.txt
删除拓扑
./storm kill Getting-Started-Toplogie

完整示例

package com;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID; import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.StormSubmitter;
import org.apache.storm.generated.AlreadyAliveException;
import org.apache.storm.generated.AuthorizationException;
import org.apache.storm.generated.InvalidTopologyException;
import org.apache.storm.kafka.KafkaSpout;
import org.apache.storm.kafka.SpoutConfig;
import org.apache.storm.kafka.StringScheme;
import org.apache.storm.kafka.ZkHosts;
import org.apache.storm.redis.bolt.RedisStoreBolt;
import org.apache.storm.redis.common.config.JedisPoolConfig;
import org.apache.storm.redis.common.mapper.RedisStoreMapper;
import org.apache.storm.spout.SchemeAsMultiScheme;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.tuple.Fields; public class MykafkaSpout {
/**
* @param args
* @throws AuthorizationException
*/
public static void main(String[] args) throws AuthorizationException {
// TODO Auto-generated method stub String host = "127.0.0.1";
int port = 6385;
String topic = "test" ;
ZkHosts zkHosts = new ZkHosts("192.168.174.200:2181,192.168.174.201:2181");
SpoutConfig spoutConfig = new SpoutConfig(zkHosts, topic,
"",
UUID.randomUUID().toString()) ;
List<String> zkServers = new ArrayList<String>() ;
zkServers.add("192.168.174.200");
zkServers.add("192.168.174.201"); spoutConfig.zkServers = zkServers;
spoutConfig.zkPort = 2181;
spoutConfig.socketTimeoutMs = 60 * 1000 ;
spoutConfig.scheme = new SchemeAsMultiScheme(new StringScheme()) ; spoutConfig.startOffsetTime = kafka.api.OffsetRequest.LatestTime();
TopologyBuilder builder = new TopologyBuilder() ;
builder.setSpout("spout", new KafkaSpout(spoutConfig) ,1) ;
builder.setBolt("bolt1", new MyKafkaBolt(), 2).shuffleGrouping("spout") ;
builder.setBolt("MyCountBolt", new MyCountBolt(), 2).fieldsGrouping("bolt1", new Fields("type"));
// 将所有单词及其次数进行汇总输出
builder.setBolt("MyReportBolt", new MyReportBolt(), 2).globalGrouping("MyCountBolt"); JedisPoolConfig poolConfig = new JedisPoolConfig.Builder().setHost(host).setPort(port).setPassword("Apple05101314").build();
RedisStoreMapper storeMapper = new MyCountStoreMapper();
RedisStoreBolt storeBolt = new RedisStoreBolt(poolConfig, storeMapper);
//向redis保存数据
builder.setBolt("redis-store-bolt", storeBolt).globalGrouping("MyReportBolt"); Config conf = new Config ();
conf.setDebug(false) ; if (args.length > 0) {
try {
StormSubmitter.submitTopology(args[0], conf, builder.createTopology());
} catch (AlreadyAliveException e) {
e.printStackTrace();
} catch (InvalidTopologyException e) {
e.printStackTrace();
}
}else {
LocalCluster localCluster = new LocalCluster();
localCluster.submitTopology("mytopology", conf, builder.createTopology());
}
}
}
package com;

import java.util.HashMap;
import java.util.Map; import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.IBasicBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; public class MyKafkaBolt extends BaseRichBolt { private OutputCollector outputCollector; // key:messageId,Data
private HashMap<String, String> waitAck = new HashMap<String, String>(); public void prepare(Map map, TopologyContext context,
OutputCollector collector) {
// TODO Auto-generated method stub
this.outputCollector = collector;
} public void execute(Tuple input) {
// TODO Auto-generated method stub
String kafkaMsg = input.getString(0);
if(kafkaMsg!=null){
this.outputCollector.emit(new Values(kafkaMsg));
this.outputCollector.ack(input);
}
} public void declareOutputFields(OutputFieldsDeclarer declarer) {
// TODO Auto-generated method stub
declarer.declare(new Fields("type"));
} }
package com;

import java.util.HashMap;
import java.util.Map; import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.IBasicBolt;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; public class MyCountBolt extends BaseRichBolt { private OutputCollector outputCollector;
private HashMap<String, Integer> count;
public void prepare(Map stormConf, TopologyContext context,
OutputCollector collector) {
// TODO Auto-generated method stub
this.outputCollector = collector;
this.count = new HashMap<String, Integer>();
} public void execute(Tuple input) {
// TODO Auto-generated method stub
String type = input.getStringByField("type");
int cnt = 1;
if(count.containsKey(type)){
cnt = count.get(type) + 1;
}
count.put(type, cnt);
this.outputCollector.emit(new Values(type, cnt));
this.outputCollector.ack(input);
} public void declareOutputFields(OutputFieldsDeclarer declarer) {
// TODO Auto-generated method stub
declarer.declare(new Fields("type", "cnt"));
} }
package com;

import org.apache.storm.redis.common.mapper.RedisDataTypeDescription;
import org.apache.storm.redis.common.mapper.RedisStoreMapper;
import org.apache.storm.tuple.ITuple; public class MyCountStoreMapper implements RedisStoreMapper {
private RedisDataTypeDescription description;
private final String hashKey = "myCount"; public MyCountStoreMapper() {
description = new RedisDataTypeDescription(
RedisDataTypeDescription.RedisDataType.HASH, hashKey);
} public RedisDataTypeDescription getDataTypeDescription() {
return description;
} public String getKeyFromTuple(ITuple tuple) {
return tuple.getStringByField("zs");
} public String getValueFromTuple(ITuple tuple) {
return tuple.getIntegerByField("cnt")+"";
}
}
package com;

import org.apache.storm.redis.bolt.RedisStoreBolt;
import org.apache.storm.redis.common.config.JedisPoolConfig;
import org.apache.storm.redis.common.mapper.RedisStoreMapper;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values; import java.util.HashMap;
import java.util.Map; import org.apache.log4j.Logger; /**
* Created by gzx on 17-2-6.
*/
public class MyReportBolt extends BaseRichBolt { private static Logger logger = Logger.getLogger(MyReportBolt.class);
private OutputCollector outputCollector;
private HashMap<String, Integer> count; public void prepare(Map map, TopologyContext topologyContext,
OutputCollector collector) {
this.count = new HashMap<String, Integer>();
this.outputCollector = collector;
} /**
* 打印单词及其出现次数
*
* @param tuple
*/
public void execute(Tuple tuple) {
String type = tuple.getStringByField("type");
int cnt = tuple.getIntegerByField("cnt"); count.put(type, cnt);
if (count.containsKey("join") && count.containsKey("out")) {
int join = count.get("join");
int out = count.get("out");
int sy = join-out;
System.out.println("join=" + join);
System.out.println("out=" + out);
//System.out.printf("===当前剩余总数==="+sy+"\r\n");
logger.debug("===当前剩余总数==="+sy);
this.outputCollector.emit(new Values("zs", sy));
this.outputCollector.ack(tuple);
} } public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("zs", "cnt"));
}
}

CentOS7搭建Storm集群及基础操作的更多相关文章

  1. centos7搭建kafka集群-第二篇

    好了,本篇开始部署kafka集群 Zookeeper集群搭建 注:Kafka集群是把状态保存在Zookeeper中的,首先要搭建Zookeeper集群(也可以用kafka自带的ZK,但不推荐) 1.软 ...

  2. 在CentOS上搭建Storm集群

    Here's a summary of the steps for setting up a Storm cluster: Set up a Zookeeper clusterInstall depe ...

  3. centos7搭建kafka集群

    一.安装jdk 1.下载jdk压缩包并移动到/usr/local目录 mv jdk-8u162-linux-x64.tar.gz /usr/local 2.解压 tar -zxvf jdk-8u162 ...

  4. CentOS7搭建Hadoop2.8.0集群及基础操作与测试

    环境说明 示例环境 主机名 IP 角色 系统版本 数据目录 Hadoop版本 master 192.168.174.200 nameNode CentOS Linux release 7.4.1708 ...

  5. Centos7搭建zookeeper集群

    centos7与之前的版本都不一样,修改主机名在/ect/hostname 和/ect/hosts 这两个文件控制 首先修改/ect/hostname vi /ect/hostname 打开之后的内容 ...

  6. 【转】centos7 搭建etcd集群

    转自http://www.cnblogs.com/zhenyuyaodidiao/p/6237019.html 一.简介 “A highly-available key value store for ...

  7. 初学Hadoop:利用VMWare+CentOS7搭建Hadoop集群

     一.前言 开始学习数据处理相关的知识了,第一步是搭建一个Hadoop集群.搭建一个分布式集群需要多台电脑,在此我选择采用VMWare+CentOS7搭建一个三台虚拟机组成的Hadoop集群. 注:1 ...

  8. centos7搭建kafka集群-第一篇

    Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候,是否会遇到这样的一些问题: 我们想分析下用户行为(pageviews),以便我们设计出更好的广告位 我想对用户 ...

  9. centos7搭建dolphinscheduler集群

    一.简述 Apache DolphinScheduler是一个分布式去中心化,易扩展的可视化DAG工作流任务调度系统.致力于解决数据处理流程中错综复杂的依赖关系,使调度系统在数据处理流程中开箱即用.有 ...

随机推荐

  1. [CSP-S模拟测试]:模板(ac)(线段树启发式合并)

    题目描述 辣鸡$ljh\ NOI$之后就退役了,然后就滚去学文化课了.他每天都被$katarina$大神虐,仗着自己学过一些姿势就给$katarina$大神出了一道题.有一棵$n$个节点的以$1$号节 ...

  2. 你还没搞懂this?

    一.前言 this关键字是JavaScript中最复杂的机制之一.它是一个很特别的关键字,被自动定义在所有函数的作用域中.对于那些没有投入时间学习this机制的JavaScript开发者来说,this ...

  3. 关于c++ error : passing " "as" " discards qualifiers

    http://www.cppblog.com/cppblogs/archive/2012/09/06/189749.html 今天写了一段小代码,本以为正确,但运行后,就somehow ”discar ...

  4. 将MSQL中的数据导出至EXCEL

    mysql> show variables like '%secure%';+------------------+---------------------+| Variable_name | ...

  5. hive_action

    w pdf469 [不直接MR访问数据的工具   查询间接转化为MR] https://en.wikipedia.org/wiki/Apache_Hive Apache Hive supports a ...

  6. 十三、python列表方法汇总

    '''1.append():更新列表'''l=[]l.append('111')l.append('[123,456]')print l-------------------------------- ...

  7. fedora23安装firefox中的flash插件-最终解决问题是: 要给libflashplayer.so以777权限, 开始给的755权限没有实现!

    下载的flash插件是一个rpm包. ===================================== rpm查看文件属于哪个包? 要看这个rpm包安装过还是没有安装过? (如果不用-p就是 ...

  8. oracle系统调优

    在Oracle数据库系统中,起到调节作用的参数叫初始化参数,在Oracle 8i及以前的版本中,这些初始化参数记录在INITsid.ora文件中:而Oracle 9i/10g/11g中将这些参数记录在 ...

  9. 从 spring-cloud-alibaba-nacos-config 进入 nacos-client

    sc 的 bootstrap context 是 main application context 的 parent,需要在 main application context 中使用的 bean 可以 ...

  10. Delphi XE2 之 FireMonkey 入门(15) - 滤镜: 获取滤镜信息

    滤镜类的继承关系: TObject -> TPersistent -> TFilter -> TShaderFilter -> { 具体的滤镜类 } //下面例子首先会用到 F ...