org.apache.pig.scripting
Class ScriptEngine

java.lang.Object
  extended by org.apache.pig.scripting.ScriptEngine
Direct Known Subclasses:
GroovyScriptEngine, JrubyScriptEngine, JsScriptEngine, JythonScriptEngine, PythonScriptEngine

public abstract class ScriptEngine
extends Object

Base class for various scripting implementations


Nested Class Summary
static class ScriptEngine.SupportedScriptLang
           
 
Field Summary
static String NAMESPACE_SEPARATOR
           
 
Constructor Summary
ScriptEngine()
           
 
Method Summary
static ScriptEngine getInstance(String scriptingLang)
          Gets instance of the scriptEngine for the given scripting language
static String getJarPath(Class<?> clazz)
          Figures out the jar location from the class
protected abstract  Map<String,Object> getParamsFromVariables()
          Returns a map from local variable names to their values
protected  Map<String,List<PigStats>> getPigStatsMap()
          Gets the collection of PigStats after the script is run.
static InputStream getScriptAsStream(String scriptPath)
          open a stream load a script locally or in the classpath
protected abstract  String getScriptingLang()
          Gets ScriptEngine classname or keyword for the scripting language
static ScriptEngine.SupportedScriptLang getSupportedScriptLang(String file)
           
protected abstract  Map<String,List<PigStats>> main(PigContext context, String scriptFile)
          Actually runs the script file.
abstract  void registerFunctions(String path, String namespace, PigContext pigContext)
          Registers scripting language functions as Pig functions with given namespace
 Map<String,List<PigStats>> run(PigContext pigContext, String scriptFile)
          Runs a script file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NAMESPACE_SEPARATOR

public static final String NAMESPACE_SEPARATOR
See Also:
Constant Field Values
Constructor Detail

ScriptEngine

public ScriptEngine()
Method Detail

getScriptAsStream

public static InputStream getScriptAsStream(String scriptPath)
open a stream load a script locally or in the classpath

Parameters:
scriptPath - the path of the script
Returns:
a stream (it is the responsibility of the caller to close it)
Throws:
IllegalStateException - if we could not open a stream

getSupportedScriptLang

public static ScriptEngine.SupportedScriptLang getSupportedScriptLang(String file)
                                                               throws IOException
Parameters:
file - the file to inspect
Returns:
the Supported Script Lang if this is a supported script language
Throws:
IOException - if there was an error reading the file or if the file defines explicitly an unknown #!

registerFunctions

public abstract void registerFunctions(String path,
                                       String namespace,
                                       PigContext pigContext)
                                throws IOException
Registers scripting language functions as Pig functions with given namespace

Parameters:
path - path of the script
namespace - namespace for the functions
pigContext - pigcontext to register functions to pig in the given namespace
Throws:
IOException

main

protected abstract Map<String,List<PigStats>> main(PigContext context,
                                                   String scriptFile)
                                            throws IOException
Actually runs the script file. This method will be implemented by individual script engines.

Parameters:
context - ScriptPigContext to run the script file
scriptFile - the file
Throws:
IOException

getScriptingLang

protected abstract String getScriptingLang()
Gets ScriptEngine classname or keyword for the scripting language


getParamsFromVariables

protected abstract Map<String,Object> getParamsFromVariables()
                                                      throws IOException
Returns a map from local variable names to their values

Throws:
IOException

getJarPath

public static String getJarPath(Class<?> clazz)
                         throws FileNotFoundException
Figures out the jar location from the class

Parameters:
clazz - class in the jar file
Returns:
the jar file location, null if the class was not loaded from a jar
Throws:
FileNotFoundException

getInstance

public static ScriptEngine getInstance(String scriptingLang)
                                throws IOException
Gets instance of the scriptEngine for the given scripting language

Parameters:
scriptingLang - ScriptEngine classname or keyword for the scriptingLang
Returns:
scriptengine for the given scripting language
Throws:
IOException

run

public Map<String,List<PigStats>> run(PigContext pigContext,
                                      String scriptFile)
                               throws ExecException,
                                      IOException
Runs a script file.

Parameters:
pigContext - PigContext to run the script file
scriptFile - the file
Returns:
a collection of PigStats objects. One for each runtime instance of Pig in the script. For named pipelines, the map key is the name of the pipeline. For unnamed pipeline, the map key is the script id associated with the instance.
Throws:
ExecException
IOException

getPigStatsMap

protected Map<String,List<PigStats>> getPigStatsMap()
Gets the collection of PigStats after the script is run.



Copyright © 2007-2012 The Apache Software Foundation