日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【原创】大叔经验分享(65)spark读取不到hive表

發(fā)布時(shí)間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【原创】大叔经验分享(65)spark读取不到hive表 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

spark 2.4.3

spark讀取hive表,步驟:

1)hive-site.xml

hive-site.xml放到$SPARK_HOME/conf下

2)enableHiveSupport

SparkSession.builder.enableHiveSupport().getOrCreate()

3) 測試代碼

    val sparkConf = new SparkConf().setAppName(getName)val sc = new SparkContext(sparkConf)val spark = SparkSession.builder.config(sparkConf).enableHiveSupport().getOrCreate()spark.sql("show databases").rdd.foreach(println)

使用$SPARK_HOME/bin/spark-submit提交任務(wù)后發(fā)現(xiàn)并不能讀取到hive的數(shù)據(jù)庫,相關(guān)日志如下

19/05/31 13:11:31 WARN SparkContext: Using an existing SparkContext; some configuration may not take effect.
19/05/31 13:11:31 INFO SharedState: loading hive config file: file:/export/spark-2.4.3-bin-hadoop2.6/conf/hive-site.xml
19/05/31 13:11:31 INFO SharedState: spark.sql.warehouse.dir is not set, but hive.metastore.warehouse.dir is set. Setting spark.sql.warehouse.dir to the value of hive.metastore.warehouse.dir ('/user/hive/warehouse').
19/05/31 13:11:31 INFO SharedState: Warehouse path is '/user/hive/warehouse'.
19/05/31 13:11:31 INFO StateStoreCoordinatorRef: Registered StateStoreCoordinator endpoin

說明已經(jīng)讀到hive-site.xml;

進(jìn)一步測試,使用$SPARK_HOME/bin/spark-sql或者$SPARK_HOME/bin/spark-shell發(fā)現(xiàn)都可以讀到hive數(shù)據(jù)庫,很神奇有沒有,

$SPARK_HOME/bin/spark-shell啟動(dòng)的類為org.apache.spark.repl.Main

"${SPARK_HOME}"/bin/spark-submit --class org.apache.spark.repl.Main --name "Spark shell" "$@"

跟進(jìn)org.apache.spark.repl.Main代碼

...val builder = SparkSession.builder.config(conf)if (conf.get(CATALOG_IMPLEMENTATION.key, "hive").toLowerCase(Locale.ROOT) == "hive") {if (SparkSession.hiveClassesArePresent) {// In the case that the property is not set at all, builder's config// does not have this value set to 'hive' yet. The original default// behavior is that when there are hive classes, we use hive catalog.sparkSession = builder.enableHiveSupport().getOrCreate()logInfo("Created Spark session with Hive support")} else {// Need to change it back to 'in-memory' if no hive classes are found// in the case that the property is set to hive in spark-defaults.confbuilder.config(CATALOG_IMPLEMENTATION.key, "in-memory")sparkSession = builder.getOrCreate()logInfo("Created Spark session")}} else {// In the case that the property is set but not to 'hive', the internal// default is 'in-memory'. So the sparkSession will use in-memory catalog.sparkSession = builder.getOrCreate()logInfo("Created Spark session")}sparkContext = sparkSession.sparkContextsparkSession
...

發(fā)現(xiàn)和測試代碼有些差異,關(guān)鍵是在倒數(shù)第二行,這里是先創(chuàng)建SparkSession,再從SparkSession中獲取SparkContext,另外注意到之前有個(gè)WARN級(jí)別的日志

19/05/31 13:11:31 WARN SparkContext: Using an existing SparkContext; some configuration may not take effect.

修改測試代碼

    val sparkConf = new SparkConf().setAppName(getName)//val sc = new SparkContext(sparkConf)val spark = SparkSession.builder.config(sparkConf).enableHiveSupport().getOrCreate()val sc = spark.sparkContextspark.sql("show databases").rdd.foreach(println)

這次果然ok了,詳細(xì)原因有空再看,未完待續(xù);

?

轉(zhuǎn)載于:https://www.cnblogs.com/barneywill/p/10959418.html

總結(jié)

以上是生活随笔為你收集整理的【原创】大叔经验分享(65)spark读取不到hive表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。