导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
编写代码
class demo1{
@Test
def readJson(): Unit = {
//1) 创建 sqlContext
val sparkConf = new SparkConf().setAppName("SQLContext").setMaster("local[*]")
val sc = new SparkContext(sparkConf)
val sqlContext = new SQLContext(sc)
// 1)相关处理
val person = sqlContext.read.format("json").load("C:\\Users\\LUFFY\\Desktop\\testData\\person.json")
//打印 Schema 约束
person.printSchema()
//打印数据,默认 20条
person.show()
sc.stop()
}
}
点击运行按钮
测试 结果