spark SQL随笔
sparkSQL
1、主要的数据结构
DataFreames
2、开始使用:SQLContext
创建步骤:
Val sc:sparkContext
Val sqlContext=new org.apache.spark.sql.SQLContext(sc)
Import sqlContext.implicits._ //隐形将RDD转化DF
3、构建DF及DF 操作
Val sc:SparkContext
Val Val sqlContext=new org.apache.spark.sql.SQLContext(sc)
Val df = sqlContext.jsonFile(“/people.json”)
0) df.show
1) df.printSchema()
2) df.select(“name”).show
3) df.select(df(“name”),df(“age”)).show
4) df.filter(df(“age”)>21).show
5)df.groupBy(“age”).count().show
4、RDDs
Spark支持两种不同的方法将现有的RDDs转化为SchemaRDD
1) 使用反射(reflection)来推断包含类型对象的RDD的格式,这种基于反射方法使得代码更简洁且运行良好,因为当你写spark应用时,你早已经知道他的格式了
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
case class Person(name: String, age: Int)
val people = sc.textFile("examples/src/main/resources/people.txt").map(_.split(",")).map(p => Person(p(0), p(1).trim.toInt)).toDF()
people.registerTempTable("people")
val teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
teenagers.map(t => "Name: " + t(0)).collect().foreach(println)
2)通过一个编程接口,允许你构建一种格式,然后将类型时其应用到现在的RDD,虽然这种方法比较繁琐,但可以让你不知道RDD的列和他们的类型时构建SchemaRDDs
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
// Create an RDD
val people = sc.textFile("examples/src/main/resources/people.txt")
// The schema is encoded in a string
val schemaString = "name age"
// Import Row.
import org.apache.spark.sql.Row;
// Import Spark SQL data types
import org.apache.spark.sql.types.{StructType,StructField,StringType};
// Generate the schema based on the string of schema
val schema = StructType(schemaString.split(" ").map(fieldName => StructField(fieldName, StringType, true)))
// Convert records of the RDD (people) to Rows.
val rowRDD = people.map(_.split(",")).map(p => Row(p(0), p(1).trim))
// Apply the schema to the RDD.
val peopleDataFrame = sqlContext.createDataFrame(rowRDD, schema)
// Register the DataFrames as a table.
peopleDataFrame.registerTempTable("people")
// SQL statements can be run by using the sql methods provided by sqlContext.
val results = sqlContext.sql("SELECT name FROM people")
// The results of SQL queries are DataFrames and support all the normal RDD operations.
// The columns of a row in the result can be accessed by ordinal.
results.map(t => "Name: " + t(0)).collect().foreach(println)
5. 数据源
1)、加载和保存(load和save)
Val df=sqlCotext.load(“people.parquet”)
df.select(“name”,”age”).save(“namesAndAges.parquet”)
2) 格式选择
1. 文件类型
Val df=sqlCotext.load(“people.parquet”)
df.select(“name”,”age”).save(“namesAndAges.parquet”,”parquet”)
2. 保存方式
SaveMode.ErrorIfExists (default)
SaveMode.Append
SaveMode.Overwrite
SaveMode.Ignore
Val df=sqlCotext.load(“people.parquet”)
df.select(“name”,”age”).save(“namesAndAges.parquet”,”parquet”,SaveMode.append)
spark SQL随笔的更多相关文章
- Spark SQL 之 Data Sources
#Spark SQL 之 Data Sources 转载请注明出处:http://www.cnblogs.com/BYRans/ 数据源(Data Source) Spark SQL的DataFram ...
- Spark SQL 之 DataFrame
Spark SQL 之 DataFrame 转载请注明出处:http://www.cnblogs.com/BYRans/ 概述(Overview) Spark SQL是Spark的一个组件,用于结构化 ...
- 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...
- Spark 官方文档(5)——Spark SQL,DataFrames和Datasets 指南
Spark版本:1.6.2 概览 Spark SQL用于处理结构化数据,与Spark RDD API不同,它提供更多关于数据结构信息和计算任务运行信息的接口,Spark SQL内部使用这些额外的信息完 ...
- Spark SQL Example
Spark SQL Example This example demonstrates how to use sqlContext.sql to create and load a table ...
- 通过Spark SQL关联查询两个HDFS上的文件操作
order_created.txt 订单编号 订单创建时间 -- :: -- :: -- :: -- :: -- :: order_picked.txt 订单编号 订单提取时间 -- :: ...
- 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 之 Performance Tuning & Distributed SQL Engine
Spark SQL 之 Performance Tuning & Distributed SQL Engine 转载请注明出处:http://www.cnblogs.com/BYRans/ 缓 ...
随机推荐
- int*p[ ]与int(*p)[ ]的不同
举例说明: 1)int* p[2] 是一个指向int型的指针数组,即:p是包含两个元素的指针数组,指针指向的是int型. 可以这样来用: #include <iostream> using ...
- UseCase事件流描述规范
文/fasiondog 整理需求用例的编写规范,分享部分UseCase事件流描述规范.其中,准则5~10.12来自<编写有效用例>([美] Alistair Cockburn 著)一书,其 ...
- 小强的HTML5移动开发之路(5)——制作一个漂亮的视频播放器
来自:http://blog.csdn.net/dawanganban/article/details/17679069 在前面几篇文章中介绍了HTML5的特点和需要掌握的基础知识,下面我们开始真正的 ...
- mysql进阶(十九)SQL语句如何精准查找某一时间段的数据
SQL语句如何精准查找某一时间段的数据 在项目开发过程中,自己需要查询出一定时间段内的交易.故需要在sql查询语句中加入日期时间要素,sql语句如何实现? SELECT * FROM lmapp.lm ...
- Java集合之Stack
Stack是栈,特性是先进后出(FILO,First In Last Out).Stack是继承于Vector(矢量队列),由于Vector是同数组实现的,Stack也是通过数组而非链表. Stack ...
- STL算法设计理念 - 二元函数,二元谓词以及在set中的应用
demo 二元函数对象 #include <iostream> #include <cstdio> #include <vector> #include <a ...
- Android电话拦截实现以及TelephonyManager监听的取消
由于毕业设计题目涉及到电话拦截这一块.所以鼓捣了一下.遇到了一些问题,总结一下,以免忘记,也希望能帮助其他新人们. 本篇博客目的:实现电话的拦截 会遇到的问题:android studio下AIDL的 ...
- OpenCV特征点提取----Fast特征
1.FAST(featuresfrom accelerated segment test)算法 http://blog.csdn.net/yang_xian521/article/details/74 ...
- Java开发机器上的配置及zookeeper配置
Java开发机器上的配置及zookeeper配置 /etc/profile 文件的后面加入下面的内容: # jdk, zookeeper, kafka, ant, maven export APACH ...
- TCP的核心系列 — ACK的处理(二)
本文主要内容:tcp_ack()中的一些细节,如发送窗口的更新.持续定时器等. 内核版本:3.2.12 Author:zhangskd @ csdn 发送窗口的更新 什么时候需要更新发送窗口呢? (1 ...