我有以下Reducer类(class)
public class CompanyMinMaxReducer extends Reducer<Text, DateClosePair, Text, Text> {
private Text rText = new Text();
public void reduce(Text key, Iterable<DateClosePair> values, Context context)
throws IOException, InterruptedException {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
LongWritable minDay = new LongWritable();
LongWritable maxDay = new LongWritable();
for(DateClosePair val: values){
LongWritable tempDate = val.getDate();
DoubleWritable tempClose = val.getClose();
if(tempDate.compareTo(maxDay) > 0){
maxDay = tempDate;
}else if(tempDate.compareTo(minDay) < 0){
minDay = tempDate;
}
if(tempClose.get() > max){
max = (int)tempClose.get();
}else if(tempClose.get() < min){
min = (int)tempClose.get();
}
}
String minDayFinal = "" + new SimpleDateFormat("yyyy").format(new Date(minDay.get()));
String maxDayFinal = "" + new SimpleDateFormat("yyyy").format(new Date(maxDay.get()));
String output = minDayFinal + " - " + maxDayFinal + " MIN: " + min + " MAX: " + max;
rText.set(output);
context.write(key, rText);
}
}
我的数据集的格式如下:
exchange, stock_symbol, date, stock_price_open,stock_price_high,stock_price_low, stock_price_close, stock_volume,stock_price_adj_close.
例如:
NASDAQ,AAPL,1970-10-22, ...
我被要求编写一个新的MapReduce程序,该程序为每家公司提供它在股票市场中存在的年限以及股票获得的最大和最小收盘价。
我的程序产生正确的输出,但是由于某些原因开始日期是恒定的:
AAON 1970 - 2002 MIN: 1 MAX: 35
AATI 1970 - 2010 MIN: 2 MAX: 15
ABCO 1970 - 2004 MIN: 14 MAX: 69
ABCW 1970 - 2007 MIN: 0 MAX: 53
ABII 1970 - 2008 MIN: 25 MAX: 78
ABIO 1970 - 1999 MIN: 0 MAX: 139
ABMC 1970 - 2004 MIN: 0 MAX: 6
ABTL 1970 - 2004 MIN: 0 MAX: 58
ACAD 1970 - 2009 MIN: 0 MAX: 17
ACAP 1970 - 2005 MIN: 15 MAX: 55
ACAT 1970 - 2009 MIN: 3 MAX: 29
ACCL 1970 - 1997 MIN: 3 MAX: 104
ACEL 1970 - 1998 MIN: 0 MAX: 10
ACET 1970 - 2004 MIN: 4 MAX: 27
ACFC 1970 - 2008 MIN: 1 MAX: 20
ACGL 1970 - 1997 MIN: 11 MAX: 80
ACLI 1970 - 2006 MIN: 2 MAX: 77
ACLS 1970 - 2001 MIN: 0 MAX: 30
我写的DateClosePair是可写的客户,就像您在网络上找到的每个示例一样。
min_closing价格和max_closing价格正确,但mix_date和max_date错误,这很奇怪。
有什么想法吗?
最佳答案
LongWritable minDay = new LongWritable()
将最小日期变量初始化为1970。
更准确地说:除非给定特定值,否则LongWritable
会根据java language spec将其内部long
初始化为0。将其输入java.util.Date
时,从Unix时代January 1, 1970, 00:00:00 UTC
解释为0毫秒。
我的猜测是1970是数据集中所有日期值的下限。这将为每个键写入。
我注意到您使用int min = Integer.MAX_VALUE
初始化了close值。也许您可以使用LongWritable minDay = LongWritable(Long.MAX_VALUE)
来解决?