不多说,直接上代码。
 

代码
package zhouls.bigdata.myWholeHadoop.HDFS.hdfs4;
import java.io.IOException;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import java.net.URI;
public class HDFStest1
{	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws URISyntaxException 
	 */
	public static void main(String[] args) throws IOException, URISyntaxException
	{
		// TODO Auto-generated method stub
		//mkdir();
		//copyToHDFS();
		//getFile();
		//ListAllFile();
		//getFileLocal();
		//rmdir();
		getHDFSNodes();
	}	
	//获取HDFS文件系统
	public static FileSystem getFileSystem() throws IOException,URISyntaxException
	{
		Configuration conf = new Configuration();//读取配置文件,比如core-site.xml
		//FileSystem fs =FileSystem.get(conf);
		
		URI uri = new URI("hdfs://HadoopMaster:9000");
		
		FileSystem fs = FileSystem.get(uri,conf); 
		return fs;
	}
	public static void mkdir() throws IOException,URISyntaxException
	{
		//第一步,获取文件系统
		FileSystem fs =getFileSystem();
		//第二步,创建文件目录
		fs.mkdirs(new Path("/zhouls/data"));
		//第三步,释放资源
		fs.close();
	}
	
	public static void copyToHDFS() throws IOException,URISyntaxException
	{
		//第一步
		FileSystem fs=getFileSystem();
		//第二步
		Path srcpath=new Path("D://Data/weibo.txt");
		Path dstpath=new Path("/zhouls/data");
		//第三步
		fs.copyFromLocalFile(srcpath, dstpath);
		//第四步
		fs.close();
	}
	
	public static void getFile() throws IOException, URISyntaxException
	{
		//第一步
		FileSystem fs=getFileSystem();
		//第二步
		Path srcpath=new Path("/zhouls/data/weibo.txt");
		Path dstpath=new Path("D://Data/test");
		//第三步
		fs.copyToLocalFile(srcpath, dstpath);	
		//第四步
		fs.close();
	}
	
	public static void ListAllFile() throws IOException, URISyntaxException
	{
		//第一步
		FileSystem fs=getFileSystem();
		//第二步
		FileStatus[] status =fs.listStatus(new Path("/zhouls"));
		//第三步
	    Path[] listedPaths = FileUtil.stat2Paths(status);
	    //第四步
	    for(Path p:listedPaths)
	    { 
	        System.out.println(p);
	        
	    }
	    //第五步
	    fs.close();
	}
	
	public static void getFileLocal() throws IOException, URISyntaxException
	{
		//第一步
		FileSystem fs=getFileSystem();
		//第二步
		Path path=new Path("/zhouls/data/weibo.txt");
		//第三步
		FileStatus fileStatus=fs.getFileLinkStatus(path);
		//第四步
		BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
		//第五步
		 for(int i=0;i< blkLocations.length;i++)
		 {
		      String[] hosts = blkLocations[i].getHosts();
		      System.out.println("block_"+i+"_location:"+hosts[0]);
		 }
		 //第六步
		 fs.close();
	}
	
	public static void rmdir() throws IOException, URISyntaxException
	{
		//第一步
		FileSystem fs=getFileSystem();
		//第二步
		fs.delete(new Path("/zhouls/data"),true);
		//第三步
		fs.close();
	}
	
	public static void getHDFSNodes() throws IOException, URISyntaxException
	{
		//第一步
		FileSystem fs=getFileSystem();
		//第二步
	    DistributedFileSystem hdfs = (DistributedFileSystem)fs;
	    //第三步
	    DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
	    //第四步
	    for(int i=0;i< dataNodeStats.length;i++)
	    {
	        System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName());
	    }
	    //第五步
	    fs.close();
	}
	
}
Hadoop HDFS编程 API入门系列之简单综合版本1(四)
原文:http://www.cnblogs.com/zlslch/p/6175593.html