1.dataframe 基本操作

 def main(args: Array[String]): Unit = {
val spark = SparkSession.builder()
.appName("test")
.master("local[*]")
.getOrCreate()
import spark.implicits._
val people = spark.read.format("json").load("people.json")
people.show()
/* +----+-------+
| age| name|
+----+-------+
|null|Michael|
| 30| Andy|
| 19| Justin|
+----+-------+ */
people.printSchema()
/*root
|-- age: long (nullable = true)
|-- name: string (nullable = true)*/
people.select($"name").show()
/* +-------+
| name|
+-------+
|Michael|
| Andy|
| Justin|
+-------+*/
people.select($"name", $"age".cast("string").as("age")).printSchema()
/* root
|-- name: string (nullable = true)
|-- age: string (nullable = true)*/
people.select($"name", ($"age" + ).as("age")).show()
/* +-------+----+
| name| age|
+-------+----+
|Michael|null|
| Andy| 31|
| Justin| 20|
+-------+----+*/
people.filter($"age" > ).show()
// +---+----+
// |age|name|
// +---+----+
// | 30|Andy|
// +---+----+
people.groupBy("age").count().show()
// +----+-----+
// | age|count|
// +----+-----+
// | 19| 1|
// |null| 1|
// | 30| 1|
// +----+-----+
spark.stop()
}

2.用sql 访问dataframe

    val people = spark.read.format("json").load("people.json")
people.createOrReplaceTempView("tb")
spark.sql("select name,age from tb").show()
// +-------+----+
// | name| age|
// +-------+----+
// |Michael|null|
// | Andy| 30|
// | Justin| 19|
// +-------+----+

3.创建dataset

    val ccDs = Seq(Person("jason",),Person("dong",)).toDS()
ccDs.select("name").show()
val pDs = Seq(,,).toDS()
pDs.map(_+).show()
pDs.printSchema()

4.反射推断模式

    val spark = SparkSession.builder()
.appName("test")
.master("local[*]")
.getOrCreate()
import spark.implicits._
val sc = spark.sparkContext
val peopleDF = sc.textFile("people.txt")
.map(_.split(",", -))
.map(arr => Person(arr().trim, arr().trim.toInt))
.toDF().cache().createOrReplaceTempView("people")
val teenagerDF = spark.sql("select * from people where age between 13 and 15").cache()
teenagerDF.map(t => "name :" + t()).show()
// +-------------+
// | value|
// +-------------+
// |name :Michael|
// +-------------+
teenagerDF.map(t => "name:" + t.getAs[String]("name")).show()
// +-------------+
// | value|
// +-------------+
// |name :Michael|
// +-------------+
implicit val mapEncoder = org.apache.spark.sql.Encoders.kryo[Map[String, Any]]
teenagerDF.map(t => t.getValuesMap[Any](Seq("name", "age"))).collect().foreach(println)
// +-------------+
// | value|
// +-------------+
// |name :Michael|
// +-------------+
spark.stop()

5.通过编程指定schema来创建DF

    val peopleRDD = sc.textFile("people.txt")
.map(_.split(",", -))
.map(arr => Row(arr().trim, arr().trim))
val schemaString = "name age"
val structfield = schemaString.split("\\s+")
.map(a => StructField(a, StringType, true))
val schema = StructType(structfield)
val peopleDF = spark.createDataFrame(peopleRDD, schema)
peopleDF.show()
// +-------+---+
// | name|age|
// +-------+---+
// |Michael| 15|
// | Andy| 30|
// | Justin| 19|
// +-------+---+

6.直接从file执行sql

    spark.sql("select name,age from json.`people.json`").show()
// +-------+----+
// | name| age|
// +-------+----+
// |Michael|null|
// | Andy| 30|
// | Justin| 19|
// +-------+----+

7.合并schema

    val squaresDF = spark.sparkContext.makeRDD( to ).map(i => (i, i * i)).toDF("value", "square")
squaresDF.write.parquet("data/test_table/key=1")
val cubesDF = spark.sparkContext.makeRDD( to ).map(i => (i, i * i * i)).toDF("value", "cube")
cubesDF.write.parquet("data/test_table/key=2")
val mergedDF = spark.read.option("mergeSchema", "true").parquet("data/test_table")
mergedDF.printSchema()
// root
// |-- value: integer (nullable = true)
// |-- square: integer (nullable = true)
// |-- cube: integer (nullable = true)
// |-- key: integer (nullable = true)
mergedDF.show()
// +-----+------+----+---+
// |value|square|cube|key|
// +-----+------+----+---+
// | 4| 16|null| 1|
// | 5| 25|null| 1|
// | 9| null| 729| 2|
// | 10| null|1000| 2|
// | 1| 1|null| 1|
// | 2| 4|null| 1|
// | 3| 9|null| 1|
// | 6| null| 216| 2|
// | 7| null| 343| 2|
// | 8| null| 512| 2|
// +-----+------+----+---+

