Initial Configuration
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(config);
Path src = new Path(srcHDFSFile);
Path dst = new Path(dstHDFSFile);
Copy a file from the local file system to HDFS
hdfs.copyFromLocalFile(src, dst);
Create HDFS file
FSDataOutputStream outputStream = hdfs.create(path);
outputStream.write(buff, 0, buff.length);
Rename HDFS file
boolean isRenamed = hdfs.rename(src, dst);
Delete HDFS file
boolean isDeleted = hdfs.delete(src, false);
Recursive delete:
boolean isDeleted = hdfs.delete(path, true);
Get HDFS file last modification time
FileStatus fileStatus = hdfs.getFileStatus(src);
long modificationTime = fileStatus.getModificationTime
Check if a file exists in HDFS
boolean isExists = hdfs.exists(path);
Check if a Path is a file
boolean isExists = hdfs.isFile(path);
Get the locations of a file in the HDFS cluster
FileStatus fileStatus = hdfs.getFileStatus(src);
BlockLocation[] blkLoc = hdfs.getFileBlockLocations(path, 0, fileStatus.getLen());
int blkCount = blkLoc.length;
for (int i=0; i < blkCount; i++) {
String[] hosts = blkLoc[i].getHosts();
// Print the values
}
Get a list of all the nodes host names in the HDFS cluster
DistributedFileSystem hdfs = (DistributedFileSystem) fs;
DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
String[] names = new String[dataNodeStats.length];
for (int i = 0; i < dataNodeStats.length; i++) {
names[i] = dataNodeStats[i].getHostName();
}
Source: http://myjavanotebook.blogspot.com/2008/05/hadoop-file-system-tutorial.html