Java Hadoop context.write()输出使用文本对象打印NaN
这是我第一次使用Hadoop,在写入输出文件时遇到了问题。当我使用System.out打印值时,它显示良好,但使用context.write(键,值)将值打印为NaN 例如:Java Hadoop context.write()输出使用文本对象打印NaN,java,hadoop,mapreduce,output,nan,Java,Hadoop,Mapreduce,Output,Nan,这是我第一次使用Hadoop,在写入输出文件时遇到了问题。当我使用System.out打印值时,它显示良好,但使用context.write(键,值)将值打印为NaN 例如: System.out.println(stockName.toString() + " " + result.toString()); 正确输出到用户日志: AAPL.csv 0.076543 但使用: context.write(stockName, result); 产出: AAPL.csv NaN resul
System.out.println(stockName.toString() + " " + result.toString());
正确输出到用户日志:
AAPL.csv 0.076543
但使用:
context.write(stockName, result);
产出:
AAPL.csv NaN
result和stockName都是先前设置的Text()对象
我还包括了我的整个reduce函数。任何想法都会很好,因为我已经尝试了我能想到的一切,谢谢
public static class Reduce extends Reducer<Text, Text, Text, Text> {
private Text stockName = new Text();
private ArrayList<Float> monthlyReturn = new ArrayList<Float>();
private String previousMonth = "";
private float numOfMonths = 0;
private float startPrice = 0;
private float endPrice = 0;
private Text result = new Text();
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
// Set the Stock Name as the Key
stockName.set(key);
for (Text val: values) {
System.out.println(val);
// Parse date & adjusted close
String[] stockValues = val.toString().split(",");
if (stockValues.length < 2) {
continue;
}
String month = stockValues[0];
String priceInput = stockValues[1];
float closingPrice = Float.parseFloat(priceInput);
// First time around setup.
if (startPrice == 0 && previousMonth.equals("")) {
startPrice = closingPrice;
previousMonth = month;
}
/*
* We check if the month has changed, and that we're not just starting.
* If the month changed, increment the number of months we have seen, and run a calculation
* for monthly return.
*
* closePrice is set to every stock value. The startPrice is only set when the month changes.
* When the month does change, we take the last set closePrice to run our calculation, and
* then set the new startPrice.
*/
if (!month.equals(previousMonth) && endPrice != 0) {
numOfMonths += 1;
monthlyReturn.add((endPrice - startPrice)/startPrice);
startPrice = closingPrice;
}
previousMonth = month;
endPrice = closingPrice;
}
// Add on the last month value
numOfMonths += 1;
monthlyReturn.add((endPrice - startPrice)/startPrice);
/*
* Generate the volatility. The equation is as follows:
*
* 1. xbar = sum(xi)/numOfMonth -> sum is over all values from 0 to N in monthlyReturn
* 2. xsum = sum( (xi-xbar)^2 ) from 0 to N in monthlyReturn
* 3. volatility = sqrt( (1/numOfMonth-1)*xsum )
*/
// 1.
float xiSum = 0;
for (int i =0; i<monthlyReturn.size(); i++) {
xiSum += monthlyReturn.get(i);
}
float xBar = xiSum/numOfMonths;
// 2.
double xSum = 0;
for (int i=0; i<monthlyReturn.size(); i++) {
xSum += Math.pow(monthlyReturn.get(i) - xBar, 2);
}
// 3.
double root = (1/(numOfMonths-1))*xSum;
result.set(String.valueOf(Math.sqrt(root)));
System.out.println(stockName.toString() + " " + result.toString());
context.write(stockName, result);
}
}
public static void main(String[] args) throws Exception {
Job job = Job.getInstance();
job.setJarByClass(StockVolatility.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
}
公共静态类Reduce扩展Reducer{
私有文本stockName=新文本();
private ArrayList monthlyReturn=new ArrayList();
私有字符串previousMonth=“”;
私人浮动月数=0;
私有浮动起始价格=0;
私人浮动价格=0;
私有文本结果=新文本();
公共void reduce(文本键、Iterable值、上下文上下文)引发IOException、InterruptedException{
//将股票名称设置为键
stockName.set(键);
用于(文本值:值){
系统输出打印项次(val);
//解析日期&调整关闭
字符串[]stockValues=val.toString().split(“,”);
如果(stockValues.length<2){
继续;
}
字符串月份=股票价值[0];
字符串priceInput=stockValues[1];
float closingPrice=float.parseFloat(priceInput);
//第一次安装。
if(startPrice==0&&previousMonth.equals(“”){
startPrice=成交价格;
上一个月=月份;
}
/*
*我们检查这个月是否有变化,并且我们不是刚刚开始。
*如果月份发生变化,则增加我们看到的月份数,并运行计算
*月报。
*
*closePrice设置为每个股票价值。startPrice仅在月份变化时设置。
*当月份确实发生变化时,我们使用最后一组closePrice来运行计算,并且
*然后设定新的开始价格。
*/
如果(!month.equals(上一个月)&&endPrice!=0){
月数+=1;
月回报添加((endPrice-startPrice)/startPrice);
startPrice=成交价格;
}
上一个月=月份;
endPrice=收盘价;
}
//加上上月的价值
月数+=1;
月回报添加((endPrice-startPrice)/startPrice);
/*
*产生波动率。方程如下:
*
*1.xbar=sum(xi)/numOfMonth->sum覆盖了monthlyReturn中从0到N的所有值
*2.xsum=月回报中从0到N的总和((xi xbar)^2)
*3.波动率=sqrt(1个月/个月)*xsum)
*/
// 1.
浮动轴=0;
对于(inti=0;i不使用job.setCombinerClass(Reduce.class);
我这样做之后,我的问题就解决了