<commit_before>/**
 * (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.framework;

import java.io.IOException;
import java.util.Map;

import org.kiji.annotations.ApiAudience;
import org.kiji.mapreduce.MapReduceJobInput;
import org.kiji.mapreduce.input.MapReduceJobInputs;
import org.kiji.mapreduce.tools.framework.JobInputSpec.Format;
import org.kiji.schema.tools.SpaceSeparatedMapParser;

/**
 * Constructs instances of MapReduceJobInput from a string specification.
 *
 * See {@link JobInputSpec} for the full inputspec specification.
 */
@ApiAudience.Framework
public final class MapReduceJobInputFactory {
  /**
   * Constructs a new factory for MapReduce job inputs.
   */
  private MapReduceJobInputFactory() { }

  /**
   * Creates a new factory for instances of {@link MapReduceJobInput}.
   *
   * @return a new factory for MapReduce job inputs.
   */
  public static MapReduceJobInputFactory create() {
    return new MapReduceJobInputFactory();
  }

  /**
   * Creates a job input from a command-line flag space-separated parameters map.
   *
   * @param ssm Space-separated parameters map from the command-line.
   * @return a job input configured from the command-line parameters.
   * @throws IOException on I/O error.
   */
  public MapReduceJobInput fromSpaceSeparatedMap(String ssm) throws IOException {
    final Map<String, String> params = SpaceSeparatedMapParser.create().parse(ssm);
    try {
      final MapReduceJobInput input =
          createJobInput(Format.parse(params.get(JobIOConfKeys.FORMAT_KEY)));
      input.initialize(params);
      return input;
    } catch (JobIOSpecParseException exn) {
      throw new IOException(String.format(
          "Invalid job output specification: '%s': %s", ssm, exn.getMessage()), exn);
    }
  }

  /**
   * Instantiates the appropriate job input.
   *
   * @param format Format of the job input.
   * @return an unconfigured job input instance.
   * @throws IOException on I/O error.
   */
  private MapReduceJobInput createJobInput(Format format) throws IOException {
    switch (format) {
    case HTABLE:
      return MapReduceJobInputs.newHTableMapReduceJobInput();
    case TEXT:
      return MapReduceJobInputs.newTextMapReduceJobInput();
    case SMALL_TEXT_FILES:
      return MapReduceJobInputs.newWholeTextFileMapReduceJobInput();
    case SEQUENCE:
      return MapReduceJobInputs.newSequenceFileMapReduceJobInput();
      // TODO(KIJIMR-61) Map file job input
    // case MAP_FILE:
    //   throw new IOException(String.format(
    //       "Map files are not supported as job input in spec '%s'.", ssm));
    case AVRO:
      return MapReduceJobInputs.newAvroKeyMapReduceJobInput();
    case AVRO_KV:
      return MapReduceJobInputs.newAvroKeyValueMapReduceJobInput();
    case KIJI:
      return MapReduceJobInputs.newKijiTableMapReduceJobInput();
    case XML:
      return MapReduceJobInputs.newXMLMapReduceJobInput();
    default:
      throw new RuntimeException(String.format("Unhandled job output format: '%s'.", format));
    }
  }

}
<commit_msg>KIJIMR-125. Improve error message in MapReduceJobInputFactory<commit_after>/**
 * (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.framework;

import java.io.IOException;
import java.util.Map;

import org.kiji.annotations.ApiAudience;
import org.kiji.mapreduce.MapReduceJobInput;
import org.kiji.mapreduce.input.MapReduceJobInputs;
import org.kiji.mapreduce.tools.framework.JobInputSpec.Format;
import org.kiji.schema.tools.SpaceSeparatedMapParser;

/**
 * Constructs instances of MapReduceJobInput from a string specification.
 *
 * See {@link JobInputSpec} for the full inputspec specification.
 */
@ApiAudience.Framework
public final class MapReduceJobInputFactory {
  /**
   * Constructs a new factory for MapReduce job inputs.
   */
  private MapReduceJobInputFactory() { }

  /**
   * Creates a new factory for instances of {@link MapReduceJobInput}.
   *
   * @return a new factory for MapReduce job inputs.
   */
  public static MapReduceJobInputFactory create() {
    return new MapReduceJobInputFactory();
  }

  /**
   * Creates a job input from a command-line flag space-separated parameters map.
   *
   * @param ssm Space-separated parameters map from the command-line.
   * @return a job input configured from the command-line parameters.
   * @throws IOException on I/O error.
   */
  public MapReduceJobInput fromSpaceSeparatedMap(String ssm) throws IOException {
    final Map<String, String> params = SpaceSeparatedMapParser.create().parse(ssm);
    try {
      final MapReduceJobInput input =
          createJobInput(Format.parse(params.get(JobIOConfKeys.FORMAT_KEY)));
      input.initialize(params);
      return input;
    } catch (JobIOSpecParseException exn) {
      throw new IOException(String.format(
          "Invalid job input specification: '%s': %s", ssm, exn.getMessage()), exn);
    }
  }

  /**
   * Instantiates the appropriate job input.
   *
   * @param format Format of the job input.
   * @return an unconfigured job input instance.
   * @throws IOException on I/O error.
   */
  private MapReduceJobInput createJobInput(Format format) throws IOException {
    switch (format) {
    case HTABLE:
      return MapReduceJobInputs.newHTableMapReduceJobInput();
    case TEXT:
      return MapReduceJobInputs.newTextMapReduceJobInput();
    case SMALL_TEXT_FILES:
      return MapReduceJobInputs.newWholeTextFileMapReduceJobInput();
    case SEQUENCE:
      return MapReduceJobInputs.newSequenceFileMapReduceJobInput();
    // TODO(KIJIMR-61) Map file job input
    // case MAP_FILE:
    //   throw new IOException(String.format(
    //       "Map files are not supported as job input in spec '%s'.", ssm));
    case AVRO:
      return MapReduceJobInputs.newAvroKeyMapReduceJobInput();
    case AVRO_KV:
      return MapReduceJobInputs.newAvroKeyValueMapReduceJobInput();
    case KIJI:
      return MapReduceJobInputs.newKijiTableMapReduceJobInput();
    case XML:
      return MapReduceJobInputs.newXMLMapReduceJobInput();
    default:
      throw new RuntimeException(String.format("Unhandled job input format: '%s'.", format));
    }
  }

}

{'fraction_non_alphanumeric': 0.08652822151224707, 'fraction_numerical': 0.0030617678381256656, 'mean_word_length': 3.695625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5111690', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2146, 'n_words': 665}