예제 #1
0
  private void closeOutput() throws Exception {

    // Flush the rest of the buffer to disk!
    //
    if (data.byteBuffer.position() > 0) {
      data.byteBuffer.flip();
      data.fileChannel.write(data.byteBuffer);
    }

    // Close the fifo file...
    //
    data.fifoOpener.close();
    data.fileChannel = null;

    // wait for the INSERT statement to finish and check for any
    // error and/or warning...
    //
    data.sqlRunner.join();
    SqlRunner sqlRunner = data.sqlRunner;
    data.sqlRunner = null;
    sqlRunner.checkExcn();

    data.sqlOutputStream.close();
    data.sqlOutputStream = null;
  }
예제 #2
0
  public boolean execute(IngresVectorwiseLoaderMeta meta) throws KettleException {
    Runtime rt = Runtime.getRuntime();

    try {
      // 1) Create the FIFO file using the "mkfifo" command...
      // Make sure to log all the possible output, also from STDERR
      //
      data.fifoFilename = environmentSubstitute(meta.getFifoFileName());

      File fifoFile = new File(data.fifoFilename);
      if (!fifoFile.exists()) {
        // MKFIFO!
        //
        String mkFifoCmd = "mkfifo " + data.fifoFilename;
        logDetailed("Creating FIFO file using this command : " + mkFifoCmd);
        Process mkFifoProcess = rt.exec(mkFifoCmd);
        StreamLogger errorLogger =
            new StreamLogger(log, mkFifoProcess.getErrorStream(), "mkFifoError");
        StreamLogger outputLogger =
            new StreamLogger(log, mkFifoProcess.getInputStream(), "mkFifoOuptut");
        new Thread(errorLogger).start();
        new Thread(outputLogger).start();
        int result = mkFifoProcess.waitFor();
        if (result != 0) {
          throw new Exception("Return code " + result + " received from statement : " + mkFifoCmd);
        }

        String chmodCmd = "chmod 666 " + data.fifoFilename;
        logDetailed("Setting FIFO file permissings using this command : " + chmodCmd);
        Process chmodProcess = rt.exec(chmodCmd);
        errorLogger = new StreamLogger(log, chmodProcess.getErrorStream(), "chmodError");
        outputLogger = new StreamLogger(log, chmodProcess.getInputStream(), "chmodOuptut");
        new Thread(errorLogger).start();
        new Thread(outputLogger).start();
        result = chmodProcess.waitFor();
        if (result != 0) {
          throw new Exception("Return code " + result + " received from statement : " + chmodCmd);
        }
      }

      // 2) Execute the Ingres "sql" command...
      //

      String cmd = createCommandLine(meta);

      try {
        // masquerading the password for log
        if (meta.isUseDynamicVNode()) {
          logDetailed(
              "Executing command: "
                  + cmd.substring(0, cmd.indexOf("["))
                  + "[username,password]"
                  + cmd.substring(cmd.indexOf("]") + 1));
        } else {
          logDetailed("Executing command: " + cmd);
        }
        data.sqlProcess = rt.exec(cmd);

        // any error message?
        //
        data.errorLogger = new StreamLogger(log, data.sqlProcess.getErrorStream(), "ERR_SQL");

        // any output?
        data.outputLogger = new StreamLogger(log, data.sqlProcess.getInputStream(), "OUT_SQL");

        // Where do we send the data to? --> To STDIN of the sql process
        //
        data.sqlOutputStream = data.sqlProcess.getOutputStream();

        // kick them off
        new Thread(data.errorLogger).start();
        new Thread(data.outputLogger).start();

      } catch (Exception ex) {
        throw new KettleException("Error while executing psql : " + cmd, ex);
      }

      logDetailed("Connected to VectorWise with the 'sql' command.");

      // OK, from here on, we need to feed in the COPY command followed by the
      // data into the pgOutputStream
      //
      String loadCommand = createLoadCommand();
      logDetailed("Executing command: " + loadCommand);
      data.sqlRunner = new SqlRunner(data, loadCommand);
      data.sqlRunner.start();

      logDetailed("LOAD TABLE command started");

      // Open a new fifo output stream, buffered.
      //
      openFifoFile();

      logDetailed("Fifo stream opened");

      // Wait until it all hooks up in the FIFO
      //
      waitForAConnection();

      logDetailed("Ready to start bulk loading!");
    } catch (Exception ex) {
      throw new KettleException(ex);
    }

    return true;
  }