Application of Apache Storm Framework in Yahoo Finance

Last updated on May 30 2022
Lalit Kolgaonkar

Table of Contents

Application of Apache Storm Framework in Yahoo Finance

Apache Storm in Yahoo! Finance

 

Yahoo! Finance is that the Internet’s leading news and financial data website. it’s a neighborhood of Yahoo! and provides information about financial news, market statistics, international market data and other information about financial resources that anyone can access.

If you’re a registered Yahoo! user, then you’ll customize Yahoo! Finance to require advantage of its certain offerings. Yahoo! Finance API is employed to question financial data from Yahoo!

This API displays data that’s delayed by 15-minutes from real time, and updates its database every 1 minute, to access current stock-related information. Now allow us to take a real-time scenario of a corporation and see the way to raise an alert when its stock value goes below 100.

Spout Creation

The purpose of spout is to urge the small print of the corporate and emit the costs to bolts. you’ll use the subsequent program code to make a spout.

Coding: YahooFinanceSpout.java

import java.util.*;

import java.io.*;

import java.math.BigDecimal;

 

//import yahoofinace packages

import yahoofinance.YahooFinance;

import yahoofinance.Stock;

 

import backtype.storm.tuple.Fields;

import backtype.storm.tuple.Values;

 

import backtype.storm.topology.IRichSpout;

import backtype.storm.topology.OutputFieldsDeclarer;

 

import backtype.storm.spout.SpoutOutputCollector;

import backtype.storm.task.TopologyContext;

 

public class YahooFinanceSpout implements IRichSpout {

private SpoutOutputCollector collector;

private boolean completed = false;

private TopologyContext context;

 

@Override

public void open(Map conf, TopologyContext context, SpoutOutputCollector collector){

this.context = context;

this.collector = collector;

}

 

@Override

public void nextTuple() {

try {

Stock stock = YahooFinance.get(“INTC”);

BigDecimal price = stock.getQuote().getPrice();

 

this.collector.emit(new Values(“INTC”, price.doubleValue()));

stock = YahooFinance.get(“GOOGL”);

price = stock.getQuote().getPrice();

 

this.collector.emit(new Values(“GOOGL”, price.doubleValue()));

stock = YahooFinance.get(“AAPL”);

price = stock.getQuote().getPrice();

 

this.collector.emit(new Values(“AAPL”, price.doubleValue()));

} catch(Exception e) {}

}

 

@Override

public void declareOutputFields(OutputFieldsDeclarer declarer) {

declarer.declare(new Fields(“company”, “price”));

}

 

@Override

public void close() {}

 

public boolean isDistributed() {

return false;

}

 

@Override

public void activate() {}

 

@Override

public void deactivate() {}

 

@Override

public void ack(Object msgId) {}

 

@Override

public void fail(Object msgId) {}

 

@Override

public Map<String, Object> getComponentConfiguration() {

return null;

}

 

}

Bolt Creation

Here the aim of bolt is to process the given company’s prices when the costs fall below 100. It uses Java Map object to line the cutoff price limit alert as true when the stock prices fall below 100; otherwise, false. the entire program code is as follows −

Coding: PriceCutOffBolt.java

import java.util.HashMap;

import java.util.Map;

 

import backtype.storm.tuple.Fields;

import backtype.storm.tuple.Values;

 

import backtype.storm.task.OutputCollector;

import backtype.storm.task.TopologyContext;

 

import backtype.storm.topology.IRichBolt;

import backtype.storm.topology.OutputFieldsDeclarer;

 

import backtype.storm.tuple.Tuple;

 

