Package org.apache.hadoop.streaming

Source Code of org.apache.hadoop.streaming.TrApp

/**
* Copyright 2006 The Apache Software Foundation
*
* 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.apache.hadoop.streaming;

import java.io.*;

import org.apache.hadoop.streaming.Environment;

/** A minimal Java implementation of /usr/bin/tr.
    Used to test the usage of external applications without adding
    platform-specific dependencies.
*/
public class TrApp
{

  public TrApp(char find, char replace)
  {
    this.find = find;
    this.replace = replace;
  }

  void testParentJobConfToEnvVars() throws IOException
  {
    env = new Environment();
    // test that some JobConf properties are exposed as expected    
    // Note the dots translated to underscore:
    // property names have been escaped in PipeMapRed.safeEnvVarName()
    expect("mapred_input_format_class", "org.apache.hadoop.streaming.StreamInputFormat");
    expect("mapred_job_tracker", "local");
    expect("mapred_input_key_class", "org.apache.hadoop.io.Text");
    expect("mapred_input_value_class", "org.apache.hadoop.io.Text");
    //expect("mapred_local_dir", "build/test/mapred/local");
    expectDefined("mapred_local_dir");
    expect("mapred_output_format_class", "org.apache.hadoop.streaming.StreamOutputFormat");
    expect("mapred_output_key_class", "org.apache.hadoop.io.Text");
    expect("mapred_output_value_class", "org.apache.hadoop.io.Text");

    expect("mapred_task_is_map", "true");
    expect("mapred_reduce_tasks", "1");
    expectDefined("mapred_task_id");

    expectDefined("map_input_file");
    expect("map_input_start", "0");
    expectDefined("map_input_length");

    expectDefined("io_sort_factor");

    // the FileSplit context properties are not available in local hadoop..
    // so can't check them in this test.

  }

  // this runs in a subprocess; won't use JUnit's assertTrue()   
  void expect(String evName, String evVal) throws IOException
  {
    String got = env.getProperty(evName);
    if(! evVal.equals(got)) {
      String msg = "FAIL evName=" + evName + " got=" + got + " expect=" + evVal;
      throw new IOException(msg);
    }
  }

  void expectDefined(String evName) throws IOException
  {
    String got = env.getProperty(evName);
    if(got == null) {
      String msg = "FAIL evName=" + evName + " is undefined. Expect defined.";
      throw new IOException(msg);
    }
  }

  public void go() throws IOException
  {
    testParentJobConfToEnvVars();
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String line;

    while ((line = in.readLine()) != null) {
      String out = line.replace(find, replace);
      System.out.println(out);
    }
  }

  public static void main(String[] args) throws IOException
  {
    args[0] = CUnescape(args[0]);
    args[1] = CUnescape(args[1]);
    TrApp app = new TrApp(args[0].charAt(0), args[1].charAt(0));
    app.go();
  }

  public static String CUnescape(String s)
  {
    if(s.equals("\\n")) {
      return "\n";
    } else {
      return s;
    }
  }
  char find;
  char replace;
  Environment env;
}
TOP

Related Classes of org.apache.hadoop.streaming.TrApp

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.