Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.apache.pig.builtin;
 
 import java.util.List;
 
 
This is a UDF which allows the user to specify a string prefix, and then filter for the columns in a relation that begin with that prefix. Example: a = load 'a' as (x, y); b = load 'b' as (x, y); c = join a by x, b by x; DEFINE pluck PluckTuple('a::'); d = foreach c generate FLATTEN(pluck(*)); describe c; c: {a::x: bytearray,a::y: bytearray,b::x: bytearray,b::y: bytearray} describe d; d: {plucked::a::x: bytearray,plucked::a::y: bytearray}
 
 public class PluckTuple extends EvalFunc<Tuple> {
     private static final TupleFactory mTupleFactory = TupleFactory.getInstance();
 
     private boolean isInitialized = false;
     private int[] indicesToInclude;
     private String prefix;
 
     public PluckTuple(String prefix) {
         this. = prefix;
     }
 
     @Override
     public Tuple exec(Tuple inputthrows IOException {
         if (!) {
             List<IntegerindicesToInclude = Lists.newArrayList();
             Schema inputSchema = getInputSchema();
             for (int i = 0; i < inputSchema.size(); i++) {
                 String alias = inputSchema.getField(i).;
                 if (alias.startsWith()) {
                     indicesToInclude.add(i);
                 }
             }
             this. = new int[indicesToInclude.size()];
             int idx = 0;
             for (int val : indicesToInclude) {
                 this.[idx++] = val;
             }
              = true;
         }
         Tuple result = .newTuple(.);
         int idx = 0;
         for (int val : ) {
             result.set(idx++, input.get(val));
         }
         return result;
     }
 
     public Schema outputSchema(Schema inputSchema) {
         if (!) {
             List<IntegerindicesToInclude = Lists.newArrayList();
             for (int i = 0; i < inputSchema.size(); i++) {
                 String alias;
                 try {
                     alias = inputSchema.getField(i).;
                 } catch (FrontendException e) {
                     throw new RuntimeException(e); // Should never happen
                 }
                 if (alias.startsWith()) {
                     indicesToInclude.add(i);
                 }
             }
             this. = new int[indicesToInclude.size()];
            int idx = 0;
            for (int val : indicesToInclude) {
                this.[idx++] = val;
            }
             = true;
        }
        Schema outputSchema = new Schema();
        for (int val : ) {
            try {
                outputSchema.add(inputSchema.getField(val));
            } catch (FrontendException e) {
                throw new RuntimeException(e);
            }
        }
        try {
            return new Schema(new Schema.FieldSchema("plucked"outputSchema.));
        } catch (FrontendException e) {
            throw new RuntimeException(e); // Should never happen
        }
    }
New to GrepCode? Check out our FAQ X