1.1 简单读取文件
val spark = SparkSession.builder() .appName("demo") .master("local[3]") .getOrCreate() // 读取hdfs文件目录 spark.sparkContext.textFile("/user/data") spark.sparkContext.textFile("hdfs://10.252.51.58:8088/user/data") // 读取本地目录 spark.sparkContext.textFile("file://user/data")
1.2 正则模式读取文件
val spark = SparkSession.builder() .appName("demo") .master("local[3]") .getOrCreate() // 读取hdfs文件目录 spark.sparkContext.textFile("/user/data/201908/0[1-9]/*")
2.1 将多个文件变成一个 list 作为参数
正确写法:sc.TextFile( filename1 + "," + filename2 + "," + filename3)
val spark = SparkSession.builder() .appName("demo") .master("local[3]") .getOrCreate() val fileList = Array("/user/data/source1","/user/data/source2","/user/data/source3") // 读取hdfs文件目录 spark.sparkContext.textFile(fileList.mkString(","))
2.2 使用 union 连接
val spark = SparkSession.builder() .appName("demo") .master("local[3]") .getOrCreate() val fileList = Array("/user/data/source1","/user/data/source2","/user/data/source3") //array[RDD] val fileRDD:Array[RDD[String]] = fileList.map(spark.sparkContext.textFile(_) spark.sparkContext.union(fileRDD)
原文:https://www.cnblogs.com/yyy-blog/p/11369389.html