Start HiveThriftServer programmatically in Python
我们在spark-shell(scala)中导入,
org.apache.spark.sql.hive.thriftserver._
用于针对特定配置单元上下文以编程方式启动配置单元Thrift服务器
HiveThriftServer2.startWithContext(hiveContext)公开该特定会话的已注册临时表。
我们如何使用python做同样的事情?在python上是否有用于导入HiveThriftServer的软件包/ API?任何其他想法/建议都表示赞赏。
我们已使用pyspark创建数据框
谢谢
Ravi Narayanan
您可以使用py4j java网关导入它。以下代码适用于spark 2.0.2,并且可以通过beeline查询在python脚本中注册的临时表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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以检查其是否正确:
1 2 3 | in terminal> $SPARK_HOME/bin/beeline beeline> !connect jdbc:hive2://localhost:10000 beeline> show tables; |
它应该显示用python创建的表和临时表/视图,包括上面的" myTable "和" myTempView "。必须具有相同的spark会话才能看到临时视图
(请参阅ans:避免以编程方式在创建的上下文中启动HiveThriftServer2。
注意:即使Thrift服务器是从终端启动并连接到同一metastore的,也可以访问hive表,但是无法访问临时视图,因为它们在spark会话中并且没有被写入metastore)