在Hadoop中,将Path对象转换为字符串输出

来源:互联网 发布:office卸载软件mac 编辑:程序博客网 时间:2024/05/14 05:24

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);Path[] listedPaths = FileUtil.stat2Paths(status);InputStream in = null;StringBuffer buff = new StringBuffer();for (Path p : listedPaths) { buff.append(p.toUri().toString()); buff.append('\n');}return buff.toString();}}


0 0