Spark笔记之Catalog
一、什么是Catalog
Spark SQL提供了执行sql语句的支持,sql语句是以表的方式组织使用数据的,而表本身是如何组织存储的呢,肯定是存在一些元数据之类的东西了,Catalog就是Spark 2.0之后提供的访问元数据的类:
Catalog提供一些API用来对数据库、表、视图、缓存、列、函数(UDF/UDAF)进行操作,下文将一一介绍。
二、如何使用Catalog
得到Catalog:
1 2 | val spark = SparkSession.builder().master( "local[*]" ).appName( "catalog-study" ).getOrCreate() val catalog = spark.catalog |
Catalog相关的代码存放在org.apache.spark.sql.catalog下:
上面的Catalog只是一个接口定义规范,具体实现还有一个org.apache.spark.sql.internal.CatalogImpl,如果只是使用Spark完成工作的话只阅读接口定义基本够用了。
三、相关API
数据库相关
看数据库相关的操作之前先看一下Catalog对数据库的表示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * A database in Spark, as returned by the `listDatabases` method defined in [[Catalog]]. * * @param name name of the database. * @param description description of the database. * @param locationUri path (in the form of a uri) to data files. * @since 2.0.0 */ @InterfaceStability .Stable class Database( val name: String, @Nullable val description: String, val locationUri: String) extends DefinedByConstructorParams { override def toString: String = { "Database[" + s "name='$name', " + Option(description).map { d => s "description='$d', " }.getOrElse( "" ) + s "path='$locationUri']" } } |
Catalog使用三个字段表示一个数据库:
name:数据库名字
descripttion:数据库描述,可以认为是注释
locationUri:数据库的数据保存位置
1 | currentDatabase: String |
返回当前使用的数据库,相当于select database();
1 | setCurrentDatabase(dbName: String): Unit |
1 | listDatabases(): Dataset[Database] |
查看所有数据库,相当于show databases;
1 | getDatabase(dbName: String): Database |
获取某数据库的元数据,返回值是Database类型的,如果指定的数据库不存在则会@throws[AnalysisException]("database does not exist")
1 | databaseExists(dbName: String): Boolean |
判断某个数据库是否已经存在,返回boolean值。
为了避免抛异常对单个数据库进行getDatabase获取元数据之前还是先使用databaseExists确定数据库已经存在。
表/视图相关
同样的,对表或视图Catalog也用一个class来表示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * A table in Spark, as returned by the `listTables` method in [[Catalog]]. * * @param name name of the table. * @param database name of the database the table belongs to. * @param description description of the table. * @param tableType type of the table (e.g. view, table). * @param isTemporary whether the table is a temporary table. * @since 2.0.0 */ @InterfaceStability .Stable class Table( val name: String, @Nullable val database: String, @Nullable val description: String, val tableType: String, val isTemporary: Boolean) extends DefinedByConstructorParams { override def toString: String = { "Table[" + s "name='$name', " + Option(database).map { d => s "database='$d', " }.getOrElse( "" ) + Option(description).map { d => s "description='$d', " }.getOrElse( "" ) + s "tableType='$tableType', " + s "isTemporary='$isTemporary']" } } |
name:表的名字
database:表所属的数据库的名字
description:表的描述信息
tableType:用于区分是表还是视图,两个取值:table或view。
isTemporary:是否是临时表或临时视图,解释一下啥是临时表,临时表就是使用Dataset或DataFrame的createOrReplaceTempView等类似的API注册的视图或表,当此次Spark任务结束后这些表就没了,再次使用的话还要再进行注册,而非临时表就是在Hive中真实存在的,开启Hive支持就能够直接使用的,本次Spark任务结束后表仍然能存在,下次启动不需要重新做任何处理就能够使用,表是持久的,这种不是临时表。
1 | listTables(): Dataset[Table] |
查看所有表或视图,相当于show tables;
1 | listTables(dbName: String): Dataset[Table] |
返回指定数据库下的表或视图,如果指定的数据库不存在则会抛出@throws[AnalysisException]("database does not exist")表示数据库不存在。
1 2 | getTable(tableName: String): Table getTable(dbName: String, tableName: String): Table |
获取表的元信息,不存在则会抛出异常。
1 2 | tableExists(tableName: String): Boolean tableExists(dbName: String, tableName: String): Boolean |
判断表或视图是否存在,返回boolean值。
1 2 | dropTempView(viewName: String): Boolean dropGlobalTempView(viewName: String): Boolean |
使用createOrReplaceTempView类似API注册的临时视图可以使用此方法删除,如果这个视图已经被缓存过的话会自动清除缓存。
1 | recoverPartitions(tableName: String): Unit |
1 | isCached(tableName: String): Boolean |
用于判断一个表否已经缓存过了。
1 2 | cacheTable(tableName: String): Unit cacheTable(tableName: String, storageLevel: StorageLevel): Unit |
1 | uncacheTable(tableName: String): Unit |
对表取消缓存
1 | clearCache(): Unit |
清空所有缓存
1 | refreshTable(tableName: String): Unit |
Spark为了性能考虑,对表的元数据做了缓存,所以当被缓存的表已经改变时也必须刷新元数据重新缓存。
1 | refreshByPath(path: String): Unit |
1 2 3 4 5 6 | createTable(tableName: String, path: String): DataFrame createTable(tableName: String, path: String, source: String): DataFrame createTable(tableName: String, source: String, options: java.util.Map[String, String]): DataFrame createTable(tableName: String, source: String, options: Map[String, String]): DataFrame createTable(tableName: String, source: String, schema: StructType, options: java.util.Map[String, String]): DataFrame createTable(tableName: String, source: String, schema: StructType, options: Map[String, String]): DataFrame |
函数相关
Catalog对函数的表示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * A user-defined function in Spark, as returned by `listFunctions` method in [[Catalog]]. * * @param name name of the function. * @param database name of the database the function belongs to. * @param description description of the function; description can be null. * @param className the fully qualified class name of the function. * @param isTemporary whether the function is a temporary function or not. * @since 2.0.0 */ @InterfaceStability .Stable class Function( val name: String, @Nullable val database: String, @Nullable val description: String, val className: String, val isTemporary: Boolean) extends DefinedByConstructorParams { override def toString: String = { "Function[" + s "name='$name', " + Option(database).map { d => s "database='$d', " }.getOrElse( "" ) + Option(description).map { d => s "description='$d', " }.getOrElse( "" ) + s "className='$className', " + s "isTemporary='$isTemporary']" } } |
name:函数的名字
database:函数注册在哪个数据库下,函数是跟数据库绑定的
description:对函数的描述信息,可以理解成注释
className:函数其实就是一个class,调用函数就是调用类的方法,className表示函数对应的class的全路径类名
isTemporary:是否是临时函数。
1 | listFunctions(): Dataset[Function] |
列出当前数据库下的所有函数,包括注册的临时函数。
1 | listFunctions(dbName: String): Dataset[Function] |
列出指定数据库下注册的所有函数,包括临时函数,如果指定的数据库不存在的话则会抛出@throws[AnalysisException]("database does not exist")表示数据库不存在。
1 2 | getFunction(functionName: String): Function getFunction(dbName: String, functionName: String): Function |
获取函数的元信息,函数不存在则会抛出异常。
1 2 | functionExists(functionName: String): Boolean functionExists(dbName: String, functionName: String): Boolean |
对表或视图的列相关的操作
Catalog对列的表示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** * A column in Spark, as returned by `listColumns` method in [[Catalog]]. * * @param name name of the column. * @param description description of the column. * @param dataType data type of the column. * @param nullable whether the column is nullable. * @param isPartition whether the column is a partition column. * @param isBucket whether the column is a bucket column. * @since 2.0.0 */ @InterfaceStability .Stable class Column( val name: String, @Nullable val description: String, val dataType: String, val nullable: Boolean, val isPartition: Boolean, val isBucket: Boolean) extends DefinedByConstructorParams { override def toString: String = { "Column[" + s "name='$name', " + Option(description).map { d => s "description='$d', " }.getOrElse( "" ) + s "dataType='$dataType', " + s "nullable='$nullable', " + s "isPartition='$isPartition', " + s "isBucket='$isBucket']" } } |
name:列的名字
description:列的描述信息,与注释差不多
dataType:列的数据类型
nullable:列是否允许为null
isPartition:是否是分区列
isBucket:是否是桶列
1 2 | listColumns(tableName: String): Dataset[Column] listColumns(dbName: String, tableName: String): Dataset[Column] |
列出指定的表或视图有哪些列,表不存在则抛异常。
相关资料:
1. Spark 2.0介绍:Catalog API介绍和使用
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架