不多說,直接上代碼。
代碼版本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 }