Java Code Examples for org.apache.pig.impl.PigContext#registerFunction()

The following examples show how to use org.apache.pig.impl.PigContext#registerFunction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsScriptEngine.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
public void registerFunctions(String path, String namespace,
        PigContext pigContext) throws IOException {
    // to enable passing of information to the slave
    this.scriptPath = path;
    JsScriptEngine.clientInstance = this;

    pigContext.addScriptJar(getJarPath(Context.class));
    namespace = (namespace == null) ? "" : namespace + NAMESPACE_SEPARATOR;
    FileInputStream fis = new FileInputStream(path);
    try {
        load(path, fis);
    } finally {
        fis.close();
    }
    Object[] ids = scope.getIds();
    for (Object id : ids) {
        if (id instanceof String) {
            String name = (String) id;
            Object value = scope.get(name, scope);
            if (value instanceof Function
                    && !name.equals("print") // ignoring the print functions that were added by this class in the initialization
                    && !name.equals("println")) {

                FuncSpec funcspec = new FuncSpec(JsFunction.class.getCanonicalName() + "('" + name + "')");
                LOG.info("Register scripting UDF: " + name);
                pigContext.registerFunction(namespace + name, funcspec);           
            }
        }
    }
    pigContext.addScriptFile(path);

}
 
Example 2
Source File: PythonScriptEngine.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
public void registerFunctions(String path, String namespace,
        PigContext pigContext) throws IOException {

    String command = pigContext.getProperties().getProperty(
            PigConfiguration.PIG_STREAMING_UDF_PYTHON_COMMAND, "python");
    String fileName = path.substring(0, path.length() - ".py".length());
    log.debug("Path: " + path + " FileName: " + fileName + " Namespace: " + namespace);
    File f = new File(path);

    if (!f.canRead()) {
        throw new IOException("Can't read file: " + path);
    }
    
    FileInputStream fin = new FileInputStream(f);
    List<String[]> functions = null;
    try {
        functions = getFunctions(fin);
    } finally {
        fin.close();
    }
    namespace = namespace == null ? "" : namespace + NAMESPACE_SEPARATOR;
    for(String[] functionInfo : functions) {
        String name = functionInfo[0];
        String schemaString = functionInfo[1];
        String schemaLineNumber = functionInfo[2];
        String alias = namespace + name;
        String execType = (pigContext.getExecType() == ExecType.LOCAL? "local" : "mapreduce");
        String isIllustrate = (Boolean.valueOf(pigContext.inIllustrator)).toString();
        log.debug("Registering Function: " + alias);
        pigContext.registerFunction(alias, 
                                    new FuncSpec("StreamingUDF", 
                                            new String[] {
                                                command, 
                                                fileName, name, 
                                                schemaString, schemaLineNumber,
                                                execType, isIllustrate
                                    }));
    }
}