// Path: kiji-mapreduce/src/main/java/org/kiji/mapreduce/MapReduceJobOutput.java
// @ApiAudience.Public
// @ApiStability.Stable
// @Inheritance.Sealed
// public abstract class MapReduceJobOutput {
//   /**
//    * Initializes the job output from command-line parameters.
//    *
//    * @param params Initialization parameters.
//    * @throws IOException on I/O error.
//    */
//   public abstract void initialize(Map<String, String> params) throws IOException;
// 
//   /**
//    * Configures the output for a MapReduce job.
//    *
//    * @param job The job to configure.
//    * @throws IOException If there is an error.
//    */
//   public void configure(Job job) throws IOException {
//     job.setOutputFormatClass(getOutputFormatClass());
//   }
// 
//   /**
//    * Gets the Hadoop MapReduce output format class.
//    *
//    * @return The job output format class.
//    */
//   protected abstract Class<? extends OutputFormat> getOutputFormatClass();
// }


import java.io.IOException;
import java.util.List;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.kiji.annotations.ApiAudience;
import org.kiji.common.flags.Flag;
import org.kiji.mapreduce.MapReduceJobInput;
import org.kiji.mapreduce.MapReduceJobOutput;
import org.kiji.mapreduce.input.KijiTableMapReduceJobInput;
import org.kiji.mapreduce.output.DirectKijiTableMapReduceJobOutput;
import org.kiji.mapreduce.output.HFileMapReduceJobOutput;
import org.kiji.mapreduce.output.KijiTableMapReduceJobOutput;
import org.kiji.mapreduce.pivot.KijiPivotJobBuilder;
import org.kiji.mapreduce.pivot.impl.KijiPivoters;
import org.kiji.mapreduce.tools.framework.KijiJobTool;
import org.kiji.mapreduce.tools.framework.MapReduceJobInputFactory;
import org.kiji.mapreduce.tools.framework.MapReduceJobOutputFactory;
import org.kiji.schema.Kiji;
import org.kiji.schema.KijiTable;
import org.kiji.schema.tools.KijiToolLauncher;
import org.kiji.schema.tools.RequiredFlagException;
import org.kiji.schema.util.ResourceUtils;
/**
 * (c) Copyright 2012 WibiData, Inc.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed 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.kiji.mapreduce.tools;




/**
 * Runs a KijiPivoter a Kiji table.
 *
 * <p>
 * Here is an example of command-line to launch a KijiPivoter named {@code package.SomePivoter}
 * on the rows from an input table {@code kiji://.env/input_instance/input_table}
 * while writing cells to another output table {@code kiji://.env/output_instance/output_table}:
 * <pre><blockquote>
 *   $ kiji pivot \
 *       --pivoter='package.SomePivoter' \
 *       --input="format=kiji table=kiji://.env/default/input_table" \
 *       --output="format=kiji table=kiji://.env/default/output_table nsplits=5" \
 *       --lib=/path/to/libdir/
 * </blockquote></pre>
 * </p>
 */
@ApiAudience.Private
public final class KijiPivot extends KijiJobTool<KijiPivotJobBuilder> {
  private static final Logger LOG = LoggerFactory.getLogger(KijiPivot.class);

  @Flag(name="pivoter",
      usage="KijiPivoter class to run over the table.")
  private String mPivoter= "";

  /** {@inheritDoc} */
  @Override
  public String getName() {
    return "pivot";
  }

  /** {@inheritDoc} */
  @Override
  public String getDescription() {
    return "Run a pivoter over a Kiji table.";
  }

  /** {@inheritDoc} */
  @Override
  public String getCategory() {
    return "MapReduce";
  }

  /** Kiji instance where the output table lives. */
  private Kiji mKiji;

  /** KijiTable to import data into. */
  private KijiTable mTable;

  /** Job output. */
  private KijiTableMapReduceJobOutput mOutput;

  /** {@inheritDoc} */
  @Override
  protected void validateFlags() throws Exception {
    // Parse --input and --output flags:
    //   --input is guaranteed to be a Kiji table, --output is not.
    super.validateFlags();

    if (mInputFlag.isEmpty()) {
      throw new RequiredFlagException("input");
    }
    if (mPivoter.isEmpty()) {
      throw new RequiredFlagException("pivoter");
    }
    if (mOutputFlag.isEmpty()) {
      throw new RequiredFlagException("output");
    }

// Path: kiji-mapreduce/src/main/java/org/kiji/mapreduce/MapReduceJobOutput.java
// @ApiAudience.Public
// @ApiStability.Stable
// @Inheritance.Sealed
// public abstract class MapReduceJobOutput {
//   /**
//    * Initializes the job output from command-line parameters.
//    *
//    * @param params Initialization parameters.
//    * @throws IOException on I/O error.
//    */
//   public abstract void initialize(Map<String, String> params) throws IOException;
// 
//   /**
//    * Configures the output for a MapReduce job.
//    *
//    * @param job The job to configure.
//    * @throws IOException If there is an error.
//    */
//   public void configure(Job job) throws IOException {
//     job.setOutputFormatClass(getOutputFormatClass());
//   }
// 
//   /**
//    * Gets the Hadoop MapReduce output format class.
//    *
//    * @return The job output format class.
//    */
//   protected abstract Class<? extends OutputFormat> getOutputFormatClass();
// }


// Path: kiji-mapreduce/src/main/java/org/kiji/mapreduce/tools/KijiPivot.java
import java.io.IOException;
import java.util.List;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.kiji.annotations.ApiAudience;
import org.kiji.common.flags.Flag;
import org.kiji.mapreduce.MapReduceJobInput;
import org.kiji.mapreduce.MapReduceJobOutput;
import org.kiji.mapreduce.input.KijiTableMapReduceJobInput;
import org.kiji.mapreduce.output.DirectKijiTableMapReduceJobOutput;
import org.kiji.mapreduce.output.HFileMapReduceJobOutput;
import org.kiji.mapreduce.output.KijiTableMapReduceJobOutput;
import org.kiji.mapreduce.pivot.KijiPivotJobBuilder;
import org.kiji.mapreduce.pivot.impl.KijiPivoters;
import org.kiji.mapreduce.tools.framework.KijiJobTool;
import org.kiji.mapreduce.tools.framework.MapReduceJobInputFactory;
import org.kiji.mapreduce.tools.framework.MapReduceJobOutputFactory;
import org.kiji.schema.Kiji;
import org.kiji.schema.KijiTable;
import org.kiji.schema.tools.KijiToolLauncher;
import org.kiji.schema.tools.RequiredFlagException;
import org.kiji.schema.util.ResourceUtils;

/**
 * (c) Copyright 2012 WibiData, Inc.
 *
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * Licensed 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.kiji.mapreduce.tools;




/**
 * Runs a KijiPivoter a Kiji table.
 *
 * <p>
 * Here is an example of command-line to launch a KijiPivoter named {@code package.SomePivoter}
 * on the rows from an input table {@code kiji://.env/input_instance/input_table}
 * while writing cells to another output table {@code kiji://.env/output_instance/output_table}:
 * <pre><blockquote>
 *   $ kiji pivot \
 *       --pivoter='package.SomePivoter' \
 *       --input="format=kiji table=kiji://.env/default/input_table" \
 *       --output="format=kiji table=kiji://.env/default/output_table nsplits=5" \
 *       --lib=/path/to/libdir/
 * </blockquote></pre>
 * </p>
 */
@ApiAudience.Private
public final class KijiPivot extends KijiJobTool<KijiPivotJobBuilder> {
  private static final Logger LOG = LoggerFactory.getLogger(KijiPivot.class);

  @Flag(name="pivoter",
      usage="KijiPivoter class to run over the table.")
  private String mPivoter= "";

  /** {@inheritDoc} */
  @Override
  public String getName() {
    return "pivot";
  }

  /** {@inheritDoc} */
  @Override
  public String getDescription() {
    return "Run a pivoter over a Kiji table.";
  }

  /** {@inheritDoc} */
  @Override
  public String getCategory() {
    return "MapReduce";
  }

  /** Kiji instance where the output table lives. */
  private Kiji mKiji;

  /** KijiTable to import data into. */
  private KijiTable mTable;

  /** Job output. */
  private KijiTableMapReduceJobOutput mOutput;

  /** {@inheritDoc} */
  @Override
  protected void validateFlags() throws Exception {
    // Parse --input and --output flags:
    //   --input is guaranteed to be a Kiji table, --output is not.
    super.validateFlags();

    if (mInputFlag.isEmpty()) {
      throw new RequiredFlagException("input");
    }
    if (mPivoter.isEmpty()) {
      throw new RequiredFlagException("pivoter");
    }
    if (mOutputFlag.isEmpty()) {
      throw new RequiredFlagException("output");
    }

    final MapReduceJobOutput mrJobOutput =