logan
logan

Reputation: 8346

hive table does not exist in spark job

I am using Hive Metastore in EMR.
I am able to query the table manually through HiveSQL or SparkSQL.
But When i use the same table in Spark Job, it says Table or view not found

File "/usr/lib/spark/python/lib/pyspark.zip/pyspark/sql/utils.py", line 69, in deco pyspark.sql.utils.AnalysisException: 
  u"Table or view not found: `logan_test`.`salary_csv`; line 1 pos 21;
'Aggregate [unresolvedalias(count(1), None)]
+- 'UnresolvedRelation `logan_test`.`salary_csv`

Here is my full code

from pyspark import SparkContext, HiveContext
from pyspark import SQLContext
from pyspark.sql import SparkSession

sc = SparkContext(appName = "test")
sqlContext = SQLContext(sparkContext=sc)
sqlContext.sql("select count(*) from logan_test.salary_csv").show()
print("done..")

I submitted my job as below to use hive catalog tables.

spark-submit test.py --files /usr/lib/hive/conf/hive-site.xml

Upvotes: 3

Views: 3993

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191681

Looks like you're using Spark 2, therefore SQLContext and HiveContext should be replaced with SparkSession.sql() after you enableHiveSupport()

And instead of .sql(), you can use SparkSession.table() to get a DataFrame of the entire table, then follow it with a count(), then whatever other queries you want.

from pyspark.sql import SparkSession

spark = SparkSession.builder.enableHiveSupport().appName("Hive Example").getOrCreate()
salary_csv = spark.table("logan_test.salary_csv")
print(salary_csv.count())

Upvotes: 2

Alper t. Turker
Alper t. Turker

Reputation: 35219

You imported HiveContext but initialized standard SQLContext which doesn't support Hive queries. It should be:

sqlContext = HiveContext(sparkContext=sc)

Upvotes: 2

Related Questions