zoukankan      html  css  js  c++  java
  • 第2章 执行SparkSQL查询

    2章 执行SparkSQL查询

    2.1 命令行查询流程

    打开Spark shell

    例子:查询大于30岁的用户

    创建如下JSON文件,注意JSON的格式:

    {"name":"Michael"}
    {"name":"Andy", "age":30}
    {"name":"Justin", "age":19}

    2.2 IDEA创建SparkSQL程序

    IDEA中程序的打包和运行方式都和SparkCore类似,Maven依赖中需要添加新的依赖项:

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.1.1</version>
        <scope>provided</scope>
    </dependency>

    程序如下:

    package com.atguigu.sparksql
    
    import org.apache.spark.sql.SparkSession
    import org.apache.spark.{SparkConf, SparkContext}
    import org.slf4j.LoggerFactory
    
    
    object HelloWorld {
    
      def main(args: Array[String]) {
        //创建SparkConf()并设置App名称
        val spark = SparkSession
          .builder()
          .appName("Spark SQL basic example")
          .config("spark.some.config.option", "some-value")
          .getOrCreate()
    
        // For implicit conversions like converting RDDs to DataFrames
        import spark.implicits._
    
        val df = spark.read.json("examples/src/main/resources/people.json")
    
        // Displays the content of the DataFrame to stdout
        df.show()
    
        df.filter($"age" > 21).show()
    
        df.createOrReplaceTempView("persons")
    
        spark.sql("SELECT * FROM persons where age > 21").show()
    
        spark.stop()
      }
    
    }
    

      

  • 相关阅读:
    spring-webmvc 4.3.4 与 freemarker、easyui 整合
    CentOS 7 网络配置
    CentOS 7 安装 mariaDB
    CentOS 7 安装 tomcat7.0
    CentOS 7 安装JDK
    利用icepdf将pdf文件转为图片
    jwplayer 网页在线播放插件
    postgresql 行转列,拼接字符串
    activemq-5.13 在windows下部署应用
    几款Java常用基础工具库
  • 原文地址:https://www.cnblogs.com/Diyo/p/11342425.html
Copyright © 2011-2022 走看看