Support Questions

Find answers, ask questions, and share your expertise
Check out our newest addition to the community, the Cloudera Data Analytics (CDA) group hub.

Error while reading data from Hive JDBC in Spark2 from a remote cluster


I'm trying to read a hive table using the Hive JDBC url in spark. Given below are the steps:



import org.apache.spark.sql.{DataFrame, Row,SparkSession}

val connectionProperties = new java.util.Properties()

val hiveQuery = "(SELECT * from hive_table limit 10) tmp"

val hiveResult ="jdbc:hive2://hiveServerHostname:10000/hiveDBName;user=hive;password=hive", hiveQuery, connectionProperties).collect()

But when I check for the records in hiveResult it shows empty though this table has records when queried in Hive.

Hive Version - 1.2.10000 and Spark 2.0

Any suggestions on why the data is not being read from the Hive table?

I'm trying to read the data from a remote Hive cluster using Hive jdbc. Also, when I check in the logs a query which I used as below in the code:

(SELECT MIN(my_key) as lower, MAX(my_key) as upper FROM my_table)tmp gets converted to something like below:

SELECT "lower","upper" FROM (SELECT MIN(my_key) as lower, MAX(my_key) as upper FROM my_table)tmp

Any ideas around why does this happen and if it can be prevented i.e. if the above query can be fired without the double quotes through JDBC?



Rising Star

@Shikhar Agarwal

Have you tried reading the Hive table using SparkSession instead? The following code works for me using Spark 2 on HDP 2.5.3:

/usr/hdp/current/spark2-client/bin/spark-shell --num-executors 2 --executor-memory 512m --master yarn

import org.apache.spark.sql.Row
import org.apache.spark.sql.SparkSession

val hiveSession = SparkSession
.appName("Spark Hive Example")

val hiveResult = sql("SELECT * FROM timesheet LIMIT 10")

Details for this can be found in the Apache Spark docs:

Is it even possible to read the data from Hive using

New Contributor

@Shikhar Agarwal

Please mention fetchsize property, It will solve the issue of no data.

fetchsize - The JDBC fetch size, which determines how many rows to fetch per round trip. This can help performance on JDBC drivers which default to low fetch size (eg. Oracle with 10 rows). This option applies only to reading.

val jdbcDF =   
Take a Tour of the Community
Don't have an account?
Your experience may be limited. Sign in to explore more.