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

后端 未结 8 1363
我寻月下人不归
我寻月下人不归 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 13:02

    In spark 2.0.+ it should look something like that:

    Don't forget to replace the "hive.metastore.uris" with yours. This assume that you have a hive metastore service started already (not a hiveserver).

     val spark = SparkSession
              .builder()
              .appName("interfacing spark sql to hive metastore without configuration file")
              .config("hive.metastore.uris", "thrift://localhost:9083") // replace with your hivemetastore service's thrift url
              .enableHiveSupport() // don't forget to enable hive support
              .getOrCreate()
    
            import spark.implicits._
            import spark.sql
            // create an arbitrary frame
            val frame = Seq(("one", 1), ("two", 2), ("three", 3)).toDF("word", "count")
            // see the frame created
            frame.show()
            /**
             * +-----+-----+
             * | word|count|
             * +-----+-----+
             * |  one|    1|
             * |  two|    2|
             * |three|    3|
             * +-----+-----+
             */
            // write the frame
            frame.write.mode("overwrite").saveAsTable("t4")
    

提交回复
热议问题