一个spark SQL和DataFrames的故事
package com.lin.spark
import org.apache.spark.sql.{Row, SparkSession}
import org.apache.spark.sql.types.{StringType, StructField, StructType}
/**
* Created by Yaooo on 2019/6/8.
*/
object SparkSQLExample {
case class Person(name:String,age:Long)
def main(args: Array[String]): Unit = {
val spark = SparkSession
.builder()
.appName("Spark SQL")
.config("spark.come.config.option","some-value")
.master("local[2]")
.getOrCreate()
runBasicDataFrameExample(spark)
runDatasetCreationExample(spark)
runInferSchemaExample(spark)
runProgrammaticSchemaExample(spark)
}
private def runProgrammaticSchemaExample(spark:SparkSession): Unit ={
import spark.implicits._
val personRDD = spark.sparkContext.textFile("src/main/resources/people.txt")
val schemaString = "name age"
val fields = schemaString.split(" ")
.map(fieldName => StructField(fieldName, StringType, nullable = true))
val schema = StructType(fields)
val rowRDD = personRDD
.map(_.split(","))
.map(att => Row(att(0),att(1).trim))
val peopleDF = spark.createDataFrame(rowRDD,schema)
peopleDF.createOrReplaceTempView("people")
val results = spark.sql("select * from people")
results.map(att=>"Name : "+att(0)).show()
}
private def runInferSchemaExample(spark:SparkSession): Unit ={
import spark.implicits._
val personDF = spark.sparkContext
.textFile("src/main/resources/people.txt")
.map(_.split(","))
.map(attributes => Person(attributes(0),attributes(1).trim.toInt))
.toDF()
personDF.createOrReplaceTempView("people")
val teenagersDF = spark.sql("select * from people where age between 13 and 19")
teenagersDF.show()
teenagersDF.map(teenager =>"name: "+teenager(0)).show()
teenagersDF.map(teenager => "Name: "+ teenager.getAs[String]("name")).show()
implicit val mapEncoder = org.apache.spark.sql.Encoders.kryo[Map[String, Any]]
teenagersDF.map(teenager => teenager.getValuesMap[Any](List("name","age"))).collect()
.foreach(println)
}
private def runDatasetCreationExample(spark:SparkSession): Unit ={
import spark.implicits._
val caseClassDS = Seq(Person("Andy",18)).toDF()
caseClassDS.show()
val primitiveDS = Seq(1, 2, 3).toDS()
primitiveDS.map(_+1).collect().foreach(println)
val path = "src/main/resources/person.json"
val personDS = spark.read.json(path).as[Person]
personDS.show()
}
private def runBasicDataFrameExample(spark:SparkSession): Unit ={
import spark.implicits._
val df = spark.read.json("src/main/resources/person.json")
df.show()
df.printSchema()
df.select("name").show()
df.select($"name",$"age"+1).show()
df.filter($"age">21).show()
df.groupBy($"age").count().show()
/*df.createOrReplaceTempView("people")
val sqlDF = spark.sql("select * from people")
sqlDF.show()*/
df.createOrReplaceGlobalTempView("people")
spark.sql("select * from global_temp.people").show()
}
}
一个spark SQL和DataFrames的故事的更多相关文章
- Spark 官方文档(5)——Spark SQL,DataFrames和Datasets 指南
Spark版本:1.6.2 概览 Spark SQL用于处理结构化数据,与Spark RDD API不同,它提供更多关于数据结构信息和计算任务运行信息的接口,Spark SQL内部使用这些额外的信息完 ...
- Apache Spark 2.2.0 中文文档 - Spark SQL, DataFrames and Datasets Guide | ApacheCN
Spark SQL, DataFrames and Datasets Guide Overview SQL Datasets and DataFrames 开始入门 起始点: SparkSession ...
- Apache Spark 2.2.0 中文文档 - Spark SQL, DataFrames and Datasets
Spark SQL, DataFrames and Datasets Guide Overview SQL Datasets and DataFrames 开始入门 起始点: SparkSession ...
- Spark SQL 之 Migration Guide
Spark SQL 之 Migration Guide 支持的Hive功能 转载请注明出处:http://www.cnblogs.com/BYRans/ Migration Guide 与Hive的兼 ...
- Spark SQL 官方文档-中文翻译
Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...
- Spark SQL官方文档阅读--待完善
1,DataFrame是一个将数据格式化为列形式的分布式容器,类似于一个关系型数据库表. 编程入口:SQLContext 2,SQLContext由SparkContext对象创建 也可创建一个功能更 ...
- spark SQL (二) 聚合
聚合内置功能DataFrames提供共同聚合,例如count(),countDistinct(),avg(),max(),min(),等.虽然这些功能是专为DataFrames,spark SQL还拥 ...
- Spark1.0新特性-->Spark SQL
Spark1.0出来了,变化还是挺大的,文档比以前齐全了,RDD支持的操作比以前多了一些,Spark on yarn功能我居然跑通了.但是最最重要的就是多了一个Spark SQL的功能,它能对RDD进 ...
- Spark SQL概念学习系列之如何使用 Spark SQL(六)
val sqlContext = new org.apache.spark.sql.SQLContext(sc) // 在这里引入 sqlContext 下所有的方法就可以直接用 sql 方法进行查询 ...
随机推荐
- 烂漫爱心表白动画 分类: C# 2014-10-07 19:08 28人阅读 评论(0) 收藏
曾经我说过我会用程序来表达我对你的爱. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- 【刷题】java 常见的几种运行时异常RuntimeException
常见的几种罗列如下: -NullPointerException - 空指针引用异常 ClassCastException - 类型强制转换异常. IllegalArgumentException - ...
- Java JNA (三)—— 结构体使用及简单示例
JNA简介 JNA全称Java Native Access,是一个建立在经典的JNI技术之上的Java开源框架(https://github.com/twall/jna).JNA提供一组Java工具类 ...
- rtp/rtcp
rtp/rtcp stack custom rtp ORTP UCL Common RTP library Bell Labs RTP Library jrtplib 1.custom rtp sen ...
- umount device is busy 的处理方法
[root@web2-server yum.repos.d]# umount /mnt/cdrom/ umount: /mnt/cdrom: device is busy. (In some case ...
- SYSAUX表空间满,
step1. 确认到底是哪个段占用了sysaux空间: select segment_name,sum(bytes)/1024/1024 from dba_segments where tables ...
- 人生苦短_我用Python_openpyxl库读取Excel文件数据_008
上图为读取的目标文件--------------------------------------------------------------------------------- # coding ...
- Delphi 安装Cnpack cnvcl后界面不断弹出 Memory Manager's list capablity overflow ,please enlarge it!
Delphi 安装Cnpack cnvcl后界面不断弹出 Memory Manager's list capablity overflow ,please enlarge it! 解决方法:删除指定 ...
- Spring框架之接口实现覆盖(接口功能扩展)
在日常开发中,存在着这种一种场景,框架对接口A提供了一个种默认的实现AImpl,随着需求的变更,现今AImpl不能满足了功能需要,这时,我们该怎么办? 当然是修改AImpl的实现代码了,但是,如果它是 ...
- spring-boot整合Cxf的webservice案例
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 <?xml version="1.0&q ...