HDFS API示例代码

无干货,仅供复制

public class HdfsExample {

	public static void main(String[] args) throws IOException {
		String dir = "/home/kent";
		String fileUrl = "hdfs://localhost" + dir + "/" + System.currentTimeMillis() + "hdfsExample.txt";

		FileSystem fs = FileSystem.get(URI.create(fileUrl), new Configuration());

		// create a file
		System.out.println("Creating hdfs file : " + fileUrl);
		Path path = new Path(fileUrl);
		FSDataOutputStream out = fs.create(path);

		// write to a file
		out.write("Hello, HDFS Example!".getBytes());
		out.flush();
		out.sync();

		// check a file's status status
		FileStatus status = fs.getFileStatus(path);
		System.out.println("The example file's status is: " + ToStringBuilder.reflectionToString(status, ToStringStyle.SHORT_PREFIX_STYLE));

		// read a file and print the content on console
		FSDataInputStream in = fs.open(path);
		System.out.println("The content of " + fileUrl + " is: ");
		IOUtils.copyBytes(in, System.out, 4096);
		System.out.println();

		// detelte a file
		fs.delete(path, false);

		// list the dir
		FileStatus[] listStatus = fs.listStatus(path);
		System.out.println("Num of files in dir now " + dir + " is: " + (listStatus == null ? 0 : listStatus.length));

	}
}


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.