scala - 从 HDFS 读取文件,Scala Spark
问题描述
我正在尝试从 HDFS 读取文件,但我在这里遇到了问题。该文件不存在,因此我必须检查是否存在。如果文件存在,我会读取该文件,否则我会读取一个空的 DF。
所以我正在尝试的是:
val fs: FilySystem = FileSystem.get(new URI(path), new Configuration())
if (fs.exists(new org.apache.hadoop.fs.Path(s"$Path"))) {
val df6 = spark.read.parquet(path)
} else {
val df6 = df1.limit(0)
}
val df6.show()
但是我在 Jupyter 上收到以下错误:
Message: <console>:28: error: not found: type FileSystem
我做错了什么?
解决方案
Try something like this (with your adjustment) -
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
import java.net.URI
import scala.io.Source
val hdfs = FileSystem.get(new URI("hdfs://cluster:8020/"), new Configuration())
val path = new Path("/HDFS/FILE/LOCATION")
val stream = hdfs.open(path)
val temp = Source.fromInputStream(stream).getLines()
推荐阅读
- react-native - 如何使用反应钩子仅用一行代码获取我的所有状态数据?
- r - 查找每个人的最大或最小日期值
- python - 如何在 python 中更新列表列表?
- symmetricds - 删除转换不适用于列
- reactjs - 有没有办法在 Typescript 中使用类型中的值作为 defaultProp
- arrays - 如果缺少另一个数组中存在的值,如何在数组中插入 NaN?
- c# - 联系肥皂网络服务时是否考虑命名空间?
- python - keras 模型权重 1.x 到 2.x
- python-3.x - python - 如何将来自两个不同字典的数据帧连接成python中的新数据帧?
- binding - 如何通过 Binding 和 ObservedObject 更新 UIViewRepresentable 地图