Search code examples
pythonapache-pigudf

Usage of Python UDF in Apache Pig


I am new to Apache Pig and Python. When I try to REGISTER Python function in Pig, it is giving some error related to Jython. My python script udf1.py converts any string to upper case.

from pig_util import outputSchema

@outputSchema('output_field_name:chararray')
def charupper(x):
    b = x.upper()
    return b

c=charlower('bbbb')

print(c)

When I try to register in Pig local mode in Grunt shell, it throwing the below error

grunt> REGISTER '/home/cloudera/PycharmProjects/Project1/udf1.py' USING jython as pyudf                      
2015-04-06 22:31:45,792 [main] WARN  org.apache.hadoop.conf.Configuration - fs.default.name is deprecated. Instead, use fs.defaultFS
2015-04-06 22:31:45,793 [main] WARN  org.apache.hadoop.conf.Configuration - io.bytes.per.checksum is deprecated. Instead, use dfs.bytes-per-checksum
2015-04-06 22:31:45,836 [main] WARN  org.apache.pig.scripting.jython.JythonScriptEngine - pig.cmd.args.remainders is empty. This is not expected unless on testing.
2015-04-06 22:31:45,842 [main] WARN  org.apache.pig.scripting.jython.JythonScriptEngine - module file does not exist: encodings, /usr/lib/pig/lib/jython-standalone-2.5.2.jar/Lib/encodings/__init__.py
2015-04-06 22:31:45,842 [main] WARN  org.apache.pig.scripting.jython.JythonScriptEngine - module file does not exist: encodings.utf_8, /usr/lib/pig/lib/jython-standalone-2.5.2.jar/Lib/encodings/utf_8.py
2015-04-06 22:31:45,842 [main] WARN  org.apache.pig.scripting.jython.JythonScriptEngine - module file does not exist: types, /usr/lib/pig/lib/jython-standalone-2.5.2.jar/Lib/types.py
2015-04-06 22:31:45,842 [main] WARN  org.apache.pig.scripting.jython.JythonScriptEngine - module file does not exist: encodings.aliases, /usr/lib/pig/lib/jython-standalone-2.5.2.jar/Lib/encodings/aliases.py
2015-04-06 22:31:45,842 [main] WARN  org.apache.pig.scripting.jython.JythonScriptEngine - module file does not exist: codecs, /usr/lib/pig/lib/jython-standalone-2.5.2.jar/Lib/codecs.py
2015-04-06 22:31:46,026 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 1121: Python Error. Traceback (most recent call last):
  File "/home/cloudera/PycharmProjects/Project1/udf1.py", line 3, in <module>
    from pig_util import outputSchema
ImportError: No module named pig_util

Details at logfile: /home/cloudera/pig_1428381449281.log

Already I imported pig_util.py. Do I have to install any thing related to jython in my CDH?.I could not able to know the error.

Pig version: Apache Pig version 0.11.0-cdh4.7.0

Python script created using PyCharm Community Edition 4.0.4

Python version: Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)


Solution

  • I had the same issue. Here is what I have done.

    I downloaded the pig_util.py file from here. Then I placed the Pig_util.py file in the same directory where my python udf was saved, then executed. This solved the import error for me.

    Note: This has nothing to do with jython.