All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.util.ToolRunner Maven / Gradle / Ivy

There is a newer version: 3.4.1
Show newest version
/**
 * 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.util;

import java.io.IOException;
import java.io.PrintStream;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ipc.CallerContext;

/**
 * A utility to help run {@link Tool}s.
 * 
 * 

ToolRunner can be used to run classes implementing * Tool interface. It works in conjunction with * {@link GenericOptionsParser} to parse the * * generic hadoop command line arguments and modifies the * Configuration of the Tool. The * application-specific options are passed along without being modified. *

* * @see Tool * @see GenericOptionsParser */ @InterfaceAudience.Public @InterfaceStability.Stable public class ToolRunner { /** * Runs the given Tool by {@link Tool#run(String[])}, after * parsing with the given generic arguments. Uses the given * Configuration, or builds one if null. * * Sets the Tool's configuration with the possibly modified * version of the conf. * * @param conf Configuration for the Tool. * @param tool Tool to run. * @param args command-line arguments to the tool. * @return exit code of the {@link Tool#run(String[])} method. */ public static int run(Configuration conf, Tool tool, String[] args) throws Exception{ if (CallerContext.getCurrent() == null) { CallerContext ctx = new CallerContext.Builder("CLI").build(); CallerContext.setCurrent(ctx); } if(conf == null) { conf = new Configuration(); } GenericOptionsParser parser = new GenericOptionsParser(conf, args); //set the configuration back, so that Tool can configure itself tool.setConf(conf); //get the args w/o generic hadoop args String[] toolArgs = parser.getRemainingArgs(); return tool.run(toolArgs); } /** * Runs the Tool with its Configuration. * * Equivalent to run(tool.getConf(), tool, args). * * @param tool Tool to run. * @param args command-line arguments to the tool. * @return exit code of the {@link Tool#run(String[])} method. */ public static int run(Tool tool, String[] args) throws Exception{ return run(tool.getConf(), tool, args); } /** * Prints generic command-line argurments and usage information. * * @param out stream to write usage information to. */ public static void printGenericCommandUsage(PrintStream out) { GenericOptionsParser.printGenericCommandUsage(out); } /** * Print out a prompt to the user, and return true if the user * responds with "y" or "yes". (case insensitive) */ public static boolean confirmPrompt(String prompt) throws IOException { while (true) { System.err.print(prompt + " (Y or N) "); StringBuilder responseBuilder = new StringBuilder(); while (true) { int c = System.in.read(); if (c == -1 || c == '\r' || c == '\n') { break; } responseBuilder.append((char)c); } String response = responseBuilder.toString(); if (response.equalsIgnoreCase("y") || response.equalsIgnoreCase("yes")) { return true; } else if (response.equalsIgnoreCase("n") || response.equalsIgnoreCase("no")) { return false; } System.err.println("Invalid input: " + response); // else ask them again } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy