Reputation: 381
I write a hive UDF which looks like below:
public class DemoUDF extends GenericUDF{
private ObjectMapper mapper = new ObjectMapper();
... initialize() method...
public Object evaluate(DeferredObject[] deferredObjects) throws HiveException{
... ...
ActionVO vo = mapper.readValue(target_str, ActionVO.class);
... ...
}
}
While I add this function into hive,and execute it with some query,there exists the exception below:
Caused by : java.lang.NullPointerException at com.fasterxml.jackson.databind.util.LRUMap.get(LRUMap.java:68) at com.fasterxml.jackson.databind.type.TypeFactory._fromClass(TypeFactory.java:1255) at com.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1194) at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:608)
while I change the ObjectMapper declaration to below,it works fine!
private static final ObjectMapper mapper = new ObjectMapper();
So why? What effect does it make?
Upvotes: 0
Views: 341
Reputation: 5791
I think once you make a variable static then its class level thing and only one copy of that will be stored. The JVM allocates static variable during the class loading time. For non-static ones multiple copies will be created as per class instances and hence need more memory. Static variables are shared by all objects of a class and have a single instance, while non-static variables are unique to each object and have different values for different objects.
Upvotes: 0