java从映射器写入多个输出
下面是示例数据输入。txt,它有两列键和;价值对于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 楼答案
Split使用regexp。您需要转义类似
'|'
的.split("\\|");
请参见此处的文档:http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html