随机生成的东西加入,系统也是有包的,不过这边自己写了
public class RandomSpout extends BaseRichSpout {
String[] phones={"iphone","huawei","xiaomi","xiaolajiao","meizu"};
private SpoutOutputCollector collector;
public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
this.collector=spoutOutputCollector;
}
public void nextTuple() {
Random random=new Random();
int index = random.nextInt(phones.length);
String phonename=phones[index];
collector.emit(new Values(phonename));
}
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
outputFieldsDeclarer.declare(new Fields("pn"));
}
}
第一步 比如先把他变成大写
public class UpperBolt extends BaseBasicBolt {
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
String phonename=tuple.getString(0);
String upName=phonename.toUpperCase();
basicOutputCollector.emit(new Values(upName));
}
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
outputFieldsDeclarer.declare(new Fields("uppername"));
}
}
再来一步 加上ok
public class OkBolt extends BaseBasicBolt {
FileWriter fileWriter=null;
@Override
public void prepare(Map stormConf, TopologyContext context) {
try {
fileWriter=new FileWriter("/home/hadoop/stormdata/"+ UUID.randomUUID());//第一条协调服务,二三计算
} catch (IOException e) {
e.printStackTrace();
}
}
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
String name=tuple.getString(0);//传了一个值所以0
String fName=name+"_ok";
try {
//HUAWEI,XIAO 写一个换行方便查看
fileWriter.write(fName);
fileWriter.write("\n");
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
}
}
最后一部连接
public class ToMain {
public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException {
TopologyBuilder builder=new TopologyBuilder();
builder.setSpout("rspout",new RandomSpout());
// 定义 对象 words||task个数 传来的参数
builder.setBolt("ubolt",new UpperBolt(),4).shuffleGrouping("rspout");
builder.setBolt("okBolt",new OkBolt(),4).shuffleGrouping("ubolt");
StormTopology topology = builder.createTopology();
Config conf=new Config();
conf.setNumWorkers(4);
StormSubmitter.submitTopology("ramtopology",conf,topology);
}
}
不过这个架包要用maven,不然可能出现连接问题,然后再把jar导入linux运行
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.storm/storm-core -->
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>0.9.3</version>
<scope>provided</scope>
</dependency>
</dependencies>