Advertisement
Guest User

Untitled

a guest
May 21st, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. package com.adition.pig.filtering.string;
  2.  
  3. import java.io.IOException;
  4. import java.util.List;
  5. import java.util.ArrayList;
  6.  
  7. import org.apache.pig.impl.logicalLayer.schema.Schema;
  8. import org.apache.pig.impl.logicalLayer.FrontendException;
  9. import org.apache.pig.data.DataType;
  10. import org.apache.pig.FuncSpec;
  11. import org.apache.pig.PigWarning;
  12. import org.apache.pig.FilterFunc;
  13. import org.apache.pig.data.Tuple;
  14.  
  15. public class CONTAINS extends FilterFunc {
  16.     public Boolean exec(Tuple input) throws IOException {
  17.         if (input == null || input.size() == 0  || input.get(0) == null
  18.                 || input.get(1) == null) {
  19.             warn("Ignoring input as it is 'null' or of size 0.", PigWarning.UDF_WARNING_1);
  20.             return false;
  21.         }
  22.         String str = null;
  23.         try {
  24.             try {
  25.                 str = (String)input.get(0);
  26.             } catch (ClassCastException e) {
  27.                 warn("Unable to cast input " + input.get(0) + " of class " +
  28.                         input.get(0).getClass() + " to String.", PigWarning.UDF_WARNING_2);
  29.                 return false;
  30.             }
  31.             try {
  32.                 return str.contains((String)input.get(1));
  33.             } catch (ClassCastException e) {
  34.                 warn("Unable to cast input " + input.get(1) + " of class " +
  35.                             input.get(1).getClass() + " to String.", PigWarning.UDF_WARNING_2);
  36.                 return false;
  37.             }
  38.         } catch(Exception e) {
  39.             throw new IOException("Could not process input ", e);
  40.         }
  41.     }
  42.  
  43.     @Override
  44.     public Schema outputSchema(Schema input) {
  45.         return new Schema(
  46.                 new Schema.FieldSchema(
  47.                     getSchemaName(
  48.                         this.getClass().getName().toLowerCase(),
  49.                         input
  50.                     ),
  51.                     DataType.BOOLEAN
  52.                 )
  53.         );
  54.     }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement