SparkSQL demo

Posted xubiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SparkSQL demo相关的知识,希望对你有一定的参考价值。

1、数据样本:data1.txt

xiaoming,25,chengdu
xiaohua,23,beijing
liuyang,16,hangzhou
xiaoqiang,19,zhejiang

2、demo例子

package com.test.sparksql

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by admin on 2017/12/26-16:45.
  * Description : 
  */
object SparkSqlApplication {

  case class Person(name:String, age:Int, addr:String)

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("Spark Sql Test").setMaster("local")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)

    import sqlContext._
    import sqlContext.implicits._

    val people = sc.textFile("D:\\SparkSqlDataTest\\data1.txt").map(_.split(",")).map( p => Person(p(0),p(1).trim.toInt,p(2))).toDF()

    people.registerTempTable("people")

    val teenagers = sql("SELECT name, age, addr FROM people ORDER BY age")

    teenagers.map( t => "name:" + t(0) + " age:" + t(1) + " addr:" + t(2)).collect().foreach(println)

    sc.stop();
  }
}

 

3、输出结果

name:liuyang age:16 addr:hangzhou
name:xiaoqiang age:19 addr:zhejiang
name:xiaohua age:23 addr:beijing
name:xiaoming age:25 addr:chengdu

 







以上是关于SparkSQL demo的主要内容,如果未能解决你的问题,请参考以下文章

[vscode]--HTML代码片段(基础版,reactvuejquery)

Spark Sql源码详细分析

Spark Sql源码详细分析

Spark Sql源码详细分析

12mmaction2 行为识别商用级别X3D复现 demo实现 检测自己的视频 Expanding Architecturesfor Efficient Video Recognition(代码片段

[异常解决] Keil安装好nRF51822开发环境,运行DEMO报错:Error:“GPIOTE_CONFIG_NUM_OF_LOW_POWER_ENVENTS” is undefined(代码片段