Path对象转换为字符串的方法如下:
先把Path对象转换为URI对象,然后转换为String对象。
如果对Path对象直接调用toString()方法,只会打印出Path对象的名字+@+hashcode()。
原因是Hadoop中,Path类的toString()没有对Object类中的toString()进行重载。而Hadoop中,URI对象的toString()重载了Object中的方法。因而只能间接得到String类型。
如下例子是从Hadoop中读取一个文件目录的列表:
public class ListStatus {
public static String ls(String arg) throws Exception {
String uri = arg;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(uri), conf);
Path path = new Path(uri);
FileStatus[] status = fs.listStatus(path