【问题标题】:Split method in mapreducemapreduce中的拆分方法
【发布时间】: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


【解决方案1】:

您在驱动程序类中使用了组合器,在这种情况下您不需要。

尝试在split():中使用正则表达式

value.toString().split("\\t+"); //if split-en by multiple tabs
value.toString().split("\\t");   //if split-en by single tab

【讨论】:

  • 这些字段是按制表符拆分的,我试过“\\s+”,它没有帮助
  • @Mariia - 查看更新! - 尝试\\t\\t+,如答案中所述
  • 不幸的是没有帮助
  • 您是否在mapperreducer 方法中都进行了此更改?
  • 问题是组合器类,我在这里不需要它
【解决方案2】:

我有同样的错误。这是与组合器类。我删除了组合器类,它现在工作正常。谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 2011-12-29
    • 2022-09-27
    相关资源
    最近更新 更多