/**
   * Reads back the evaluations.
   *
   * @param fs File System
   * @param conf Job configuration
   * @param outpath output <code>Path</code>
   * @param evaluations <code>List&lt;Fitness&gt;</code> that contains the evaluated fitness for
   *     each candidate from the input population, sorted in the same order as the candidates.
   * @throws IOException
   */
  private static void importEvaluations(
      FileSystem fs, JobConf conf, Path outpath, List<CDFitness> evaluations) throws IOException {
    Sorter sorter = new Sorter(fs, LongWritable.class, CDFitness.class, conf);

    // merge and sort the outputs
    Path[] outfiles = OutputUtils.listOutputFiles(fs, outpath);
    Path output = new Path(outpath, "output.sorted");
    sorter.merge(outfiles, output);

    // import the evaluations
    LongWritable key = new LongWritable();
    CDFitness value = new CDFitness();
    Reader reader = new Reader(fs, output, conf);

    while (reader.next(key, value)) {
      evaluations.add(new CDFitness(value));
    }

    reader.close();
  }
  public static void main(String[] args) throws IOException {
    Configuration conf = new Configuration();
    Path p = new Path("hdfs://localhost:9000/testsort/data.seq");
    // URI uri = new File(".").getAbsoluteFile().toURI();
    // Path local = new Path(uri);

    // Path p = new Path(new Path(local, "tmp"), "data.seq");
    // {
    // Writer writer = SequenceFile.createWriter(conf,
    // Writer.valueClass(LongWritable.class),
    // Writer.keyClass(BytesWritable.class), Writer.file(p));
    //
    // LongWritable value = new LongWritable();
    // BytesWritable key = new BytesWritable();
    // Random random = new Random(1);
    // byte[] buf = new byte[50];
    // for (int i = 0; i < 10000000; i++) {
    // value.set(random.nextLong());
    // random.nextBytes(buf);
    // key.set(buf, 0, buf.length);
    // writer.append(key, value);
    // }
    // writer.close();
    // }

    // {
    // // io.seqfile.local.dir
    // URI uri = new File(".").getAbsoluteFile().toURI();
    // Path local = new Path(uri);
    //
    // conf.set("io.seqfile.local.dir", new Path(local, "tmp").toString());
    // Path tempDir = new Path(p.getParent(), "tmp");
    // Sorter sorter = new SequenceFile.Sorter(p.getFileSystem(conf),
    // BytesWritable.class, LongWritable.class, conf);
    // sorter.setMemory(10 * 1024 * 1024);
    // // Path tempDir = new Path(p.getParent(), "tmp");
    //
    // RawKeyValueIterator iterate = sorter.sortAndIterate(new Path[] { p },
    // tempDir, false);
    // Path sortedPath = new Path(p.getParent(), "sorted.seq");
    // Writer writer = SequenceFile.createWriter(conf,
    // Writer.valueClass(LongWritable.class),
    // Writer.keyClass(BytesWritable.class), Writer.file(sortedPath));
    // while (iterate.next()) {
    // DataOutputBuffer key = iterate.getKey();
    // byte[] keyData = key.getData();
    // int keyOffset = 0;
    // int keyLength = key.getLength();
    // ValueBytes val = iterate.getValue();
    // writer.appendRaw(keyData, keyOffset, keyLength, val);
    // }
    // writer.close();
    // }
    {
      conf.setInt("io.sort.factor", 1000);
      Sorter sorter =
          new SequenceFile.Sorter(
              p.getFileSystem(conf), BytesWritable.class, LongWritable.class, conf);
      sorter.setMemory(10 * 1024 * 1024);
      sorter.sort(new Path[] {p}, new Path(p.getParent(), "sorted.seq"), false);
    }
  }