python - 在 Python 中以编程方式启动 HiveThriftServer

标签 python scala hive thrift hivecontext

在 spark-shell (scala) 中,我们导入, org.apache.spark.sql.hive.thriftserver._ 为特定的配置单元上下文以编程方式启动 Hive Thrift 服务器 HiveThriftServer2.startWithContext(hiveContext) 为该特定 session 公开注册的临时表。

我们如何使用 python 做同样的事情? python 上是否有用于导入 HiveThriftServer 的包/api?任何其他想法/建议表示赞赏。

我们使用 pyspark 创建数据框

谢谢

拉维·纳拉亚南

最佳答案

您可以使用 py4j java 网关导入它。以下代码适用于 spark 2.0.2,可以通过直线查询在 python 脚本中注册的临时表。

from py4j.java_gateway import java_import
java_import(sc._gateway.jvm,"")

spark = SparkSession \
        .builder \
        .appName(app_name) \
        .master(master)\
        .enableHiveSupport()\
        .config('spark.sql.hive.thriftServer.singleSession', True)\
        .getOrCreate()
sc=spark.sparkContext
sc.setLogLevel('INFO')

#Start the Thrift Server using the jvm and passing the same spark session corresponding to pyspark session in the jvm side.
sc._gateway.jvm.org.apache.spark.sql.hive.thriftserver.HiveThriftServer2.startWithContext(spark._jwrapped)

spark.sql('CREATE TABLE myTable')
data_file="path to csv file with data"
dataframe = spark.read.option("header","true").csv(data_file).cache()
dataframe.createOrReplaceTempView("myTempView")

然后去beeline检查是否正确启动:

in terminal> $SPARK_HOME/bin/beeline
beeline> !connect jdbc:hive2://localhost:10000
beeline> show tables;

它应该显示在 python 中创建的表和临时表/ View ,包括上面的“myTable”和“myTempView”。必须有相同的 spark session 才能看到临时 View

(参见答案:Avoid starting HiveThriftServer2 with created context programmatically
注意:即使 Thrift 服务器从终端启动并连接到同一个 Metastore,也可以访问 Hive 表,但是无法访问临时 View ,因为它们在 Spark session 中并且没有写入 Metastore)

关于python - 在 Python 中以编程方式启动 HiveThriftServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36628918/

相关文章:

hive - 在 Hive 中获取数据的交集

amazon-web-services - 将数据从 Hive 写入 Amazon S3,同时保持表分区目录结构

python - 使用 ThreadPoolExecutor 时记录线程

Python 在字典中搜索键时仅使用字符串中的第一个字母

python - NLTK 数据安装问题

scala - 以单个值收集多个偏函数的结果?

hadoop - 如何使用PIG/HIVE/HBASE从文件动态加载没有文本限定符的数据?

python - 如何使用python请求修改本地存储

Scala Liftweb - 模式类型与预期类型不兼容

scala - 隐式函数无法注入(inject)