8.dataframe 字符串拼接

    val squaresDF = spark.sparkContext.makeRDD( to ).map(i => (i, i * i)).toDF("value", "square")
squaresDF.createOrReplaceTempView("vs")
squaresDF.show()
squaresDF.map{case Row(key:Int,value:Int)=>s"$key$value"}.toDF("vv").show()
spark.sql("select concat(value,square) as vv from vs").show()

spark 基本操作(二)的更多相关文章

  1. Spark(二)算子详解

    目录 Spark(二)算子讲解 一.wordcountcount 二.编程模型 三.RDD数据集和算子的使用 Spark(二)算子讲解 @ 一.wordcountcount 基于上次的wordcoun ...

  2. Arduboy基本操作(二)

    Arduboy基本操作(二) 方向键控制物体移动 #include<Arduboy.h> Arduboy arduboy; int i,j; void setup() { arduboy. ...

  3. 分别使用Hadoop和Spark实现二次排序

    零.序(注意本部分与标题无太大关系,可直接调至第一部分) 既然没用为啥会有序?原因不想再开一篇文章,来抒发点什么感想或者计划了,就在这里写点好了: 前些日子买了几本书,打算学习和研究大数据方面的知识, ...

  4. spark的二次排序

    通过scala实现二次排序 package _core.SortAndTopN import org.apache.spark.{SparkConf, SparkContext} /** * Auth ...

  5. 大数据入门第二十二天——spark(二)RDD算子(2)与spark其它特性

    一.JdbcRDD与关系型数据库交互 虽然略显鸡肋,但这里还是记录一下(点开JdbcRDD可以看到限制比较死,基本是鸡肋.但好在我们可以通过自定义的JdbcRDD来帮助我们完成与关系型数据库的交互.这 ...

  6. 大数据入门第二十二天——spark(二)RDD算子(1)

    一.RDD概述 1.什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的 ...

  7. Spark(二)CentOS7.5搭建Spark2.3.1分布式集群

    一 下载安装包 1 官方下载 官方下载地址:http://spark.apache.org/downloads.html 2  安装前提 Java8         安装成功 zookeeper  安 ...

  8. spark streaming (二)

    一.基础核心概念 1.StreamingContext详解 (一) 有两种创建StreamingContext的方式:             val conf = new SparkConf().s ...

  9. spark 学习(二) RDD及共享变量

    声明:本文基于spark的programming guide,并融合自己的相关理解整理而成      Spark应用程序总是包括着一个driver program(驱动程序),它运行着用户的main方 ...

随机推荐

  1. Python - Win10系统下Python3.x环境配置

    Win10系统下Python3.x环境配置 https://blog.csdn.net/qq_41952474/article/details/82630551

  2. express和express-generator搭建web应用

    nodemon的安装使用 安装 npm i nodemon -D 配置 新建nodemon.json { "watch": "src/**/*.* } 修改package ...

  3. Linux 如何用命令查看binlog文件的创建时间

    目录 背景 分析 方法 注意 背景 MySQL在26日 16:23:49产生了大量的慢查询,在这段时间内,binlog文件刷新的很快(查看慢日志是mysql DML并发比较多),想知道写完一个binl ...

  4. Navicat Premium 12 永久使用办法

    1.按步骤安装Navicat Premium,如果没有可以去官网下载:http://www.navicat.com.cn/download/navicat-premium 2.安装好后下载激活文件:h ...

  5. LuoguP5540:【模板】最小乘积生成树(几何逼近)

    题意:给定N点,M边,每条边有两个属性(a,b),现在让你选N-1条边出来,然后使得∑a*∑b最小.N<200,M<1e4: 思路:我们把∑a看成x,∑b看成y,那么一个方案对应一个二维坐 ...

  6. Spring AOP技术本质认识

    Spring AOP技术本质认识 一.AOP简介   AOP(Aspect Oriented Programming,面向切面编程),把某一类问题集中在一个地方进行处理,比如处理程序中的点击事件.打印 ...

  7. MySQL体系结构与存储引擎

    MySQL 体系结构 先看 MySQL 数据库的体系结构,如下图所示. MySQL 体系结构由 Client Connectors 层.MySQL Server 层及存储引擎层组成. Client C ...

  8. 使用msRequestFullscreen全屏在IE 11无效

    问题产生原因:项目需要实现百度地图全屏的功能, 通过github上http://robnyman.github.io/fullscreen/这个demo初步实现在这个功能,在谷歌上也完美运行,而在ie ...

  9. JDOJ 2157 Increasing

    洛谷 P3902 递增 洛谷传送门 JDOJ 2157: Increasing JDOJ传送门 Description 数列A1,A2,--,AN,修改最少的数字,使得数列严格单调递增. Input ...

  10. 第二阶段冲刺(个人)——two

    今天的计划: 测试登录功能并优化. 昨天做了什么呢?  修改登录界面. 遇到的困难:一些标签运用不好,过程进度慢,改了又改.