[英]Check if a table exists in Hive in pyspark sparksession
I have the following code.我有以下代码。 What I am trying to achieve is to check if a table already exists in a database in Hive.我想要实现的是检查 Hive 中的数据库中是否已存在表。 If yes, then append new data into.如果是,则将新数据附加到。 If not then create a new table.如果没有,则创建一个新表。
if "table1" in sqlContext.tableNames("db1"):
df.write.format("orc").mode("append").insertInto("db1.table1")
else:
df.write.format("orc").saveAsTable("db1.table1")
But I am getting this error:但我收到此错误:
NameError: name 'sqlContext' is not defined
I am using spark session in Pyspark with Jupyter Notebook:我正在使用 Jupyter Notebook 在 Pyspark 中使用 spark 会话:
spark = SparkSession.builder.config(conf=conf).enableHiveSupport().getOrCreate()
How can I fix the error?我该如何修复错误?
It depends on your Apache Spark version.这取决于您的 Apache Spark 版本。 If your Spark version is 1.x then you need to do as follows:如果您的 Spark 版本是 1.x,那么您需要执行以下操作:
from pyspark.sql import SQLContext
sqlContext = SQLContext(sc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.