Pig UDF Properties

"To store information, the UDF calls getUDFProperties. This returns a Properties object which the UDF can record the information in or read the information from. To avoid name space conflicts UDFs are required to provide a signature when obtaining a Properties object. This can be done in two ways. The UDF can provide its Class object (via this.getClass()). In this case, every instantiation of the UDF will be given the same Properties object. The UDF can also provide its Class plus an array of Strings. The UDF can pass its constructor arguments, or some other identifying strings. This allows each instantiation of the UDF to have a different properties object thus avoiding name space collisions between instantiations of the UDF."

For example, if you need the object key to persist and remain the same between different calls to the UDF, you can do the following:

UDFContext context = UDFContext.getUDFContext();    
Properties properties = context.getUDFProperties(this.getClass());    
if (properties.containsKey("key")) {
        key = properties.get("key");    
} else {        
      key = new generateKey();        
      properties.put("key", key);    
}

You can also make this key a class variable to the EvalFunc implementation class.

http://pig.apache.org/docs/r0.16.0/udf.html#Advanced+Topics

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容