Common hadoop hdfs operations java code
Package hadoopTest; import java. io. IOException; import java. util. logging; import org. apache. hadoop. conf. configuration; import org. apache. hadoop. fs. blockLocation; import org. apache. hadoop. fs. FSDataInputStream; import org. apache. hadoop. fs. FSDataOutputStream; import org. apache. hadoop. fs. fileStatus; import org. apache. hadoop. fs. fileSystem; import org. apache. hadoop. fs. path; import org. apache. hadoop. hdfs. distributedFileSystem; import org. apache. hadoop. hdfs. protocol. datanodeInfo; public class MKdir {/*** @ Description: create a file * @ Author: Cao bigang * @ time: october 15, 2015 11:32:24 * @ throws Exception */private static void mkdir () throws Exception {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test"); fileSystem. mkdirs (path);}/*** @ Description: delete a file * @ Author: Cao bigang * @ time: october 15, 2015 11:33:05 * @ throws IOException */private static void deletedir () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test"); fileSystem. delete (path, true);}/*** @ Description: Write File * @ Author: Cao bigang * @ time: october 15, 2015 11:37:46 * @ param args * @ throws IOException * @ throws Exception */private static void writerFile () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test.txt"); FSDataOutputStream dataOutputStream = fileSystem. create (path); dataOutputStream. writeUTF ("hello hadoop test");}/*** @ throws IOException * @ Description: Read the file * @ Author: Cao bigang * @ time: october 15, 2015 11:42:06 */private static void readFile () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("hadoop_test.txt"); if (fileSystem. exists (path) {FSDataInputStream dataInputStream = fileSystem. open (path); FileStatus fileStatus = fileSystem. getFileStatus (path); byte B [] = new byte [Integer. parseInt (String. valueOf (fileStatus. getLen ()]; dataInputStream. readFully (B) ;}}/*** @ Description: traverse directory files * @ Author: Cao bigang * @ time: october 15, 2015 1:21:32 * @ throws IOException */private static void listDir () throws IOException {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("/"); FileStatus [] fileStatus = fileSystem. listStatus (path); print (fileStatus);} private static void print (FileStatus [] fileStatus) {for (int I = 0; I <fileStatus. length; I ++) {FileStatus status = fileStatus [I]; if (status. isDir () {} else {System. out. println (status. getPath (). getName () ;}}/ *** @ throws Exception * @ Description: File Location * @ Author: Cao bigang * @ time: october 15, 2015 1:26:13 */private static void fileLocal () throws Exception {Configuration conf = new Configuration (); FileSystem fileSystem = FileSystem. get (conf); Path path = new Path ("/user/centos"); FileStatus fileStatus = fileSystem. getFileStatus (path); BlockLocation [] blockLocations = fileSystem. getFileBlockLocations (fileStatus, 0, fileStatus. getLen (); for (int I = 0; I <blockLocations. length; I ++) {String hosts [] = blockLocations [I]. getHosts (); for (int j = 0; j