我正在尝试创建一个按天计算和求和值的函数(以便稍后计算平均值)。我做到了这一点:
CREATE OR REPLACE FUNCTION state_group_count_and_sum( state map<timestamp, frozen<tuple<bigint,double>>>, timestamp timestamp, value double )
CALLED ON NULL INPUT
RETURNS map<timestamp, frozen<tuple<bigint,double>>>
LANGUAGE java AS '
Date date = (Date) timestamp;
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
TupleValue tupleValue = state.get(date);
Long count = (Long) tupleValue.getLong(0);
if (count == null) count = 1L;
else count = count + 1L;
Double sum = (Double) tupleValue.getDouble(1);
if (sum == null) sum = value;
else sum = sum + value;
//if (tupleValue == null) ?
tupleValue.setLong(0, count);
tupleValue.setDouble(1, sum);
state.put(date, tupleValue);
return state; ' ;
CREATE OR REPLACE AGGREGATE group_count_and_sum(timestamp, double)
SFUNC state_group_count_and_sum
STYPE map<timestamp, frozen<tuple<bigint,double>>>
INITCOND {};
这失败了,因为 tupleValue 在每个新的一天都为空,但尚未在地图中。如何在 UDA 中实例化元组值?