In the process of uploading local files to HDFs, in many cases a directory contains many files, and we need to filter them to select the files that meet our requirements and upload them to HDFs. In this case, we need to use the file mode. Before the project starts, we'll start with the file mode
1. File mode
It is common to work with a series of files in a single operation. For example, a log-processing MapReduce job may have to analyze the one-month log volume. If a file is a file or a directory declaration of a directory that is too much trouble, we can use a wildcard (wild card) to match multiple files (this operation is also called globbing).
Hadoop provides two ways to work with filegroups:
1 Public throws IOException; 2 3 Public throws IOException;
Using file mode sometimes does not effectively describe the series of files you want, such as if you want to exclude a particular file. So filesystem's Liststatus () and Globstatus () methods provide an optional parameter: pathfilter--It allows you to have some more granular control matches:
1 Package Org.apache.hadoop.fs; 2 3 Public Interface Pathfilter 4 {5 Boolean Accept (path path); 6 }
- The match in Hadoop is the same as Bash in Unix, as shown in:,
2. Data
We use wildcards and Pathfilter objects to upload files from multiple local formats to HDFS and filter out files other than txt text format
I made some random data, as follows
3. Analysis
Based on the requirements, we complete the following two steps:
1, first use Globstatus (Path Pathpattern, Pathfilter Filter), complete file format filtering, get all txt format files.
2. Then use the Java API interface copyfromlocalfile to upload all TXT-formatted files to HDFS
4. Realize
First define a class Regexacceptpathfilter implementation pathfilter, filter out the TXT text format of the file.
1 /** 2 * @ProjectName Pathfilter3 * @PackageName COM.BUAA4 * @ClassName Regexacceptpathfilter5 * @Description only accept regex-compliant files6 * @Author Liu Jishu7 * @Date 2016-04-15 20:39:218 */9 Public Static classRegexacceptpathfilterImplementsPathfilter {Ten Private FinalString regex; One A PublicRegexacceptpathfilter (String regex) { - This. Regex =regex; - } the - @Override - Public BooleanAccept (path path) { - BooleanFlag =path.tostring (). Matches (regex); + //accept only regex-compliant files - returnFlag; + } A}
if you want to receive a regex-formatted file, the Accept () method will return flag, and if you want to filter out the Regex-formatted file, the Accept () method will return!flag.
Next, in the UploadFile method, use the Globstatus method to get all the txt files and then upload the files to HDFs using the Copyfromlocalfile method.
1 /**2 * Filter file format to upload multiple files to HDFS3 * 4 * @paramSrcpath Source Path5 * @paramdestpath Target Path6 * @paramFilter Regular7 * @throwsurisyntaxexception8 * @throwsIOException9 */Ten Public Static voidUploadFile (String srcpath,string destpath,string filter)throwsURISyntaxException, IOException { One //reading configuration Files AConfiguration conf =NewConfiguration (); - //Remote File System -Uri uri =NewURI (Hdfsuri.trim ()); theFileSystem remote =Filesystem.get (uri,conf); - //get local file system -FileSystem local =filesystem.getlocal (conf); - + //upload only files that meet the filter criteria in the Srcpath directory -filestatus[] Localstatus = Local.globstatus (NewPath (Srcpath),NewRegexacceptpathfilter (filter)); + //get all file Paths Apath[] Listedpaths =fileutil.stat2paths (localstatus); at - if(Listedpaths! =NULL){ - for(Path path:listedpaths) { - //uploading local files to HDFs -Remote.copyfromlocalfile (Path,NewPath (Hdfsuri +destpath)); - } in } -}
In the main () method, when calling UploadFile, perform a multi-file upload to HDFS
1 public static void main (string[] args) throws ioexception,urisyntaxexception { 2 //< /span> first argument: represents the source path 3 // second parameter: Represents the directory path 4 // third argument: The representation is regular, here we only upload txt file, so the regular is ^.*txt$ 5 uploadfile ("d:\\data\\*", "/buaa/data", "^.*txt$" 6 }
If you think reading this blog gives you something to gain, you might want to click " recommend " in the lower right corner.
If you want to find my new blog more easily, click on " Follow me " in the lower left corner.
If you are interested in what my blog is talking about, please keep following my follow-up blog, I am " Liu Chao ★ljc".
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Address: Download
Use the Java API to upload multiple files to HDFs via path filtering