You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ya...@apache.org on 2009/12/04 23:20:53 UTC

svn commit: r887387 [1/2] - in /hadoop/pig/branches/branch-0.6/contrib/zebra: ./ src/java/org/apache/hadoop/zebra/mapred/ src/test/org/apache/hadoop/zebra/mapred/ src/test/org/apache/hadoop/zebra/pig/

Author: yanz
Date: Fri Dec  4 22:20:53 2009
New Revision: 887387

URL: http://svn.apache.org/viewvc?rev=887387&view=rev
Log:
PIG-1111 Multiple Outputs Support (Gaurav Jain via yanz)

Added:
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/ZebraOutputPartition.java
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs.java
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs2.java
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs3.java
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs4.java
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/pig/TestOrderPreserveMultiTable.java
Modified:
    hadoop/pig/branches/branch-0.6/contrib/zebra/CHANGES.txt
    hadoop/pig/branches/branch-0.6/contrib/zebra/build.xml
    hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/BasicTableOutputFormat.java

Modified: hadoop/pig/branches/branch-0.6/contrib/zebra/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.6/contrib/zebra/CHANGES.txt?rev=887387&r1=887386&r2=887387&view=diff
==============================================================================
--- hadoop/pig/branches/branch-0.6/contrib/zebra/CHANGES.txt (original)
+++ hadoop/pig/branches/branch-0.6/contrib/zebra/CHANGES.txt Fri Dec  4 22:20:53 2009
@@ -6,6 +6,8 @@
 
   IMPROVEMENTS
 
+    PIG-1111 Multiple Outputs Support (Gaurav Jain via yanz)
+
     PIG-653  Pig Projection Push Down (Gaurav Jain via yanz)
 
     PIG-1098 Zebra Performance Optimizations (yanz via gates)

Modified: hadoop/pig/branches/branch-0.6/contrib/zebra/build.xml
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.6/contrib/zebra/build.xml?rev=887387&r1=887386&r2=887387&view=diff
==============================================================================
--- hadoop/pig/branches/branch-0.6/contrib/zebra/build.xml (original)
+++ hadoop/pig/branches/branch-0.6/contrib/zebra/build.xml Fri Dec  4 22:20:53 2009
@@ -116,7 +116,8 @@
       <sysproperty key="build.test" value="${build.test}"/>
       <sysproperty key="contrib.name" value="${name}"/>
       <sysproperty key="hadoop.log.dir" value="${pig.log.dir}"/>
-      
+       <sysproperty key="whichCluster" value="miniCluster"/>
+	      
       <!-- requires fork=yes for: 
         relative File paths to use the specified user.dir 
         classpath to use build/contrib/*.jar
@@ -160,7 +161,7 @@
       <sysproperty key="build.test" value="${build.test}"/>
       <sysproperty key="contrib.name" value="${name}"/>
       <sysproperty key="hadoop.log.dir" value="${pig.log.dir}"/>
-      
+      <sysproperty key="whichCluster" value="miniCluster"/>
       <!-- requires fork=yes for: 
         relative File paths to use the specified user.dir 
         classpath to use build/contrib/*.jar
@@ -182,6 +183,7 @@
           <not>
             <filename name="**/TestColumnSecurity.java"/>
           </not>
+
         </fileset> 
       </batchtest>
     </junit>

Modified: hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/BasicTableOutputFormat.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/BasicTableOutputFormat.java?rev=887387&r1=887386&r2=887387&view=diff
==============================================================================
--- hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/BasicTableOutputFormat.java (original)
+++ hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/BasicTableOutputFormat.java Fri Dec  4 22:20:53 2009
@@ -17,11 +17,14 @@
 package org.apache.hadoop.zebra.mapred;
 
 import java.io.IOException;
+import org.apache.hadoop.util.StringUtils;
+import org.apache.hadoop.conf.Configuration;
 
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.io.BytesWritable;
 import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.Mapper;
 import org.apache.hadoop.mapred.OutputFormat;
 import org.apache.hadoop.mapred.RecordWriter;
 import org.apache.hadoop.mapred.Reporter;
@@ -34,6 +37,7 @@
 import org.apache.hadoop.zebra.schema.Schema;
 import org.apache.pig.data.Tuple;
 import org.apache.hadoop.zebra.pig.comparator.*;
+import org.apache.hadoop.util.ReflectionUtils;
 
 
 /**
@@ -54,11 +58,38 @@
  * The above code does the following things:
  * <UL>
  * <LI>Set the output format class to BasicTableOutputFormat.
- * <LI>Set the path to the BasicTable to be created.
+ * <LI>Set the single path to the BasicTable to be created.
  * <LI>Set the schema of the BasicTable to be created. In this case, the
  * to-be-created BasicTable contains three columns with names "Name", "Age",
  * "Salary", "BonusPct".
  * </UL>
+ * 
+ * To create multiple output paths. ZebraOutputPartitoner interface needs to be implemented
+ * <pre>
+ * String multiLocs = &quot;commaSeparatedPaths&quot;    
+ * jobConf.setOutputFormat(BasicTableOutputFormat.class);
+ * BasicTableOutputFormat.setMultipleOutputPaths(jobConf, multiLocs);
+ * jobConf.setOutputFormat(BasicTableOutputFormat.class);
+ * BasicTableOutputFormat.setSchema(jobConf, &quot;Name, Age, Salary, BonusPct&quot;);
+ * BasicTableOutputFormat.setZebraOutputPartitionClass(
+ * 		jobConf, MultipleOutputsTest.OutputPartitionerClass.class);
+ * </pre>
+ * 
+ * 
+ * The user ZebraOutputPartitionClass should like this
+ * 
+ * <pre>
+ * 
+ *   static class OutputPartitionerClass implements ZebraOutputPartition {
+ *   @Override
+ *	  public int getOutputPartition(BytesWritable key, Tuple value) {		 
+ *
+ *        return someIndexInOutputParitionlist0;
+ *	  }
+ * 
+ * </pre>
+ * 
+ * 
  * The user Reducer code (or similarly Mapper code if it is a Map-only job)
  * should look like the following:
  * 
@@ -113,6 +144,7 @@
 public class BasicTableOutputFormat implements
     OutputFormat<BytesWritable, Tuple> {
   private static final String OUTPUT_PATH = "mapred.lib.table.output.dir";
+  public static final String MULTI_OUTPUT_PATH = "mapred.lib.table.multi.output.dirs";
   private static final String OUTPUT_SCHEMA = "mapred.lib.table.output.schema";
   private static final String OUTPUT_STORAGEHINT =
       "mapred.lib.table.output.storagehint";
@@ -120,7 +152,109 @@
       "mapred.lib.table.output.sortcolumns";
   private static final String OUTPUT_COMPARATOR =
       "mapred.lib.table.output.comparator";
+  static final String IS_MULTI = "multi";
+  public static final String ZEBRA_OUTPUT_PARTITIONER_CLASS = "zebra.output.partitioner.class";
+  
+  
+  /**
+   * Set the multiple output paths of the BasicTable in JobConf
+   * 
+   * @param conf
+   *          The JobConf object.
+   * @param commaSeparatedLocations
+   *          The comma separated output paths to the tables. 
+   *          The path must either not existent, or must be an empty directory.
+   * @param theClass
+   * 	      Zebra output partitoner class
+   */
+  
+  public static void setMultipleOutputs(JobConf conf, String commaSeparatedLocations, Class<? extends ZebraOutputPartition> theClass)
+	throws IOException {
+	  
+	conf.set(MULTI_OUTPUT_PATH, commaSeparatedLocations);
+
+	if(conf.getBoolean(IS_MULTI, true) == false) {
+      throw new IllegalArgumentException("Job has been setup as single output path");
+	}
+	conf.setBoolean(IS_MULTI, true);
+	setZebraOutputPartitionClass(conf, theClass);
+	  
+  }
+  
+  /**
+   * Set the multiple output paths of the BasicTable in JobConf
+   * 
+   * @param conf
+   *          The JobConf object.
+   * @return path
+   *          The comma separated output paths to the tables. 
+   *          The path must either not existent, or must be an empty directory.
+   */
+  
+  public static Path[] getOutputPaths(JobConf conf)
+	throws IOException {
+
+	Path[] result;
+	String paths = conf.get(MULTI_OUTPUT_PATH);
+	String path = conf.get(OUTPUT_PATH);
+	
+	if(paths != null && path != null) {
+		throw new IllegalArgumentException("Illegal output paths specs. Both multi and single output locs are set");
+	}	
+	
+	if(conf.getBoolean(IS_MULTI, false) == true) {	  
+	    if (paths == null || paths.equals("")) {
+	    	throw new IllegalArgumentException("Illegal multi output paths");
+	    }	    
+		String [] list = StringUtils.split(paths);
+	    result = new Path[list.length];
+	    for (int i = 0; i < list.length; i++) {
+	      result[i] = new Path(StringUtils.unEscapeString(list[i]));
+	    }
+	} else {
+	    if (path == null || path.equals("")) {
+	      throw new IllegalArgumentException("Cannot find output path");
+	    }	    
+		result = new Path[1];
+		result[0] = new Path(path);
+	}
 
+    return result;
+	  
+  }
+  
+  
+  private static void setZebraOutputPartitionClass(
+		  JobConf conf, Class<? extends ZebraOutputPartition> theClass) throws IOException {
+	    if (!ZebraOutputPartition.class.isAssignableFrom(theClass))
+	        throw new IOException(theClass+" not "+ZebraOutputPartition.class.getName());
+	      conf.set(ZEBRA_OUTPUT_PARTITIONER_CLASS, theClass.getName());
+  }
+  
+
+  public static Class<? extends ZebraOutputPartition> getZebraOutputPartitionClass(JobConf conf) throws IOException {
+
+	Class<?> theClass;	  
+    String valueString = conf.get(ZEBRA_OUTPUT_PARTITIONER_CLASS);
+    if (valueString == null)
+    	throw new IOException("zebra output partitioner class not found");
+    try {
+    	theClass = conf.getClassByName(valueString);
+    } catch (ClassNotFoundException e) {
+      throw new IOException(e);
+    }	  
+
+    if (theClass != null && !ZebraOutputPartition.class.isAssignableFrom(theClass))
+        throw new IOException(theClass+" not "+ZebraOutputPartition.class.getName());
+    else if (theClass != null)
+      return theClass.asSubclass(ZebraOutputPartition.class);
+    else
+      return null;
+    
+  }
+  
+  
+  
   /**
    * Set the output path of the BasicTable in JobConf
    * 
@@ -132,6 +266,11 @@
    */
   public static void setOutputPath(JobConf conf, Path path) {
     conf.set(OUTPUT_PATH, path.toString());
+	if(conf.getBoolean(IS_MULTI, false) == true) {
+	      throw new IllegalArgumentException("Job has been setup as multi output paths");
+	}
+	conf.setBoolean(IS_MULTI, false);
+
   }
 
   /**
@@ -353,12 +492,14 @@
    * @return The output BasicTable.Writer object.
    * @throws IOException
    */
