SparkSQL中DataFrame registerTempTable源码浅析

dataFrame.registerTempTable(tableName);
  最近在使用SparkSQL时想到1万条数据注册成临时表和1亿条数据注册成临时表时,效率上是否会有很大的差距,也对DataFrame注册成临时表到底做了哪些比较好奇,拿来源码拜读了下相关部分,记录一下。

 

临时表的生命周期是和创建该DataFrame的SQLContext有关系的,SQLContext生命周期结束,该临时表的生命周期也结束了
 

DataFrame.scala相关源码
 /**
   * Registers this [[DataFrame]] as a temporary table using the given name.  The lifetime of this
   * temporary table is tied to the [[SQLContext]] that was used to create this DataFrame.
   *
   * @group basic
   * @since 1.3.0
   */
  def registerTempTable(tableName: String): Unit = {
    sqlContext.registerDataFrameAsTable(this, tableName)
  }

  
 DataFrame中的registerTempTable调用SQLContext中的registerDataFrameAsTable,
 SQLContext中使用SimpleCatalog类去实现Catalog接口中的registerTable方法.
 

SQLContext.scala相关源码
  @transient
  protected[sql] lazy val catalog: Catalog = new SimpleCatalog(conf)
  /**
   * Registers the given [[DataFrame]] as a temporary table in the catalog. Temporary tables exist
   * only during the lifetime of this instance of SQLContext.
   */
  private[sql] def registerDataFrameAsTable(df: DataFrame, tableName: String): Unit = {
    catalog.registerTable(Seq(tableName), df.logicalPlan)
  }
    
    在SimpleCatalog中定义了Map,registerTable中按tableIdentifier为key,logicalPlan为Value注册到名为tables的map中


  Catalog.scala相关源码
  val tables = new mutable.HashMap[String, LogicalPlan]()
  override def registerTable(
      tableIdentifier: Seq[String],
      plan: LogicalPlan): Unit = {
    val tableIdent = processTableIdentifier(tableIdentifier)
    tables += ((getDbTableName(tableIdent), plan))
  }
 
  protected def processTableIdentifier(tableIdentifier: Seq[String]): Seq[String] = {
    if (conf.caseSensitiveAnalysis) {
      tableIdentifier
    } else {
      tableIdentifier.map(_.toLowerCase)
    }
  }
 
  protected def getDbTableName(tableIdent: Seq[String]): String = {
    val size = tableIdent.size
    if (size <= 2) {
      tableIdent.mkString(".")
    } else {
      tableIdent.slice(size - 2, size).mkString(".")
    }
  }

  阅读以上代码,最终registerTempTable是将表名(或表的标识)和对应的逻辑计划加载到Map中,并随着SQLContext的消亡而消亡

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SparkSQLDataFrame是一种分布式数据集合,它提供了一系列的高级操作来处理结构化数据。DataFrame可以很方便地写入到外部存储系统,比如Hive表或者其他支持的数据库和文件系统。以下是使用DataFrame将数据写入表的基本步骤: 1. 准备DataFrame:首先你需要有一个Spark DataFrame,这个DataFrame可以是通过各种方式创建的,比如从外部数据源读取数据,或者通过转换现有的RDD或Dataset得到。 2. 指定写入选项:根据你的需求,你可能需要指定一些写入选项,比如数据写入的格式、分隔符、压缩方式、是否允许覆盖等。 3. 使用write方法:调用DataFrame的write方法,并传入之前指定的写入选项,最后指定写入模式。常见的写入模式有`append`(追加)、`overwrite`(覆盖)、`ignore`(忽略)和`errorIfExists`(存在错误)。 4. 指定表名:使用`into`方法指定要写入的目标表名。 下面是一个使用DataFrame将数据写入Hive表的示例代码: ```scala // 假设df是一个已经存在的DataFrame val tableName = "your_table_name" // 你想要写入的Hive表名 // 将DataFrame写入Hive表,假设是追加模式 df.write .format("hive") // 指定使用Hive格式 .option("codec", "org.apache.hadoop.io.compress.GzipCodec") // 指定压缩编解码器 .saveAsTable(tableName) // 保存为Hive表 ``` 上面的代码展示了如何将DataFrame保存为Hive表。如果要写入其他类型的表或文件系统,需要相应地调整`format`方法的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值