Java lab booklet
Lab: Writing a Hive User Defined Function (UDF)
importstocks.hive
CREATE TABLE stocks (xchange STRING,
symbol STRING,
priceDate STRING,
open FLOAT,
high FLOAT,
low FLOAT,
close FLOAT,
volume INT,
adjClose FLOAT)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',';
LOAD DATA LOCAL INPATH '/root/java/labs/data/stock_prices/NYSE_daily_prices_A.csv' OVERWRITE INTO TABLE stocks;
MoneyFlow.java
package hiveudfs;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
public class MoneyFlow extends UDF {
private DoubleWritable result = new DoubleWritable();
public DoubleWritable evaluate(double high, double low, double close, int volume) {
double typicalPrice = (high + low + close) / 3;
double moneyFlow = typicalPrice * volume;
result.set(moneyFlow);
return result;
}
}
moneyflow.hive
ADD JAR hiveudfs.jar;
CREATE TEMPORARY FUNCTION moneyflow AS 'hiveudfs.MoneyFlow';
SELECT symbol, priceDate, moneyflow(high, low, close, volume) FROM stocks LIMIT 10000;