Search code examples
pythonapache-pigputty

Call Python function from Pig script


I am new to Pig and would like to call a local python file from Pig after I have connected to the server via PuTTy. Below are the codes I have tried and the error message I received:

REGISTER ‘myudf.py’ using jython as my_udf

The error message is below and I don't know how to tell Pig the path to the py file.

File myudf.py does not exist

Another code i tried is:

DEFINE mycommand `python myudf.py` ship(‘C:\Users\myname\Documents\code\myudf.py’);

The error message is

unexpected character ’S’

This may sound super easy but I have spent hours on it and failed. Any advice will be highly appreciated.


Solution

  • It sounds like the Python file is saved on your own computer, but you're running Pig on a server. The server doesn't have access to files on your computer.

    You can transfer the file using software like WinSCP (assuming you're on Windows), or start a text editor on the server and copy/paste the text from your Python file into the text editor. For example, vi myudf.py or emacs myudf.py in PuTTy will start a text editor and create a file named myudf.py on the server once you've saved.

    Once you've created the Python file, you might want to include the full path to your file on the server in your REGISTER statement to avoid confusion. (pwd shows your current directory on the server.)