当前位置: 首页 > 知识库问答 >
问题:

在java中使用processbuilder执行sqoop命令

司寇飞航
2023-03-14
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class SqoopTest {
    static ProcessBuilder processBuilder = null;
    static Process spawnProcess = null;
    static int exitValue;
    static int pid;
    static List<String> commands;

    public static void main(String[] args) {

      runSqoop();



    }

    /*
     * Executes the shell script with a series of sqoop commands.
     * 
     * 
     * 
     * sqoop import -D mapred.child.java.opts='\-Djava.security.egd=file:/dev/../dev/urandom' 
     * --connect 'jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))' 
     * --username XXCSS_O --password "c******3" --split-by LINE_ITEM_ID -m 10 
     * --query "select LINE_ITEM_ID,LIST_PRICE,SERVICE_VALUE from XXCSS_KTN_REQ_LINE_DETAIL where \$CONDITIONS AND lid_date > to_date('2016-10-13 03:04:18','MM/dd/yyyy hh24:mi:ss') and lid_date <= to_date('2016-10-13 03:04:18','MM/dd/yyyy hh24:mi:ss')" 
     * --map-column-hive LINE_ITEM_ID=BIGINT,LIST_PRICE=BIGINT,SERVICE_VALUE=BIGINT 
     * --null-string '\\\\N' --null-non-string '\\\\N' --hcatalog-home 
     * /opt/mapr/hive/hive-1.2/hcatalog --hcatalog-table XXCSS_KTN_REQ_LINE_DETAIL --hcatalog-database frameworks_dataingestion
     * 
     */
    public static void runSqoop() {
        commands = new ArrayList<String>();
        commands.add("sqoop import -D mapred.child.java.opts=\'\\-Djava.security.egd=file:/dev/../dev/urandom\' --connect \'jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))\' --username XXCSS_O --password \"c*****3\" --split-by LINE_ITEM_ID -m 10 --query \"select LINE_ITEM_ID,LIST_PRICE,SERVICE_VALUE from XXCSS_KTN_REQ_LINE_DETAIL where \\$CONDITIONS AND lid_date > to_date(\'2016-10-13 00:00:00\',\'yyyy-MM-dd hh24:mi:ss\') and lid_date <= to_date(\'2016-10-13 03:04:18\',\'yyyy-MM-dd hh24:mi:ss\')\" --map-column-hive LINE_ITEM_ID=BIGINT,LIST_PRICE=BIGINT,SERVICE_VALUE=BIGINT --null-string '\\\\N' --null-non-string '\\\\N' --hcatalog-home /opt/mapr/hive/hive-1.2/hcatalog --hcatalog-table XXCSS_KTN_REQ_LINE_DETAIL --hcatalog-database frameworks_dataingestion");
        processBuilder = new ProcessBuilder(commands);
        try {
            System.out.println("Executing " + commands.toString());
            spawnProcess = processBuilder.start();
            BufferedReader reader = 
                    new BufferedReader(new InputStreamReader(spawnProcess.getErrorStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ( (line = reader.readLine()) != null) {
               builder.append(line);
               builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();
            System.out.println(result);
            try {
                exitValue = spawnProcess.waitFor();
                pid = getPID(spawnProcess);
                System.out.println("The PID is " + pid);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println("Process exited with the status :" + exitValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * Retrieves the process id of the executed command.
     * 
     * @param process the executed process
     * 
     * @return PID
     */

    public static int getPID(Process process) {
        try {
            Class<?> processImplClass = process.getClass();
            Field fpid = processImplClass.getDeclaredField("pid");
            if (!fpid.isAccessible()) {
                fpid.setAccessible(true);
            }
            return fpid.getInt(process);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return -1;
        }
    }

    /*
     * Retrieves the current java process id
     * 
     * @return java process ID
     */

//  @SuppressWarnings("restriction")
//  public static int getCurrentJavaPID() {
//      int jvmpid = 0;
//      java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
//      try {
//          java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
//          jvm.setAccessible(true);
//          sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
//          java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
//          pid_method.setAccessible(true);
//
//          jvmpid = (Integer) pid_method.invoke(mgmt);
//      } catch (Exception e) {
//          e.getMessage();
//      }
//      return jvmpid;
//  }

}
public static void runSqoop() {
        String[] commands = {"sqoop","import","-D mapred.child.java.opts=\'\\-Djava.security.egd=file:/dev/../dev/urandom\'","--connect", "\'jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.370)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))\'","--username","XXCSS_O","--password","\"c***3\"", "--split-by","LINE_ITEM_ID","-m","10","--query","\"select LINE_ITEM_ID,LIST_PRICE,SERVICE_VALUE from XXCSS_KTN_REQ_LINE_DETAIL where \\$CONDITIONS AND lid_date > to_date(\'2016-10-13 00:00:00\',\'yyyy-MM-dd hh24:mi:ss\') and lid_date <= to_date(\'2016-10-13 03:04:18\',\'yyyy-MM-dd hh24:mi:ss\')\"","--null-string","'\\\\N'","--null-non-string","'\\\\N'","--hcatalog-home","/opt/mapr/hive/hive-1.2/hcatalog","--hcatalog-table","XXCSS_KTN_REQ_LINE_DETAIL","--hcatalog-database","frameworks_dataingestion"};
        processBuilder = new ProcessBuilder(commands);
        try {
            System.out.println("Executing " + commands.toString());
            spawnProcess = processBuilder.start();
            BufferedReader reader = 
                    new BufferedReader(new InputStreamReader(spawnProcess.getErrorStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ( (line = reader.readLine()) != null) {
               builder.append(line);
               builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();
            System.out.println(result);
            try {
                exitValue = spawnProcess.waitFor();
                pid = getPID(spawnProcess);
                System.out.println("The PID is " + pid);
            } catch (InterruptedException e) {
                System.out.println(e.getMessage());
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            System.out.println("Process exited with the status :" + exitValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

堆栈跟踪:

cat://opt/mapr/zookeeper/zookeeeperversion:没有这样的文件或目录16/10/25 07:41:12信息Sqoop.Sqoop:运行Sqoop版本:1.4.6-mapr-1609 16/10/25 07:41:12警告工具。basesqooptool:在命令行设置密码不安全。考虑改用-p。16/10/25 07:41:13错误tool.basesQooptool:创建数据库管理器时出错:java.io.ioException:没有用于org.apache.sqoop.connfactory.getManager(connfactory.java:191)连接字符串的管理器:“在org.apache.sqoop.tool.basesqoPotool.init(basesqoPotool.java:256),在org.apache.sqoop.tool.importTool.init(importTool.java:89),在org.apache.sqoop.tool.importTool.run(importTool.java:593),在org.apache.sqoop.tool.importTool.run(importTool.java:593),在org.apache.sqoop.tool.importTool.run(.java:218)在org.apache.sqoop.sqoop.runtool(sqoop.java:227)在org.apache.sqoop.sqoop.main(sqoop.java:236)

共有1个答案

常永长
2023-03-14

您必须分别提供命令和arg,如下所示

  Process p = new ProcessBuilder("myCommand", "myArg").start();

    ProcessBuilder pb =
       new ProcessBuilder("myCommand", "myArg1", "myArg2");
pb.start();

这里有一个例子

 类似资料:
  • 我试图使用Java的类来执行包含管道的命令。例如: 然而,我得到了一个错误: 然后: 尽管该命令在命令行中运行得很好,但我无法让执行将其输出重定向到另一个的命令。 有没有办法做到这一点?

  • 问题内容: 当我使用执行命令时,它如何知道在哪里寻找该命令?使用此技巧,我将PATH变量(通过检查进行了验证)修改为错误(空,工作目录等),但ProcessBuilder仍然可以执行sort,echo,bash等。它是怎么做到的? 注意:我特定的开发环境是OSX,但是此代码也将在Red Hat Enterprise Linux上运行。 问题答案: 该文件说 一个命令,一个字符串列表,表示要调用的外

  • I使用Processbuilder如下所示: 在方法中,我设置要执行的linux命令,如下所示: 我可以从终端执行相同的命令。

  • 线程“main”java.io.ioException中出现异常:无法运行程序“java” 但是当我从终端上运行任何java命令时,它们都工作得很好。 我发现的另一件事是,当我在终端中运行命令:并使用ProcessBuilder()时,它们会显示不同的输出。即ProcessBuilder命令中不显示jdk/bin的路径。 我如何解决这个问题?

  • 问题内容: 我正在尝试创建一个程序,允许我通过带有参数的终端(如果您想知道,它是用于树莓派的OmxPlayer)来执行命令,但是我希望能够在启动后与它进行交互命令。 例如,我要执行以下操作:omxplayer -win x1 y1 x2 y2,然后可以按“ p”暂停视频/音频媒体 我已经有了一些可以用参数启动omxplayer的东西(实际上是“ ls”,但是它应该以完全相同的方式工作),但是我不了