如何使用pyspark和sparksession设置到配置单元的连接(如何添加用户名和密码)?

kulphzqa  于 2021-06-25  发布在  Hive
关注(0)|答案(2)|浏览(742)

我一直在尝试使用 PySpark 在阅读了其他一些帖子之后,这就是人们推荐的连接Hive的方式。但它不起作用。然后我意识到我可能必须传递我的用户名和密码,但我不知道怎么做。那么,有没有一种方法可以在设置时传递用户名和密码 SparkSession ,或者还有什么问题?

import sys
from pyspark import SparkContext, SparkConf, HiveContext
from pyspark.sql import SparkSession

if __name__ == "__main__":

# create Spark context with Spark configuration

spark = SparkSession.builder()
      .appName("interfacing spark sql to hive metastore without configuration file")
      .config("hive.metastore.uris", "thrift://my_server:10000")
      .enableHiveSupport()
      .getOrCreate()
sc = spark.sparkContext
df = sc.parallelize([(1, 2, 3, 'a b c'),(4, 5, 6, 'd e f'),(7, 8, 9, 'g h i')]).toDF(['col1', 'col2', 'col3','col4'])
df.write.mode("overwrite").saveAsTable("test_spark")

回溯

Exception in thread "main" org.apache.spark.SparkException: Application application_1575789516697_258641 finished with failed status
    at org.apache.spark.deploy.yarn.Client.run(Client.scala:1122)
    at org.apache.spark.deploy.yarn.Client$.main(Client.scala:1168)
    at org.apache.spark.deploy.yarn.Client.main(Client.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:780)
    at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:180)
    at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:205)
    at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:119)
    at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
yr9zkbsy

yr9zkbsy1#

spark直接连接到hive,无需传递用户名和密码,只需在提交spark应用程序时传递hive-site.xml即可。
使用下面的代码,

from pyspark.sql import SparkSession

   sparkSession = SparkSession.builder.appName("ApplicationName").enableHiveSupport().getOrCreate()

在提交应用程序时,传递hive-site.xml文件,如下所示:,

spark-submit --files /<location>/hive-site.xml --py-files <List_of_Pyfiles>
u2nhd7ah

u2nhd7ah2#

尝试将下面的内容添加到配置中

.config("spark.sql.warehouse.dir", your_warehouse_location)

以此为参考。

相关问题