public class PriceCutOffBolt implements IRichBolt {

Map<String, Integer> cutOffMap;

Map<String, Boolean> resultMap;

 

private OutputCollector collector;

 

@Override

public void prepare(Map conf, TopologyContext context, OutputCollector collector) {

this.cutOffMap = new HashMap <String, Integer>();

this.cutOffMap.put(“INTC”, 100);

this.cutOffMap.put(“AAPL”, 100);

this.cutOffMap.put(“GOOGL”, 100);

 

this.resultMap = new HashMap<String, Boolean>();

this.collector = collector;

}

 

@Override

public void execute(Tuple tuple) {

String company = tuple.getString(0);

Double price = tuple.getDouble(1);

 

if(this.cutOffMap.containsKey(company)){

Integer cutOffPrice = this.cutOffMap.get(company);

 

if(price < cutOffPrice) {

this.resultMap.put(company, true);

} else {

this.resultMap.put(company, false);

}

}

 

collector.ack(tuple);

}

 

@Override

public void cleanup() {

for(Map.Entry<String, Boolean> entry:resultMap.entrySet()){

System.out.println(entry.getKey()+” : ” + entry.getValue());

}

}

 

@Override

public void declareOutputFields(OutputFieldsDeclarer declarer) {

declarer.declare(new Fields(“cut_off_price”));

}

 

@Override

public Map<String, Object> getComponentConfiguration() {

return null;

}

 

}

Submitting a Topology

This is the main application where YahooFinanceSpout.java and PriceCutOffBolt.java are connected together and produce a topology. The following program code shows how you can submit a topology.

Coding: YahooFinanceStorm.java

import backtype.storm.tuple.Fields;

import backtype.storm.tuple.Values;

 

import backtype.storm.Config;

import backtype.storm.LocalCluster;

import backtype.storm.topology.TopologyBuilder;

 

public class YahooFinanceStorm {

public static void main(String[] args) throws Exception{

Config config = new Config();

config.setDebug(true);

 

TopologyBuilder builder = new TopologyBuilder();

builder.setSpout(“yahoo-finance-spout”, new YahooFinanceSpout());

 

builder.setBolt(“price-cutoff-bolt”, new PriceCutOffBolt())

.fieldsGrouping(“yahoo-finance-spout”, new Fields(“company”));

 

LocalCluster cluster = new LocalCluster();

cluster.submitTopology(“YahooFinanceStorm”, config, builder.createTopology());

Thread.sleep(10000);

cluster.shutdown();

}

}

Building and Running the Application

The complete application has three Java codes. They are as follows −

  • YahooFinanceSpout.java
  • PriceCutOffBolt.java
  • YahooFinanceStorm.java

The application can be built using the following command −

javac -cp “/path/to/storm/apache-storm-0.9.5/lib/*”:”/path/to/yahoofinance/lib/*” *.java

The application can be run using the following command −

javac -cp “/path/to/storm/apache-storm-0.9.5/lib/*”:”/path/to/yahoofinance/lib/*”:.

YahooFinanceStorm

Output

The output will be similar to the following −

GOOGL : false

AAPL : false

INTC : true

So, this brings us to the end of blog. This Tecklearn ‘Application of Apache Storm Framework in Yahoo Finance’ helps you with commonly asked questions if you are looking out for a job in Apache Storm and Big Data Domain.

If you wish to learn Apache Storm and build a career in Apache Storm or Big Data domain, then check out our interactive, Apace Storm Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/apache-strom-training/

Apache Storm Training

About the Course

Tecklearn Apache Storm training will give you a working knowledge of the open-source computational engine, Apache Storm. You will be able to do distributed real-time data processing and come up with valuable insights. You will learn about the deployment and development of Apache Storm applications in real world for handling Big Data and implementing various analytical tools for powerful enterprise-grade solutions. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Apache Storm.

Why Should you take Apache Storm Training?

  • The average pay of Apache Storm Professional stands at $90,167 P.A – ​Indeed.com​​
  • Groupon, Twitter and many companies using Apache Storm for business purposes like real-time analytics and micro-batch processing.
  • Apache Storm is a free and open source, distributed real-time computation system for processing fast, large streams of data

What you will Learn in this Course?

Introduction to Apache Storm

  • Apache Storm
  • Apache Storm Data Model

Architecture of Storm

  • Apache Storm Architecture
  • Hadoop distributed computing
  • Apache Storm features

Installation and Configuration

  • Pre-requisites for Installation
  • Installation and Configuration

Storm UI

  • Zookeeper
  • Storm UI

Storm Topology Patterns

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

0 responses on "Application of Apache Storm Framework in Yahoo Finance"

Leave a Message

Your email address will not be published. Required fields are marked *