-  private static BasicTable.Writer getOutput(JobConf conf) throws IOException {
-    String path = conf.get(OUTPUT_PATH);
-    if (path == null) {
-      throw new IllegalArgumentException("Cannot find output path");
-    }
-    return new BasicTable.Writer(new Path(path), conf);
+  private static BasicTable.Writer[] getOutput(JobConf conf) throws IOException {
+	Path[] paths = getOutputPaths(conf);
+	BasicTable.Writer[] writers = new BasicTable.Writer[paths.length]; 
+	for(int i = 0; i < paths.length; i++) {
+		writers[i] = new BasicTable.Writer(paths[i], conf);
+	}
+	
+	return writers;
   }
 
   /**
@@ -371,10 +512,6 @@
   @Override
   public void checkOutputSpecs(FileSystem ignored, JobConf conf)
       throws IOException {
-    String path = conf.get(OUTPUT_PATH);
-    if (path == null) {
-      throw new IllegalArgumentException("Cannot find output path");
-    }
 
     String schema = conf.get(OUTPUT_SCHEMA);
     if (schema == null) {
@@ -389,10 +526,14 @@
     catch (ParseException e) {
       throw new IOException(e);
     }
-    BasicTable.Writer writer =
-        new BasicTable.Writer(new Path(path), schema, storehint, sortColumns, comparator, conf);
-
-    writer.finish();
+    
+    Path [] paths = getOutputPaths(conf);
+    
+    for(int i = 0; i < paths.length; ++i) {
+    	BasicTable.Writer writer =
+    		new BasicTable.Writer(paths[i], schema, storehint, sortColumns, comparator, conf);
+    	writer.finish();
+    }	
   }
 
   /**
@@ -403,10 +544,6 @@
   public RecordWriter<BytesWritable, Tuple> getRecordWriter(FileSystem ignored,
       JobConf conf, String name, Progressable progress) throws IOException {
     String path = conf.get(OUTPUT_PATH);
-    if (path == null) {
-      throw new IllegalArgumentException("Cannot find output path");
-    }
-
     return new TableRecordWriter(path, name, conf, progress);
   }
 
@@ -419,8 +556,12 @@
    * @throws IOException
    */
   public static void close(JobConf conf) throws IOException {
-    BasicTable.Writer table = getOutput(conf);
-    table.close();
+    BasicTable.Writer tables[] = getOutput(conf);
+    
+    for(int i =0; i < tables.length; ++i) {
+        tables[i].close();    	
+    }
+    
   }
 }
 
@@ -428,25 +569,51 @@
  * Adaptor class for BasicTable RecordWriter.
  */
 class TableRecordWriter implements RecordWriter<BytesWritable, Tuple> {
-  private final TableInserter inserter;
+  private final TableInserter inserter[];
   private final Progressable progress;
+  private org.apache.hadoop.zebra.mapred.ZebraOutputPartition op = null;
 
+  
   public TableRecordWriter(String path, String name, JobConf conf,
       Progressable progress) throws IOException {
-    BasicTable.Writer writer = new BasicTable.Writer(new Path(path), conf);
-    this.inserter = writer.getInserter(name, true);
+	
+	if(conf.getBoolean(BasicTableOutputFormat.IS_MULTI, false) == true) {	  
+      op = (org.apache.hadoop.zebra.mapred.ZebraOutputPartition) 
+    	  ReflectionUtils.newInstance(BasicTableOutputFormat.getZebraOutputPartitionClass(conf), conf);
+
+	}  
+    Path [] paths = BasicTableOutputFormat.getOutputPaths(conf);
+    inserter = new TableInserter[paths.length];
+    for(int i = 0; i < paths.length; ++i) {
+    	BasicTable.Writer writer =
+    		new BasicTable.Writer(paths[i], conf);
+    	this.inserter[i] = writer.getInserter(name, true);
+    }	
+	  
     this.progress = progress;
   }
 
   @Override
   public void close(Reporter reporter) throws IOException {
-    inserter.close();
+    for(int i = 0; i < this.inserter.length; ++i) {  
+      inserter[i].close();
+    }  
     reporter.progress();
   }
 
   @Override
   public void write(BytesWritable key, Tuple value) throws IOException {
-    inserter.insert(key, value);
-    progress.progress();
+	  
+	if(op != null ) {	  
+		int idx = op.getOutputPartition(key, value);
+		if(idx < 0 || (idx >= inserter.length)) {
+			throw new IllegalArgumentException("index returned by getOutputPartition is out of range");
+		}
+		inserter[idx].insert(key, value);
+	} else {
+		inserter[0].insert(key, value);
+	}
+	progress.progress();
+
   }
 }

