Search code examples
javahadoophiveapache-pig

How to pass configurations from hive script to UDF


In pig, you can pass a configuration from your pig script to pig UDF via UDFContext. For example,

// in pig script
SET my.conf dummy-conf

// in UDF java code
Configuration conf = UDFContext.getUDFContext().getJobConf();
String myConf = conf.get("my.conf");

So, is there a similar way to pass configuration from a hive script to a hive UDF? For example, if I have set MY_CONF='foobar' in a hive script, how can I retrieve that in a java UDF, which needs to consume the value of MY_CONF?


Solution

  • Instead of extending UDF class, you can try subclassing GenericUDF. This class has the following method you can override:

    /**
     * Additionally setup GenericUDF with MapredContext before initializing.
     * This is only called in runtime of MapRedTask.
     *
     * @param context context
     */
    public void configure(MapredContext context) {
    }
    

    MapredContext has a method just like UDFContext from Pig to retrieve the Job configuration. So you could just do the following:

    @Override
    public void configure(MapredContext context) {
        Configuration conf = context.getJobConf();  
    }