准备json数据
users.json
[{"name":"张三" ,"age":18} ,{"name":"李四" ,"age":15}]
注意,必须得是一行,不能是换行的.
Maven依赖
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包插件, 否则 scala 类不会编译并打包进去 -->
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.4.6</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
代码
import org.apache.spark.sql.SparkSession
object CreateDataFrame {
def main(args: Array[String]): Unit = {
val sparkSession = SparkSession.builder()
.appName("CreateDataFrame")
.master("local[2]")
.getOrCreate()
val dataFrame = sparkSession.read.json("E:\\ZJJ_SparkSQL\\demo01\\src\\main\\resources\\users.json")
dataFrame.createOrReplaceTempView("user")
dataFrame.cache()
sparkSession.sql("select * from user").show()
sparkSession.stop()
}
}
结果
+---+----+
|age|name|
+---+----+
| 18| 张三|
| 15| 李四|
+---+----+
码云地址
https://gitee.com/crow1/ZJJ_SparkSQL/blob/master/demo01/src/main/java/com/CreateDataFrame.scala