This article source: http://blog.csdn.net/bluishglc/article/details/50748937 prohibited any form of reprint, or will entrust CSDN official maintenance rights!
Problem description
If you try to invoke a custom function (UDF) registered in hive on spark SQL, you may encounter an error like this:
Errorinundefinedfunction xxxxxx
This problem occurs on Spark 1.5.0, 1.5.1 and 1.5.2, there is a special bug for this spark official report:https://issues.apache.org/jira/browse/spark-11191
Simply put, the cause of this bug is that spark SQL has a problem with the UDF in its interaction with hive (primarily hive metadata), causing spark SQL to not get the UDF information correctly. The simplest solution to this problem, of course, is to upgrade spark, but it may be subject to other factors, and we cannot upgrade, and we need to solve this problem from other sources.
Solution Solutions
Here is a reference solution, the idea is to bypass hive and register the UDF directly on Spark. But here's a place to be considered, and that's where and when to register the UDF. Here's the assumption that all SQL statements are pumped out into a dedicated SQL file and then executed by Spark-sql-f/your/sql/file, which we generally think is a more prescriptive approach, Instead of embedding SQL statements in Scala or Java code, in such a premise, the entry of the registered UDF should be somewhere in the initialization of the Spark-sql script, and by browsing the spark source, we know that Spark-sql's entrance is
org.apache.spark.sql.hive.thriftserver.SparkSQLCLIDriver
and Sparksqlclidriver will pass
org.apache.spark.sql.hive.thriftserver.SparkSQLEnv#init
To initialize a hivecontext, this hivecontext is the Hivecontext instance that was established after Spark-sql startup, and also the Hivecontext instance that is used for subsequent SQL execution. So the initialization method in this instance is that we register our UDF's entry and we can
hiveContext.setConf("spark.sql.hive.version", HiveContext.hiveExecutionVersion)
After this line, add the code to register the UDF:
hiveContext.udf.register("your_function_name",yourFunction _)
After the modification is completed, the compiled Sparksqlenv all class files are updated to the spark jar packages of each node.
Workaround for Spark sql:error in query:undefined function error