我有一个输入文件:
101 Alice 23 female IT 45
102 Bob 34 male Finance 89
103 Chris 67 male IT 97
我的映射器:
package EmpCtcPack;
import java.io.IOException;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
public class EmpctcMapper extends Mapper<Object, Text, Text, Text>{
private Text MKey=new Text();
private Text MValue=new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
String tempKey= new String();
String tempValue=new String();
try
{
tempValue=value.toString();
tempKey=value.toString().split(" ")[3];
}
catch (Exception e)
{
e.printStackTrace();
}
MKey.set(tempKey);
MValue.set(tempValue);
context.write(MKey, MValue);
}
}
我的 reducer :
package EmpCtcPack;
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
public class EmpCtcReducer extends Reducer<Text,Text,Text,Text> {
private Text RValue=new Text();
private Text RKey= new Text();
public void reduce(Text key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
Integer i= new Integer(0);
String s=new String();
Integer t=new Integer(0);
Text text=new Text();
try
{
for (Text val : values)
{
String arr[]=val.toString().split(" ");
s=arr[3];
text.set(s);
context.write(key, text);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
问题出在拆分方法中。
当我尝试获取
arr[0]
时,它可以正常工作,并且我获得了ID号(101、102等)。但是,如果我尝试获取
arr[1]
或arr[2]
,则会得到 0 。有谁知道为什么会这样吗?
先感谢您!
最佳答案
您已经在驱动程序类中使用了合并器,在这种情况下不需要使用合并器。
尝试在split():
中使用regEx
value.toString().split("\\t+"); //if split-en by multiple tabs
value.toString().split("\\t"); //if split-en by single tab
关于java - MapReduce中的分割方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41873734/