org.apache.spark.sql.hive.thriftserver.server.UdfLoadUtils
package org.apache.spark.sql.hive.thriftserver.server
import org.apache.spark.SparkFiles
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction
import org.apache.spark.sql.types.{DataType, DataTypes}
import scala.collection.mutable.ArrayBuffer
import scala.io.Source
object UdfLoadUtils {
var configArray: Array[String] = getConfigArray
def udfRegister( spark: SparkSession): Unit = {
// name,classname,returnType(udf need)
configArray.foreach(record => {
val registerInfoArray = record.split(",")
println(s"register udf info : $record")
if (registerInfoArray.size == 2) {
val Array(udfName, className) = registerInfoArray
val instance = getUDAFInstanceByClass(className)
spark.sqlContext.udf.register(udfName, instance)
} else if (registerInfoArray.size == 3) {
val Array(udfName, className, returnType) = registerInfoArray
var returnDataType: DataType = null
returnType match {
// Numeric types
case "ByteType" => returnDataType = DataTypes.ByteType
case "ShortType" => returnDataType = DataTypes.ShortType
case "IntegerType" =>returnDataType = DataTypes.IntegerType
case "LongType" => returnDataType = DataTypes.LongType
case "FloatType" => returnDataType = DataTypes.FloatType
case "DoubleType" => returnDataType = DataTypes.DoubleType
//case "DecimalType" => returnDataType = DecimalType
// String types
case "StringType" => returnDataType = DataTypes.StringType
// Binary type
case "BinaryType" => returnDataType = DataTypes.BinaryType
// Boolean type
case "BooleanType" => returnDataType = DataTypes.BooleanType
// Datetime type
case "TimestampType" =>returnDataType = DataTypes.TimestampType
case "DateType" => returnDataType = DataTypes.DateType
// Complex types
//case "ArrayType" => returnDataType = ArrayType
//case "MapType" => returnDataType = MapType
//case "StructType" => returnDataType = StructType
case _ => None
}
spark.sqlContext.udf.registerJava(udfName, className, returnDataType)
}
})
}
def getUDAFInstanceByClass(className: String): UserDefinedAggregateFunction = {
var instance: UserDefinedAggregateFunction = null
try {
instance = Class.forName(className).newInstance.asInstanceOf[UserDefinedAggregateFunction]
} catch {
case ex: Throwable => {
println(s" instance $className error ,error info : ${ex.getCause} ...................... ")
ex.printStackTrace()
}
}
instance
}
def getConfigArray():Array[String] ={
val configArray = new ArrayBuffer[String]()
try {
println(s"SparkFiles config.properties , path :" + SparkFiles.get("udf.config"))
val source = Source.fromFile(SparkFiles.get("udf.config"))
val sparkFiles = source.getLines().toArray
configArray ++= sparkFiles
println(s"SparkFiles udf.config , path : SparkFiles.get(udf.config) done!")
} catch {
case x: Throwable =>
}
try {
println(s"local config.properties , path : ./udf.config")
val source = Source.fromFile("./udf.config")
val localFiles = source.getLines().toArray
if(configArray.size == 0 ) configArray ++= localFiles
//localFiles.foreach(kv => println(s"localFiles config pop : key ${kv._1} ,value ${kv._2} "))
println(s"local udf.config , path : ./udf.config done!")
} catch {
case x: Throwable =>
}
try {
val path = SparkFiles.getRootDirectory() + "/udf.config"
println(s"SparkFilesroot udf.config , path : ${path}")
val source = Source.fromFile(path)
val sparkFilesroot = source.getLines().toArray
if(configArray.size == 0 ) configArray ++= sparkFilesroot
println(s"sparkFilesroot udf.config , path : ./udf.config done!")
} catch {
case x: Throwable =>
}
configArray.toArray
}
}
org.apache.spark.sql.hive.thriftserver.server.SparkSQLOperationManager
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.hive.thriftserver.server
import java.util.{Map => JMap}
import java.util.concurrent.ConcurrentHashMap
import org.apache.hive.service.cli._
import org.apache.hive.service.cli.operation.{ExecuteStatementOperation, Operation, OperationManager}
import org.apache.hive.service.cli.session.HiveSession
import org.apache.spark.internal.Logging
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.hive.HiveUtils
import org.apache.spark.sql.hive.thriftserver.{ReflectionUtils, SparkExecuteStatementOperation}
/**
* Executes queries using Spark SQL, and maintains a list of handles to active queries.
*/
private[thriftserver] class SparkSQLOperationManager()
extends OperationManager with Logging {
val handleToOperation = ReflectionUtils
.getSuperField[JMap[OperationHandle, Operation]](this, "handleToOperation")
val sessionToActivePool = new ConcurrentHashMap[SessionHandle, String]()
val sessionToContexts = new ConcurrentHashMap[SessionHandle, SQLContext]()
var udfNotInited = true
override def newExecuteStatementOperation(
parentSession: HiveSession,
statement: String,
confOverlay: JMap[String, String],
async: Boolean): ExecuteStatementOperation = synchronized {
val sqlContext = sessionToContexts.get(parentSession.getSessionHandle)
require(sqlContext != null, s"Session handle: ${parentSession.getSessionHandle} has not been" +
s" initialized or had already closed.")
if(udfNotInited) {
UdfLoadUtils.udfRegister(sqlContext.sparkSession)
udfNotInited = false
}
val conf = sqlContext.sessionState.conf
val runInBackground = async && conf.getConf(HiveUtils.HIVE_THRIFT_SERVER_ASYNC)
val operation = new SparkExecuteStatementOperation(parentSession, statement, confOverlay,
runInBackground)(sqlContext, sessionToActivePool)
handleToOperation.put(operation.getHandle, operation)
logDebug(s"Created Operation for $statement with session=$parentSession, " +
s"runInBackground=$runInBackground")
operation
}
}
spark thrift server modification
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326118118&siteId=291194637
Recommended
Ranking