Added: hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/ZebraOutputPartition.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/ZebraOutputPartition.java?rev=887387&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/ZebraOutputPartition.java (added)
+++ hadoop/pig/branches/branch-0.6/contrib/zebra/src/java/org/apache/hadoop/zebra/mapred/ZebraOutputPartition.java Fri Dec  4 22:20:53 2009
@@ -0,0 +1,44 @@
+/**
+ * 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.hadoop.zebra.mapred;
+
+import java.io.IOException;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.conf.*;
+import org.apache.hadoop.io.BytesWritable;
+import org.apache.pig.data.Tuple;
+
+
+public abstract class ZebraOutputPartition  implements Configurable{
+
+  protected  Configuration jobConf;	
+	
+  @Override
+  public void setConf( Configuration conf) {
+	  jobConf = conf;
+  }
+
+  @Override
+  public Configuration getConf( ) {
+	  return jobConf;
+  }
+  
+  public abstract int getOutputPartition(BytesWritable key, Tuple Value)
+  throws IOException;
+}

Added: hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs.java?rev=887387&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs.java (added)
+++ hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs.java Fri Dec  4 22:20:53 2009
@@ -0,0 +1,785 @@
+/**
+ * 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.hadoop.zebra.mapred;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.TreeMap;
+
+import junit.framework.Assert;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocalFileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RawLocalFileSystem;
+import org.apache.hadoop.io.BytesWritable;
+import org.apache.hadoop.io.LongWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapred.TextInputFormat;
+import org.apache.hadoop.mapred.TextOutputFormat;
+import org.apache.hadoop.mapred.lib.MultipleOutputs;
+import org.apache.hadoop.zebra.mapred.BasicTableOutputFormat;
+import org.apache.hadoop.zebra.mapred.TestBasicTableIOFormatLocalFS.InvIndex;
+import org.apache.hadoop.zebra.parser.ParseException;
+import org.apache.hadoop.zebra.schema.Schema;
+import org.apache.hadoop.zebra.types.TypesUtils;
+import org.apache.pig.ExecType;
+import org.apache.pig.PigServer;
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.backend.hadoop.datastorage.ConfigurationUtil;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.DefaultTuple;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.test.MiniCluster;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * This is a sample a complete MR sample code for Table. It doens't contain
+ * 'read' part. But, it should be similar and easier to write. Refer to test
+ * cases in the same directory.
+ * 
+ * Assume the input files contain rows of word and count, separated by a space:
+ * 
+ * <pre>
+ * us 2
+ * japan 2
+ * india 4
+ * us 2
+ * japan 1
+ * india 3
+ * nouse 5
+ * nowhere 4
+ * 
+ * 
+ */
+public class TestMultipleOutputs {
+
+  static String inputPath;
+  static String inputFileName = "multi-input.txt";
+  protected static ExecType execType = ExecType.MAPREDUCE;
+  private static MiniCluster cluster;
+  protected static PigServer pigServer;
+  // private static Path pathWorking, pathTable1, path2, path3,
+  // pathTable4, pathTable5;
+  private static Configuration conf;
+  public static String sortKey = null;
+
+  private static FileSystem fs;
+
+  private static String zebraJar;
+  private static String whichCluster;
+  private static String multiLocs;
+  private static String strTable1 = null;
+  private static String strTable2 = null;
+  private static String strTable3 = null;
+
+  @BeforeClass
+  public static void setUpOnce() throws IOException {
+    if (System.getenv("hadoop.log.dir") == null) {
+      String base = new File(".").getPath(); // getAbsolutePath();
+      System
+          .setProperty("hadoop.log.dir", new Path(base).toString() + "./logs");
+    }
+
+    if (System.getProperty("whichCluster") == null) {
+      System.setProperty("whichCluster", "realCluster");
+      System.out.println("should be called");
+      whichCluster = System.getProperty("whichCluster");
+    } else {
+      whichCluster = System.getProperty("whichCluster");
+    }
+
+    System.out.println("clusterddddd: " + whichCluster);
+    System.out.println(" get env hadoop home: " + System.getenv("HADOOP_HOME"));
+    System.out.println(" get env user name: " + System.getenv("USER"));
+    if ((whichCluster.equalsIgnoreCase("realCluster") && System
+        .getenv("HADOOP_HOME") == null)) {
+      System.out.println("Please set HADOOP_HOME");
+      System.exit(0);
+    }
+
+    conf = new Configuration();
+
+    if ((whichCluster.equalsIgnoreCase("realCluster") && System.getenv("USER") == null)) {
+      System.out.println("Please set USER");
+      System.exit(0);
+    }
+    zebraJar = System.getenv("HADOOP_HOME") + "/lib/zebra.jar";
+
+    File file = new File(zebraJar);
+    if (!file.exists() && whichCluster.equalsIgnoreCase("realCluster")) {
+      System.out.println("Please put zebra.jar at hadoop_home/lib");
+      System.exit(0);
+    }
+
+    // set inputPath and output path
+    String workingDir = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      inputPath = new String("/user/" + System.getenv("USER") + "/"
+          + inputFileName);
+      System.out.println("inputPath: " + inputPath);
+      multiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + "," + "/user/" + System.getenv("USER") + "/" + "india" + ","
+          + "/user/" + System.getenv("USER") + "/" + "japan");
+      fs = new Path(inputPath).getFileSystem(conf);
+
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      workingDir = fs.getWorkingDirectory().toString().split(":")[1];
+      inputPath = new String(workingDir + "/" + inputFileName);
+      System.out.println("inputPath: " + inputPath);
+      multiLocs = new String(workingDir + "/" + "us" + "," + workingDir + "/"
+          + "india" + "," + workingDir + "/" + "japan");
+    }
+    writeToFile(inputPath);
+    // check inputPath existence
+    File inputFile = new File(inputPath);
+    if (!inputFile.exists() && whichCluster.equalsIgnoreCase("realCluster")) {
+      System.out.println("Please put inputFile in hdfs: " + inputPath);
+      // System.exit(0);
+    }
+    if (!inputFile.exists() && whichCluster.equalsIgnoreCase("miniCluster")) {
+      System.out
+          .println("Please put inputFile under workingdir. working dir is : "
+              + workingDir);
+      System.exit(0);
+    }
+
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      pigServer = new PigServer(ExecType.MAPREDUCE, ConfigurationUtil
+          .toProperties(conf));
+      pigServer.registerJar(zebraJar);
+
+    }
+
+    if (whichCluster.equalsIgnoreCase("miniCluster")) {
+      if (execType == ExecType.MAPREDUCE) {
+        cluster = MiniCluster.buildCluster();
+        pigServer = new PigServer(ExecType.MAPREDUCE, cluster.getProperties());
+        fs = cluster.getFileSystem();
+
+      } else {
+        pigServer = new PigServer(ExecType.LOCAL);
+      }
+    }
+  }
+  @AfterClass
+  public static void tearDown() throws Exception {
+    if (whichCluster.equalsIgnoreCase("miniCluster")) {
+    pigServer.shutdown();
+    }
+  }
+  public String getCurrentMethodName() {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    PrintWriter pw = new PrintWriter(baos);
+    (new Throwable()).printStackTrace(pw);
+    pw.flush();
+    String stackTrace = baos.toString();
+    pw.close();
+
+    StringTokenizer tok = new StringTokenizer(stackTrace, "\n");
+    tok.nextToken(); // 'java.lang.Throwable'
+    tok.nextToken(); // 'at ...getCurrentMethodName'
+    String l = tok.nextToken(); // 'at ...<caller to getCurrentRoutine>'
+    // Parse line 3
+    tok = new StringTokenizer(l.trim(), " <(");
+    String t = tok.nextToken(); // 'at'
+    t = tok.nextToken(); // '...<caller to getCurrentRoutine>'
+    StringTokenizer st = new StringTokenizer(t, ".");
+    String methodName = null;
+    while (st.hasMoreTokens()) {
+      methodName = st.nextToken();
+    }
+    return methodName;
+  }
+
+  public static void writeToFile (String inputFile) throws IOException{
+    if (whichCluster.equalsIgnoreCase("miniCluster")){
+    FileWriter fstream = new FileWriter(inputFile);
+    BufferedWriter out = new BufferedWriter(fstream);
+    out.write("us 2\n");
+    out.write("japan 2\n");
+    out.write("india 4\n");
+    out.write("us 2\n");
+    out.write("japan 1\n");
+    out.write("india 3\n");
+    out.write("nouse 5\n");
+    out.write("nowhere 4\n");
+    out.close();
+    }
+    if (whichCluster.equalsIgnoreCase("realCluster")){
+    FSDataOutputStream fout = fs.create(new Path (inputFile));
+    fout.writeBytes("us 2\n");
+    fout.writeBytes("japan 2\n");
+    fout.writeBytes("india 4\n");
+    fout.writeBytes("us 2\n");
+    fout.writeBytes("japan 1\n");
+    fout.writeBytes("india 3\n");
+    fout.writeBytes("nouse 5\n");
+    fout.writeBytes("nowhere 4\n");
+    fout.close();
+    }
+  }
+  
+  public Path generateOutPath(String currentMethod) {
+    Path outPath = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      outPath = new Path("/user/" + System.getenv("USER") + "/multiOutput/"
+          + currentMethod);
+    } else {
+      String workingDir = fs.getWorkingDirectory().toString().split(":")[1];
+      outPath = new Path(workingDir + "/multiOutput/" + currentMethod);
+      System.out.println("output file: " + outPath.toString());
+    }
+    return outPath;
+  }
+
+  public void removeDir(Path outPath) throws IOException {
+    String command = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      command = System.getenv("HADOOP_HOME") + "/bin/hadoop fs -rmr "
+          + outPath.toString();
+    } else {
+      StringTokenizer st = new StringTokenizer(outPath.toString(), ":");
+      int count = 0;
+      String file = null;
+      while (st.hasMoreElements()) {
+        count++;
+        String token = st.nextElement().toString();
+        if (count == 2)
+          file = token;
+      }
+      command = "rm -rf " + file;
+    }
+    Runtime runtime = Runtime.getRuntime();
+    Process proc = runtime.exec(command);
+    int exitVal = -1;
+    try {
+      exitVal = proc.waitFor();
+    } catch (InterruptedException e) {
+      System.err.println(e);
+    }
+
+  }
+
+  public static void getTablePaths(String myMultiLocs) {
+    StringTokenizer st = new StringTokenizer(myMultiLocs, ",");
+
+    // get how many tokens inside st object
+    System.out.println("tokens count: " + st.countTokens());
+    int count = 0;
+
+    // iterate st object to get more tokens from it
+    while (st.hasMoreElements()) {
+      count++;
+      String token = st.nextElement().toString();
+      if (count == 1)
+        strTable1 = token;
+      if (count == 2)
+        strTable2 = token;
+      if (count == 3)
+        strTable3 = token;
+      System.out.println("token = " + token);
+    }
+  }
+
+  public static void checkTable(String myMultiLocs) throws IOException {
+    System.out.println("myMultiLocs:" + myMultiLocs);
+    System.out.println("sorgetTablePathst key:" + sortKey);
+
+    getTablePaths(myMultiLocs);
+    String query1 = null;
+    String query2 = null;
+    String query3 = null;
+
+    if (strTable1 != null) {
+
+      query1 = "records1 = LOAD '" + strTable1
+          + "' USING org.apache.hadoop.zebra.pig.TableLoader();";
+    }
+    if (strTable2 != null) {
+      query2 = "records2 = LOAD '" + strTable2
+          + "' USING org.apache.hadoop.zebra.pig.TableLoader();";
+    }
+    if (strTable3 != null) {
+      query3 = "records3 = LOAD '" + strTable3
+          + "' USING org.apache.hadoop.zebra.pig.TableLoader();";
+    }
+
+    int count1 = 0;
+    int count2 = 0;
+    int count3 = 0;
+
+    if (query1 != null) {
+      System.out.println(query1);
+      pigServer.registerQuery(query1);
+      Iterator<Tuple> it = pigServer.openIterator("records1");
+      while (it.hasNext()) {
+        count1++;
+        Tuple RowValue = it.next();
+        System.out.println(RowValue);
+        // test 1 us table
+        if (query1.contains("test1") || query1.contains("test2")) {
+          if (count1 == 1) {
+            Assert.assertEquals("nouse", RowValue.get(0));
+            Assert.assertEquals(5, RowValue.get(1));
+          }
+          if (count1 == 2) {
+            Assert.assertEquals("nowhere", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count1 == 3) {
+            Assert.assertEquals("us", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count1 == 4) {
+            Assert.assertEquals("us", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+        } // test1, test2
+        if (query1.contains("test3")) {
+          if (count1 == 1) {
+            System.out.println("test3: rowvalue: " + RowValue.toString());
+            Assert.assertEquals("us", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count1 == 2) {
+            Assert.assertEquals("us", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count1 == 3) {
+            Assert.assertEquals("nowhere", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count1 == 4) {
+            Assert.assertEquals("nouse", RowValue.get(0));
+            Assert.assertEquals(5, RowValue.get(1));
+          }
+        } // test3
+      }// while
+      if (query1.contains("test1") || query1.contains("test2")
+          || query1.contains("test3")) {
+        Assert.assertEquals(4, count1);
+      }
+    }// if query1 != null
+
+    if (query2 != null) {
+      pigServer.registerQuery(query2);
+      Iterator<Tuple> it = pigServer.openIterator("records2");
+
+      while (it.hasNext()) {
+        count2++;
+        Tuple RowValue = it.next();
+        System.out.println(RowValue);
+
+        // if test1 india table
+        if (query2.contains("test1")) {
+          if (count2 == 1) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(3, RowValue.get(1));
+          }
+          if (count2 == 2) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+        }// if test1
+        if (query1.contains("test2")) {
+          if (count2 == 1) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count2 == 2) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(3, RowValue.get(1));
+          }
+        }// if test2
+
+        if (query1.contains("test3")) {
+          if (count1 == 1) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(3, RowValue.get(1));
+          }
+          if (count1 == 2) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+        } // test3
+
+      }// while
+      if (query2.contains("test1") || query2.contains("test2")
+          || query2.contains("test3")) {
+        Assert.assertEquals(2, count2);
+      }
+    }// if query2 != null
+    // test 1 us table should have 4 lines
+
+    if (query3 != null) {
+      pigServer.registerQuery(query3);
+      Iterator<Tuple> it = pigServer.openIterator("records3");
+
+      while (it.hasNext()) {
+        count3++;
+        Tuple RowValue = it.next();
+        System.out.println(RowValue);
+
+        // if test1 japan table
+        if (query3.contains("test1")) {
+          if (count3 == 1) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(1, RowValue.get(1));
+          }
+          if (count3 == 2) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+        }// if test1 test2 japan table
+
+        if (query3.contains("test2")) {
+          if (count3 == 1) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count3 == 2) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(1, RowValue.get(1));
+          }
+        }// test2
+        if (query1.contains("test3")) {
+          if (count1 == 1) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(1, RowValue.get(1));
+          }
+          if (count1 == 2) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+        } // test3
+
+      }// while
+      if (query3.contains("test1") || query3.contains("test2")
+          || query3.contains("test3")) {
+        Assert.assertEquals(2, count3);
+      }
+    }// if query2 != null
+  }
+
+  @Test
+  public void test1() throws ParseException, IOException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    /*
+     * test combine sort keys
+     */
+    System.out.println("******Start  testcase: " + getCurrentMethodName());
+    sortKey = "word,count";
+    System.out.println("hello sort on word and count");
+    String methodName = getCurrentMethodName();
+    String myMultiLocs = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      myMultiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "india"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "japan"
+          + methodName);
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      myMultiLocs = new String(fs.getWorkingDirectory() + "/" + "us"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "india"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "japan"
+          + methodName);
+    }
+    getTablePaths(myMultiLocs);
+    System.out.println("strTable1: " + strTable1.toString());
+    System.out.println("strTable2: " + strTable2.toString());
+    removeDir(new Path(strTable1));
+    removeDir(new Path(strTable2));
+    removeDir(new Path(strTable3));
+    runMR(myMultiLocs, sortKey);
+    checkTable(myMultiLocs);
+    System.out.println("DONE test 1");
+
+  }
+
+  @Test
+  public void test2() throws ParseException, IOException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    /*
+     * test 'word' sort key
+     */
+    System.out.println("******Start  testcase: " + getCurrentMethodName());
+    sortKey = "word";
+    System.out.println("hello sort on word and count");
+    String methodName = getCurrentMethodName();
+    String myMultiLocs = null;
+    System.out.println("which cluster: " + whichCluster);
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      myMultiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "india"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "japan"
+          + methodName);
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      myMultiLocs = new String(fs.getWorkingDirectory() + "/" + "us"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "india"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "japan"
+          + methodName);
+    }
+    getTablePaths(myMultiLocs);
+    removeDir(new Path(strTable1));
+    removeDir(new Path(strTable2));
+    removeDir(new Path(strTable3));
+    runMR(myMultiLocs, sortKey);
+    checkTable(myMultiLocs);
+  }
+
+  @Test
+  public void test3() throws ParseException, IOException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    /*
+     * test 'count' sort key
+     */
+    // setUpOnce();
+    System.out.println("******Start  testcase: " + getCurrentMethodName());
+    sortKey = "count";
+    System.out.println("hello sort on word and count, which cluster: "
+        + whichCluster);
+    String methodName = getCurrentMethodName();
+    String myMultiLocs = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      myMultiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "india"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "japan"
+          + methodName);
+    } else {
+
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      myMultiLocs = new String(fs.getWorkingDirectory() + "/" + "us"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "india"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "japan"
+          + methodName);
+    }
+    getTablePaths(myMultiLocs);
+    removeDir(new Path(strTable1));
+    removeDir(new Path(strTable2));
+    removeDir(new Path(strTable3));
+    runMR(myMultiLocs, sortKey);
+    checkTable(myMultiLocs);
+  }
+
+  static class MapClass implements
+      Mapper<LongWritable, Text, BytesWritable, Tuple> {
+    private BytesWritable bytesKey;
+    private Tuple tupleRow;
+    private Object javaObj;
+    private JobConf conf;
+
+    @Override
+    public void map(LongWritable key, Text value,
+        OutputCollector<BytesWritable, Tuple> output, Reporter reporter)
+        throws IOException {
+      // value should contain "word count"
+      String[] wdct = value.toString().split(" ");
+      if (wdct.length != 2) {
+        // LOG the error
+        return;
+      }
+
+      byte[] word = wdct[0].getBytes();
+      bytesKey.set(word, 0, word.length);
+      System.out.println("word: " + new String(word));
+      tupleRow.set(0, new String(word));
+      tupleRow.set(1, Integer.parseInt(wdct[1]));
+      System.out.println("count:  " + Integer.parseInt(wdct[1]));
+
+      // This key has to be created by user
+      /*
+       * Tuple userKey = new DefaultTuple(); userKey.append(new String(word));
+       * userKey.append(Integer.parseInt(wdct[1]));
+       */
+      System.out.println("in map, sortkey: " + sortKey);
+      Tuple userKey = new DefaultTuple();
+      if (sortKey.equalsIgnoreCase("word,count")) {
+        userKey.append(new String(word));
+        userKey.append(Integer.parseInt(wdct[1]));
+      }
+
+      if (sortKey.equalsIgnoreCase("count")) {
+        userKey.append(Integer.parseInt(wdct[1]));
+      }
+
+      if (sortKey.equalsIgnoreCase("word")) {
+        userKey.append(new String(word));
+      }
+
+      try {
+
+        /* New M/R Interface */
+        /* Converts user key to zebra BytesWritable key */
+        /* using sort key expr tree */
+        /* Returns a java base object */
+        /* Done for each user key */
+
+        bytesKey = BasicTableOutputFormat.getSortKey(javaObj, userKey);
+      } catch (Exception e) {
+
+      }
+
+      output.collect(bytesKey, tupleRow);
+    }
+
+    @Override
+    public void configure(JobConf job) {
+      bytesKey = new BytesWritable();
+      conf = job;
+      sortKey = job.get("sortKey");
+      try {
+        Schema outSchema = BasicTableOutputFormat.getSchema(job);
+        tupleRow = TypesUtils.createTuple(outSchema);
+        javaObj = BasicTableOutputFormat.getSortKeyGenerator(job);
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      } catch (org.apache.hadoop.zebra.parser.ParseException e) {
+        throw new RuntimeException(e);
+      }
+    }
+
+    @Override
+    public void close() throws IOException {
+      // no-op
+    }
+  }
+
+  static class ReduceClass implements
+      Reducer<BytesWritable, Tuple, BytesWritable, Tuple> {
+    Tuple outRow;
+
+    @Override
+    public void configure(JobConf job) {
+    }
+
+    @Override
+    public void close() throws IOException {
+    }
+
+    public void reduce(BytesWritable key, Iterator<Tuple> values,
+        OutputCollector<BytesWritable, Tuple> output, Reporter reporter)
+        throws IOException {
+      try {
+        for (; values.hasNext();) {
+          output.collect(key, values.next());
+        }
+      } catch (ExecException e) {
+        e.printStackTrace();
+      }
+    }
+
+  }
+
+  static class OutputPartitionerClass extends ZebraOutputPartition {
+
+    @Override
+    public int getOutputPartition(BytesWritable key, Tuple value) {
+
+      // System.out.println(this.jobConf);
+
+      String reg = null;
+      try {
+        reg = (String) (value.get(0));
+      } catch (Exception e) {
+        //
+      }
+
+      if (reg.equals("us"))
+        return 0;
+      if (reg.equals("india"))
+        return 1;
+      if (reg.equals("japan"))
+        return 2;
+
+      return 0;
+    }
+
+  }
+
+  public void runMR(String myMultiLocs, String sortKey) throws ParseException,
+      IOException, Exception, org.apache.hadoop.zebra.parser.ParseException {
+
+    JobConf jobConf = new JobConf();
+    jobConf.setJobName("tableMRSample");
+    jobConf.set("table.output.tfile.compression", "gz");
+    jobConf.set("sortKey", sortKey);
+    // input settings
+    jobConf.setInputFormat(TextInputFormat.class);
+    jobConf.setMapperClass(TestMultipleOutputs.MapClass.class);
+    jobConf.setMapOutputKeyClass(BytesWritable.class);
+    jobConf.setMapOutputValueClass(DefaultTuple.class);
+    FileInputFormat.setInputPaths(jobConf, inputPath);
+
+    jobConf.setNumMapTasks(1);
+
+    // output settings
+
+    jobConf.setOutputFormat(BasicTableOutputFormat.class);
+    BasicTableOutputFormat.setMultipleOutputs(jobConf, myMultiLocs,
+        TestMultipleOutputs.OutputPartitionerClass.class);
+
+    // set the logical schema with 2 columns
+    BasicTableOutputFormat.setSchema(jobConf, "word:string, count:int");
+    // for demo purposes, create 2 physical column groups
+    BasicTableOutputFormat.setStorageHint(jobConf, "[word];[count]");
+    BasicTableOutputFormat.setSortInfo(jobConf, sortKey);
+    System.out.println("in runMR, sortkey: " + sortKey);
+    // set map-only job.
+    jobConf.setNumReduceTasks(1);
+    JobClient.runJob(jobConf);
+    BasicTableOutputFormat.close(jobConf);
+  }
+
+  public static void main(String[] args) throws ParseException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    TestMultipleOutputs test = new TestMultipleOutputs();
+    TestMultipleOutputs.setUpOnce();
+    System.out.println("after setup");
+    test.test1();
+    test.test2();
+    test.test3();
+
+  }
+}

