rdd读取一个文件
val rdd = sc.textFile("hdfs://172.20.20.17:9000/tmp/wht/account/accounts.txt").map(_.split(","))
rdd读取多个文本文件
val rdd = sc.textFile("hdfs://172.20.20.17/tmp/wht/account/accounts.txt,hdfs://172.20.20.17/tmp/wht/account/account02.txt").map(_.split(","))
rdd读取一个文件夹
val rdd1 = sc.textFile("hdfs://172.20.20.17/tmp/wht/account/").map(_.split(","))
val rdd2 = sc.textFile("hdfs://172.20.20.17/tmp/wht/mix/").map(_.split(","))
rdd读取嵌套的文件夹下的文件
val rdd3 = sc.textFile("hdfs://172.20.20.17/tmp/wht/*/*").map(_.split(","))
rdd读取本地文件
val rdd4 = sc.textFile("file:///root/Downloads/data/").map(_.split(","))
rdd使用通配符读取文件
val rdd5 = sc.textFile("hdfs://172.20.20.17/tmp/wht/*.txt").map(_.split(","))
上述示例都是以文本方式读取文件,该方式下rdd中的数据是按行来组织的,即读取了多个文件时,rdd.count()的值即为多个文本文件中的行数之和。
当目录下存在非文本文件如orc文件时,会在使用rdd时报错,如下所示:
scala> rdd4.count()
java.io.IOException: Not a file: file:/root/Downloads/data/nameAndBanlance.orc
参考:https://blog.youkuaiyun.com/HeatDeath/article/details/81871651