Spark-SQL在IDEA中创建SparkSQL程序|DF风格编程|RDD转换为DF
程序员文章站
2022-04-16 10:01:43
...
POM
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
<build>
<finalName>SparkCoreTest</finalName>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
DF风格编程代码实现
// 1. 创建SparkSession对象
val spark: SparkSession = SparkSession.builder()
.master("local[*]")
.appName("SparkSQL")
.getOrCreate()
val context: SparkContext = spark.sparkContext
// 2. 导入隐式转换 : spark为创建得SparkSession对象
import spark.implicits._
// 3. 读取数据创建DF
val df: DataFrame = spark.read.json("D:\\MyWork\\WorkSpaceIDEA\\scalaDemo\\sparksql\\src\\main\\resources\\people.json")
// 4. SQL风格编程
df.createTempView("people")
spark.sql("select * from people").show()
// 5. DSL风格编程
df.select("name").show()
// 6. 关闭连接
spark.stop()
RDD转换为DF代码实现
上一篇: eslint 入门项目搭建过程
下一篇: JavaScript面向对象学习笔记