Added: hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs2.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs2.java?rev=887387&view=auto
==============================================================================
--- hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs2.java (added)
+++ hadoop/pig/branches/branch-0.6/contrib/zebra/src/test/org/apache/hadoop/zebra/mapred/TestMultipleOutputs2.java Fri Dec  4 22:20:53 2009
@@ -0,0 +1,784 @@
+/**
+ * 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.hadoop.zebra.mapred;
+
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.TreeMap;
+
+import junit.framework.Assert;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.LocalFileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.RawLocalFileSystem;
+import org.apache.hadoop.io.BytesWritable;
+import org.apache.hadoop.io.LongWritable;
+import org.apache.hadoop.io.Text;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reducer;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hadoop.mapred.TextInputFormat;
+import org.apache.hadoop.mapred.TextOutputFormat;
+import org.apache.hadoop.mapred.lib.MultipleOutputs;
+import org.apache.hadoop.zebra.mapred.BasicTableOutputFormat;
+import org.apache.hadoop.zebra.mapred.TestBasicTableIOFormatLocalFS.InvIndex;
+import org.apache.hadoop.zebra.parser.ParseException;
+import org.apache.hadoop.zebra.schema.Schema;
+import org.apache.hadoop.zebra.types.TypesUtils;
+import org.apache.pig.ExecType;
+import org.apache.pig.PigServer;
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.backend.hadoop.datastorage.ConfigurationUtil;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.DefaultTuple;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.test.MiniCluster;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * This is a sample a complete MR sample code for Table. It doens't contain
+ * 'read' part. But, it should be similar and easier to write. Refer to test
+ * cases in the same directory.
+ * 
+ * Assume the input files contain rows of word and count, separated by a space:
+ * 
+ * <pre>
+ * us 2
+ * japan 2
+ * india 4
+ * us 2
+ * japan 1
+ * india 3
+ * nouse 5
+ * nowhere 4
+ * 
+ * 
+ */
+public class TestMultipleOutputs2 {
+
+  static String inputPath;
+  static String inputFileName = "multi-input.txt";
+  protected static ExecType execType = ExecType.MAPREDUCE;
+  private static MiniCluster cluster;
+  protected static PigServer pigServer;
+  // private static Path pathWorking, pathTable1, path2, path3,
+  // pathTable4, pathTable5;
+  private static Configuration conf;
+  public static String sortKey = null;
+
+  private static FileSystem fs;
+
+  private static String zebraJar;
+  private static String whichCluster;
+  private static String multiLocs;
+  private static String strTable1 = null;
+  private static String strTable2 = null;
+  private static String strTable3 = null;
+
+  @BeforeClass
+  public static void setUpOnce() throws IOException {
+    if (System.getenv("hadoop.log.dir") == null) {
+      String base = new File(".").getPath(); // getAbsolutePath();
+      System
+          .setProperty("hadoop.log.dir", new Path(base).toString() + "./logs");
+    }
+
+    if (System.getProperty("whichCluster") == null) {
+      System.setProperty("whichCluster", "realCluster");
+      System.out.println("should be called");
+      whichCluster = System.getProperty("whichCluster");
+    } else {
+      whichCluster = System.getProperty("whichCluster");
+    }
+
+    System.out.println("clusterddddd: " + whichCluster);
+    System.out.println(" get env hadoop home: " + System.getenv("HADOOP_HOME"));
+    System.out.println(" get env user name: " + System.getenv("USER"));
+    if ((whichCluster.equalsIgnoreCase("realCluster") && System
+        .getenv("HADOOP_HOME") == null)) {
+      System.out.println("Please set HADOOP_HOME");
+      System.exit(0);
+    }
+
+    conf = new Configuration();
+
+    if ((whichCluster.equalsIgnoreCase("realCluster") && System.getenv("USER") == null)) {
+      System.out.println("Please set USER");
+      System.exit(0);
+    }
+    zebraJar = System.getenv("HADOOP_HOME") + "/lib/zebra.jar";
+
+    File file = new File(zebraJar);
+    if (!file.exists() && whichCluster.equalsIgnoreCase("realCluster")) {
+      System.out.println("Please put zebra.jar at hadoop_home/lib");
+      System.exit(0);
+    }
+
+    // set inputPath and output path
+    String workingDir = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      inputPath = new String("/user/" + System.getenv("USER") + "/"
+          + inputFileName);
+      System.out.println("inputPath: " + inputPath);
+      multiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + "," + "/user/" + System.getenv("USER") + "/" + "india" + ","
+          + "/user/" + System.getenv("USER") + "/" + "japan");
+      fs = new Path(inputPath).getFileSystem(conf);
+
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      workingDir = fs.getWorkingDirectory().toString().split(":")[1];
+      inputPath = new String(workingDir + "/" + inputFileName);
+      System.out.println("inputPath: " + inputPath);
+      multiLocs = new String(workingDir + "/" + "us" + "," + workingDir + "/"
+          + "india" + "," + workingDir + "/" + "japan");
+    }
+    writeToFile(inputPath);
+    // check inputPath existence
+    File inputFile = new File(inputPath);
+    if (!inputFile.exists() && whichCluster.equalsIgnoreCase("realCluster")) {
+      System.out.println("Please put inputFile in hdfs: " + inputPath);
+      // System.exit(0);
+    }
+    if (!inputFile.exists() && whichCluster.equalsIgnoreCase("miniCluster")) {
+      System.out
+          .println("Please put inputFile under workingdir. working dir is : "
+              + workingDir);
+      System.exit(0);
+    }
+
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      pigServer = new PigServer(ExecType.MAPREDUCE, ConfigurationUtil
+          .toProperties(conf));
+      pigServer.registerJar(zebraJar);
+
+    }
+
+    if (whichCluster.equalsIgnoreCase("miniCluster")) {
+      if (execType == ExecType.MAPREDUCE) {
+        cluster = MiniCluster.buildCluster();
+        pigServer = new PigServer(ExecType.MAPREDUCE, cluster.getProperties());
+        fs = cluster.getFileSystem();
+
+      } else {
+        pigServer = new PigServer(ExecType.LOCAL);
+      }
+    }
+  }
+
+  public String getCurrentMethodName() {
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    PrintWriter pw = new PrintWriter(baos);
+    (new Throwable()).printStackTrace(pw);
+    pw.flush();
+    String stackTrace = baos.toString();
+    pw.close();
+
+    StringTokenizer tok = new StringTokenizer(stackTrace, "\n");
+    tok.nextToken(); // 'java.lang.Throwable'
+    tok.nextToken(); // 'at ...getCurrentMethodName'
+    String l = tok.nextToken(); // 'at ...<caller to getCurrentRoutine>'
+    // Parse line 3
+    tok = new StringTokenizer(l.trim(), " <(");
+    String t = tok.nextToken(); // 'at'
+    t = tok.nextToken(); // '...<caller to getCurrentRoutine>'
+    StringTokenizer st = new StringTokenizer(t, ".");
+    String methodName = null;
+    while (st.hasMoreTokens()) {
+      methodName = st.nextToken();
+    }
+    return methodName;
+  }
+  public static void writeToFile (String inputFile) throws IOException{
+    if (whichCluster.equalsIgnoreCase("miniCluster")){
+    FileWriter fstream = new FileWriter(inputFile);
+    BufferedWriter out = new BufferedWriter(fstream);
+    out.write("us 2\n");
+    out.write("japan 2\n");
+    out.write("india 4\n");
+    out.write("us 2\n");
+    out.write("japan 1\n");
+    out.write("india 3\n");
+    out.write("nouse 5\n");
+    out.write("nowhere 4\n");
+    out.close();
+    }
+    if (whichCluster.equalsIgnoreCase("realCluster")){
+    FSDataOutputStream fout = fs.create(new Path (inputFile));
+    fout.writeBytes("us 2\n");
+    fout.writeBytes("japan 2\n");
+    fout.writeBytes("india 4\n");
+    fout.writeBytes("us 2\n");
+    fout.writeBytes("japan 1\n");
+    fout.writeBytes("india 3\n");
+    fout.writeBytes("nouse 5\n");
+    fout.writeBytes("nowhere 4\n");
+    fout.close();
+    }
+  }
+  
+
+  public Path generateOutPath(String currentMethod) {
+    Path outPath = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      outPath = new Path("/user/" + System.getenv("USER") + "/multiOutput/"
+          + currentMethod);
+    } else {
+      String workingDir = fs.getWorkingDirectory().toString().split(":")[1];
+      outPath = new Path(workingDir + "/multiOutput/" + currentMethod);
+      System.out.println("output file: " + outPath.toString());
+    }
+    return outPath;
+  }
+
+  public void removeDir(Path outPath) throws IOException {
+    String command = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      command = System.getenv("HADOOP_HOME") + "/bin/hadoop fs -rmr "
+          + outPath.toString();
+    } else {
+      StringTokenizer st = new StringTokenizer(outPath.toString(), ":");
+      int count = 0;
+      String file = null;
+      while (st.hasMoreElements()) {
+        count++;
+        String token = st.nextElement().toString();
+        if (count == 2)
+          file = token;
+      }
+      command = "rm -rf " + file;
+    }
+    Runtime runtime = Runtime.getRuntime();
+    Process proc = runtime.exec(command);
+    int exitVal = -1;
+    try {
+      exitVal = proc.waitFor();
+    } catch (InterruptedException e) {
+      System.err.println(e);
+    }
+
+  }
+
+  public static void getTablePaths(String myMultiLocs) {
+    StringTokenizer st = new StringTokenizer(myMultiLocs, ",");
+
+    // get how many tokens inside st object
+    System.out.println("tokens count: " + st.countTokens());
+    int count = 0;
+
+    // iterate st object to get more tokens from it
+    while (st.hasMoreElements()) {
+      count++;
+      String token = st.nextElement().toString();
+      if (whichCluster.equalsIgnoreCase("miniCluster")) {
+        System.out.println("in mini, token: "+token); 
+        //in mini, token: file:/homes/jing1234/grid/multipleoutput/pig-table/contrib/zebra/ustest3
+        if (count == 1)
+          strTable1 = token;
+        if (count == 2)
+          strTable2 = token;
+        if (count == 3)
+          strTable3 = token;
+      }
+      if (whichCluster.equalsIgnoreCase("realCluster")) {
+        System.out.println("in real, token: "+token);
+        //in real, token: /user/hadoopqa/ustest3
+        //note: no prefix file:  in real cluster
+        if (count == 1)
+          strTable1 = token;
+        if (count == 2)
+          strTable2 = token;
+        if (count == 3)
+          strTable3 = token;
+      }
+      
+    }
+  }
+  public static void checkTableExists(boolean expected, String strDir) throws IOException{
+  
+     File theDir = null; 
+     boolean actual = false;
+     if (whichCluster.equalsIgnoreCase("miniCluster")){
+     theDir = new File(strDir.split(":")[1]);
+     actual = theDir.exists();
+     
+     }
+     if (whichCluster.equalsIgnoreCase("realCluster")){
+       theDir = new File(strDir.split(":")[0]);
+       actual = fs.exists(new Path (theDir.toString()));
+        }
+     System.out.println("the dir : "+ theDir.toString());
+     //the dir : /user/hadoopqa/ustest3
+      //the dir : /homes/jing1234/grid/multipleoutput/pig-table/contrib/zebra/ustest3
+    
+     if (actual != expected){
+       Assert.fail("dir exists or not is different from what expected.");
+     }
+   }
+  public static void checkTable(String myMultiLocs) throws IOException {
+    System.out.println("myMultiLocs:" + myMultiLocs);
+    System.out.println("sorgetTablePathst key:" + sortKey);
+
+    getTablePaths(myMultiLocs);
+    String query1 = null;
+    String query2 = null;
+ 
+    if (strTable1 != null) {
+
+      query1 = "records1 = LOAD '" + strTable1
+          + "' USING org.apache.hadoop.zebra.pig.TableLoader();";
+    }
+    if (strTable2 != null) {
+      query2 = "records2 = LOAD '" + strTable2
+          + "' USING org.apache.hadoop.zebra.pig.TableLoader();";
+    }
+
+    int count1 = 0;
+    int count2 = 0;
+  
+    if (query1 != null) {
+      System.out.println(query1);
+      pigServer.registerQuery(query1);
+      Iterator<Tuple> it = pigServer.openIterator("records1");
+      while (it.hasNext()) {
+        count1++;
+        Tuple RowValue = it.next();
+        System.out.println(RowValue);
+        // test 1 us table
+        if (query1.contains("test1") || query1.contains("test2")|| query1.contains("test3")) {
+          
+          if (count1 == 1) {
+            Assert.assertEquals("us", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count1 == 2) {
+            Assert.assertEquals("us", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+        } // test1, test2
+    
+      }// while
+      if (query1.contains("test1") || query1.contains("test2")
+          || query1.contains("test3")) {
+        Assert.assertEquals(2, count1);
+      }
+    }// if query1 != null
+
+    if (query2 != null) {
+      pigServer.registerQuery(query2);
+      Iterator<Tuple> it = pigServer.openIterator("records2");
+
+      while (it.hasNext()) {
+        count2++;
+        Tuple RowValue = it.next();
+        System.out.println(RowValue);
+
+        // if test1 other table
+        if (query2.contains("test1")) {
+          if (count2 == 1) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(3, RowValue.get(1));
+          }
+          if (count2 == 2) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count2 == 3) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(1, RowValue.get(1));
+          }
+          if (count2 == 4) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          
+          if (count1 == 5) {
+            Assert.assertEquals("nouse", RowValue.get(0));
+            Assert.assertEquals(5, RowValue.get(1));
+          }
+          if (count1 == 6) {
+            Assert.assertEquals("nowhere", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+        }// if test1
+     // if test2 other table
+        if (query2.contains("test2")) {
+          if (count2 == 1) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count2 == 2) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(3, RowValue.get(1));
+          }
+          if (count2 == 3) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count2 == 4) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(1, RowValue.get(1));
+          }
+          
+          if (count1 == 5) {
+            Assert.assertEquals("nouse", RowValue.get(0));
+            Assert.assertEquals(5, RowValue.get(1));
+          }
+          if (count1 == 6) {
+            Assert.assertEquals("nowhere", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+        }// if test2
+        // if test3 other table
+        if (query2.contains("test3")) {
+          if (count2 == 1) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(1, RowValue.get(1));
+          }
+          if (count2 == 2) {
+            Assert.assertEquals("japan", RowValue.get(0));
+            Assert.assertEquals(2, RowValue.get(1));
+          }
+          if (count2 == 3) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(3, RowValue.get(1));
+          }
+          if (count2 == 4) {
+            Assert.assertEquals("india", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count1 == 5) {
+            Assert.assertEquals("nowhere", RowValue.get(0));
+            Assert.assertEquals(4, RowValue.get(1));
+          }
+          if (count1 == 6) {
+            Assert.assertEquals("nouse", RowValue.get(0));
+            Assert.assertEquals(5, RowValue.get(1));
+          }
+          
+        }// if test3
+        
+      }// while
+      if (query2.contains("test1") || query2.contains("test2")
+          || query2.contains("test3")) {
+        Assert.assertEquals(6, count2);
+      }
+    }// if query2 != null
+
+  }
+
+  
+  @Test
+  public void test1() throws ParseException, IOException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    /*
+     * test combine sort keys
+     */
+    System.out.println("******Start  testcase: " + getCurrentMethodName());
+    sortKey = "word,count";
+    System.out.println("hello sort on word and count");
+    String methodName = getCurrentMethodName();
+    String myMultiLocs = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      myMultiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "others"
+          + methodName );
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      myMultiLocs = new String(fs.getWorkingDirectory() + "/" + "us"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "others"
+          + methodName );
+    }
+    getTablePaths(myMultiLocs);
+    System.out.println("strTable1: "+strTable1);
+    removeDir(new Path(strTable1));
+    removeDir(new Path(strTable2));
+    runMR(myMultiLocs, sortKey);
+    checkTable(myMultiLocs);
+    System.out.println("DONE test" +getCurrentMethodName());
+  
+  }
+  @Test
+  public void test2() throws ParseException, IOException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    /*
+     * test word sort keys
+     */
+    System.out.println("******Start  testcase: " + getCurrentMethodName());
+    sortKey = "word";
+    System.out.println("hello sort on word and count");
+    String methodName = getCurrentMethodName();
+    String myMultiLocs = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      myMultiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "others"
+          + methodName );
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      myMultiLocs = new String(fs.getWorkingDirectory() + "/" + "us"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "others"
+          + methodName );
+    }
+    getTablePaths(myMultiLocs);
+    removeDir(new Path(strTable1));
+    removeDir(new Path(strTable2));
+    runMR(myMultiLocs, sortKey);
+    checkTable(myMultiLocs);
+    System.out.println("DONE test" +getCurrentMethodName());
+  
+  }
+ @Test
+  public void test3() throws ParseException, IOException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    /*
+     * test count sort keys
+     */
+    System.out.println("******Start  testcase: " + getCurrentMethodName());
+    sortKey = "count";
+    System.out.println("hello sort on word and count");
+    String methodName = getCurrentMethodName();
+    String myMultiLocs = null;
+    if (whichCluster.equalsIgnoreCase("realCluster")) {
+      myMultiLocs = new String("/user/" + System.getenv("USER") + "/" + "us"
+          + methodName + "," + "/user/" + System.getenv("USER") + "/" + "others"
+          + methodName );
+    } else {
+      RawLocalFileSystem rawLFS = new RawLocalFileSystem();
+      fs = new LocalFileSystem(rawLFS);
+      myMultiLocs = new String(fs.getWorkingDirectory() + "/" + "us"
+          + methodName + "," + fs.getWorkingDirectory() + "/" + "others"
+          + methodName );
+    }
+    getTablePaths(myMultiLocs);
+    removeDir(new Path(strTable1));
+    removeDir(new Path(strTable2));
+    runMR(myMultiLocs, sortKey);
+ 
+    checkTableExists(true, strTable1);
+    checkTableExists(true, strTable2);
+    checkTable(myMultiLocs);
+    System.out.println("DONE test" +getCurrentMethodName());
+  
+  }
+
+ static class MapClass implements
+      Mapper<LongWritable, Text, BytesWritable, Tuple> {
+    private BytesWritable bytesKey;
+    private Tuple tupleRow;
+    private Object javaObj;
+    private JobConf conf;
+
+    @Override
+    public void map(LongWritable key, Text value,
+        OutputCollector<BytesWritable, Tuple> output, Reporter reporter)
+        throws IOException {
+      // value should contain "word count"
+      String[] wdct = value.toString().split(" ");
+      if (wdct.length != 2) {
+        // LOG the error
+        return;
+      }
+
+      byte[] word = wdct[0].getBytes();
+      bytesKey.set(word, 0, word.length);
+      System.out.println("word: " + new String(word));
+      tupleRow.set(0, new String(word));
+      tupleRow.set(1, Integer.parseInt(wdct[1]));
+      System.out.println("count:  " + Integer.parseInt(wdct[1]));
+
+      // This key has to be created by user
+      /*
+       * Tuple userKey = new DefaultTuple(); userKey.append(new String(word));
+       * userKey.append(Integer.parseInt(wdct[1]));
+       */
+      System.out.println("in map, sortkey: " + sortKey);
+      Tuple userKey = new DefaultTuple();
+      if (sortKey.equalsIgnoreCase("word,count")) {
+        userKey.append(new String(word));
+        userKey.append(Integer.parseInt(wdct[1]));
+      }
+
+      if (sortKey.equalsIgnoreCase("count")) {
+        userKey.append(Integer.parseInt(wdct[1]));
+      }
+
+      if (sortKey.equalsIgnoreCase("word")) {
+        userKey.append(new String(word));
+      }
+
+      try {
+
+        /* New M/R Interface */
+        /* Converts user key to zebra BytesWritable key */
+        /* using sort key expr tree */
+        /* Returns a java base object */
+        /* Done for each user key */
+
+        bytesKey = BasicTableOutputFormat.getSortKey(javaObj, userKey);
+      } catch (Exception e) {
+
+      }
+
+      output.collect(bytesKey, tupleRow);
+    }
+
+    @Override
+    public void configure(JobConf job) {
+      bytesKey = new BytesWritable();
+      conf = job;
+      sortKey = job.get("sortKey");
+      try {
+        Schema outSchema = BasicTableOutputFormat.getSchema(job);
+        tupleRow = TypesUtils.createTuple(outSchema);
+        javaObj = BasicTableOutputFormat.getSortKeyGenerator(job);
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      } catch (org.apache.hadoop.zebra.parser.ParseException e) {
+        throw new RuntimeException(e);
+      }
+    }
+
+    @Override
+    public void close() throws IOException {
+      // no-op
+    }
+  }
+
+  static class ReduceClass implements
+      Reducer<BytesWritable, Tuple, BytesWritable, Tuple> {
+    Tuple outRow;
+
+    @Override
+    public void configure(JobConf job) {
+    }
+
+    @Override
+    public void close() throws IOException {
+    }
+
+    public void reduce(BytesWritable key, Iterator<Tuple> values,
+        OutputCollector<BytesWritable, Tuple> output, Reporter reporter)
+        throws IOException {
+      try {
+        for (; values.hasNext();) {
+          output.collect(key, values.next());
+        }
+      } catch (ExecException e) {
+        e.printStackTrace();
+      }
+    }
+
+  }
+
+  static class OutputPartitionerClass extends ZebraOutputPartition {
+
+    @Override
+    public int getOutputPartition(BytesWritable key, Tuple value) throws ExecException {
+
+     //check table schema and table data are passed to this class
+/*
+ * value0 : india
+value0 : india
+value0 : japan
+value0 : japan
+value0 : nouse
+value0 : nowhere
+value0 : us
+value0 : us
+ */
+System.out.println("value0 : "+value.get(0));
+      String reg = null;
+      try {
+        reg = (String) (value.get(0));
+      } catch (Exception e) {
+        //
+      }
+
+      if (reg.equals("us"))
+        return 0;
+      else
+        return 1;
+
+    }
+
+  }
+
+  public void runMR(String myMultiLocs, String sortKey) throws ParseException,
+      IOException, Exception, org.apache.hadoop.zebra.parser.ParseException {
+
+    JobConf jobConf = new JobConf();
+    jobConf.setJobName("tableMRSample");
+    jobConf.set("table.output.tfile.compression", "gz");
+    jobConf.set("sortKey", sortKey);
+    // input settings
+    jobConf.setInputFormat(TextInputFormat.class);
+    jobConf.setMapperClass(TestMultipleOutputs2.MapClass.class);
+    jobConf.setMapOutputKeyClass(BytesWritable.class);
+    jobConf.setMapOutputValueClass(DefaultTuple.class);
+    FileInputFormat.setInputPaths(jobConf, inputPath);
+
+    jobConf.setNumMapTasks(1);
+
+    // output settings
+
+    jobConf.setOutputFormat(BasicTableOutputFormat.class);
+    BasicTableOutputFormat.setMultipleOutputs(jobConf, myMultiLocs,
+        TestMultipleOutputs2.OutputPartitionerClass.class);
+
+    // set the logical schema with 2 columns
+    BasicTableOutputFormat.setSchema(jobConf, "word:string, count:int");
+    // for demo purposes, create 2 physical column groups
+    BasicTableOutputFormat.setStorageHint(jobConf, "[word];[count]");
+    BasicTableOutputFormat.setSortInfo(jobConf, sortKey);
+    System.out.println("in runMR, sortkey: " + sortKey);
+    // set map-only job.
+    jobConf.setNumReduceTasks(1);
+    JobClient.runJob(jobConf);
+    BasicTableOutputFormat.close(jobConf);
+  }
+
+  public static void main(String[] args) throws ParseException,
+      org.apache.hadoop.zebra.parser.ParseException, Exception {
+    TestMultipleOutputs2 test = new TestMultipleOutputs2();
+    TestMultipleOutputs2.setUpOnce();
+    test.test1();
+    test.test2();
+    test.test3();
+   
+  }
+}