Search code examples
apache-sparkhivethrift

How to connect to hive databases in spark Using Java


I am able to connect to hive using hive.metastore.uris in Sparksession. What I want is to connect to a particular database of hive with this connection so that I don't need to add database name to each table names in queries. Is there any way to achieve this ?

Expecting code something like

SparkSession sparkSession = SparkSession.config("hive.metastore.uris", "thrift://dhdhdkkd136.india.sghjd.com:9083/hive_database")

Solution

  • You can use the catalog API accessible from the SparkSession.

    https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.catalog.Catalog

    You can then call sparkSession.catalog.setCurrentDatabase(<db_name>)