zoukankan      html  css  js  c++  java
  • Hadoop HDFS编程 API入门系列之简单综合版本1(四)

      

      不多说,直接上代码。

     

    代码版本1

      1 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs4;
      2 
      3 import java.io.IOException;
      4 
      5 import java.net.URISyntaxException;
      6 import org.apache.hadoop.conf.Configuration;
      7 import org.apache.hadoop.fs.BlockLocation;
      8 import org.apache.hadoop.fs.FileStatus;
      9 import org.apache.hadoop.fs.FileSystem;
     10 import org.apache.hadoop.fs.FileUtil;
     11 import org.apache.hadoop.fs.Path;
     12 import org.apache.hadoop.hdfs.DistributedFileSystem;
     13 import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
     14 
     15 import java.net.URI;
     16 
     17 public class HDFStest1
     18 {    
     19 
     20     /**
     21      * @param args
     22      * @throws IOException 
     23      * @throws URISyntaxException 
     24      */
     25     public static void main(String[] args) throws IOException, URISyntaxException
     26     {
     27         // TODO Auto-generated method stub
     28         //mkdir();
     29         //copyToHDFS();
     30         //getFile();
     31         //ListAllFile();
     32         //getFileLocal();
     33         //rmdir();
     34         getHDFSNodes();
     35     }    
     36     //获取HDFS文件系统
     37     public static FileSystem getFileSystem() throws IOException,URISyntaxException
     38     {
     39         Configuration conf = new Configuration();//读取配置文件,比如core-site.xml
     40         //FileSystem fs =FileSystem.get(conf);
     41         
     42         URI uri = new URI("hdfs://HadoopMaster:9000");
     43         
     44         FileSystem fs = FileSystem.get(uri,conf); 
     45         return fs;
     46     }
     47 
     48     public static void mkdir() throws IOException,URISyntaxException
     49     {
     50         //第一步,获取文件系统
     51         FileSystem fs =getFileSystem();
     52         //第二步,创建文件目录
     53         fs.mkdirs(new Path("/zhouls/data"));
     54         //第三步,释放资源
     55         fs.close();
     56     }
     57     
     58     public static void copyToHDFS() throws IOException,URISyntaxException
     59     {
     60         //第一步
     61         FileSystem fs=getFileSystem();
     62         //第二步
     63         Path srcpath=new Path("D://Data/weibo.txt");
     64         Path dstpath=new Path("/zhouls/data");
     65         //第三步
     66         fs.copyFromLocalFile(srcpath, dstpath);
     67         //第四步
     68         fs.close();
     69     }
     70     
     71     public static void getFile() throws IOException, URISyntaxException
     72     {
     73         //第一步
     74         FileSystem fs=getFileSystem();
     75         //第二步
     76         Path srcpath=new Path("/zhouls/data/weibo.txt");
     77         Path dstpath=new Path("D://Data/test");
     78         //第三步
     79         fs.copyToLocalFile(srcpath, dstpath);    
     80         //第四步
     81         fs.close();
     82 
     83     }
     84     
     85     public static void ListAllFile() throws IOException, URISyntaxException
     86     {
     87         //第一步
     88         FileSystem fs=getFileSystem();
     89         //第二步
     90         FileStatus[] status =fs.listStatus(new Path("/zhouls"));
     91         //第三步
     92         Path[] listedPaths = FileUtil.stat2Paths(status);
     93         //第四步
     94         for(Path p:listedPaths)
     95         { 
     96             System.out.println(p);
     97             
     98         }
     99         //第五步
    100         fs.close();
    101     }
    102     
    103     public static void getFileLocal() throws IOException, URISyntaxException
    104     {
    105         //第一步
    106         FileSystem fs=getFileSystem();
    107         //第二步
    108         Path path=new Path("/zhouls/data/weibo.txt");
    109         //第三步
    110         FileStatus fileStatus=fs.getFileLinkStatus(path);
    111         //第四步
    112         BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
    113         //第五步
    114          for(int i=0;i< blkLocations.length;i++)
    115          {
    116               String[] hosts = blkLocations[i].getHosts();
    117               System.out.println("block_"+i+"_location:"+hosts[0]);
    118          }
    119          //第六步
    120          fs.close();
    121     }
    122     
    123     public static void rmdir() throws IOException, URISyntaxException
    124     {
    125         //第一步
    126         FileSystem fs=getFileSystem();
    127         //第二步
    128         fs.delete(new Path("/zhouls/data"),true);
    129         //第三步
    130         fs.close();
    131     }
    132     
    133     public static void getHDFSNodes() throws IOException, URISyntaxException
    134     {
    135         //第一步
    136         FileSystem fs=getFileSystem();
    137         //第二步
    138         DistributedFileSystem hdfs = (DistributedFileSystem)fs;
    139         //第三步
    140         DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
    141         //第四步
    142         for(int i=0;i< dataNodeStats.length;i++)
    143         {
    144             System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName());
    145         }
    146         //第五步
    147         fs.close();
    148     }
    149     
    150 }

    代码版本2

      1 package com.dajiangtai.Hadoop.HDFS;
      2 
      3 import java.io.IOException;
      4 import java.net.URISyntaxException;
      5 import org.apache.hadoop.conf.Configuration;
      6 import org.apache.hadoop.fs.BlockLocation;
      7 import org.apache.hadoop.fs.FileStatus;
      8 import org.apache.hadoop.fs.FileSystem;
      9 import org.apache.hadoop.fs.FileUtil;
     10 import org.apache.hadoop.fs.Path;
     11 import org.apache.hadoop.hdfs.DistributedFileSystem;
     12 import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
     13 
     14 import java.net.URI;
     15 
     16 public class hdfsTest{
     17     /**
     18      * @param args   //@param args是生成文档的时候用的东西,现在不用管。以后慢慢就知道了
     19      * @throws IOException 
     20      * @throws URISyntaxException 
     21      */
     22     
     23     
     24     public static void main(String[] args) throws IOException, URISyntaxException{
     25                     // TODO Auto-generated method stub     
     26                     //这是在你用eclipse这样的Java集成开发环境是开发环境自动帮你写的,
     27                     //意思是告诉你这些代码是自动生成的,不是你自己写的,就是一个提示的作用,没大作用
     28         
     29         
     30 //        mkdir();
     31 //        copyToHDFS();
     32 //        getFile();
     33 //        ListAllFile();
     34 //        getFileLocal();
     35 //        rmdir();
     36         getHDFSNodes();
     37     }    
     38     
     39     
     40     //获取HDFS文件系统
     41     public static FileSystem getFileSystem() throws IOException,URISyntaxException{
     42                     //getFileSystem()是获取文件系统
     43         Configuration conf = new Configuration();//读取配置文件,比如core-site.xml、hdfs-site.xml等等。
     44         
     45         
     46         //若是在集群里,则如下一行代码即可。
     47         //FileSystem fs =FileSystem.get(conf);
     48         
     49         
     50         //若是在本地里来想去获取,则如下两行代码即可。
     51         URI uri = new URI("hdfs://djt002:9000");  
     52         FileSystem fs = FileSystem.get(uri,conf); 
     53                         
     54         
     55         
     56         return fs;
     57     }
     58     
     59     
     60     
     61     //创建文件目录
     62     public static void mkdir() throws IOException,URISyntaxException{
     63         //第一步,获取文件系统
     64         FileSystem fs =getFileSystem();//因为上述有个方法
     65         //第二步,创建文件目录
     66         fs.mkdirs(new Path("/dajiangtai/data"));
     67         //第三步,释放资源
     68         fs.close();
     69     }
     70     
     71     
     72     
     73     //文件上传至HDFS
     74     public static void copyToHDFS() throws IOException,URISyntaxException{
     75         //第一步
     76         FileSystem fs=getFileSystem();//因为上述有个方法
     77         //第二步
     78         Path srcpath=new Path("D://Data/weibo.txt");//原路径
     79         Path dstpath=new Path("/dajiangtai/data");//终路径
     80         //第三步
     81         fs.copyFromLocalFile(srcpath, dstpath);
     82         //第四步
     83         fs.close();
     84     }
     85     
     86     
     87     //获取目录下的所有文件
     88     public static void getFile() throws IOException, URISyntaxException{
     89         //第一步
     90         FileSystem fs=getFileSystem();//因为上述有个方法
     91         //第二步
     92         Path srcpath=new Path("/dajiangtai/data/weibo.txt");//原路径
     93         Path dstpath=new Path("D://Data/test");//终路径
     94         //第三步
     95         fs.copyToLocalFile(srcpath, dstpath);    
     96         //第四步
     97         fs.close();
     98 
     99     }
    100     
    101     
    102     //列出指定路径下的所有文件
    103     public static void ListAllFile() throws IOException, URISyntaxException{
    104         //第一步
    105         FileSystem fs=getFileSystem();//因为上述有个方法
    106         //第二步
    107         FileStatus[] status =fs.listStatus(new Path("/dajiangtai"));
    108             // 任何文件系统的一个重要特性都是提供其目录结构浏览和检索它所存文件和目录相关信息的功能。
    109             //FileStatus对象,即status,它封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。
    110             //FileStatus对象,即status,由FileSystem的listStatus()方法获得,调用该方法的时候要把文件的Path传进去。
    111         //第三步
    112         Path[] listedPaths = FileUtil.stat2Paths(status);
    113         //第四步
    114         for(Path p:listedPaths)
    115         { 
    116             System.out.println(p);
    117             
    118         }
    119         //第五步
    120         fs.close();
    121     }
    122     
    123     
    124     //查找某个文件在HDFS集群的位置
    125     public static void getFileLocal() throws IOException, URISyntaxException{
    126         //第一步
    127         FileSystem fs=getFileSystem();//因为上述有个方法
    128         //第二步
    129         Path path=new Path("/dajiangtai/data/weibo.txt");
    130         //第三步
    131         FileStatus fileStatus=fs.getFileLinkStatus(path);
    132     
    133             // 任何文件系统的一个重要特性都是提供其目录结构浏览和检索它所存文件和目录相关信息的功能。
    134             //FileStatus对象,即fileStatus,它封装了文件系统中文件和目录的元数据,包括文件的长度、块大小、备份数、修改时间、所有者以及权限等信息。
    135             //FileStatus对象,即fileStatus,由FileSystem的getFileStatus()方法获得,调用该方法的时候要把文件的Path传进去。
    136         //第四步
    137         BlockLocation[] blkLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
    138             // 获取到整个文件的所有block的位置信息,比如示例BlockLocation[] blkLocations = fs.getFileBlockLocations(file, 0,length);  
    139         //第五步
    140          for(int i=0;i< blkLocations.length;i++)
    141          {
    142               String[] hosts = blkLocations[i].getHosts();
    143               System.out.println("block_"+i+"_location:"+hosts[0]);//这里为什么是hosts[0],因为单节点。
    144          }
    145          //第六步
    146          fs.close();
    147     }
    148     
    149     
    150     //删除文件或文件的目录
    151     public static void rmdir() throws IOException, URISyntaxException{
    152         //第一步
    153         FileSystem fs=getFileSystem();//因为上述有个方法
    154         //第二步
    155         fs.delete(new Path("/dajiangtai/data"),true);
    156         //第三步
    157         fs.close();
    158     }
    159     
    160     
    161     //获取HDFS集群节点信息
    162     public static void getHDFSNodes() throws IOException, URISyntaxException{
    163         //第一步
    164         FileSystem fs=getFileSystem();//因为上述有个方法
    165         //第二步
    166         DistributedFileSystem hdfs = (DistributedFileSystem)fs;
    167         //第三步
    168         DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
    169         //第四步
    170         for(int i=0;i< dataNodeStats.length;i++)
    171         {
    172             System.out.println("DataNode_"+i+"_Name:"+dataNodeStats[i].getHostName());
    173         }
    174         //第五步
    175         fs.close();
    176     }
    177     
    178 }
  • 相关阅读:
    一起谈.NET技术,SharePoint开发笔记SharePoint2010添加ASP.NET应用程序 狼人:
    一起谈.NET技术,系统架构技能之设计模式—代理模式 狼人:
    一起谈.NET技术,.NET中的异步编程(一)为什么需要异步 狼人:
    一起谈.NET技术,Microsoft NLayerApp案例理论与实践 项目简介与环境搭建 狼人:
    一起谈.NET技术,构建高性能ASP.NET站点之减少不必要的请求 狼人:
    一起谈.NET技术,分享在MVC3.0中使用jQuery DataTable 插件 狼人:
    一起谈.NET技术,ASP.NET 4的Demo实践:URL路由改进支持 狼人:
    一起谈.NET技术,构建高性能ASP.NET站点之优化HTTP请求 狼人:
    一起谈.NET技术,Silverlight 游戏开发小技巧:传说中的透视跑马灯 狼人:
    paip.Winista HTMLParser文本结点的获取
  • 原文地址:https://www.cnblogs.com/zlslch/p/6175593.html
Copyright © 2011-2022 走看看