01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
public
class
RandomSpout
extends
BaseRichSpout{
SpoutOutputCollector collector=
null
;
String[] goods={
"iphone"
,
"xiaomi"
,
"meizu"
,
"zhongxing"
,
"huawei"
,
"moto"
,
"sumsung"
,
"simens"
};
/*
* 获取消息并发送给下一个组件的方法,会被storm 不断地调用
* 从goods 数组中随机获取一个商品名封装到tuple中去
*/
@Override
public void nextTuple() {
Random random=new Random();
String good=goods[random.nextInt(goods.length)];
//封装到tuple中发送给下一个组件
collector.emit(new Values(good));
}
//进行初始化,只在开始时调用一次
@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector=collector;
}
/*
* 定义tunple的schema
*
*/
@Override
public
void
declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(
new
Fields(
"src_word"
));
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
public
class
UpperBolt
extends
BaseBasicBolt {
//每来一个消息元组tuple,都会被执行一次该方法
@Override
public
void
execute(Tuple tuple,BasicOutputCollector collector) {
//从tuple 中拿到数据--原始商品名
String src_word=tuple.getString(
0
);
//获取下标第一个消息
String upper=src_word.toUpperCase();
//发送出去
collector.emit(
new
Values(upper));
}
//给消息申明一个字段名
@Override
public
void
declareOutputFields(OutputFieldsDeclarer declare) {
declare.declare(
new
Fields(
"upper"
));
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
/*
* 给商品名称添加后缀,然后写入文件中
*/
public
class
SuffixBolt
extends
BaseBasicBolt{
FileWriter file =
null
;
@Override
public
void
prepare(Map stormConf, TopologyContext context) {
try
{
file =
new
FileWriter(
"D://eclipse_plugin"
+UUID.randomUUID());
}
catch
(IOException e) {
e.printStackTrace();
}
}
//每一次执行都去new 一个writer ,应该在调用excute 之前先把writer 初始化好==持续运行
@Override
public
void
execute(Tuple tuple, BasicOutputCollector collector) {
//从消息元组中拿到上一个组件发送过来的数据
String upper=tuple.getString(
0
);
String result=upper +
"_suffix"
;
try
{
file.append(result);
file.append(
"/n"
);
}
catch
(IOException e) {
e.printStackTrace();
}
}
//声明该组件要发送出去的tuple的字段定义
@Override
public
void
declareOutputFields(OutputFieldsDeclarer declare) {
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/*
* 描述topology的结构,以及创建topology并提交给集群
*/
public
class
TopoMain {
public
static
void
main(String[] args)
throws
AlreadyAliveException, InvalidTopologyException {
TopologyBuilder builder=
new
TopologyBuilder();
//设置消息源组件 4表示spout进程个数
builder.setSpout(
"randomSpout"
,
new
RandomSpout(),
4
);
//设置逻辑处理组件
//shuffleGrouping 指定接收哪个组件传过来的消息
builder.setBolt(
"upper"
,
new
UpperBolt(),
4
).shuffleGrouping(
"randomSpout"
);
builder.setBolt(
"result"
,
new
SuffixBolt(),
4
).shuffleGrouping(
"upper"
);
//创建一个topology
StormTopology topology=builder.createTopology();
Config config=
new
Config();
config.setNumWorkers(
4
);
//设置进程个数
config.setDebug(
true
);
//设置调试状态
config.setNumAckers(
0
);
//消息应答器,事务性不是很强,可设置为0
//提交topology到storm 定义一个名称,好在集群里去标识;通过配置对象传递参数给集群,集群根据这些参数,任务调度进行调整
StormSubmitter.submitTopology(
"demotopo"
, config, topology);
}
}
|