HDFS的文件操作

来源:互联网 发布:千里眼软件空间站 编辑:程序博客网 时间:2024/04/27 23:34

HDFS的文件操作
格式化HDFS
命令:user@namenode:hadoop$ bin/hadoop namenode -format 
启动HDFS
命令:user@namenode:hadoop$ bin/start-dfs.sh

列出HDFS上的文件

命令:user@namenode:hadoop$ bin/hadoop dfs -ls 

使用hadoop API

 

Java代码  收藏代码
  1. <span style="">public List<String[]> GetFileBolckHost(Configuration conf, String FileName) {  
  2.         try {  
  3.             List<String[]> list = new ArrayList<String[]>();  
  4.             FileSystem hdfs = FileSystem.get(conf);  
  5.             Path path = new Path(FileName);  
  6.             FileStatus fileStatus = hdfs.getFileStatus(path);  
  7.   
  8.             BlockLocation[] blkLocations = hdfs.getFileBlockLocations(  
  9.                     fileStatus, 0, fileStatus.getLen());  
  10.   
  11.             int blkCount = blkLocations.length;  
  12.             for (int i = 0; i < blkCount; i++) {  
  13.                 String[] hosts = blkLocations[i].getHosts();  
  14.                 list.add(hosts);  
  15.             }  
  16.             return list;  
  17.         } catch (IOException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.         return null;  
  21.     }</span>  

 

 

在HDFS上创建目录
命令:user@namenode:hadoop$ bin/hadoop dfs -mkdir /文件名

使用hadoop API

Java代码  收藏代码
  1. <span style="">// 在HDFS新建文件  
  2.     public FSDataOutputStream CreateFile(Configuration conf, String FileName) {  
  3.         try {  
  4.             FileSystem hdfs = FileSystem.get(conf);  
  5.             Path path = new Path(FileName);  
  6.             FSDataOutputStream outputStream = hdfs.create(path);  
  7.             return outputStream;  
  8.         } catch (IOException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.         return null;  
  12.     }</span>  

 
上传一个文件到HDFS
命令:user@namenode:hadoop$ bin/hadoop dfs -put 文件名 /user/yourUserName/

使用hadoop API

Java代码  收藏代码
  1. <span style="">// 上传文件到HDFS  
  2.     public void PutFile(Configuration conf, String srcFile, String dstFile) {  
  3.         try {  
  4.             FileSystem hdfs = FileSystem.get(conf);  
  5.             Path srcPath = new Path(srcFile);  
  6.             Path dstPath = new Path(dstFile);  
  7.             hdfs.copyFromLocalFile(srcPath, dstPath);  
  8.         } catch (IOException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.     }</span>  

 
从 HDFS 中导出数据

命令:user@namenode:hadoop$ bin/hadoop dfs -cat foo

使用hadoop API

Java代码  收藏代码
  1. <span style="">// 从HDFS读取文件  
  2.     public void ReadFile(Configuration conf, String FileName) {  
  3.         try {  
  4.             FileSystem hdfs = FileSystem.get(conf);  
  5.             FSDataInputStream dis = hdfs.open(new Path(FileName));  
  6.             IOUtils.copyBytes(dis, System.out, 4096false);  
  7.             dis.close();  
  8.         } catch (IOException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.     }</span>  

 
HDFS 的关闭 
命令:user@namenode:hadoop$ bin/stop-dfs.sh

 

HDFS全局状态信息

命令:bin/hadoop dfsadmin -report

我们可以得到一份全局状态报告。这份报告包含了HDFS集群的基本信息,当然也有每台机器的一些情况。 

    以上讲的都是本地操作HDFS,都是基于在ubuntu下并配置有hadoop环境下对HDFS的操作,作为客户端也可以在window系统下远程的对 HDFS进行操作,其实原理基本上差不多,只需要集群中namenode对外开放的IP和端口,就可以访问到HDFS

Java代码  收藏代码
  1. <span style="">/** 
  2.  * 对HDFS操作 
  3.  * @author yujing 
  4.  * 
  5.  */  
  6. public class Write {  
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             uploadTohdfs();  
  10.             readHdfs();  
  11.             getDirectoryFromHdfs();  
  12.         } catch (FileNotFoundException e) {  
  13.             e.printStackTrace();  
  14.         } catch (IOException e) {  
  15.             e.printStackTrace();  
  16.         }  
  17.     }  
  18.   
  19.     public static void uploadTohdfs() throws FileNotFoundException, IOException {  
  20.         String localSrc = "D://qq.txt";  
  21.         String dst = "hdfs://192.168.1.11:9000/usr/yujing/test.txt";  
  22.         InputStream in = new BufferedInputStream(new FileInputStream(localSrc));  
  23.         Configuration conf = new Configuration();  
  24.         FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  25.         OutputStream out = fs.create(new Path(dst), new Progressable() {  
  26.             public void progress() {  
  27.                 System.out.println(".");  
  28.             }  
  29.         });  
  30.         System.out.println("上传文件成功");  
  31.         IOUtils.copyBytes(in, out, 4096true);  
  32.     }  
  33.   
  34.     /** 从HDFS上读取文件 */  
  35.     private static void readHdfs() throws FileNotFoundException, IOException {  
  36.         String dst = "hdfs://192.168.1.11:9000/usr/yujing/test.txt";  
  37.         Configuration conf = new Configuration();  
  38.         FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  39.         FSDataInputStream hdfsInStream = fs.open(new Path(dst));  
  40.   
  41.         OutputStream out = new FileOutputStream("d:/qq-hdfs.txt");  
  42.         byte[] ioBuffer = new byte[1024];  
  43.         int readLen = hdfsInStream.read(ioBuffer);  
  44.   
  45.         while (-1 != readLen) {  
  46.             out.write(ioBuffer, 0, readLen);  
  47.             readLen = hdfsInStream.read(ioBuffer);  
  48.         }  
  49.         System.out.println("读文件成功");  
  50.         out.close();  
  51.         hdfsInStream.close();  
  52.         fs.close();  
  53.     }  
  54.   
  55.     /** 
  56.      * 以append方式将内容添加到HDFS上文件的末尾;注意:文件更新,需要在hdfs-site.xml中添<property><name>dfs. 
  57.      * append.support</name><value>true</value></property> 
  58.      */  
  59.     private static void appendToHdfs() throws FileNotFoundException,  
  60.             IOException {  
  61.         String dst = "hdfs://192.168.1.11:9000/usr/yujing/test.txt";  
  62.         Configuration conf = new Configuration();  
  63.         FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  64.         FSDataOutputStream out = fs.append(new Path(dst));  
  65.   
  66.         int readLen = "zhangzk add by hdfs java api".getBytes().length;  
  67.   
  68.         while (-1 != readLen) {  
  69.             out.write("zhangzk add by hdfs java api".getBytes(), 0, readLen);  
  70.         }  
  71.         out.close();  
  72.         fs.close();  
  73.     }  
  74.   
  75.     /** 从HDFS上删除文件 */  
  76.     private static void deleteFromHdfs() throws FileNotFoundException,  
  77.             IOException {  
  78.         String dst = "hdfs://192.168.1.11:9000/usr/yujing";  
  79.         Configuration conf = new Configuration();  
  80.         FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  81.         fs.deleteOnExit(new Path(dst));  
  82.         fs.close();  
  83.     }  
  84.   
  85.     /** 遍历HDFS上的文件和目录 */  
  86.     private static void getDirectoryFromHdfs() throws FileNotFoundException,  
  87.             IOException {  
  88.         String dst = "hdfs://192.168.1.11:9000/usr/yujing";  
  89.         Configuration conf = new Configuration();  
  90.         FileSystem fs = FileSystem.get(URI.create(dst), conf);  
  91.         FileStatus fileList[] = fs.listStatus(new Path(dst));  
  92.         int size = fileList.length;  
  93.         for (int i = 0; i < size; i++) {  
  94.             System.out.println("文件名name:" + fileList[i].getPath().getName()  
  95.                     + "文件大小/t/tsize:" + fileList[i].getLen());  
  96.         }  
  97.         fs.close();  
  98.     }  
  99.   
  100. }  
  101. </span>  
原创粉丝点击