zoukankan      html  css  js  c++  java
  • 一脸懵逼学习hadoop之HDFS的java客户端编写

    1:eclipse创建一个项目,然后导入对应的jar包:

    鼠标右击项目,点击properties或者alt+enter快捷键--->java build path--->libraries--->add library--->user library--->next--->user libraries--->new--->hdfsLib(根据自己的需要填写)---》add external jars(添加自己的需求包):

    2:开始添加自己的需求包,路径如

      hadoop-2.4.1sharehadoophdfs的hadoop-hdfs-2.4.1.jar和hadoop-2.4.1sharehadoophdfslib下面的全部包;

      hadoop-2.4.1sharehadoopcommon的hadoop-common-2.4.1.jar和hadoop-2.4.1sharehadoopcommonlib下面的全部包;

      1 package com.master01;
      2 
      3 import java.io.FileInputStream;
      4 import java.io.IOException;
      5 import java.net.URISyntaxException;
      6 
      7 import org.apache.commons.io.IOUtils;
      8 import org.apache.hadoop.conf.Configuration;
      9 import org.apache.hadoop.fs.FSDataOutputStream;
     10 import org.apache.hadoop.fs.FileStatus;
     11 import org.apache.hadoop.fs.FileSystem;
     12 import org.apache.hadoop.fs.LocatedFileStatus;
     13 import org.apache.hadoop.fs.Path;
     14 import org.apache.hadoop.fs.RemoteIterator;
     15 
     16 public class HdfsTest {
     17 
     18     
     19     //public FileSystem fs = null;
     20     /*
     21     @Before
     22     public void init() throws IOException, InterruptedException, URISyntaxException{
     23         //读配置文件
     24         Configuration conf = new Configuration();
     25         //这里直接拷贝配置或者直接设置值
     26         conf.set("fs.defaultFS", "hdfs://master:9000/");
     27         
     28         //获取配置文件里面的内容
     29         fs = FileSystem.get(conf);
     30         //fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
     31     }
     32     */
     33     
     34     
     35     /**
     36      * 上传文件
     37      * @throws IOException 
     38      */
     39     public static void upload() throws IOException{
     40         //读配置文件
     41         //读取classpath下的core-site.xml配置文件,并且解析其的内容,封装到conf的对象中;
     42         Configuration conf = new Configuration();
     43         //这里直接拷贝配置或者直接设置值
     44         //也可以在代码中对conf的配置信息进行手动设置,会覆盖配置文件中的配置信息
     45         conf.set("fs.defaultFS", "hdfs://master:9000");
     46         
     47         //获取配置文件里面的内容
     48         //根据配置信息,去获取一个具体文件系统的客户端操作实例对象
     49         FileSystem fs = FileSystem.get(conf);
     50         //本地文件是输入流,hdfs是输出流
     51         
     52         //先搞出路径
     53         Path src = new Path("hdfs://master:9000/aa/test.txt");
     54         //搞出输出流,即向hdfs上面写内容
     55         FSDataOutputStream create = fs.create(src);
     56         
     57         //输入流就是读,本地文件,输入流
     58         FileInputStream fileInputStream = new FileInputStream("d:/test.txt");
     59         
     60         //将文件fileInputStream到create即完成上传到hdfs
     61         IOUtils.copy(fileInputStream, create);
     62     }
     63     
     64     
     65     //最快的上传文件的方法
     66     public void upload02() throws IllegalArgumentException, IOException, InterruptedException, URISyntaxException{
     67         //读配置文件
     68         Configuration conf = new Configuration();
     69         //这里直接拷贝配置或者直接设置值
     70         conf.set("fs.defaultFS", "hdfs://master:9000");
     71         
     72         //获取配置文件里面的内容
     73         FileSystem fs = FileSystem.get(conf);
     74         //FileSystem fs = FileSystem.get(new URI("hdfs://master:9000"), conf, "root");
     75         fs.copyFromLocalFile(new Path("d:/test.txt"), new Path("hdfs://master:9000/aa/test.txt"));
     76     }
     77     
     78     
     79     /**
     80      * 下载文件
     81      * @throws IOException 
     82      * @throws IllegalArgumentException 
     83      */
     84     public void download02() throws IllegalArgumentException, IOException{
     85         //去配置文件
     86         Configuration conf = new Configuration();
     87         conf.set("fs.defaultFS", "hdfs://master:9000");
     88         
     89         //获取配置文件里面的内容
     90         FileSystem fs = FileSystem.get(conf);
     91         fs.copyToLocalFile(new Path("hdfs://master:9000/aa/test.txt"), new Path("d:/test2.txt"));
     92         
     93     }
     94     
     95     /***
     96      * 创建文件夹的方法
     97      * @throws IOException 
     98      */
     99     public void mkdir02() throws IOException{
    100         //主配置文件
    101         Configuration conf = new Configuration();
    102         //设置配置文件的值
    103         conf.set("fs.defaultFS", "hdfs://master:9000");
    104         //获取配置文件里面的内容
    105         FileSystem fs = FileSystem.get(conf);
    106         
    107         //文件夹的创建
    108         fs.mkdirs(new Path("hdfs://master:9000/aaa/bbb/ccc"));
    109     }
    110     
    111     
    112     /**
    113      * 删除文件
    114      * @throws IOException 
    115      */
    116     public void remove02() throws IOException{
    117         //主配置文件
    118         Configuration conf = new Configuration();
    119         //设置值
    120         conf.set("fs.defaultFS", "hdfs://master:9000");
    121         //获取配置文件里面的内容
    122         FileSystem fs = FileSystem.get(conf);
    123         
    124         //执行删除操作
    125         fs.delete(new Path("hdfs://master:9000/aaa/bbb/ccc"), true);
    126     }
    127     
    128     /**
    129      * 文件的移动
    130      * @throws IOException 
    131      */
    132     public void move() throws IOException{
    133         //主配置文件
    134         Configuration conf = new Configuration();
    135         //设置值
    136         conf.set("fs.defaultFS", "hdfs://master:9000");
    137         //获取配置文件里面的内容
    138         FileSystem fs = FileSystem.get(conf);
    139         
    140         //移动操作
    141         fs.rename(new Path("hdfs://master:9000/aa/test.txt"), new Path("hdfs://master:9000/aaa/bbb"));
    142     }
    143     
    144     /***
    145      * 查看文件的信息
    146      * @throws IOException 
    147      */
    148     public void listFiles() throws IOException{
    149         //主配置文件
    150         Configuration conf = new Configuration();
    151         //设置值
    152         conf.set("fs.defaultFS", "hdfs://master:9000");
    153         //获取配置文件里面的内容
    154         FileSystem fs = FileSystem.get(conf);
    155                 
    156         //查看的是文件,不是文件夹
    157         //listFiles列出的是文件信息,而且提供递归遍历
    158         RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("hdfs://master:9000/"), true);
    159         //迭代输出信息
    160         while(listFiles.hasNext()){
    161             LocatedFileStatus file = listFiles.next();
    162             //文件路径
    163             Path path = file.getPath();
    164             System.out.println(path.getName());
    165         }
    166         
    167         System.out.println("=============================================");
    168         //listStatus列出文件和文件夹的信息,但是不提供自带的递归遍历
    169         FileStatus[] listStatus = fs.listStatus(new Path("hdfs://master:9000/"));
    170         /*for(int i = 0 ; i<listStatus.length; i++){
    171             System.out.println(listStatus[i]);
    172         }*/
    173         for(FileStatus fileStatus : listStatus){
    174             //根据获取的路径获取文件夹的名称
    175             Path path = fileStatus.getPath();
    176             System.out.println(path.getName());
    177         }
    178         
    179     }
    180     
    181     public static void main(String[] args) {
    182         HdfsTest hdfsTest = new HdfsTest();
    183         try {
    184             //上传文件的调用
    185             //hdfsTest.upload02();
    186             
    187             //下载文件的调用
    188             //hdfsTest.download02();
    189             
    190             //文件夹的创建
    191             //hdfsTest.mkdir02();
    192             
    193             //删除操作
    194             //hdfsTest.remove02();
    195             
    196             //移动文件的操作
    197             //hdfsTest.move();
    198             
    199             //查看文件信息
    200             hdfsTest.listFiles();
    201         } catch (Exception e) {
    202             e.printStackTrace();
    203         }
    204     }
    205 
    206 }

    3:NameNode的职责

    (1):维护元数据的信息;

    (2):维护hdfs的目录树;

    (3):响应客户端的请求;

  • 相关阅读:
    项目部署在windows下的tomcat里
    获取tomcat端口号的三种方式
    windows 下载安装github
    Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 189......
    HTTP协议头了解
    服务器nginx安装
    php源码安装
    php简化指令,以及php.ini配置
    配置php7 以支持swoole
    swoole install
  • 原文地址:https://www.cnblogs.com/biehongli/p/7504044.html
Copyright © 2011-2022 走看看