spark 基本操作(二)
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 基本操作(二)的更多相关文章
- Spark(二)算子详解
目录 Spark(二)算子讲解 一.wordcountcount 二.编程模型 三.RDD数据集和算子的使用 Spark(二)算子讲解 @ 一.wordcountcount 基于上次的wordcoun ...
- Arduboy基本操作(二)
Arduboy基本操作(二) 方向键控制物体移动 #include<Arduboy.h> Arduboy arduboy; int i,j; void setup() { arduboy. ...
- 分别使用Hadoop和Spark实现二次排序
零.序(注意本部分与标题无太大关系,可直接调至第一部分) 既然没用为啥会有序?原因不想再开一篇文章,来抒发点什么感想或者计划了,就在这里写点好了: 前些日子买了几本书,打算学习和研究大数据方面的知识, ...
- spark的二次排序
通过scala实现二次排序 package _core.SortAndTopN import org.apache.spark.{SparkConf, SparkContext} /** * Auth ...
- 大数据入门第二十二天——spark(二)RDD算子(2)与spark其它特性
一.JdbcRDD与关系型数据库交互 虽然略显鸡肋,但这里还是记录一下(点开JdbcRDD可以看到限制比较死,基本是鸡肋.但好在我们可以通过自定义的JdbcRDD来帮助我们完成与关系型数据库的交互.这 ...
- 大数据入门第二十二天——spark(二)RDD算子(1)
一.RDD概述 1.什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的 ...
- Spark(二)CentOS7.5搭建Spark2.3.1分布式集群
一 下载安装包 1 官方下载 官方下载地址:http://spark.apache.org/downloads.html 2 安装前提 Java8 安装成功 zookeeper 安 ...
- spark streaming (二)
一.基础核心概念 1.StreamingContext详解 (一) 有两种创建StreamingContext的方式: val conf = new SparkConf().s ...
- spark 学习(二) RDD及共享变量
声明:本文基于spark的programming guide,并融合自己的相关理解整理而成 Spark应用程序总是包括着一个driver program(驱动程序),它运行着用户的main方 ...
随机推荐
- 私钥、公钥与https
HTTP的安全缺陷 通信内容不加密,导致被窃听 不验证客户端和服务端的身份,导致: 服务器伪装 响应返回到了其他的客户端 海量恶意连接 无法证明报文的完整性,导致:请求和响应内容被篡改,这称为中间人攻 ...
- Flask基于websocket的简单聊天室
1.安装gevent-websocket pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ gevent-websocket 2.cha ...
- ElasticSearch之安装及基本操作API
ElasticSearch 是目前非常流行的搜索引擎,对海量数据搜索是非常友好,并且在高并发场景下,也能发挥出稳定,快速特点.也是大数据和索搜服务的开发人员所极力追捧的中间件.虽然 ElasticSe ...
- HighChat 动态绑定数据记录
最近刚开始做图形操作,纠结了一上午,highchat 动态绑定数据这块一直不知道怎么绑定,后来多次尝试,发现 1.x轴的数据是个数组格式,我从后台传到前台的时候,js中用数组进行处理数据,然后赋值到c ...
- 8、如何将本地新创建的项目上传到gitHub(gitLab)上
Git 常用命令 1. cd 项目目录 2.git init //变成git仓库 3.git add . //将项目添加到本地仓库 4.git commit -m '备注' //将项目提交 ...
- vue+element表单校验功能
要实现这个功能其实并不难,element组件直接用就可以, 但是我在使用过程中碰到了几个坑,就记录下来,分享给大家,避免落坑,话不多说,直接上过程...... 表单校验功能: 实现这个功能,总共分 ...
- WorkFlow四:添加用户决策步骤
沿用之前的例子,做个用户决策步骤. 1.事物代码SWDD: 进入抬头,点击类的绑定按钮. 2.选择类的绑定,点击继续. 这是类的绑定已经变色了.这时候点击保存,再点击返回到图片逻辑流界面. 3.在发送 ...
- using 中写 return 一样会释放using 中对象 但是会在外面定义一个一样的对象 赋值后 释放 最后 return 外面定义的那个对象
static DataTable getDataTable() { ")) { SqlCommand com = new SqlCommand("", con); Sql ...
- Linux Firewalld 基础实例
本次是一个Firewalld的基础操作实例,利用Firewalld图形操作界面进行访问控制操作. 实验拓扑 需求分析 首先拓扑涉及到两个区域,这里使用work和public区域,分别做相应的规则. 1 ...
- vue使用swiper模块滑动时报错:[Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example becaus
报错: vue报这个错 [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for ex ...