Workaround for Spark sql:error in query:undefined function error

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.