Go Map reduce code framework template for HIVE UDF/UDAF/UDTF

Source: Internet
Author: User

from:http://hugh-wangp.iteye.com/blog/1472371

The template to use when you write your own code

UDF steps:
1. org.apache.hadoop.hive.ql.exec.UDF2 must be inherited. The Evaluate function must be implemented, and the Evaluate function supports overloading
Java code
  1. <span style="Font-size:x-small;"  > Packagecom.alibaba.hive.udf;
  2. Import Org.apache.hadoop.hive.ql.exec.UDF
  3. Public class Helloword extends udf{
  4. Public String Evaluate () {
  5. return "Hello world!";
  6. }
  7. Public string Evaluate (String str) {
  8. return "Hello World:" + str;
  9. }
  10. }</span>
UDAF steps:
1. Must Inherit Org.apache.hadoop.hive.ql.exec.UDAF (function class inheritance) Org.apache.hadoop.hive.ql.exec.UDAFEvaluator (inner class evaluator Real Now Udafevaluator interface) 2.Evaluator need to implement INIT, iterate, terminatepartial, merge, terminate these functions init (): Similar to constructors, for UDAF initialization of ITE Rate (): Receives incoming parameters and makes internal rotation. Its return type is Boolean terminatepartial (): No parameter, which returns the iterate function after the rotation of the functions, iterate and terminatepartial similar to Hadoop combiner ( Iterate--mapper;terminatepartial--reducer) Merge (): Receive terminatepartial return result, data merge operation with a return type of Boolean terminate () : Returns the result of the final aggregation function
Java code
  1. <span style="Font-size:x-small;"  > Packagecom.alibaba.hive;
  2. Import Org.apache.hadoop.hive.ql.exec.UDAF;
  3. Import Org.apache.hadoop.hive.ql.exec.UDAFEvaluator;
  4. Public class Myavg extends udaf{
  5. public static class avgscore{
  6. private long pSum;
  7. private double pcount;
  8. }
  9. public static class Avgevaluator extends udafevaluator{
  10. Avgscore score;
  11. Public Avgevaluator () {
  12. Score = new Avgscore ();
  13. Init ();
  14. }
  15. /* 
  16. The *init function is similar to a constructor for UDAF initialization
  17. */
  18. public void init () {
  19. Score.psum = 0;
  20. Score.pcount = 0;
  21. }
  22. /* 
  23. The *iterate receives the incoming parameters and makes internal rotations. Its return type is Boolean
  24. * Similar to mapper in Combiner
  25. */
  26. Public Boolean iterate (Double in) {
  27. if (in = null) {
  28. Score.psum + = in;
  29. Score.pcount + +;
  30. }
  31. return true;
  32. }
  33. /* 
  34. *terminatepartial no parameters, which is the iterate function after the end of the rotation, return to the rotation data
  35. * Similar to reducer in combiner
  36. */
  37. Public Avgscore terminatepartial () {
  38. return score.pcount = = 0?  Null:score;
  39. }
  40. /* 
  41. The *merge receives the return result of the terminatepartial and makes a data merge operation with a return type of Boolean
  42. */
  43. Public Boolean merge (Avgscore in) {
  44. if (in = null) {
  45. Score.psum + = In.psum;
  46. Score.pcount + = In.pcount;
  47. }
  48. return true;
  49. }
  50. /* 
  51. *terminate returns the result of the final aggregation function
  52. */
  53. Public Double Terminate () {
  54. return score.pcount = = 0?  null:Double.valueof (Score.psum/score.pcount);
  55. }
  56. }
  57. }</span>

UDTF steps: 1. Must inherit Org.apache.hadoop.hive.ql.udf.generic.GenericUDTF
2. Implement initialize, process, close three methods
3.UDTF will first
A. Call the Initialize method, which returns information about the returned rows of the UDTF (number of returns, type)
B. Once the initialization is complete, the process method is called, the passed parameters are processed, and the result can be returned by the ForWord () method.
C. Last Close () method call to clean up the method that needs to be cleaned Java code
  1. <span style="Font-size:x-small;" ><span style="Font-size:xx-small;" >public class Genericudtfexplode extends GENERICUDTF {
  2. private Listobjectinspector listoi = null;
  3. @Override
  4. public Void Close () throws hiveexception {
  5. }
  6. @Override
  7. Public Structobjectinspector Initialize (objectinspector[] args) throws udfargumentexception {
  8. if (args.length! = 1) {
  9. throw New Udfargumentexception ("explode () takes only one argument");
  10. }
  11. if (args[0].getcategory ()! = ObjectInspector.Category.LIST) {
  12. throw New Udfargumentexception ("explode () takes an array as a parameter");
  13. }
  14. Listoi = (listobjectinspector) args[0];
  15. arraylist<string> FieldNames = new arraylist<string> ();
  16. arraylist<objectinspector> Fieldois = new arraylist<objectinspector> ();
  17. Fieldnames.add ("col");
  18. Fieldois.add (Listoi.getlistelementobjectinspector ());
  19. return Objectinspectorfactory.getstandardstructobjectinspector (FieldNames,
  20. Fieldois);
  21. }
  22. private final object[] forwardobj = new object[1];
  23. @Override
  24. public void Process (object[] o) throws hiveexception {
  25. list<?> list = listoi.getlist (o[0]);
  26. if (list = = null) {
  27. return;
  28. }
  29. For (Object r:list) {
  30. forwardobj[0] = R;
  31. Forward (forwardobj);
  32. }
  33. }
  34. @Override
  35. Public String toString () {
  36. return "explode";
  37. }
  38. }</span></span>

Go Map reduce code framework template for HIVE UDF/UDAF/UDTF

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.