Beispiel #1
0
  void init() throws Exception {
    //		 FileOutputStream out = new FileOutputStream(new File(tempdir,filename));
    //		 localdir = new File(this.job.config.localdirpath);
    //		 if(!localdir.exists())
    //			 localdir.mkdirs();

    this.hdfsdir = new Path(job.config.hdfsdatadirpath);
    if (this.job.genlocalfile()) {
      datafile = new File(localdir, taskInfo.filename);
      if (datafile.exists()) datafile.delete();
      datafile.createNewFile();
      writer = new PrintWriter(new BufferedWriter(new FileWriter(datafile)));

    } else {
      hdfsdatafile = new Path(job.getHdfsdatadirpath(), taskInfo.filename);
      out = job.getFileSystem().create(hdfsdatafile);

      writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, "UTF-8")));
    }
  }
Beispiel #2
0
  void close() {

    if (closed) return;
    else {

      closed = true;
    }
    if (out != null) {
      try {
        out.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      out = null;
    }
    if (writer != null) {
      try {
        if (!flushed) {
          try {
            this.flush();
          } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
        }
        writer.close();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      writer = null;
    }

    if (this.rows == 0) // 删除空文件
    {
      if (!this.job.genlocalfile()) {
        try {
          log.info("清除没有数据的hdfs file[" + hdfsdatafile + "],");
          job.getFileSystem().delete(hdfsdatafile, true);
          log.info("清除没有数据的hdfs file[" + hdfsdatafile + "]完成,");
        } catch (Exception e) {
          log.info("清除没有数据的hdfs file[" + hdfsdatafile + "]失败,", e);
        }
      } else {

      }
    }
  }