zoukankan      html  css  js  c++  java
  • Hadoop HDFS编程 API入门系列之路径过滤上传多个文件到HDFS(二)

      不多说,直接上代码。

     

    代码版本1

      1 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs6;
      2 
      3 import java.io.IOException;
      4 import java.net.URI;
      5 import java.net.URISyntaxException;
      6 
      7 import org.apache.hadoop.conf.Configuration;
      8 import org.apache.hadoop.fs.FSDataInputStream;
      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.FileUtil;
     13 import org.apache.hadoop.fs.Path;
     14 import org.apache.hadoop.fs.PathFilter;
     15 /**
     16 * @function 将指定格式的多个文件上传至 HDFS
     17 *
     18 *
     19 */
     20 public class CopyManyFilesToHDFS {
     21 
     22 private static FileSystem fs = null;
     23 private static FileSystem local = null;
     24 
     25 /**
     26 * @function Main 方法
     27 * @param args
     28 * @throws IOException
     29 * @throws URISyntaxException
     30 */
     31 public static void main(String[] args) throws IOException,URISyntaxException
     32 {
     33 //文件源路径 这是在 Windows 下测试运行,如果在 Linux 修改srcPath路径即可
     34 String srcPath = "/home/hadoop/data/*";
     35 //String srcPath = "D://Data/testdata/*";
     36 //或者Path srcPath =new Path("D://Data/testdata/*");
     37 
     38 
     39 //文件目的路径 如果在 Hadoop 环境下运行,使用 dstPath 的相对路径"/copyManyFilesToHDFS/"也可以
     40 String dstPath = "hdfs://HadoopMaster:9000/copyManyFilesToHDFS/";
     41 //或者Path dstPath = new Path("hdfs://HadoopMaster:9000/copyManyFilesToHDFS/");
     42 //调用文件上传 list 方法
     43 list(srcPath,dstPath);
     44 }
     45 
     46 /**
     47 * function 过滤文件格式 将多个文件上传至 HDFS
     48 * @param dstPath 目的路径
     49 * @throws IOException
     50 * @throws URISyntaxException
     51 */
     52 //2.接下来在 list 方法中,使用 globStatus 方法获取所有 txt 文件,然后通过 copyFromLocalFile 方法将文件上传至 HDFS。
     53 public static void list(String srcPath,String dstPath) throws IOException, URISyntaxException {
     54 //读取hadoop配置文件
     55 Configuration conf = new Configuration();
     56 
     57 //获取默认文件系统 在Hadoop 环境下运行,也可以使用此种方法获取文件系统
     58 fs = FileSystem.get(conf);
     59 
     60 //HDFS接口和获取文件系统对象,本地环境运行模式
     61 //URI uri = new URI("hdfs://HadoopMaster:9000");
     62 //fs = FileSystem.get(uri, conf);
     63 //获得本地文件系统
     64 local = FileSystem.getLocal(conf);
     65 //只上传Data/testdata 目录下 txt 格式的文件 ,获得文件目录,即D://Data/testdata/
     66 //FileStatus[] localStatus = local.globStatus(new Path("D://Data/testdata/*"),new RegexAcceptPathFilter("^.*txt$"));
     67 FileStatus[] localStatus = local.globStatus(new Path("/home/hadoop/data/*"),new RegexAcceptPathFilter("^.*txt$"));
     68 // 获得所有文件路径
     69 Path[] listedPaths = FileUtil.stat2Paths(localStatus);
     70 Path out= new Path(dstPath);
     71 //循坏所有文件
     72 for(Path p:listedPaths)
     73 {
     74 //将本地文件上传到HDFS
     75 fs.copyFromLocalFile(p, out);
     76 }
     77 }
     78 
     79 /**
     80 * @function 只接受 txt 格式的文件
     81 * @author 
     82 *
     83 */
     84 // 1.首先定义一个类 RegexAcceptPathFilter实现 PathFilter,过滤掉 txt 文本格式以外的文件。
     85 public static class RegexAcceptPathFilter implements PathFilter 
     86 {
     87 private final String regex;
     88 
     89 public RegexAcceptPathFilter(String regex)
     90 {
     91 this.regex = regex;
     92 }
     93 // 如果要接收 regex 格式的文件,则accept()方法就return flag; 如果想要过滤掉regex格式的文件,则accept()方法就return !flag。
     94 
     95 public boolean accept(Path path) 
     96 {
     97 // TODO Auto-generated method stub
     98 boolean flag = path.toString().matches(regex);
     99 //只接受 regex 格式的文件
    100 return flag;
    101 }
    102 }
    103 }

    在Hadoop集群里测试的代码版本

     1 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs6;
     2 
     3 import java.io.IOException;
     4 import java.net.URI;
     5 import java.net.URISyntaxException;
     6 
     7 import org.apache.hadoop.conf.Configuration;
     8 import org.apache.hadoop.fs.FSDataInputStream;
     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.FileUtil;
    13 import org.apache.hadoop.fs.Path;
    14 import org.apache.hadoop.fs.PathFilter;
    15 /**
    16 * @function 将指定格式的多个文件上传至HDFS,在Hadoop集群里测试
    17 * 
    18 *
    19 */
    20 public class CopyManyFilesToHDFS 
    21 {
    22 
    23 private static FileSystem fs = null;//定义文件系统对象,是HDFS上的
    24 private static FileSystem local = null; //定义文件系统对象,是本地上的
    25 
    26 /**
    27 * @function Main 方法
    28 * @param args    //@param args是生成文档的时候用的东西,现在不用管。以后慢慢就知道了
    29 * @throws IOException
    30 * @throws URISyntaxException
    31 */
    32 public static void main(String[] args) throws IOException,URISyntaxException
    33 {
    34 //文件的原路径,这是在Windows下测试运行,如果在 Linux 修改srcPath路径即可
    35 String srcPath = "/home/hadoop/djt/data/*";
    36 //String srcPath = "D://Data/testdata/*";
    37 //或者Path srcPath =new Path("D://Data/testdata/*");
    38 
    39 
    40 //文件目的路径 如果在 Hadoop 环境下运行,使用 dstPath 的相对路径"/middle/filter/"也可以
    41 String dstPath = "hdfs://HadoopMaster:9000/middle/filter/";
    42 //或者Path dstPath = new Path("hdfs://HadoopMaster:9000/middle/filter/");
    43 //调用文件上传 list 方法
    44 list(srcPath,dstPath);
    45 }
    46 
    47 /**
    48 * function 过滤文件格式 将多个文件上传至 HDFS
    49 * @param dstPath 目的路径
    50 * @throws IOException
    51 * @throws URISyntaxException
    52 */
    53 //2.接下来在 list 方法中,使用 globStatus 方法获取所有 txt 文件,然后通过 copyFromLocalFile 方法将文件上传至 HDFS。
    54 public static void list(String srcPath,String dstPath) throws IOException, URISyntaxException 
    55 {
    56 Configuration conf = new Configuration();//读取hadoop配置文件
    57 fs = FileSystem.get(conf);//获取默认文件系统对象,fs。 在Hadoop 环境下运行,也可以使用此种方法获取文件系统
    58 //URI uri = new URI("hdfs://HadoopMaster:9000");//HDFS接口和获取文件系统对象,本地环境运行模式
    59 //fs = FileSystem.get(uri, conf);
    60 local = FileSystem.getLocal(conf);//获得本地文件系统对象,local
    61 //只上传Data/testdata 目录下 txt 格式的文件 ,获得文件目录,即D://Data/testdata/
    62 //FileStatus[] localStatus = local.globStatus(new Path("D://Data/testdata/*"),new RegexAcceptPathFilter("^.*txt$"));
    63 FileStatus[] localStatus = local.globStatus(new Path("/home/hadoop/djt/data/*"),new RegexAcceptPathFilter("^.*txt$"));//接收目录下的 txt 文件
    64 // 获得所有文件路径
    65 Path[] listedPaths = FileUtil.stat2Paths(localStatus);
    66 Path out= new Path(dstPath);
    67 //循坏所有文件
    68 for(Path p:listedPaths)
    69 {
    70 //将本地文件上传到HDFS
    71 fs.copyFromLocalFile(p, out);
    72 }
    73 }
    74 
    75 /**
    76 * @function 只接受 txt 格式的文件
    77 * @author 
    78 *
    79 */
    80 // 1.首先定义一个类 RegexAcceptPathFilter实现 PathFilter,过滤掉 txt 文本格式以外的文件。
    81 public static class RegexAcceptPathFilter implements PathFilter 
    82 {
    83 private final String regex;
    84 
    85 public RegexAcceptPathFilter(String regex)
    86 {
    87 this.regex = regex;
    88 }
    89 // 如果要接收 regex 格式的文件,则accept()方法就return flag; 如果想要过滤掉regex格式的文件,则accept()方法就return !flag。
    90 @Override
    91 public boolean accept(Path path) 
    92 {
    93 // TODO Auto-generated method stub
    94 boolean flag = path.toString().matches(regex);
    95 //只接受 regex 格式的文件
    96 return flag;
    97 }
    98 }
    99 }

    在Eclipse/MyEclipse集群里测试的代码版本

      1 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs6;
      2 
      3 import java.io.IOException;
      4 import java.net.URI;
      5 import java.net.URISyntaxException;
      6 
      7 import org.apache.hadoop.conf.Configuration;
      8 import org.apache.hadoop.fs.FSDataInputStream;
      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.FileUtil;
     13 import org.apache.hadoop.fs.Path;
     14 import org.apache.hadoop.fs.PathFilter;
     15 /**
     16 * @function 将指定格式的多个文件上传至 HDFS,在MyEclipse里测试
     17 * @author 小讲
     18 *
     19 */
     20 public class CopyManyFilesToHDFS {
     21 
     22 
     23 private static FileSystem fs = null;//定义文件系统对象,是HDFS上的
     24 private static FileSystem local = null;//定义文件系统对象,是本地上的
     25 
     26 /**
     27 * @function Main 方法
     28 * @param args
     29 * @throws IOException
     30 * @throws URISyntaxException
     31 */
     32 public static void main(String[] args) throws IOException,URISyntaxException
     33 {
     34 //文件源路径 这是在 Windows 下测试运行,如果在 Linux 修改srcPath路径即可
     35 String srcPath = "D://data/testdata/*";
     36 //或者Path srcPath =new Path("D://Data/testdata/*");
     37 
     38 //文件目的路径 如果在 Hadoop 环境下运行,使用 dstPath 的相对路径"/middle/filter/"也可以
     39 String dstPath = "hdfs://HadoopMaster:9000/middle/filter/";
     40 //或者Path dstPath = new Path("hdfs://HadoopMaster:9000/middle/filter/");
     41 //调用文件上传 list 方法
     42 list(srcPath,dstPath);
     43 }
     44 
     45 /**
     46 * function 过滤文件格式 将多个文件上传至 HDFS
     47 * @param dstPath 目的路径
     48 * @throws IOException
     49 * @throws URISyntaxException
     50 */
     51 //2.接下来在 list 方法中,使用 globStatus 方法获取所有 txt 文件,然后通过 copyFromLocalFile 方法将文件上传至 HDFS。
     52 public static void list(String srcPath,String dstPath) throws IOException, URISyntaxException
     53 {
     54 //读取hadoop配置文件
     55 Configuration conf = new Configuration();
     56 
     57 //获取默认文件系统 在Hadoop 环境下运行,也可以使用此种方法获取文件系统
     58 //fs = FileSystem.get(conf);
     59 
     60 //HDFS接口和获取文件系统对象,本地环境运行模式
     61 URI uri = new URI("hdfs://HadoopMaster:9000");
     62 fs = FileSystem.get(uri, conf);
     63 
     64 local = FileSystem.getLocal(conf);//获得本地文件系统
     65 //只上传Data/testdata 目录下 txt 格式的文件 ,获得文件目录,即D://Data/testdata/
     66 FileStatus[] localStatus = local.globStatus(new Path("D://Data/testdata/*"),new RegexAcceptPathFilter("^.*txt$"));
     67 // 获得所有文件路径
     68 Path[] listedPaths = FileUtil.stat2Paths(localStatus);
     69 Path out= new Path(dstPath);
     70 //循坏所有文件
     71 for(Path p:listedPaths)
     72 {
     73 //将本地文件上传到HDFS
     74 fs.copyFromLocalFile(p, out);
     75 }
     76 }
     77 
     78 /**
     79 * @function 只接受 txt 格式的文件
     80 * @author 
     81 *
     82 */
     83 // 1.首先定义一个类 RegexAcceptPathFilter实现 PathFilter,过滤掉 txt 文本格式以外的文件。
     84 public static class RegexAcceptPathFilter implements PathFilter 
     85 {
     86 private final String regex;
     87 
     88 public RegexAcceptPathFilter(String regex)
     89 {
     90 this.regex = regex;
     91 }
     92 // 如果要接收 regex 格式的文件,则accept()方法就return flag; 如果想要过滤掉regex格式的文件,则accept()方法就return !flag。
     93 @Override
     94 public boolean accept(Path path) 
     95 {
     96 // TODO Auto-generated method stub
     97 boolean flag = path.toString().matches(regex);
     98 //只接受 regex 格式的文件
     99 return flag;
    100 }
    101 }
    102 }

    代码版本2

      1 package com.dajiangtai.Hadoop.HDFS;
      2 
      3 import java.io.IOException;
      4 import java.net.URI;
      5 import java.net.URISyntaxException;
      6 
      7 import org.apache.hadoop.conf.Configuration;
      8 import org.apache.hadoop.fs.FSDataInputStream;
      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.FileUtil;
     13 import org.apache.hadoop.fs.Path;
     14 import org.apache.hadoop.fs.PathFilter;
     15 /**
     16  * @function 将指定格式的多个文件上传至 HDFS
     17  * 使用文件模式,实现多文件上传至HDFS
     18  * @author 小讲
     19  *
     20  */
     21 @SuppressWarnings("unused")
     22 public class CopyManyFilesToHDFS {
     23     
     24     private static FileSystem fs = null;//FileSystem实例对象,即fs
     25     private static FileSystem local = null;//FileSystem实例对象,即Local,本地文件系统
     26 
     27     /**
     28      * @function Main 方法
     29      * @param args
     30      * @throws IOException
     31      * @throws URISyntaxException
     32      */
     33     public static void main(String[] args) throws IOException,URISyntaxException {
     34         //文件上传路径
     35 //        Path dstPath = new Path("hdfs://djt002:9000/outData/copyManyFilesToHDFS/");//这样会在这个默认的copyManyFilesToHDFS.txt里
     36         Path dstPath = new Path("hdfs://djt002:9000/outCopyManyFilesToHDFS/");//要么,你先可以新建好outCopyManyFilesToHDFS这个目录
     37 
     38         
     39         //调用文件上传 list 方法
     40         list(dstPath);
     41     }
     42 
     43     /**
     44      * function 过滤文件格式   将多个文件上传至 HDFS
     45      * @param dstPath 目的路径
     46      * @throws IOException
     47      * @throws URISyntaxException
     48      */
     49     public static void list(Path dstPath) throws IOException, URISyntaxException {
     50         //读取hadoop文件系统的配置
     51         Configuration conf = new Configuration();
     52         //HDFS 接口
     53         URI uri = new URI("hdfs://djt002:9000");
     54         
     55 //        URL、URI与Path三者的区别
     56 //        Hadoop文件系统中通过Hadoop Path对象来代表一个文件    
     57 //        URL(相当于绝对路径)    ->   (文件) ->    URI(相当于相对路径,即代表URL前面的那一部分)
     58 //        URI:如hdfs://dajiangtai:9000
     59 //        如,URL.openStream
     60         
     61         
     62         //获得FileSystem实例fs
     63         fs = FileSystem.get(uri, conf);
     64 //        返回类型是FileSystem,等价于  FileSystem fs = FileSystem.get(uri, conf);
     65     
     66         
     67         //获得FileSystem实例,即Local
     68         local = FileSystem.getLocal(conf);
     69 //        返回类型是LocalFileSystem,等价于  LocalFileSystem  local = FileSystem.getLocal(conf);
     70         
     71 //        为什么要获取到Local呢,因为,我们要把本地D盘下data/74目录下的文件要合并后,上传到HDFS里,所以,我们需先获取到Local,再来做复制工作啦!
     72 
     73          
     74         //只上传data/testdata 目录下 txt 格式的文件
     75         FileStatus[] localStatus = local.globStatus(new Path("D://data/74/*"),new RegexAcceptPathFilter("^.*txt$"));
     76 //        FileStatus[] localStatus = local.globStatus(new Path("./data/copyManyFilesToHDFS/*"),new RegexAcceptPathFilter("^.*txt$"));
     77 //        ^表示匹配我们字符串开始的位置               *代表0到多个字符                        $代表字符串结束的位置
     78 //        RegexAcceptPathFilter来只接收我们需要的,即格式
     79 //        RegexAcceptPathFilter这个方法我们自己写
     80         
     81 //        但是我们,最终是要处理文件里的东西,最终是要转成Path类型,因为Path对象f,它对应着一个文件。
     82         
     83         //获取74目录下的所有文件路径,注意FIleUtil中stat2Paths()的使用,它将一个FileStatus对象数组转换为Path对象数组。
     84         Path[] listedPaths = FileUtil.stat2Paths(localStatus);//localStatus是FileStatus数组类型
     85 
     86         for(Path p:listedPaths){//for星型循环,即将listedPaths是Path对象数组,一一传给Path p
     87             //将本地文件上传到HDFS
     88             fs.copyFromLocalFile(p, dstPath);
     89             //因为每一个Path对象p,就是对应本地下的一个文件,
     90             
     91         }
     92     }
     93 
     94     /**
     95      * @function 只接受 txt 格式的文件aa
     96      * @author 小讲
     97      *
     98      */
     99     public static class RegexAcceptPathFilter implements PathFilter {
    100         private final String regex;//变量
    101 
    102         public RegexAcceptPathFilter(String regex) {
    103             this.regex = regex;//意思是String regex的值,赋给当前类RegexAcceptPathFilter所定义的private final String regex;
    104         }
    105 
    106         public boolean accept(Path path) {//主要是实现accept方法
    107             // TODO Auto-generated method stub
    108             boolean flag = path.toString().matches(regex);//匹配正则表达式,这里是^.*txt$
    109             //只接受 regex 格式的文件
    110             return flag;//如果要接收 regex 格式的文件,则accept()方法就return flag; 如果想要过滤掉regex格式的文件,则accept()方法就return !flag。
    111         }
    112     }
    113 }
  • 相关阅读:
    获取ip
    PHP大牛笔记收藏
    WordPress伪静态规则设置
    PHP 中 include 和 require 的区别详解
    Wordpress学习链接整理
    手机访问自动跳转
    微信web开发工具
    接入支付宝出现交易订单处理失败,请稍后再试(ALI64)的错误【转】
    HTTPS科普扫盲帖【转】
    php 好用的函数
  • 原文地址:https://www.cnblogs.com/zlslch/p/6173719.html
Copyright © 2011-2022 走看看