All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.table.hive.functions.HiveFunctionWrapper Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.table.hive.functions;

import org.apache.hadoop.hive.ql.exec.UDF;

import java.io.Serializable;
import java.util.List;

/**
 * This class provides the UDF creation and also the UDF instance serialization and
 * de-serialization cross process boundary.
 *
 * @param  The type of UDF.
 */
public class HiveFunctionWrapper implements Serializable {

	private String className;
	private List constants;

	private transient UDFType instance = null;

	public HiveFunctionWrapper(String className) {
		this.className = className;
	}

	public HiveFunctionWrapper(Class clazz) {
		this.className = clazz.getName();
	}

	public UDFType createFunction()
			throws ClassNotFoundException, IllegalAccessException, InstantiationException {
		if (null != instance) {
			return instance;
		}
		UDFType func =  (UDFType) getClassLoader().loadClass(className).newInstance();
		if (!(func instanceof UDF)) {
			// We cache the function if it is not the Simple UDF,
			// as we always have to create new instance for Simple UDF.
			instance = func;
		}
		return func;
	}

	public Class getUDFClass() throws ClassNotFoundException {
		return (Class) Class.forName(className);
	}

	public static ClassLoader getClassLoader() {
		return Thread.currentThread().getContextClassLoader();
	}

	public void setConstants(List constants) {
		this.constants = constants;
	}

	public List getConstants() {
		return constants;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy