Save This Page
Home » hive-0.5.0-dev » org.apache.hadoop.hive.contrib.udtf.example » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing, software
   13    * distributed under the License is distributed on an "AS IS" BASIS,
   14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    * See the License for the specific language governing permissions and
   16    * limitations under the License.
   17    */
   18   
   19   package org.apache.hadoop.hive.contrib.udtf.example;
   20   
   21   import java.util.ArrayList;
   22   import java.util.List;
   23   
   24   import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
   25   import org.apache.hadoop.hive.ql.exec.description;
   26   import org.apache.hadoop.hive.ql.metadata.HiveException;
   27   import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
   28   import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
   29   import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
   30   import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
   31   import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
   32   
   33   @description(
   34       name = "explode2",
   35       value = "_FUNC_(a) - like explode, but outputs two identical columns (for "+
   36               "testing purposes)"
   37   )
   38   public class GenericUDTFExplode2 extends GenericUDTF {
   39   
   40     ListObjectInspector listOI = null;
   41     
   42     @Override
   43     public void close() throws HiveException{
   44     }
   45     
   46     @Override
   47     public StructObjectInspector initialize(ObjectInspector [] args) 
   48     throws UDFArgumentException {
   49       
   50       if (args.length != 1) {
   51         throw new UDFArgumentException("explode() takes only one argument");
   52       }
   53       
   54       if (args[0].getCategory() != ObjectInspector.Category.LIST) {
   55         throw new UDFArgumentException("explode() takes an array as a parameter");
   56       }
   57       listOI = (ListObjectInspector)args[0];
   58       
   59       ArrayList<String> fieldNames = new ArrayList<String>();
   60       ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
   61       fieldNames.add("col1");
   62       fieldNames.add("col2");
   63       fieldOIs.add(listOI.getListElementObjectInspector());
   64       fieldOIs.add(listOI.getListElementObjectInspector());
   65       return ObjectInspectorFactory.getStandardStructObjectInspector(
   66           fieldNames, fieldOIs);
   67     }
   68   
   69     Object forwardObj[] = new Object[2];
   70     @Override
   71     public void process(Object [] o) throws HiveException {
   72      
   73       List<?> list = listOI.getList(o[0]);
   74       for (Object r : list) {
   75         forwardObj[0] = r;
   76         forwardObj[1] = r;
   77         this.forward(forwardObj);
   78       }
   79     }
   80   
   81     @Override
   82     public String toString() {
   83       return "explode";
   84     }
   85   }

Save This Page
Home » hive-0.5.0-dev » org.apache.hadoop.hive.contrib.udtf.example » [javadoc | source]