hadoop - 我可以使用Text作为值写入MapReduce中的上下文吗

标签 hadoop mapreduce hadoop2

我有一个方案来计算map reduce中两列的平均值。所以我所做的是,我已经使用映射器从文件中获取了值并将它们连接为Text,然后尝试将它们写入如下所示的Context中。

class TestMapper extends Mapper<LongWritable, Text, Text, Text> {
  private Text outputKey;
  private Text outputVal;


  @Override
  public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  
  //more code here

context.write(outputkey,OutputVal);


    
  }
}

最佳答案

您应该在此处使用自定义数据类型,例如一个TextPair类,该类具有两个Text元素来存储所需的数据。下面是一个示例代码,用于以mapper上下文的值输出一对String。

// Mapper's map code
protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, TextPair>.Context context)
        throws IOException, InterruptedException {

    String line = value.toString();
    String year = line.substring(15, 19);
    int airTemperature;
    if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs
      airTemperature = Integer.parseInt(line.substring(88, 92));
    } else {
      airTemperature = Integer.parseInt(line.substring(87, 92));
    }
    String quality = line.substring(92, 93);
    if (airTemperature != MISSING && quality.matches("[01459]")) {
        System.out.println("Year "+year+" "+airTemperature);
      context.write(new Text(year), new TextPair(String.valueOf(airTemperature),1));
    }

//文本对-下面的自定义数据类型代码
public class TextPair implements WritableComparable<TextPair> {

private Text first;
private Text second;

//Default constructor is a must
public TextPair() {
    this.first=new Text();
    this.second=new Text();
}

public TextPair(String first,int second) {
    try {
        this.first=new Text(first);
        this.second=new Text(String.valueOf(second));
    }catch(Exception ex) {
        System.out.println("Exception occurred "+ex.getCause());
    }

}

// Other methods such as compare, equals, hashcode, write, readFields etc implementation also needs to done

public Text getFirst() {
    return first;
}

public Text getSecond() {
    return second;
}

@Override
public String toString() {
    return this.first+"\t"+this.second+"\t";
}

}

如果您还需要更多详细信息,请引用《 Hadoop权威指南》。希望这可以帮助。

关于hadoop - 我可以使用Text作为值写入MapReduce中的上下文吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020485/

相关文章:

java - 使用 Hadoop 将列表作为值处理的最佳方法?

hadoop - 运行Hadoop 2.7.1时的文件权限问题

hadoop - 有效地在Spark中重用Hadoop代码?

hadoop - Hive始终创建mapreduce作业

xml - 使用 Hadoop MapReduce 处理 XML

hadoop - 具有一个事件名称节点的HA群集性能低下

hadoop - Apache Sqoop导入错误

Hadoop 作业完成时间随着输入 gz 文件数量的增加而增加

hadoop - 我们可以有任何命令来检查名称节点元数据吗

java - Pig UDF 找不到 WritableComparable