Bufferreader和Bufferedwriter用于读写hdfs文件

时间:2013-05-12 18:27:35

标签: hadoop hdfs

我尝试逐行读取hdfs文件,然后创建一个hdfs文件并逐行写入。我使用的代码如下所示:

            Path FileToRead=new Path(inputPath);
        FileSystem hdfs = FileToRead.getFileSystem(new Configuration());            
        FSDataInputStream fis = hdfs.open(FileToRead);
        BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

        String line;
            line = reader.readLine(); 
            while (line != null){

                String[] lineElem = line.split(",");
                for(int i=0;i<10;i++){

                    MyMatrix[i][Integer.valueOf(lineElem[0])-1] = Double.valueOf(lineElem[i+1]);
                }

                line=reader.readLine();
        } 

        reader.close();
        fis.close();


        Path FileToWrite = new Path(outputPath+"/V"); 
        FileSystem fs = FileSystem.get(new Configuration());
        FSDataOutputStream fileOut = fs.create(FileToWrite);
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOut));
        writer.write("check");
        writer.close();
        fileOut.close();

当我在outputPath文件中运行此代码时,尚未创建V.但是如果我用部件替换要读取的部件,那么将创建文件并将检查写入其中。 任何人都可以帮我理解如何正确使用它们,以便能够先读取整个文件,然后逐行写入文件?

我还尝试了另一个代码,用于从一个文件中读取并写入另一个文件,但是文件将被创建,但没有任何内容写入其中!

我用这样的话:

  hadoop jar main.jar program2.Main input output

然后在我的第一份工作中,我从arg [0]读取并写入args [1] +&#34; / NewV&#34;使用map reduce类并且它可以工作。 在我的其他课程(非地图缩小)中,我使用args [1] +&#34; / NewV&#34;作为输入路径和输出+&#34; / V_0&#34;作为输出路径(我将这些字符串传递给构造函数)。这是该类的代码:

 public class Init_V {

String inputPath, outputPath;


public Init_V(String inputPath, String outputPath) throws Exception {

    this.inputPath = inputPath;
    this.outputPath = outputPath;


    try{            

        FileSystem fs = FileSystem.get(new Configuration());
        Path FileToWrite = new Path(outputPath+"/V.txt"); 
        Path FileToRead=new Path(inputPath);
        BufferedWriter output = new BufferedWriter
         (new OutputStreamWriter(fs.create(FileToWrite,
                 true)));  

        BufferedReader reader = new
            BufferedReader(new InputStreamReader(fs.open(FileToRead)));
                 String data;
                 data = reader.readLine();
                 while ( data != null ) 
                 {
                     output.write(data);
                     data = reader.readLine();
                 }
                 reader.close();                     
                 output.close(); }catch(Exception e){
}

}

}

1 个答案:

答案 0 :(得分:1)

我想,你需要了解hadoop如何正常工作。在hadoop中,很多事情都是由系统完成的,你只是提供输入和输出路径,然后如果路径有效则由hadoop打开和创建。请检查以下示例;

public int run (String[] args) throws Exception{

    if(args.length != 3){
        System.err.println("Usage: MapReduce <input path> <output path> ");
        ToolRunner.printGenericCommandUsage(System.err);
    }
    Job job = new Job();
    job.setJarByClass(MyClass.class);
    job.setNumReduceTasks(5);
    job.setJobName("myclass");
    FileInputFormat.addInputPath(job, new Path(args[0]) );
    FileOutputFormat.setOutputPath(job, new Path(args[1]));

    job.setMapperClass(MyMapper.class);
    job.setReducerClass(MyReducer.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(Text.class);

    return job.waitForCompletion(true) ? 0:1 ;
}


/* ----------------------main---------------------*/
public static void main(String[] args) throws Exception{    

    int exitCode = ToolRunner.run(new MyClass(), args);
    System.exit(exitCode);
}

如您所见,您只需初始化必要的变量,阅读和写作由hadoop完成。

另外,在您的 Mapper类中,您在地图中说context.write(key, value),而在您的 Reduce类中也是如此,它会为您写信。

如果您使用BufferedWriter / Reader,它将写入您的本地文件系统而不是HDFS。要查看HDFS中的文件,您应该编写hadoop fs -ls <path>ls命令所查找的文件位于本地文件系统中

编辑:为了使用读/写,您应该知道以下内容:假设您的hadoop网络中有N台机器。当你想阅读时,你不会知道哪个映射器正在阅读,同样写作。因此,所有映射器和减速器都应该具有不提供异常的路径。

我不知道您是否可以使用任何其他类,但出于特定原因可以使用两种方法:startupcleanup。这些方法在每个地图中仅使用一次并减少工作人员。因此,如果您想要读写,您可以使用该文件。读写与普通的java代码相同。例如,您希望查看每个键的内容,并希望将其写入txt。您可以执行以下操作:

//in reducer
BufferedReader bw ..;

void startup(...){
     bw  = new ....;
}

void reduce(...){
    while(iter.hasNext()){ ....;
    }
    bw.write(key, ...);
}
void cleanup(...){
    bw.close();
}
相关问题