给我带来麻烦的代码是将Text()中的内容转换为int,以便我可以使用一些算术。我知道如何将字符串转换为int,但由于某种原因,Hadoop需要将文件中的项目作为TExt()读取。我无法找到执行此转换的方法。关于如何做到这一点的任何想法?
package BaseballStats;
import java.io.IOException;
import java.util.Iterator;
import mrtools.CountMap;
import mrtools.NBest;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
public class StatReducer extends MapReduceBase
implements Reducer<Text, Text, Text, Text[]> {
private Text Hits = new Text();
private Text AtBats = new Text();
//private Text Year = new Text();
int totalAtBats = 0;
int totalHits = 0;
float average = 0;
Text [] Pair = {AtBats, Hits};
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text[]> output,
Reporter reporter) throws IOException {
//Need to pull values from the Array and assign to AtBats
//THen total all at bats and hits per year (key)
//Divide hits by at Bats to get batting average
//Output batting average per year
for (Text s: Pair) {
//Do your stuff here
Pair[0] = AtBats;
Pair[1] = Hits;
}
//Get values to key to sum up and get a total per year.
while (values.hasNext()){
int AB = Integer.parseInt(AtBats);
int i = Integer.valueOf(AtBats);
totalAtBats = AB + totalAtBats;
}
}
}
答案 0 :(得分:2)
您可以先使用Text
方法将.toString()
转换为字符串,然后将结果转换为int
。