java - MapReduce中的分割方法

标签 java eclipse hadoop mapreduce

我有一个输入文件:

    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/

相关文章:

java - 未经修改抛出 ConcurrentModificationException

android - 在 MacBook Pro 上通过 adb 调试 HTC Desire HD

bash - Shell脚本未与文本文件输入一起运行

hadoop - 如何在 Hadoop Hive 中执行 "Order of Events"查询?

hadoop - 运行 Hadoop 作业时不是有效的 Jar

java - Spring JPA : entityManager remove not working

java - 根据匹配的关键字分离过滤的推文 : Twitter4j API

Java 定时器与 Linux Cronjob

eclipse - 对于小型(~40 开发人员)项目来说,什么是简单的 Mylyn 任务存储库?

eclipse - Eclipse 有什么好的 SVN 插件?