How to get table names from SQL query?

前端 未结 6 1522
太阳男子
太阳男子 2020-12-18 04:48

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

Lets say user sends a SQL query which looks like:

select * from table_1 as         


        
6条回答
  •  执念已碎
    2020-12-18 04:49

    Hope it will help you

    Parse the given query using spark sql parser (spark internally does same). You can get sqlParser from session's state. It will give Logical plan of query. Iterate over logical plan of query & check whether it is instance of UnresolvedRelation (leaf logical operator to represent a table reference in a logical query plan that has yet to be resolved) & get table from it.

    def getTables(query: String) : Seq[String] ={
        val logical : LogicalPlan = localsparkSession.sessionState.sqlParser.parsePlan(query)
        val tables = scala.collection.mutable.LinkedHashSet.empty[String]
        var i = 0
        while (true) {
          if (logical(i) == null) {
            return tables.toSeq
          } else if (logical(i).isInstanceOf[UnresolvedRelation]) {
            val tableIdentifier = logical(i).asInstanceOf[UnresolvedRelation].tableIdentifier
            tables += tableIdentifier.unquotedString.toLowerCase
          }
          i = i + 1
        }
        tables.toSeq
    }
    

提交回复
热议问题