欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hive基于UDAF的编写

程序员文章站 2022-03-31 18:17:21
...
-------------
select concat(account,'\t',bfdutmb,'\t',page_url) from (
select account,bfdutmb,actionPath(page_url,'->') page_url from (
select create_time,account,bfdutmb,page_url page_url from ods_action_event
order by account,bfdutmb,create_time)c
group by account,bfdutmb)b 

 

import org.apache.hadoop.hive.ql.exec.UDAF;
import org.apache.hadoop.hive.ql.exec.UDAFEvaluator;

public class ActionPathUDAF extends UDAF {
	public static class ConcatUDAFEvaluator implements UDAFEvaluator {
		public static class PartialResult {
			String result;
			String delimiter;
		}

		private PartialResult partial;

		public void init() {
			partial = null;
		}

		public boolean iterate(String value, String deli) {

			if (value == null) {
				return true;
			}
			if (partial == null) {
				partial = new PartialResult();
				partial.result = new String("");
				if (deli == null || deli.equals("")) {
					partial.delimiter = new String(",");
				} else {
					partial.delimiter = new String(deli);
				}

			}
			if (partial.result.length() > 0) {
				partial.result = partial.result.concat(partial.delimiter);//result='aa',delimiter='->' | aa->
			}

			partial.result = partial.result.concat(value);
			return true;
		}

		public PartialResult terminatePartial() {
			return partial;
		}

		public boolean merge(PartialResult other) {
			if (other == null) {
				return true;
			}
			if (partial == null) {
				partial = new PartialResult();
				partial.result = new String(other.result);
				partial.delimiter = new String(other.delimiter);
			} else {
				if (partial.result.length() > 0) {
					partial.result = partial.result.concat(partial.delimiter);
				}
				partial.result = partial.result.concat(other.result);
			}
			return true;
		}

		public String terminate() {
			return new String(partial.result);
		}
	}
}