我正在尝试编写一个可以读取输入文件并将输出写入另一个文本文件的 MapReduce 程序。我打算为此使用 BufferedReader 类。但我真的不知道如何在 MapReduce 程序中使用它。
如何为它编写代码片段?
附言我对 Hadoop 和 MapReduce 编程完全陌生。
以下代码帮助您从 HDFS 读取文件并在控制台中显示内容
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class Cat{
public static void main (String [] args) throws Exception{
try{
Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
String line;
line=br.readLine();
while (line != null){
System.out.println(line);
line=br.readLine();
}
}catch(Exception e){
}
}
}
编辑
司机
public class ReadFile {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "Read a File");
FileSystem fs = FileSystem.get(conf);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
if (fs.exists(new Path(args[1])))
fs.delete(new Path(args[1]), true);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setJarByClass(ReadFile.class);
job.waitForCompletion(true);
}
}
映射器
public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
public void setup(Context context) throws IOException{
Path pt=new Path("hdfs:/path/to/file");//Location of file in HDFS
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
String line;
line=br.readLine();
while (line != null){
System.out.println(line);
line=br.readLine();
}
}
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//as your wish
}
}
}
以上代码帮助您从 HDFS 读取文本文件。