Hadoop源码阅读

9 篇文章 0 订阅

1、创建HDFS目录

[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileSystem;  
  4. import org.apache.hadoop.fs.Path;  
  5.   
  6. public class MakeDir {  
  7.     public static void main(String[] args) throws IOException {  
  8.         Configuration conf = new Configuration();  
  9.         FileSystem fs = FileSystem.get(conf);  
  10.         Path path = new Path("/user/hadoop/data/20130709");  
  11.         fs.create(path);  
  12.         fs.close();  
  13.     }  
  14. }  

2、删除HDFS目录

[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileSystem;  
  4. import org.apache.hadoop.fs.Path;  
  5.   
  6. public class DeleteDir {  
  7.     public static void main(String[] args) throws IOException {  
  8.         Configuration conf = new Configuration();  
  9.         FileSystem fs = FileSystem.get(conf);  
  10.           
  11.         Path path = new Path("/user/hadoop/data/20130710");  
  12.         fs.delete(path);  
  13.         fs.close();  
  14.     }  
  15. }  

3、HDFS写文件

[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FSDataOutputStream;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6.   
  7. public class WriteFile {  
  8.     public static void main(String[] args) throws IOException {  
  9.         Configuration conf = new Configuration();  
  10.         FileSystem fs = FileSystem.get(conf);  
  11.         Path path = new Path("/user/hadoop/data/write.txt");  
  12.         FSDataOutputStream out = fs.create(path);  
  13.         out.writeUTF("da jia hao,cai shi zhen de hao!");  
  14.         fs.close();  
  15.     }  
  16. }  
4、HDFS读文件

[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FSDataInputStream;  
  4. import org.apache.hadoop.fs.FileStatus;  
  5. import org.apache.hadoop.fs.FileSystem;  
  6. import org.apache.hadoop.fs.Path;  
  7.   
  8. public class ReadFile {  
  9.     public static void main(String[] args) throws IOException {  
  10.         Configuration conf = new Configuration();  
  11.         FileSystem fs = FileSystem.get(conf);  
  12.         Path path = new Path("/user/hadoop/data/write.txt");  
  13.           
  14.         if(fs.exists(path)){  
  15.             FSDataInputStream is = fs.open(path);  
  16.             FileStatus status = fs.getFileStatus(path);  
  17.             byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];  
  18.             is.readFully(0, buffer);  
  19.             is.close();  
  20.             fs.close();  
  21.             System.out.println(buffer.toString());  
  22.         }  
  23.     }  
  24. }  

5、 上传本地文件到HDFS
[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileSystem;  
  4. import org.apache.hadoop.fs.Path;  
  5.   
  6. public class CopyFromLocalFile {  
  7.   
  8.     public static void main(String[] args) throws IOException {  
  9.           
  10.         Configuration conf = new Configuration();  
  11.         FileSystem fs = FileSystem.get(conf);  
  12.         Path src = new Path("/home/hadoop/word.txt");  
  13.         Path dst = new Path("/user/hadoop/data/");  
  14.         fs.copyFromLocalFile(src, dst);  
  15.         fs.close();  
  16.     }  
  17. }  

6、 删除文件

[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileSystem;  
  4. import org.apache.hadoop.fs.Path;  
  5.   
  6. public class DeleteFile {  
  7.   
  8.     public static void main(String[] args) throws IOException {  
  9.         Configuration conf = new Configuration();  
  10.         FileSystem fs = FileSystem.get(conf);  
  11.           
  12.         Path path = new Path("/user/hadoop/data/word.txt");  
  13.         fs.delete(path);  
  14.         fs.close();  
  15.     }  
  16. }  
7、 获取给定目录下的所有子目录以及子文件

[java]  view plain copy
  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.FileStatus;  
  4. import org.apache.hadoop.fs.FileSystem;  
  5. import org.apache.hadoop.fs.Path;  
  6.   
  7. public class GetAllChildFile {  
  8.     static Configuration conf = new Configuration();  
  9.       
  10.       
  11.     public static void main(String[] args)throws IOException {  
  12.         FileSystem fs = FileSystem.get(conf);  
  13.         Path path = new Path("/user/hadoop");  
  14.         getFile(path,fs);  
  15.         //fs.close();  
  16.     }  
  17.       
  18.     public static void getFile(Path path,FileSystem fs) throws IOException {  
  19.           
  20.         FileStatus[] fileStatus = fs.listStatus(path);  
  21.         for(int i=0;i<fileStatus.length;i++){  
  22.             if(fileStatus[i].isDir()){  
  23.                 Path p = new Path(fileStatus[i].getPath().toString());  
  24.                 getFile(p,fs);  
  25.             }else{  
  26.                 System.out.println(fileStatus[i].getPath().toString());  
  27.             }  
  28.         }  
  29.     }  
  30.   
  31. }  
8、 查找某个文件在HDFS集群的位置

[java]  view plain copy
  1. package com.hadoop.file;  
  2.   
  3. import java.io.IOException;  
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.BlockLocation;  
  6. import org.apache.hadoop.fs.FileStatus;  
  7. import org.apache.hadoop.fs.FileSystem;  
  8. import org.apache.hadoop.fs.Path;  
  9. import org.apache.hadoop.hdfs.DistributedFileSystem;  
  10. import org.apache.hadoop.hdfs.protocol.DatanodeInfo;  
  11.   
  12. public class FindFile {  
  13.       
  14.     public static void main(String[] args) throws IOException {   
  15.         getHDFSNode();  
  16.     }  
  17.       
  18.     /** 
  19.      * HDFS集群上所有节点名称信息 
  20.      * @Title:   
  21.      * @Description:  
  22.      * @param  
  23.      * @return  
  24.      * @throws 
  25.      */  
  26.     public static void getHDFSNode() throws IOException{  
  27.         Configuration conf = new Configuration();  
  28.         FileSystem fs = FileSystem.get(conf);  
  29.   
  30.         DistributedFileSystem  dfs = (DistributedFileSystem)fs;  
  31.         DatanodeInfo[] dataNodeStats = dfs.getDataNodeStats();  
  32.           
  33.         for(int i=0;i<dataNodeStats.length;i++){  
  34.             System.out.println("DataNode_" + i + "_Node:" + dataNodeStats[i].getHostName());  
  35.         }  
  36.           
  37.     }  
  38. }  
9、 HDFS集群上所有节点名称信息

[java]  view plain copy
  1. package com.hadoop.file;  
  2. import java.io.IOException;  
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.BlockLocation;  
  5. import org.apache.hadoop.fs.FileStatus;  
  6. import org.apache.hadoop.fs.FileSystem;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.hdfs.DistributedFileSystem;  
  9. import org.apache.hadoop.hdfs.protocol.DatanodeInfo;  
  10.   
  11. public class FindFile {  
  12.       
  13.     public static void main(String[] args) throws IOException {   
  14.         getHDFSNode();  
  15.     }  
  16.       
  17.     /** 
  18.      * HDFS集群上所有节点名称信息 
  19.      * @Title:   
  20.      * @Description:  
  21.      * @param  
  22.      * @return  
  23.      * @throws 
  24.      */  
  25.     public static void getHDFSNode() throws IOException{  
  26.         Configuration conf = new Configuration();  
  27.         FileSystem fs = FileSystem.get(conf);  
  28.   
  29.         DistributedFileSystem  dfs = (DistributedFileSystem)fs;  
  30.         DatanodeInfo[] dataNodeStats = dfs.getDataNodeStats();  
  31.           
  32.         for(int i=0;i<dataNodeStats.length;i++){  
  33.             System.out.println("DataNode_" + i + "_Node:" + dataNodeStats[i].getHostName());  
  34.         }         
  35.     }         
  36. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值