Advertisement
Guest User

vitim

a guest
Jan 25th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. package finance;
  2.  
  3. import java.io.IOException;
  4. import java.math.BigDecimal;
  5. import java.time.LocalDate;
  6. import java.time.format.DateTimeFormatter;
  7. import java.util.Map;
  8.  
  9. import org.apache.storm.spout.SpoutOutputCollector;
  10. import org.apache.storm.task.TopologyContext;
  11. import org.apache.storm.topology.IRichSpout;
  12. import org.apache.storm.topology.OutputFieldsDeclarer;
  13. import org.apache.storm.tuple.Fields;
  14. import org.apache.storm.tuple.Values;
  15.  
  16. import yahoofinance.YahooFinance;
  17. import yahoofinance.quotes.stock.StockQuote;
  18.  
  19.  
  20. public class YahooFinanceSpout implements IRichSpout {
  21. private SpoutOutputCollector collector;
  22.  
  23. public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
  24. this.collector = collector;
  25. }
  26.  
  27. public void declareOutputFields(OutputFieldsDeclarer declarer) {
  28. declarer.declare(new Fields("company", "timestamp", "price", "prev_close"));
  29. }
  30.  
  31. public void nextTuple() {
  32. DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
  33. try {
  34. StockQuote quote = YahooFinance.get("MSFT").getQuote(); // Financas
  35. // da
  36. // Microsoft
  37. BigDecimal price = quote.getPrice();
  38. BigDecimal prevClose = quote.getPreviousClose();
  39. collector.emit(new Values("MSFT", formatter.format(LocalDate.now()), price.doubleValue(),
  40. prevClose.doubleValue()));
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45.  
  46. public void ack(Object arg0) {
  47.  
  48. }
  49.  
  50. public void activate() {
  51.  
  52. }
  53.  
  54. public void close() {
  55.  
  56. }
  57.  
  58. public void deactivate() {
  59.  
  60. }
  61.  
  62. public void fail(Object arg0) {
  63.  
  64. }
  65.  
  66. public void open1(Map arg0, TopologyContext arg1, SpoutOutputCollector arg2) {
  67.  
  68.  
  69. }
  70.  
  71. public Map<String, Object> getComponentConfiguration() {
  72.  
  73. return null;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement