本文介绍了如何从SQL查询中获取表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Scala从Spark中的sql查询中获取所有表名.

I want to get all the tables names from a sql query in Spark using Scala.

假设用户发送了一个SQL查询,如下所示:

Lets say user sends a SQL query which looks like:

select * from table_1 as a left join table_2 as b on a.id=b.id

我想获取所有表列表,例如table_1table_2.

I would like to get all tables list like table_1 and table_2.

正则表达式是唯一的选择吗?

Is regex the only option ?

推荐答案

非常感谢@Swapnil Chougule提供的答案 .这启发了我提供一种惯用的方式来收集结构化查询中的所有表.

Thanks a lot @Swapnil Chougule for the answer. That inspired me to offer an idiomatic way of collecting all the tables in a structured query.

scala> spark.version
res0: String = 2.3.1

def getTables(query: String): Seq[String] = {
  val logicalPlan = spark.sessionState.sqlParser.parsePlan(query)
  import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation
  logicalPlan.collect { case r: UnresolvedRelation => r.tableName }
}

val query = "select * from table_1 as a left join table_2 as b on a.id=b.id"
scala> getTables(query).foreach(println)
table_1
table_2

这篇关于如何从SQL查询中获取表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:06