@@ -27,8 +27,10 @@ import scala.language.reflectiveCalls
2727import org .apache .hadoop .fs .Path
2828import org .apache .hadoop .hive .metastore .api .Database
2929import org .apache .hadoop .hive .conf .HiveConf
30+ import org .apache .hadoop .hive .metastore .api
3031import org .apache .hadoop .hive .metastore .api .FieldSchema
31- import org .apache .hadoop .hive .ql .metadata ._
32+ import org .apache .hadoop .hive .ql .metadata
33+ import org .apache .hadoop .hive .ql .metadata .Hive
3234import org .apache .hadoop .hive .ql .session .SessionState
3335import org .apache .hadoop .hive .ql .processors ._
3436import org .apache .hadoop .hive .ql .Driver
@@ -65,13 +67,6 @@ class ClientWrapper(
6567 conf.set(k, v)
6668 }
6769
68- private def properties = Seq (
69- " javax.jdo.option.ConnectionURL" ,
70- " javax.jdo.option.ConnectionDriverName" ,
71- " javax.jdo.option.ConnectionUserName" )
72-
73- properties.foreach(p => logInfo(s " Hive Configuration: $p = ${conf.get(p)}" ))
74-
7570 // Circular buffer to hold what hive prints to STDOUT and ERR. Only printed when failures occur.
7671 private val outputBuffer = new java.io.OutputStream {
7772 var pos : Int = 0
@@ -117,7 +112,10 @@ class ClientWrapper(
117112
118113 private val client = Hive .get(conf)
119114
120- private def withClassLoader [A ](f : => A ): A = synchronized {
115+ /**
116+ * Runs `f` with ThreadLocal session state and classloaders configured for this version of hive.
117+ */
118+ private def withHiveState [A ](f : => A ): A = synchronized {
121119 val original = Thread .currentThread().getContextClassLoader
122120 Thread .currentThread().setContextClassLoader(getClass.getClassLoader)
123121 Hive .set(client)
@@ -135,25 +133,32 @@ class ClientWrapper(
135133 ret
136134 }
137135
138- def currentDatabase : String = withClassLoader {
136+ override def currentDatabase : String = withHiveState {
139137 state.getCurrentDatabase
140138 }
141139
142- def createDatabase (tableName : String ): Unit = withClassLoader {
143- val table = new Table (" default" , tableName)
140+ override def createDatabase (database : HiveDatabase ): Unit = withHiveState {
144141 client.createDatabase(
145- new Database (" default" , " " , new File (" " ).toURI.toString, new java.util.HashMap ), true )
142+ new Database (
143+ database.name,
144+ " " ,
145+ new File (database.location).toURI.toString,
146+ new java.util.HashMap ),
147+ true )
146148 }
147149
148- def getDatabaseOption (name : String ): Option [HiveDatabase ] = withClassLoader {
150+ override def getDatabaseOption (name : String ): Option [HiveDatabase ] = withHiveState {
149151 Option (client.getDatabase(name)).map { d =>
150152 HiveDatabase (
151153 name = d.getName,
152154 location = d.getLocationUri)
153155 }
154156 }
155157
156- def getTableOption (dbName : String , tableName : String ): Option [HiveTable ] = withClassLoader {
158+ override def getTableOption (
159+ dbName : String ,
160+ tableName : String ): Option [HiveTable ] = withHiveState {
161+
157162 logDebug(s " Looking up $dbName. $tableName" )
158163
159164 val hiveTable = Option (client.getTable(dbName, tableName, false ))
@@ -185,8 +190,8 @@ class ClientWrapper(
185190 Class .forName(name)
186191 .asInstanceOf [Class [_ <: org.apache.hadoop.hive.ql.io.HiveOutputFormat [_, _]]]
187192
188- private def toQlTable (table : HiveTable ): Table = {
189- val qlTable = new Table (table.database, table.name)
193+ private def toQlTable (table : HiveTable ): metadata. Table = {
194+ val qlTable = new metadata. Table (table.database, table.name)
190195
191196 qlTable.setFields(table.schema.map(c => new FieldSchema (c.name, c.hiveType, c.comment)))
192197 qlTable.setPartCols(
@@ -208,25 +213,23 @@ class ClientWrapper(
208213 qlTable
209214 }
210215
211- def createTable (table : HiveTable ): Unit = withClassLoader {
216+ override def createTable (table : HiveTable ): Unit = withHiveState {
212217 val qlTable = toQlTable(table)
213218 client.createTable(qlTable)
214219 }
215220
216- def alterTable (table : HiveTable ): Unit = withClassLoader {
221+ override def alterTable (table : HiveTable ): Unit = withHiveState {
217222 val qlTable = toQlTable(table)
218223 client.alterTable(table.qualifiedName, qlTable)
219224 }
220225
221- def getTables (dbName : String ): Seq [String ] = withClassLoader {
222- client.getAllTables(dbName).toSeq
223- }
224-
225- def getAllPartitions (hTable : HiveTable ): Seq [HivePartition ] = withClassLoader {
226+ override def getAllPartitions (hTable : HiveTable ): Seq [HivePartition ] = withHiveState {
226227 val qlTable = toQlTable(hTable)
227228 val qlPartitions = version match {
228- case hive.v12 => client.call[Table , Set [Partition ]](" getAllPartitionsForPruner" , qlTable)
229- case hive.v13 => client.call[Table , Set [Partition ]](" getAllPartitionsOf" , qlTable)
229+ case hive.v12 =>
230+ client.call[metadata.Table , Set [metadata.Partition ]](" getAllPartitionsForPruner" , qlTable)
231+ case hive.v13 =>
232+ client.call[metadata.Table , Set [metadata.Partition ]](" getAllPartitionsOf" , qlTable)
230233 }
231234 qlPartitions.map(_.getTPartition).map { p =>
232235 HivePartition (
@@ -239,14 +242,14 @@ class ClientWrapper(
239242 }.toSeq
240243 }
241244
242- def listTables (dbName : String ): Seq [String ] = withClassLoader {
245+ override def listTables (dbName : String ): Seq [String ] = withHiveState {
243246 client.getAllTables
244247 }
245248
246249 /**
247250 * Runs the specified SQL query using Hive.
248251 */
249- def runSqlHive (sql : String ): Seq [String ] = {
252+ override def runSqlHive (sql : String ): Seq [String ] = {
250253 val maxResults = 100000
251254 val results = runHive(sql, maxResults)
252255 // It is very confusing when you only get back some of the results...
@@ -258,7 +261,7 @@ class ClientWrapper(
258261 * Execute the command using Hive and return the results as a sequence. Each element
259262 * in the sequence is one row.
260263 */
261- protected def runHive (cmd : String , maxRows : Int = 1000 ): Seq [String ] = withClassLoader {
264+ protected def runHive (cmd : String , maxRows : Int = 1000 ): Seq [String ] = withHiveState {
262265 logDebug(s " Running hiveql ' $cmd' " )
263266 if (cmd.toLowerCase.startsWith(" set" )) { logDebug(s " Changing config: $cmd" ) }
264267 try {
@@ -331,7 +334,7 @@ class ClientWrapper(
331334 replace : Boolean ,
332335 holdDDLTime : Boolean ,
333336 inheritTableSpecs : Boolean ,
334- isSkewedStoreAsSubdir : Boolean ): Unit = withClassLoader {
337+ isSkewedStoreAsSubdir : Boolean ): Unit = withHiveState {
335338
336339 client.loadPartition(
337340 new Path (loadPath), // TODO: Use URI
@@ -347,7 +350,7 @@ class ClientWrapper(
347350 loadPath : String , // TODO URI
348351 tableName : String ,
349352 replace : Boolean ,
350- holdDDLTime : Boolean ): Unit = withClassLoader {
353+ holdDDLTime : Boolean ): Unit = withHiveState {
351354 client.loadTable(
352355 new Path (loadPath),
353356 tableName,
@@ -362,7 +365,7 @@ class ClientWrapper(
362365 replace : Boolean ,
363366 numDP : Int ,
364367 holdDDLTime : Boolean ,
365- listBucketingEnabled : Boolean ): Unit = withClassLoader {
368+ listBucketingEnabled : Boolean ): Unit = withHiveState {
366369 client.loadDynamicPartitions(
367370 new Path (loadPath),
368371 tableName,
@@ -373,7 +376,7 @@ class ClientWrapper(
373376 listBucketingEnabled)
374377 }
375378
376- def reset (): Unit = withClassLoader {
379+ def reset (): Unit = withHiveState {
377380 client.getAllTables(" default" ).foreach { t =>
378381 logDebug(s " Deleting table $t" )
379382 val table = client.getTable(" default" , t)
0 commit comments