不多說,直接上代碼。



代碼版本1
1 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs5; 2 3 import java.io.IOException; 4 5 import java.net.URI; 6 import java.net.URISyntaxException; 7 8 import org.apache.hadoop.conf.Configuration; 9 import org.apache.hadoop.fs.FileSystem; 10 import org.apache.hadoop.fs.Path; 11 12 /** 13 * 14 * @author 15 * @function Copying from Local file system to HDFS,即把本地文件(如windows或linux文件拷貝到hdfs上) 16 * 17 */ 18 public class CopyingLocalFileToHDFS 19 { 20 /** 21 * @function Main() 方法 22 * @param args 23 * @throws IOException 24 * @throws URISyntaxException 25 */ 26 public static void main(String[] args) throws IOException,URISyntaxException{ 27 // 本地文件路徑(如windows或linux文件) 28 // String source = "D://Data/weibo.txt"; 29 String source = "./data/weibo.txt"; 30 // hdfs文件路徑 31 String dest = "hdfs://HadoopMaster:9000/middle/weibo/"; 32 copyFromLocal(source, dest); 33 } 34 35 /** 36 * @function 本地文件上傳至 HDFS 37 * @param source 原文件路徑 38 * @param dest 目的文件路徑 39 * @throws IOException 40 * @throws URISyntaxException 41 */ 42 public static void copyFromLocal(String source, String dest)throws IOException, URISyntaxException { 43 // 讀取hadoop文件系統的配置 44 Configuration conf = new Configuration(); 45 URI uri = new URI("hdfs://HadoopMaster:9000"); 46 // FileSystem是用戶操作HDFS的核心類,它獲得URI對應的HDFS文件系統 47 FileSystem fileSystem = FileSystem.get(uri, conf); 48 // 源文件路徑 49 Path srcPath = new Path(source); 50 // 目的路徑 51 Path dstPath = new Path(dest); 52 // 查看目的路徑是否存在 53 if (!(fileSystem.exists(dstPath))) { 54 // 如果路徑不存在,即刻創建 55 fileSystem.mkdirs(dstPath); 56 } 57 // 得到本地文件名稱 58 String filename = source.substring(source.lastIndexOf('/') + 1,source.length()); 59 try { 60 // 將本地文件上傳到HDFS 61 fileSystem.copyFromLocalFile(srcPath, dstPath); 62 System.out.println("File " + filename + " copied to " + dest); 63 } catch (Exception e) { 64 System.err.println("Exception caught! :" + e); 65 System.exit(1); 66 } finally { 67 fileSystem.close(); 68 } 69 } 70 71 }
代碼版本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 }
