欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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()

Spark-SQL在IDEA中创建SparkSQL程序|DF风格编程|RDD转换为DF

RDD转换为DF代码实现

Spark-SQL在IDEA中创建SparkSQL程序|DF风格编程|RDD转换为DF