【发布时间】:2017-01-26 12:44:10
【问题描述】:
我有一个输入文件:
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);
}
}
我的减速机:
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。
有谁知道为什么会这样?
提前谢谢你!
【问题讨论】:
-
请查看stackoverflow.com/help/mcve 了解如何创建最小、完整且可验证的示例
-
您能否展示一下您使用 arr[0]、arr[1] 等的代码段吗?
-
我的意思是在 s=arr[3] 行中,如果是 s=arr[0],那么如果有任何其他索引 (1,2,3. ..),比我得到的只是 0
-
你能添加你的驱动类吗?我认为您在驱动程序类中使用了组合器,在这种情况下您不需要。
-
是的,移除组合器后它现在可以正常工作了。非常感谢
标签: java eclipse hadoop mapreduce