Spark-SparkSQL深入学习系列十(转自OopsOutOfMemory)
/** Spark SQL源码分析系列文章*/
前面讲到了Spark SQL In-Memory Columnar Storage的存储结构是基于列存储的。
那么基于以上存储结构,我们查询cache在jvm内的数据又是如何查询的,本文将揭示查询In-Memory Data的方式。
一、引子
当我们将src表cache到了内存后,再次查询src,可以通过analyzed执行计划来观察内部调用。
即parse后,会形成InMemoryRelation结点,最后执行物理计划时,会调用InMemoryColumnarTableScan这个结点的方法。
如下:
- scala> val exe = executePlan(sql("select value from src").queryExecution.analyzed)
- 14/09/26 10:30:26 INFO parse.ParseDriver: Parsing command: select value from src
- 14/09/26 10:30:26 INFO parse.ParseDriver: Parse Completed
- exe: org.apache.spark.sql.hive.test.TestHive.QueryExecution =
- == Parsed Logical Plan ==
- Project [value#5]
- InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None)
- == Analyzed Logical Plan ==
- Project [value#5]
- InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None)
- == Optimized Logical Plan ==
- Project [value#5]
- InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None)
- == Physical Plan ==
- InMemoryColumnarTableScan [value#5], (InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None)) //查询内存中表的入口
- Code Generation: false
- == RDD ==
二、InMemoryColumnarTableScan
- private[sql] case class InMemoryColumnarTableScan(
- attributes: Seq[Attribute],
- relation: InMemoryRelation)
- extends LeafNode {
- override def output: Seq[Attribute] = attributes
- override def execute() = {
- relation.cachedColumnBuffers.mapPartitions { iterator =>
- // Find the ordinals of the requested columns. If none are requested, use the first.
- val requestedColumns = if (attributes.isEmpty) {
- Seq(0)
- } else {
- attributes.map(a => relation.output.indexWhere(_.exprId == a.exprId)) //根据表达式exprId找出对应列的ByteBuffer的索引
- }
- iterator
- .map(batch => requestedColumns.map(batch(_)).map(ColumnAccessor(_)))//根据索引取得对应请求列的ByteBuffer,并封装为ColumnAccessor。
- .flatMap { columnAccessors =>
- val nextRow = new GenericMutableRow(columnAccessors.length) //Row的长度
- new Iterator[Row] {
- override def next() = {
- var i = 0
- while (i < nextRow.length) {
- columnAccessors(i).extractTo(nextRow, i) //根据对应index和长度,从byterbuffer里取得值,封装到row里
- i += 1
- }
- nextRow
- }
- override def hasNext = columnAccessors.head.hasNext
- }
- }
- }
- }
- }
查询请求的列,如下:
- scala> exe.optimizedPlan
- res93: org.apache.spark.sql.catalyst.plans.logical.LogicalPlan =
- Project [value#5]
- InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None)
- scala> val relation = exe.optimizedPlan(1)
- relation: org.apache.spark.sql.catalyst.plans.logical.LogicalPlan =
- InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None)
- scala> val request_relation = exe.executedPlan
- request_relation: org.apache.spark.sql.execution.SparkPlan =
- InMemoryColumnarTableScan [value#5], (InMemoryRelation [key#4,value#5], false, 1000, (HiveTableScan [key#4,value#5], (MetastoreRelation default, src, None), None))
- scala> request_relation.output //请求的列,我们请求的只有value列
- res95: Seq[org.apache.spark.sql.catalyst.expressions.Attribute] = ArrayBuffer(value#5)
- scala> relation.output //默认保存在relation中的所有列
- res96: Seq[org.apache.spark.sql.catalyst.expressions.Attribute] = ArrayBuffer(key#4, value#5)
- scala> val attributes = request_relation.output
- attributes: Seq[org.apache.spark.sql.catalyst.expressions.Attribute] = ArrayBuffer(value#5)
- //根据exprId找出对应ID
- scala> val attr_index = attributes.map(a => relation.output.indexWhere(_.exprId == a.exprId))
- attr_index: Seq[Int] = ArrayBuffer(1) //找到请求的列value的索引是1, 我们查询就从Index为1的bytebuffer中,请求数据
- scala> relation.output.foreach(e=>println(e.exprId))
- ExprId(4) //对应<span style="font-family: Arial, Helvetica, sans-serif;">[key#4,value#5]</span>
- ExprId(5)
- scala> request_relation.output.foreach(e=>println(e.exprId))
- ExprId(5)
三、ColumnAccessor
ColumnAccessor对应每一种类型,类图如下:
最后返回一个新的迭代器:
- new Iterator[Row] {
- override def next() = {
- var i = 0
- while (i < nextRow.length) { //请求列的长度
- columnAccessors(i).extractTo(nextRow, i)//调用columnType.setField(row, ordinal, extractSingle(buffer))解析buffer
- i += 1
- }
- nextRow//返回解析后的row
- }
- override def hasNext = columnAccessors.head.hasNext
- }
四、总结
Spark SQL In-Memory Columnar Storage的查询相对来说还是比较简单的,其查询思想主要和存储的数据结构有关。
即存储时,按每列放到一个bytebuffer,形成一个bytebuffer数组。
查询时,根据请求列的exprId查找到上述数组的索引,然后使用ColumnAccessor对buffer中字段进行解析,最后封装为Row对象,返回。
——EOF——
创文章,转载请注明:
转载自:OopsOutOfMemory盛利的Blog,作者: OopsOutOfMemory
本文链接地址:http://blog.csdn.net/oopsoom/article/details/39577419
注:本文基于署名-非商业性使用-禁止演绎 2.5 中国大陆(CC BY-NC-ND 2.5 CN)协议,欢迎转载、转发和评论,但是请保留本文作者署名和文章链接。如若需要用于商业目的或者与授权方面的协商,请联系我。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spark-SparkSQL深入学习系列九(转自OopsOutOfMemory)
/**Spark SQL源码分析系列文章*/ SparkSQL 可以将数据缓存到内存中,我们可以见到的通过调用cache table tableName即可将一张表缓存到内存中,来极大的提高查询效率。 这就涉及到内存中的数据的存储形式,我们知道基于关系型的数据可以存储为基于行存储结构 或 者基于列存储结构,或者基于行和列的混合存储,即Row Based Storage、Column Based Storage、 PAX Storage。 Spark SQL 的内存数据是如何组织的? Spark SQL 将数据加载到内存是以列的存储结构。称为In-Memory Columnar Storage。 若直接存储JavaObject 会产生很大的内存开销,并且这样是基于Row的存储结构。查询某些列速度略慢,虽然数据以及载入内存,查询效率还是低于面向列的存储结构。 基于Row的Java Object存储: 内存开销大,且容易FULL GC,按列查询比较慢。 基于Column的ByteBuffer存储(Spark SQL): 内存开销小,按列查询速度较快。 Spark SQL的In-Me...
- 下一篇
Spark-SparkSQL深入学习系列十一(转自OopsOutOfMemory)
上周Spark1.2刚发布,周末在家没事,把这个特性给了解一下,顺便分析下源码,看一看这个特性是如何设计及实现的。 /**SparkSQL源码分析系列文章*/ (Ps: External DataSource使用篇地址:Spark SQL之External DataSource外部数据源(一)示例http://blog.csdn.net/oopsoom/article/details/42061077) 一、Sources包核心 Spark SQL在Spark1.2中提供了External DataSource API,开发者可以根据接口来实现自己的外部数据源,如avro, csv, json, parquet等等。 在Spark SQL源代码的org/spark/sql/sources目录下,我们会看到关于External DataSource的相关代码。这里特别介绍几个: 1、DDLParser 专门负责解析外部数据源SQL的SqlParser,解析createtemporarytable xxx using options (key 'value', key 'v...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- CentOS关闭SELinux安全模块
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7安装Docker,走上虚拟化容器引擎之路
- SpringBoot2整合Thymeleaf,官方推荐html解决方案