本文中所有数据以本地数据为数据源
1、读取json文件
object ReadJson {
def main(args: Array[String]): Unit = {
val spark = SparkSession.builder().appName("testpeople").master("local")
.getOrCreate()
import spark.implicits._
val test = spark.read.json("C:/Users/Administrator/Desktop/课程代码/employee.json")
test.select("name").show()
test.show()
}
}
2、读取txt文件
spark.read下除了json方法用于读取json文件外,还有读取数据的而其他方式,使用testFile方法时一直不行,不清楚是对txt文件的格式有要求还是后续的方法不对,有朋友知道的劳烦指点下
val testtxt = spark.read.textFile("C:/Users/Administrator/Desktop/课程代码/employee.txt")
testtxt.show() //成功读取文件数据
testtxt.select("name").show()