How to connect Spark SQL to remote Hive metastore (via thrift protocol) with no hive-site.xml?

后端 未结 8 1391
我寻月下人不归
我寻月下人不归 2020-11-22 12:07

I\'m using HiveContext with SparkSQL and I\'m trying to connect to a remote Hive metastore, the only way to set the hive metastore is through including the hive-site.xml on

8条回答
  •  鱼传尺愫
    2020-11-22 12:56

    I too faced same problem, but resolved. Just follow this steps in Spark 2.0 Version

    Step1: Copy hive-site.xml file from Hive conf folder to spark conf.

    Step 2: edit spark-env.sh file and configure your mysql driver. (If you are using Mysql as a hive metastore.)

    Or add MySQL drivers to Maven/SBT (If using those)

    Step3: When you are creating spark session add enableHiveSupport()

    val spark = SparkSession.builder.master("local").appName("testing").enableHiveSupport().getOrCreate()

    Sample code:

    package sparkSQL
    
    /**
      * Created by venuk on 7/12/16.
      */
    
    import org.apache.spark.sql.SparkSession
    
    object hivetable {
      def main(args: Array[String]): Unit = {
        val spark = SparkSession.builder.master("local[*]").appName("hivetable").enableHiveSupport().getOrCreate()
    
        spark.sql("create table hivetab (name string, age int, location string) row format delimited fields terminated by ',' stored as textfile")
        spark.sql("load data local inpath '/home/hadoop/Desktop/asl' into table hivetab").show()
        val x = spark.sql("select * from hivetab")
        x.write.saveAsTable("hivetab")
      }
    }
    

    Output:

提交回复
热议问题