Example usage for org.apache.hadoop.hdfs DFSTestUtil readFile

List of usage examples for org.apache.hadoop.hdfs DFSTestUtil readFile

Introduction

In this page you can find the example usage for org.apache.hadoop.hdfs DFSTestUtil readFile.

Prototype

public static String readFile(FileSystem fs, Path fileName) throws IOException 

Source Link

Usage

From source file:org.talend.components.test.MiniDfsResource.java

License:Open Source License

/**
 * @param path the name of the file on the HDFS cluster
 * @param lines the lines to write to the file (with terminating end-of-lines).
 *//* w ww  .j a v a 2 s  . c om*/
public static String writeFile(FileSystem fs, String path, String... lines) throws IOException {
    try (PrintWriter w = new PrintWriter(fs.create(new Path(path)))) {
        for (String line : lines)
            w.println(line);
    }
    return DFSTestUtil.readFile(fs, new Path(path));
}

From source file:org.talend.components.test.MiniDfsResourceTest.java

License:Open Source License

/**
 * An example of using the MiniDFSCluster.
 *//*from  ww w .  j  a v a 2 s . c o  m*/
@Test
public void testBasic() throws IOException, URISyntaxException {
    mini.writeFile(mini.getFs(), "/user/test/stuff.txt", "1;one", "2;two", "3;three");

    assertThat(mini.getFs().exists(new Path("/user/test/stuff.txt")), is(true));
    FileStatus[] status = mini.getFs().listStatus(new Path("/user/test/"));
    assertThat(status, arrayWithSize(1));

    // Read the file in one chunk.
    assertThat(IOUtils.readLines(new StringReader(DFSTestUtil.readFile(mini.getFs(), status[0].getPath()))),
            is(Arrays.asList("1;one", "2;two", "3;three")));

    // Read the file as lines.
    mini.assertReadFile(mini.getFs(), "/user/test/stuff.txt", "1;one", "2;two", "3;three");
}

From source file:org.talend.components.test.RecordSetUtil.java

License:Open Source License

public static String writeRandomCsvFile(FileSystem fs, String path, long seed, int startId, int lines,
        int columns, int tagLength, String fieldDelimiter, String recordDelimiter) throws IOException {
    Random r = new Random(seed);
    try (PrintWriter w = new PrintWriter(fs.create(new Path(path)))) {
        for (int id = 0; id < lines; id++) {
            w.print(startId + id);//  ww w .j  a v a 2 s. c  o  m
            for (int column = 1; column < columns; column++) {
                w.print(fieldDelimiter);
                for (int i = 0; i < tagLength; i++)
                    w.print(CHARS[r.nextInt(CHARS.length)]);
            }
            w.print(recordDelimiter);
        }
    }
    return DFSTestUtil.readFile(fs, new Path(path));
}