java - map 如何知道关键是什么

标签 java hadoop mapreduce

我有一个csv文件

文字|文字|键|文字|文字
文字|文字|键|文字|文字
文字|文字|键|文字|文字
文字|文字|键|文字|文字

和一个Java文件

import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;

public class MTransactionPerDay implements Mapper<WritableComparable, Text, Text, Text>{

    public void map(WritableComparable key, Text value, OutputCollector<Text, Text> outputCollector, Reporter reporter) throws IOException {

    }

我的问题是。如何告诉map方法第三个字段是键?

编辑

这解决了我的问题
public void map(WritableComparable key, Text value, OutputCollector<Text, Text> outputCollector, Reporter reporter) throws IOException {
        //split string
        String[] row = value.toString().split("[|]");
        //define key value pairs
        Text keyString = new Text(row[3]);
        Text valueString = new Text(row[2]);
        //result
        outputCollector.collect(keyString, valueString);
    }

但是提出了另一个问题。我知道map接受文件并返回键/值对。那么 WritableComparable键是什么?

实际上我写了一个测试
@Test
    public void testMapReduce() {
        System.setProperty("hadoop.home.dir", "C:\\WorkSpace\\");
        mapReduceDriver.addInput(new LongWritable(1), new Text("0|9050000001|20160125204123"));
        mapReduceDriver.addInput(new LongWritable(1), new Text("0|9050000001|20160125204123"));
        mapReduceDriver.addInput(new LongWritable(1), new Text("0|9050000002|20160125204123"));
        mapReduceDriver.addOutput(new Text("9050000001"), new IntWritable(2));
        mapReduceDriver.addOutput(new Text("9050000002"), new IntWritable(1));
        mapReduceDriver.runTest();
    }

并必须在此添加
mapReduceDriver.addInput(new LongWritable(1), new Text("0|9050000001|20160125204123"));

甚至以为我从来没有用过那个 key 。

最佳答案

您将必须实现自定义RecordReader。

例如:
Hadoop为TextInputFormat实现了一个记录读取器,它读取文本文件的行。它为每个记录发出的键是读取的行的字节偏移量(作为LongWritable),值是直到终止符'\ n'的行的内容(作为Text对象)。

Refer this to develop custom record reader

关于java - map 如何知道关键是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40016667/

相关文章:

hadoop - 随机排序,节点少于配对

python - 如何加速我在 hadoop 上的 tensorflow 执行?

hadoop - HBase MR-键/值不匹配

hadoop - 如何增加 hadoop 中 mapreduce 程序中映射器和缩减器的数量?

hadoop - MapR 是 MapReduce 的替代品吗

java - 将整数值添加到数据库表错误

java - 信息 : No Spring WebApplicationInitializer types detected on classpath on tomcat 8

java - 使经过身份验证的请求在本地开发服务器上运行

linux - 将CSV文件中的第一行和第三行转换为Shell脚本中的列

java - 这个maven认证错误表示什么?