Stock Analysis
Stock Analysis
KIMMI KUMARI
DATASET TO BE CONSIDERED
Mapper Class
The Mapper class will extract the stock symbol and closing price from each line.
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import java.io.IOException;
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException,
InterruptedException {
String line = value.toString();
String[] fields = line.split(",");
stockSymbol.set(stock);
closePrice.set(close);
context.write(stockSymbol, closePrice);
}
}
STOCK ANALYSIS USING MAPREDUCE By: Dr. KIMMI KUMARI
Reducer Class
The Reducer class will calculate the average closing price for each stock.
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
@Override
protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException,
InterruptedException {
double sum = 0.0;
int count = 0;
Driver Class
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
job.setReducerClass(StockReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DoubleWritable.class);
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
# Output
The output will be a list of stock symbols and their average closing
prices:
AAPL 303.5
GOOG 1465.0