从Mapper写入多个输出

时间:2017-04-27 13:06:13

标签: java mapreduce hdfs

在示例数据input.txt下面,它有2列密钥&值。对于Mapper处理的每条记录,map的输出应写入

1)HDFS =>需要根据键列

创建新文件

2)上下文对象

下面是代码,其中需要根据键列创建4个文件,但是没有创建文件。输出也不正确。我期待wordcount输出,但我得到字符数输出。

input.txt
------------
key         value
HelloWorld1|ID1
HelloWorld2|ID2
HelloWorld3|ID3
HelloWorld4|ID4



    public static class MapForWordCount extends Mapper<LongWritable, Text, Text, IntWritable> {
        public void map(LongWritable key, Text value, Context con) throws IOException, InterruptedException {

            String line = value.toString();
            String[] fileContent = line.split("|");
            Path hdfsPath = new Path("/filelocation/" + fileContent[0]);
            System.out.println("FilePath : " +hdfsPath);

            Configuration configuration = con.getConfiguration();
            writeFile(fileContent[1], hdfsPath, configuration); 

            for (String word : fileContent) {
                Text outputKey = new Text(word.toUpperCase().trim());
                IntWritable outputValue = new IntWritable(1);
                con.write(outputKey, outputValue);
            }       
        }

        static void writeFile(String fileContent, Path hdfsPath, Configuration configuration) throws IOException {

            FileSystem fs = FileSystem.get(configuration);
                FSDataOutputStream fin = fs.create(hdfsPath);
                fin.writeUTF(fileContent);
                fin.close();
        }
    }

1 个答案:

答案 0 :(得分:0)

Split使用regexp。您需要转发像'|'

这样的.split("\\|");

请参阅此处的文档:http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html