hive中自定义函数(UDAF)实现多行字符串拼接为一行

函数如何使用:

hive> desc concat_test;
OK
a       int
b       string

hive> select * from concat_test;
OK
1       good
2       other
1       nice
1       hello

hive> select a,concat(b,',') from concat_test group by a;

OK
1       good,nice,hello
2       other

 

函数实现:

 

package com.hadoopbook.hive;

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

public class concat 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);
   }
   
   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);
  }
 }
}

你可能感兴趣的:(hive中自定义函数(UDAF)实现多行字符串拼接为一行)