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 7.13 KB | None | 0 0
  1. package com.adition.pig.test;
  2.  
  3. import java.io.IOException;
  4. import java.util.Iterator;
  5. import java.util.Properties;
  6. import java.util.Map.Entry;
  7.  
  8.  
  9. import org.junit.Test;
  10. import junit.framework.Assert;
  11. import static org.junit.Assert.*;
  12. import org.junit.runner.RunWith;
  13. import org.junit.runners.JUnit4;
  14. import org.junit.AfterClass;
  15. import org.junit.Before;
  16.  
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19.  
  20. import com.adition.pig.filtering.string.CONTAINS;
  21.  
  22. import org.apache.pig.test.Util;
  23. import org.apache.pig.test.MiniCluster;
  24. import org.apache.pig.EvalFunc;
  25. import org.apache.pig.builtin.TRIM;
  26. import org.apache.pig.PigServer;
  27. import org.apache.pig.ExecType;
  28. import org.apache.pig.data.Tuple;
  29. import org.apache.pig.data.TupleFactory;
  30. import org.apache.pig.backend.executionengine.ExecException;
  31. import org.apache.pig.impl.io.FileLocalizer;
  32. import org.apache.pig.test.Util;
  33.  
  34.  
  35. @RunWith(JUnit4.class)
  36. public class TestCONTAINS {
  37.     private static final EvalFunc<Boolean> contains_ = new CONTAINS();
  38.     protected final Log log = LogFactory.getLog(getClass());
  39.     private static MiniCluster cluster = MiniCluster.buildCluster();
  40.     private PigServer pigServer = null;
  41.     private Properties props = null;
  42.     String INPUT_FILE = "TestCONTAINS-testFilteringCluster-input.txt";
  43.  
  44.     public void setup() {
  45.         FileLocalizer.setInitialized(false);
  46.         props = new Properties();
  47.         for (Entry<Object, Object> entry : cluster.getProperties().entrySet()) {
  48.             props.put(entry.getKey(), entry.getValue());
  49.         }
  50.         props.setProperty("mapred.max.split.size", "20");
  51.         String[] inputs = {
  52.             "1\t23\t42\thttp://www.google.com/url&url=http%3A%2F%2Fwww.example.com%2Fmypage.htm&q=flowers",
  53.             "2\t123\t42\thttp://www.google.com/url&url=http%3A%2F%2Fwww.zeit.de%2Findex.php&q=towers",
  54.             "3\t223\t142\thttp://www.google.com/url&url=http%3A%2F%2Fwww.nix-wie-weg.de&q=mallorca",
  55.             "4\t323\t242\thttp://www.google.com/url&url=http%3A%2F%2Fwww.tagesschau.de&q=barack%20obama",
  56.             "5\t423\t342\thttp://www.google.com/url&url=http%3A%2F%2Fwww.bild.de&q=obama",
  57.             "6\t523\t442\thttp://www.google.com/url&url=http%3A%2F%2Fwww.example.com%2Fmypage.htm&q=praesident"
  58.         };
  59.         try {
  60.             Util.createInputFile(cluster, INPUT_FILE, inputs);
  61.         } catch (Exception e) {
  62.         }
  63.     }
  64.  
  65.  
  66.     @AfterClass
  67.     public static void shutdown() {
  68.         cluster.shutDown();
  69.     }
  70.    
  71.     @Test
  72.     public void testNullDoesNotContainNull() throws IOException {
  73.         Tuple testTuple = Util.buildTuple(null, null);
  74.         assertFalse("null does not contain null", contains_.exec(testTuple));
  75.     }
  76.  
  77.     @Test
  78.     public void testNullDoesNotContainString() throws IOException {
  79.         Tuple testTuple = Util.buildTuple(null, "anything");
  80.         assertFalse("null does not contain 'anything'", contains_.exec(testTuple));
  81.     }
  82.  
  83.     @Test
  84.     public void testEmptyStringContainsEmptyString() throws IOException {
  85.         Tuple testTuple = Util.buildTuple("", "");
  86.         assertTrue("Empty string contains empty string", contains_.exec(testTuple));
  87.     }
  88.  
  89.     @Test
  90.     public void testNonEmptyStringContainsEmptyString() throws IOException {
  91.         Tuple testTuple = Util.buildTuple("A string", "");
  92.         assertTrue("Non-empty string contains empty string", contains_.exec(testTuple));
  93.     }
  94.    
  95.     @Test
  96.     public void testStringNotContained1() throws IOException {
  97.         Tuple testTuple = Util.buildTuple("A string", "Ring");
  98.         assertFalse("'A string' does not contain 'Ring'", contains_.exec(testTuple));
  99.     }
  100.    
  101.     @Test
  102.     public void testStringNotContained2() throws IOException {
  103.         Tuple testTuple = Util.buildTuple("A string", "hugo");
  104.         assertFalse("'A string' does not contain 'hugo'", contains_.exec(testTuple));
  105.     }
  106.  
  107.     @Test
  108.     public void testStringContained1() throws IOException {
  109.         Tuple testTuple = Util.buildTuple("A string", "A");
  110.         assertTrue("'A string' contains 'A'", contains_.exec(testTuple));
  111.     }
  112.    
  113.     @Test
  114.     public void testStringContained2() throws IOException {
  115.         Tuple testTuple = Util.buildTuple("A string", " stri");
  116.         assertTrue("'A string' contains ' stri'", contains_.exec(testTuple));
  117.     }
  118.    
  119.     @Test
  120.     public void testFilteringCluster() throws ExecException {
  121.         this.setup();
  122.         String[] expected = {
  123.             "(4,323,242,http://www.google.com/url&url=http%3A%2F%2Fwww.tagesschau.de&q=barack%20obama)",
  124.             "(5,423,342,http://www.google.com/url&url=http%3A%2F%2Fwww.bild.de&q=obama)"
  125.         };
  126.        
  127.        
  128.         pigServer = new PigServer(ExecType.MAPREDUCE, props);
  129.        
  130.         try {
  131.             Util.registerMultiLineQuery(pigServer, "a = LOAD '" + INPUT_FILE +
  132.                     "' AS (id:int, grp:int, additional:int, referer:chararray); " +
  133.                     "b = FILTER a BY " +
  134.                     "com.adition.pig.filtering.string.CONTAINS(referer, 'obama');");
  135.            
  136.             Iterator<Tuple> iter = pigServer.openIterator("b");
  137.             int counter = 0;
  138.             while (iter.hasNext()){
  139.                 assertEquals(expected[counter++].toString(), iter.next().toString());
  140.             }
  141.            
  142.             assertEquals(expected.length, counter);
  143.            
  144.         } catch (Exception e) {
  145.             e.printStackTrace();
  146.             Assert.fail();
  147.         }
  148.        
  149.         pigServer.shutdown();
  150.        
  151.     }
  152.    
  153.     @Test
  154.     public void testFilteringClusterWithOR1() throws ExecException {
  155.         this.setup();
  156.         String[] expected = {
  157.             "(4,323,242,http://www.google.com/url&url=http%3A%2F%2Fwww.tagesschau.de&q=barack%20obama)",
  158.             "(5,423,342,http://www.google.com/url&url=http%3A%2F%2Fwww.bild.de&q=obama)",
  159.             "(6,523,442,http://www.google.com/url&url=http%3A%2F%2Fwww.example.com%2Fmypage.htm&q=praesident)"
  160.         };
  161.        
  162.         pigServer = new PigServer(ExecType.MAPREDUCE, props);
  163.        
  164.         try {
  165.             Util.registerMultiLineQuery(pigServer, "a = LOAD '" + INPUT_FILE +
  166.             "' AS (id:int, grp:int, additional:int, referer:chararray); " +
  167.             "b = FILTER a BY " +
  168.             "com.adition.pig.filtering.string.CONTAINS(referer, 'obama') OR " +
  169.             "com.adition.pig.filtering.string.CONTAINS(referer, 'praesident');");
  170.            
  171.             Iterator<Tuple> iter = pigServer.openIterator("b");
  172.             int counter = 0;
  173.             while (iter.hasNext()){
  174.                 assertEquals(expected[counter++].toString(), iter.next().toString());
  175.             }
  176.            
  177.             assertEquals(expected.length, counter);
  178.            
  179.         } catch (Exception e) {
  180.             e.printStackTrace();
  181.             Assert.fail();
  182.         }
  183.        
  184.         pigServer.shutdown();
  185.        
  186.     }
  187.  
  188.     @Test
  189.     public void testFilteringClusterWithOR2() throws ExecException {
  190.         this.setup();
  191.         String[] expected = {
  192.             "(4,323,242,http://www.google.com/url&url=http%3A%2F%2Fwww.tagesschau.de&q=barack%20obama)",
  193.             "(5,423,342,http://www.google.com/url&url=http%3A%2F%2Fwww.bild.de&q=obama)",
  194.             "(6,523,442,http://www.google.com/url&url=http%3A%2F%2Fwww.example.com%2Fmypage.htm&q=praesident)"
  195.         };
  196.        
  197.         pigServer = new PigServer(ExecType.MAPREDUCE, props);
  198.        
  199.         try {
  200.             Util.registerMultiLineQuery(pigServer, "a = LOAD '" + INPUT_FILE +
  201.             "' AS (id:int, grp:int, additional:int, referer:chararray); " +
  202.             "b = FILTER a BY " +
  203.             "com.adition.pig.filtering.string.CONTAINS(referer, 'obama') OR " +
  204.             "id == 6;");
  205.            
  206.             Iterator<Tuple> iter = pigServer.openIterator("b");
  207.             int counter = 0;
  208.             while (iter.hasNext()){
  209.                 assertEquals(expected[counter++].toString(), iter.next().toString());
  210.             }
  211.            
  212.             assertEquals(expected.length, counter);
  213.            
  214.         } catch (Exception e) {
  215.             e.printStackTrace();
  216.             Assert.fail();
  217.         }
  218.        
  219.         pigServer.shutdown();
  220.        
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement