use of org.apache.flink.table.functions.hive.HiveSimpleUDF in project flink by apache.
the class HiveFunctionDefinitionFactory method createFunctionDefinitionFromHiveFunction.
/**
* Create a FunctionDefinition from a Hive function's class name. Called directly by {@link
* org.apache.flink.table.module.hive.HiveModule}.
*/
public FunctionDefinition createFunctionDefinitionFromHiveFunction(String name, String functionClassName) {
Class clazz;
try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(functionClassName);
LOG.info("Successfully loaded Hive udf '{}' with class '{}'", name, functionClassName);
} catch (ClassNotFoundException e) {
throw new TableException(String.format("Failed to initiate an instance of class %s.", functionClassName), e);
}
if (UDF.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveSimpleUDF", name);
return new HiveSimpleUDF(new HiveFunctionWrapper<>(functionClassName), hiveShim);
} else if (GenericUDF.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDF", name);
return new HiveGenericUDF(new HiveFunctionWrapper<>(functionClassName), hiveShim);
} else if (GenericUDTF.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDTF", name);
HiveGenericUDTF udtf = new HiveGenericUDTF(new HiveFunctionWrapper<>(functionClassName), hiveShim);
return new TableFunctionDefinition(name, udtf, GenericTypeInfo.of(Row.class));
} else if (GenericUDAFResolver2.class.isAssignableFrom(clazz) || UDAF.class.isAssignableFrom(clazz)) {
HiveGenericUDAF udaf;
if (GenericUDAFResolver2.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDAF without UDAF bridging", name);
udaf = new HiveGenericUDAF(new HiveFunctionWrapper<>(functionClassName), false, hiveShim);
} else {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDAF with UDAF bridging", name);
udaf = new HiveGenericUDAF(new HiveFunctionWrapper<>(functionClassName), true, hiveShim);
}
return new AggregateFunctionDefinition(name, udaf, GenericTypeInfo.of(Object.class), GenericTypeInfo.of(GenericUDAFEvaluator.AggregationBuffer.class));
} else {
throw new IllegalArgumentException(String.format("HiveFunctionDefinitionFactory cannot initiate FunctionDefinition for class %s", functionClassName));
}
}
use of org.apache.flink.table.functions.hive.HiveSimpleUDF in project flink by apache.
the class HiveModuleTest method testHiveBuiltInFunction.
@Test
public void testHiveBuiltInFunction() {
FunctionDefinition fd = new HiveModule().getFunctionDefinition("reverse").get();
HiveSimpleUDF udf = (HiveSimpleUDF) fd;
DataType[] inputType = new DataType[] { DataTypes.STRING() };
CallContext callContext = new HiveUDFCallContext(new Object[0], inputType);
udf.getTypeInference(null).getOutputTypeStrategy().inferType(callContext);
udf.open(null);
assertEquals("cba", udf.eval("abc"));
}
Aggregations