List of usage examples for org.apache.commons.exec CommandLine CommandLine
public CommandLine(final CommandLine other)
From source file:org.apache.bigtop.itest.hive.TestCLI.java
@Test public void hiveConf() { results = HiveHelper.execCommand(/* w ww . j a v a 2 s . c o m*/ new CommandLine("hive").addArgument("--hiveconf").addArgument("hive.root.logger=INFO,console") .addArgument("-e").addArgument("SHOW DATABASES").addArgument("--hiveconf").addArgument(db)); Assert.assertEquals("The --hiveconf option did not work in setting hive.root.logger=INFO,console.", true, results.get("outputStream").contains("INFO parse.ParseDriver: Parsing command: SHOW DATABASES")); results = HiveHelper.execCommand( new CommandLine("hive").addArgument("-hiveconf").addArgument("hive.root.logger=INFO,console") .addArgument("-e").addArgument("SHOW DATABASES").addArgument("--hiveconf").addArgument(db)); Assert.assertEquals("The -hiveconf variant option did not work in setting hive.root.logger=INFO,console.", true, results.get("outputStream").contains("INFO parse.ParseDriver: Parsing command: SHOW DATABASES")); }
From source file:org.apache.bigtop.itest.hive.TestCLI.java
@Test public void variableSubsitution() throws FileNotFoundException { results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e").addArgument("SHOW DATABASES") .addArgument("--hiveconf").addArgument(db)); if (!results.get("outputStream").contains("bigtop_runtime_hive")) { results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("CREATE DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); } else {// w ww.ja v a 2s.c o m results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("DROP DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("CREATE DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); } try (PrintWriter out = new PrintWriter("hive-define.sql")) { out.println("show ${A};"); out.println("quit;"); } results = HiveHelper.execCommand(new CommandLine("/bin/sh").addArgument("-c") .addArgument("hive -d A=DATABASES --hiveconf '" + db + "' < hive-define.sql", false)); Assert.assertEquals("The hive -d A=DATABASES option did not work.", 0, Integer.parseInt(results.get("exitValue"))); Assert.assertEquals("The hive -d A=DATABASES option did not work.", true, results.get("outputStream").contains("bigtop_runtime_hive")); results = HiveHelper.execCommand(new CommandLine("/bin/sh").addArgument("-c") .addArgument("hive --define A=DATABASES --hiveconf '" + db + "' < hive-define.sql", false)); Assert.assertEquals("The hive --define A=DATABASES option did not work.", 0, Integer.parseInt(results.get("exitValue"))); Assert.assertEquals("The hive --define A=DATABASES option did not work.", true, results.get("outputStream").contains("bigtop_runtime_hive")); results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("DROP DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); }
From source file:org.apache.bigtop.itest.hive.TestCLI.java
@Test public void hiveVar() throws FileNotFoundException { results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e").addArgument("SHOW DATABASES") .addArgument("--hiveconf").addArgument(db)); if (!results.get("outputStream").contains("bigtop_runtime_hive")) { results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("CREATE DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); } else {//from www . j a v a2s . c o m results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("DROP DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("CREATE DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); } try (PrintWriter out = new PrintWriter("hive-var.sql")) { out.println("show ${A};"); out.println("quit;"); } results = HiveHelper.execCommand(new CommandLine("/bin/sh").addArgument("-c") .addArgument("hive --hivevar A=DATABASES --hiveconf '" + db + "' < hive-var.sql", false)); Assert.assertEquals("The hive --hivevar A=DATABASES option did not work.", 0, Integer.parseInt(results.get("exitValue"))); Assert.assertEquals("The hive --hivevar A=DATABASES option did not work.", true, results.get("outputStream").contains("bigtop_runtime_hive")); try (PrintWriter out = new PrintWriter("hiveconf-var.sql")) { out.println("show ${hiveconf:A};"); out.println("quit;"); } results = HiveHelper.execCommand(new CommandLine("/bin/sh").addArgument("-c") .addArgument("hive --hiveconf A=DATABASES --hiveconf '" + db + "' < hiveconf-var.sql", false)); Assert.assertEquals("The hive --hiveconf A=DATABASES option did not work.", 0, Integer.parseInt(results.get("exitValue"))); Assert.assertEquals("The hive --hiveconf A=DATABASES option did not work.", true, results.get("outputStream").contains("bigtop_runtime_hive")); results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("DROP DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); }
From source file:org.apache.bigtop.itest.hive.TestCLI.java
@AfterClass public static void cleanup() { results = HiveHelper.execCommand(new CommandLine("hive").addArgument("-e") .addArgument("DROP DATABASE bigtop_runtime_hive").addArgument("--hiveconf").addArgument(db)); results = HiveHelper/* w w w.j a va 2 s.c om*/ .execCommand(new CommandLine("/bin/sh").addArgument("-c").addArgument("rm -rf hive-f*.sql", false)); results = HiveHelper.execCommand( new CommandLine("/bin/sh").addArgument("-c").addArgument("rm -rf hive-init*.sql", false)); results = HiveHelper.execCommand( new CommandLine("/bin/sh").addArgument("-c").addArgument("rm -rf hive-define.sql", false)); results = HiveHelper.execCommand( new CommandLine("/bin/sh").addArgument("-c").addArgument("rm -rf hive-var.sql", false)); results = HiveHelper.execCommand( new CommandLine("/bin/sh").addArgument("-c").addArgument("rm -rf hiveconf-var.sql", false)); }
From source file:org.apache.bigtop.itest.hive.TestHCatalog.java
@Test public void hcatInputFormatOutputFormat() throws TException, IOException, ClassNotFoundException, InterruptedException, URISyntaxException { // Create a table to write to final String inputTable = "bigtop_hcat_input_table_" + rand.nextInt(Integer.MAX_VALUE); SerDeInfo serde = new SerDeInfo("default_serde", conf.getVar(HiveConf.ConfVars.HIVEDEFAULTSERDE), new HashMap<String, String>()); FieldSchema schema = new FieldSchema("line", "string", ""); inputSchema = new HCatSchema(Collections.singletonList( new HCatFieldSchema(schema.getName(), HCatFieldSchema.Type.STRING, schema.getComment()))); StorageDescriptor sd = new StorageDescriptor(Collections.singletonList(schema), null, "org.apache.hadoop.mapred.TextInputFormat", "org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat", false, 0, serde, null, null, new HashMap<String, String>()); Table table = new Table(inputTable, "default", "me", 0, 0, 0, sd, null, new HashMap<String, String>(), null, null, TableType.MANAGED_TABLE.toString()); client.createTable(table);/* w w w . ja v a2 s . co m*/ final String outputTable = "bigtop_hcat_output_table_" + rand.nextInt(Integer.MAX_VALUE); sd = new StorageDescriptor( Arrays.asList(new FieldSchema("word", "string", ""), new FieldSchema("count", "int", "")), null, "org.apache.hadoop.mapred.TextInputFormat", "org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat", false, 0, serde, null, null, new HashMap<String, String>()); table = new Table(outputTable, "default", "me", 0, 0, 0, sd, null, new HashMap<String, String>(), null, null, TableType.MANAGED_TABLE.toString()); client.createTable(table); outputSchema = new HCatSchema(Arrays.asList(new HCatFieldSchema("word", HCatFieldSchema.Type.STRING, ""), new HCatFieldSchema("count", HCatFieldSchema.Type.INT, ""))); // LATER Could I use HCatWriter here and the reader to read it? // Write some stuff into a file in the location of the table table = client.getTable("default", inputTable); String inputFile = table.getSd().getLocation() + "/input"; Path inputPath = new Path(inputFile); FileSystem fs = FileSystem.get(conf); FSDataOutputStream out = fs.create(inputPath); out.writeChars("Mary had a little lamb\n"); out.writeChars("its fleece was white as snow\n"); out.writeChars("and everywhere that Mary went\n"); out.writeChars("the lamb was sure to go\n"); out.close(); Map<String, String> env = new HashMap<>(); env.put("HADOOP_CLASSPATH", System.getProperty(HCATCORE, "")); Map<String, String> results = HiveHelper.execCommand(new CommandLine("hive").addArgument("--service") .addArgument("jar").addArgument(System.getProperty(JOBJAR)).addArgument(HCatalogMR.class.getName()) .addArgument("-it").addArgument(inputTable).addArgument("-ot").addArgument(outputTable) .addArgument("-is").addArgument(inputSchema.getSchemaAsTypeString()).addArgument("-os") .addArgument(outputSchema.getSchemaAsTypeString()), env); LOG.info(results.toString()); Assert.assertEquals("HCat job failed", 0, Integer.parseInt(results.get("exitValue"))); client.dropTable("default", inputTable); client.dropTable("default", outputTable); }
From source file:org.apache.camel.component.exec.impl.DefaultExecCommandExecutor.java
/** * Transforms an {@link ExecCommand} to a {@link CommandLine}. No quoting fo * the arguments is used./* w w w. j a v a 2 s.c o m*/ * * @param execCommand a not-null <code>ExecCommand</code> instance. * @return a {@link CommandLine} object. */ protected CommandLine toCommandLine(ExecCommand execCommand) { notNull(execCommand, "execCommand"); CommandLine cl = new CommandLine(execCommand.getExecutable()); List<String> args = execCommand.getArgs(); for (String arg : args) { // do not handle quoting here, it is already quoted cl.addArgument(arg, false); } return cl; }
From source file:org.apache.cloudstack.wix.HeatMojo.java
@Override public void execute() throws MojoExecutionException, MojoFailureException { try {//from www . j a va2 s . c om CommandLine commandLine = new CommandLine("heat"); if (dir != null && !dir.trim().isEmpty()) { commandLine.addArgument("dir"); commandLine.addArgument(dir); } commandLine.addArgument("-gg"); commandLine.addArgument("-cg"); commandLine.addArgument(componentGroup); commandLine.addArgument("-ke"); commandLine.addArgument("-sfrag"); if (template == null || template.trim().isEmpty()) { commandLine.addArgument("-template"); commandLine.addArgument("fragment"); } else { commandLine.addArgument("-template"); commandLine.addArgument(template); } if (outputFile != null) { commandLine.addArgument("-out"); commandLine.addArgument(outputFile.getAbsolutePath()); } if (directoryName != null) { commandLine.addArgument("-dr"); commandLine.addArgument(directoryName); } if (vars != null) { commandLine.addArguments(vars, false); } DefaultExecutor executor = new DefaultExecutor(); getLog().debug("working directory " + commandLine.toString()); executor.setWorkingDirectory(getWorkingDirectory(workingDirectory)); int exitValue = executor.execute(commandLine); if (exitValue != 0) { throw new MojoExecutionException("Problem executing heat, return code " + exitValue); } } catch (ExecuteException e) { throw new MojoExecutionException("Problem executing heat", e); } catch (IOException e) { throw new MojoExecutionException("Problem executing heat", e); } }
From source file:org.apache.falcon.regression.core.helpers.FalconClientBuilder.java
/** * Build the CommandLine object for this FalconClientBuilder. * @return instance of CommandLine object *//*from w ww.j a v a 2s . c o m*/ @Override public CommandLine build() { suType.addArgsToCommandLine(commandLine, args); return new CommandLine(commandLine); }
From source file:org.apache.falcon.regression.core.util.KerberosHelper.java
/** * Switches user in kerberos.//w w w . j av a2 s . c o m */ public static void initUserWithKeytab(String user) { ExecUtil.executeCommand(new CommandLine("sudo").addArgument("kinit").addArgument(getPrincipal(user)) .addArgument("-k").addArgument("-t").addArgument(getKeyTab(user))); }
From source file:org.apache.hcatalog.templeton.ExecServiceImpl.java
private CommandLine makeCommandLine(String program, List<String> args) throws NotAuthorizedException, IOException { String path = validateProgram(program); CommandLine cmd = new CommandLine(path); if (args != null) for (String arg : args) cmd.addArgument(arg, false); return cmd;// w w w.j ava 2 s. c om }