ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
```scala import org.apache.spark.sql.{DataFrame, SparkSession} object JsonOrCsv { def main(args: Array[String]): Unit = { val spark:SparkSession = SparkSession.builder() .master("local[4]") .appName(this.getClass.getName) .getOrCreate() // 读取json,下面两种方式都可以 val df1:DataFrame = spark.read.json("file:///E:\\hadoop\\input\\people.json") val df2:DataFrame = spark.read.format("json").load("file:///E:\\hadoop\\input\\people.json") df1.show(5) // +----+-------+ // | age| name| // +----+-------+ // |null|Michael| // | 30| Andy| // | 19| Justin| // +----+-------+ // 读取csv,下面两种方式都可以 val df3:DataFrame = spark.read.csv("file:///E:\\hadoop\\input\\customers.csv") val df4:DataFrame = spark.read.format("csv").load("file:///E:\\hadoop\\input\\customers.csv") df3.show(5) // +---+-------+---------+---------+---------+--------------------+-----------+---+-----+ // |_c0| _c1| _c2| _c3| _c4| _c5| _c6|_c7| _c8| // +---+-------+---------+---------+---------+--------------------+-----------+---+-----+ // | 1|Richard|Hernandez|XXXXXXXXX|XXXXXXXXX| 6303 Heather Plaza|Brownsville| TX|78521| // | 2| Mary| Barrett|XXXXXXXXX|XXXXXXXXX|9526 Noble Embers...| Littleton| CO|80126| // | 3| Ann| Smith|XXXXXXXXX|XXXXXXXXX|3422 Blue Pioneer...| Caguas| PR|00725| // | 4| Mary| Jones|XXXXXXXXX|XXXXXXXXX| 8324 Little Common| San Marcos| CA|92069| // | 5| Robert| Hudson|XXXXXXXXX|XXXXXXXXX|10 Crystal River ...| Caguas| PR|00725| // +---+-------+---------+---------+---------+--------------------+-----------+---+-----+ } } ```