Search code examples
hiveapache-spark-sqlhortonworks-data-platform

org.apache.spark.sql.AnalysisException when calling saveAsTable


How can I resolve this error?

The code below works in Zeppelin but not when compiled into assembly jar and submitted with spark-submit.

Error is:

org.apache.spark.sql.AnalysisException: Specifying database name or other qualifiers are not allowed for temporary tables. If the table name has dots (.) in it, please quote the table name with backticks (`).;

Code:

    import org.apache.spark._
    import org.apache.spark.rdd.NewHadoopRDD
    import org.apache.spark.SparkContext
    import org.apache.spark.SparkContext._
    import org.apache.spark.SparkConf
    import org.apache.spark.sql.SQLContext
    import org.apache.spark.sql.hive.HiveContext 
    import java.text.SimpleDateFormat
    import java.util.Calendar  

    case class Benchmark(date: String, time: String, start_end: String, 
                         server: String, timestamp: Long, interface: String, 
                         cid: String, raw: String)

    object job {

        def main(args: Array[String]) {

            val sdf = new java.text.SimpleDateFormat("yyyyMMdd")
            val sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd")
            val calendar = Calendar.getInstance()
            calendar.set(Calendar.DAY_OF_YEAR, 
                         calendar.get(Calendar.DAY_OF_YEAR) -1)
            val date = sdf.format(calendar.getTime())
            val dt = sdf1.format(calendar.getTime())

            val conf = new SparkConf().setAppName("Interface_HtoH_Job")
            val sc = new SparkContext(conf)
            val sqlContext = new SQLContext(sc)
            import sqlContext.implicits._
            val hiveContext = new HiveContext(sc)

            val benchmarkText = sc.textFile(s"hdfs:/rawlogs/prod/log/${date}/*.gz")

            val pattern = "([0-9-]{10}) ([0-9:]{8}),[0-9]{1,3} Benchmark..* - (Start|End)<ID=([0-9a-zA-Z_]+)-([0-9]+)><([0-9a-zA-Z.,:!@() =_-]*)><cid=TaskId_([0-9A-Z#_a-z]+),.*><[,0-9:a-zA-Z ]+>".r

            benchmarkText.filter { ln => ln.startsWith("2017-") }
                         .filter { l => l.endsWith(">") }
                         .filter { k => k.contains("<cid=TaskId") }
                         .map { line =>
                                try {
                                    var pattern(date,time,startEnd,server,ts,interface,cid) = line
                                      Benchmark(date,time,startEnd,server,ts.toLong,interface,cid,line)

                                } catch {

                                    case e: Exception => Benchmark(dt,"00:00:00","bad",e.toString,"0".toLong,"bad","bad",line)

                                }

                              }.toDF()
                .write
                .mode("overwrite")
                .saveAsTable("prod_ol_bm.interface_benchmark_tmp") // error here
    }
}

Running using spark-submit on:

HDP : 2.5.3.0-37
Spark : 1.6.2.2.5.3.0-37 built for Hadoop 2.7.3.2.5.3.0-37

Solution

  • Change following line

    val sqlContext = new SQLContext(sc)
    

    to

    val sqlContext = new HiveContext(sc)
    

    Both shell and zeppelin create HiveContext with the name sqlContext, which is a little bit silly. You need HiveContext to connect to hive.