HDFS之globStatus()函数(用通配符匹配制定模式的路径)liststatus() (列出目录下的所有文件)

Globstatus.java

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Globstatus {
	//通配符
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String uri = "hdfs://back03:9000/user/root/in/weibo*.txt";
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(uri),conf);
		FileStatus[] status =  fs.globStatus(new Path(uri));
		System.out.print("heh");
		for(FileStatus p:status){
			if (fs.exists(p.getPath())) {
	              System.out.println(p.getPath());
	           }
		}
	}

}

运行结果为:
hehhdfs://back03:9000/user/root/in/weibo4.txt
通配符列表:

*                             匹配0到多个字符
?                            匹配单一字符
[ab]                          匹配{a,b}集合中的一个字符
[^ab]                         匹配非{a,b}集合里的一个字符
[a-b]                         匹配在{a,b}范围内的一个字符
[^a-b]                        同理可知
{a,b}                        匹配包含a或b中的一个字符
\c                            匹配元字符c






Liststatus.java

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;

public class Liststatus {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String uri = args[0];
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(uri), conf);
		Path[] paths = new Path[args.length];
		for(int i = 0; i < paths.length; i++){
		paths[i] = new Path(args[i]);
		}
		FileStatus[] status = fs.listStatus(paths);
		Path[] listedPaths = FileUtil.stat2Paths(status);
		for(Path p : listedPaths){
		System.out.println(p);
		}
	}
}
输入:

hdfs://back03:9000/user/root/in/ hdfs://back03:9000/user/root/

运行结果:

hdfs://back03:9000/user/root/in/amazon-meta.txt
hdfs://back03:9000/user/root/in/mywritable.txt
hdfs://back03:9000/user/root/in/test.txt
hdfs://back03:9000/user/root/in/weibo4.txt
hdfs://back03:9000/user/root/in
hdfs://back03:9000/user/root/out



你可能感兴趣的:(hdfs)