星火目录列表斯卡拉文件夹 [英] Spark Scala list folders in directory

查看:190
本文介绍了星火目录列表斯卡拉文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出使用Scala /星火一个HDFS目录中的所有文件夹。
在Hadoop中,我可以通过使用命令做到这一点: FS Hadoop的HDFS -ls://sandbox.hortonworks.com/demo/

我试了一下:

  VAL的conf =新配置()
VAL FS = FileSystem.get(新的URI(HDFS://sandbox.hortonworks.com/),CONF)VAL路径=新路径(HDFS://sandbox.hortonworks.com/demo/)VAL文件= fs.listFiles(路径,FALSE)

但它似乎并不认为他看起来在Hadoop的目录中我找不到我的文件夹/文件。

我也试过用:

  FileSystem.get(sc.hadoopConfiguration).listFiles(新路径(HDFS://sandbox.hortonworks.com/demo/),真)

但这也于事无补。

你有什么其他的想法?

PS:我还检查了这个线程:星火迭代HDFS目录,但它不工作对我来说,因为它似乎没有搜索的HDFS目录,而不是只与架构文件//.

解决方案

我们正在使用Hadoop 1.4和它没有listFiles方法,所以我们使用listStatus获得目录。它没有递归选项,但它很容易管理递归查询。

  VAL FS = FileSystem.get(新配置())
VAL状态= fs.listStatus(新路径(YOUR_HDFS_PATH))
status.foreach(X =>的println(x.getPath))

I want to list all folders within a hdfs directory using Scala/Spark. In Hadoop I can do this by using the command: hadoop fs -ls hdfs://sandbox.hortonworks.com/demo/

I tried it with:

val conf = new Configuration()
val fs = FileSystem.get(new URI("hdfs://sandbox.hortonworks.com/"), conf)

val path = new Path("hdfs://sandbox.hortonworks.com/demo/")

val files = fs.listFiles(path, false)

But it does not seem that he looks in the Hadoop directory as i cannot find my folders/files.

I also tried with:

FileSystem.get(sc.hadoopConfiguration).listFiles(new Path("hdfs://sandbox.hortonworks.com/demo/"), true)

But this also does not help.

Do you have any other idea?

PS: I also checked this thread: Spark iterate HDFS directory but it does not work for me as it does not seem to search on hdfs directory, instead only on the local file system with schema file//.

解决方案

We are using hadoop 1.4 and it doesn't have listFiles method so we use listStatus to get directories. It doesn't have recursive option but it is easy to manage recursive lookup.

val fs = FileSystem.get(new Configuration())
val status = fs.listStatus(new Path(YOUR_HDFS_PATH))
status.foreach(x=> println(x.getPath))

这篇关于星火目录列表斯卡拉文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