码迷,mamicode.com
首页 > 编程语言 > 详细

HDFS API的java代码分析与实例

时间:2016-06-23 22:28:17      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:hdfs api   java代码分析与实例   hadopapi   


HDFS API的java代码分析与实例


1.HDFS常用的方法,我已经写好,我们看一下


技术分享




// Create()方法,直接在HDFS中写入一个新的文件,path为写入路径,text为写入的文本内容
	
	public static void  Create(String path,String text) throws IOException {

		   	
	        Configuration conf=new Configuration();
	        
	        conf.set("fs.default.name", "hdfs://192.168.1.220:9000");   //Hadoop的Master的IP地址
	        
	        FileSystem hdfs=FileSystem.get(conf);

	        byte[] buff=text.getBytes();	       

	        Path dfs=new Path(path);
	       
	        FSDataOutputStream outputStream=hdfs.create(dfs);

	        outputStream.write(buff,0,buff.length);

	        System.out.println("文件创建成功!");

	}
	
	
	
	
	//从HDFS中删除一个文件,path为文件的地址
	
	public static void  delete(String path) throws IOException {

	        Configuration conf=new Configuration();
	        
	        conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	        FileSystem hdfs=FileSystem.get(conf);       

	        Path delef=new Path(path);	       

	        boolean isDeleted=hdfs.delete(delef,false);

	        System.out.println("Delete?"+isDeleted);        
	        
	        System.out.println("文件删除成功!");

	    }
	
	
	
	
	
	 //查找一个文件在位于哪个Hadoop节点上,path为文件的地址
	
	public static void  findLocation(String path) throws IOException {

	        Configuration conf=new Configuration();
	        
	        conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	        FileSystem hdfs=FileSystem.get(conf);
	        
	        Path fpath=new Path(path);

	        FileStatus filestatus = hdfs.getFileStatus(fpath);

	        BlockLocation[] blkLocations = hdfs.getFileBlockLocations(filestatus, 0, filestatus.getLen());	 

	        int blockLen = blkLocations.length;

	        for(int i=0;i<blockLen;i++){

	            String[] hosts = blkLocations[i].getHosts();

	            System.out.println("文件的位置为:");
	            
	            System.out.println("block_"+i+"_location:"+hosts[0]);

	        }
	
	    }
	
	
	
	       //将所有的Hadoop节点,列出来
	       
	        public static List  getNodes() throws IOException {
	        	 
	                Configuration conf=new Configuration();
	                	                
	                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	                FileSystem fs=FileSystem.get(conf);	               

	                DistributedFileSystem hdfs = (DistributedFileSystem)fs;

	                DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();     
	                
	                List  list = new ArrayList();
	                for(int i=0; i<dataNodeStats.length;i++)  {
	                	list.add(  dataNodeStats[i].getHostName());
	                	
	                }
	                
	                return list;
                        
	        }
	
	
	        
	        
	        
	        //显示Hadoop根节点中所有的文件,注意根节点为"\"
	        
	        public static List  listAllFile()  throws IOException {

                    String  path = "hdfs://192.168.1.220:9000/";
	        	
	                Configuration conf=new Configuration();
	                
	                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	                FileSystem hdfs=FileSystem.get(conf);
	              
	                Path listf =new Path(path);	               

	                FileStatus stats[]=hdfs.listStatus(listf);
	                	                

	                List  list = new ArrayList();
	                for(int i=0; i<stats.length;i++)  {
	                	list.add(  stats[i].getPath().toString() );
	                	
	                }
	                
	                return list;
	                
	        
	            }
	        
	        
	        
	        
	        
	        //显示path路径下的所有的文件
	        
	        public static List listFile(String path) throws IOException {


	                Configuration conf=new Configuration();
	                
	                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	                FileSystem hdfs=FileSystem.get(conf);

	               

	                Path listf =new Path(path);

	               

	                FileStatus stats[]=hdfs.listStatus(listf);
	                
	                
	                List  list = new ArrayList();
	                for(int i=0; i<stats.length;i++)  {
	                	list.add(  stats[i].getPath().toString() );
	                	
	                }
	                
	                return list;

	            }
	        
	        
	        
	        
	        
	        //新建一个文件夹,path为新建后的文件地址
	        
	        public static void  mkdir(String path) throws IOException {


	                Configuration conf=new Configuration();
	                
	                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	                FileSystem hdfs=FileSystem.get(conf);
              
	                Path dfs=new Path(path);
 
	                hdfs.mkdirs(dfs);

 

	            }
	            
	            
	            
	            
	        
	         //重命名一个文件,path为全路径
	         
	        public static void  rename(String oldPath,String newPath) throws IOException {


	                Configuration conf=new Configuration();
	                
	                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");

	                FileSystem hdfs=FileSystem.get(conf);
 
	                Path frpaht=new Path(oldPath);    //旧的文件名

	                Path topath=new Path(newPath);    //新的文件名
 
	                boolean isRename=hdfs.rename(frpaht, topath);
 
	                String result=isRename?"成功":"失败";

	                System.out.println("文件重命名结果为:"+result);

	               

	            }
  
	            
	            
	            
	        //上传一个文件到Hadoop中的path路径下, path为全路径
	        
	        public static void  upload(String filepath,String hdfsPath) throws IOException  {

	                Configuration conf=new Configuration();

	                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");
	                
	                FileSystem hdfs=FileSystem.get(conf);
	             

	                Path src =new Path(filepath);   //本地文件


	                Path dst =new Path(hdfsPath);      //HDFS为止

	               

	                hdfs.copyFromLocalFile(src, dst);

	                System.out.println("Upload to"+" "+conf.get("fs.default.name"));

	               

	                FileStatus files[]=hdfs.listStatus(dst);

	                for(FileStatus file:files){

	                    System.out.println(file.getPath());

	                }
	                
	                
	                System.out.println("文件上传成功!");

	            }
	        
	        
	        
	        
	        //查看一个文件的详细信息,path为文件的全路径
	        
	        public static void  showFile(String path) throws IOException  {

                Configuration conf=new Configuration();

                conf.set("fs.default.name", "hdfs://192.168.1.220:9000");
                
                FileSystem hdfs=FileSystem.get(conf);
                
                Path p = new Path(path);
	        
                FileStatus fileStatus = hdfs.getFileStatus(p);
                
                System.out.println("文件路径:"+fileStatus.getPath());
                System.out.println("块的大小:"+fileStatus.getBlockSize());
                System.out.println("文件所有者:"+fileStatus.getOwner()+":"+fileStatus.getGroup());
                System.out.println("文件权限:"+fileStatus.getPermission());
                System.out.println("文件长度:"+fileStatus.getLen());
                System.out.println("备份数:"+fileStatus.getReplication());
                System.out.println("修改时间:"+fileStatus.getModificationTime());
	        
	            
	        
	        }
	        
	        
	        
	        
	


本文出自 “你若盛开,清风自来” 博客,请务必保留此出处http://iqdutao.blog.51cto.com/2597934/1792333

HDFS API的java代码分析与实例

标签:hdfs api   java代码分析与实例   hadopapi   

原文地址:http://iqdutao.blog.51cto.com/2597934/1792333

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!