/*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.storm.sample.p1;
import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Arrays;
import java.util.Map;
public class PreprocessingSprout extends BaseRichSpout {
private BufferedReader reader;
SpoutOutputCollector _collector;
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("symbol", "price", "volume", "timestamp"));
}
@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
this._collector = collector;
URL url = PreprocessingSprout.class.getResource("/stockquotes.txt");
try {
String filePath = new File(url.toURI()).getAbsolutePath();
reader = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Override
public void nextTuple() {
try {
String line = reader.readLine();
if (line != null) {
String [] values = line.split(",");
_collector.emit(new Values((Object[])values));
System.out.println("Input>"+Arrays.toString(values));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}