Example #1
0
  @Test
  public void testRun_setsErrorThrowableForParamSubstitution() {
    File outputFile = null;
    try {
      String filename =
          this.getClass().getSimpleName() + "_" + "testRun_setsErrorThrowableForParamSubstitution";
      outputFile = File.createTempFile(filename, ".out");
      outputFile.delete();

      File scriptFile = File.createTempFile(filename, ".pig");
      BufferedWriter bw = new BufferedWriter(new FileWriter(scriptFile));
      bw.write("a = load '$NOEXIST';\n");
      bw.write("b = group a by $0\n");
      bw.write("c = foreach b generate group, COUNT(a) as cnt;\n");
      bw.write("store c into 'out'\n");
      bw.close();

      Main.run(new String[] {"-x", "local", scriptFile.getAbsolutePath()}, null);
      PigStats stats = PigStats.get();

      Throwable t = stats.getErrorThrowable();
      assertTrue(t instanceof IOException);

      Throwable cause = t.getCause();
      assertTrue(cause instanceof ParameterSubstitutionException);

      ParameterSubstitutionException pse = (ParameterSubstitutionException) cause;
      assertTrue(pse.getMessage().contains("NOEXIST"));
    } catch (Exception e) {
      log.error("Encountered exception", e);
      fail("Encountered Exception");
    }
  }
Example #2
0
  @Test
  public void testRun_setsErrorThrowableOnPigStats() {
    File outputFile = null;
    try {
      String filename =
          this.getClass().getSimpleName() + "_" + "testRun_setsErrorThrowableOnPigStats";
      outputFile = File.createTempFile(filename, ".out");
      outputFile.delete();

      File scriptFile = File.createTempFile(filename, ".pig");
      BufferedWriter bw = new BufferedWriter(new FileWriter(scriptFile));
      bw.write("a = load 'test/org/apache/pig/test/data/passwd';\n");
      bw.write("b = group a by $0\n");
      bw.write("c = foreach b generate group, COUNT(a) as cnt;\n");
      bw.write("store c into 'out'\n");
      bw.close();

      Main.run(new String[] {"-x", "local", scriptFile.getAbsolutePath()}, null);
      PigStats stats = PigStats.get();

      Throwable t = stats.getErrorThrowable();
      assertTrue(t instanceof FrontendException);

      FrontendException fe = (FrontendException) t;
      SourceLocation sl = fe.getSourceLocation();
      assertEquals(2, sl.line());
      assertEquals(15, sl.offset());

      Throwable cause = fe.getCause();
      assertTrue(cause instanceof ParserException);

    } catch (Exception e) {
      log.error("Encountered exception", e);
      fail("Encountered Exception");
    }
  }
  @SuppressWarnings("deprecation")
  @Override
  protected void runReportal() throws Exception {
    System.out.println("Reportal Pig: Setting up Pig");

    injectAllVariables(prop.getString(PIG_SCRIPT));

    String[] args = getParams();

    System.out.println("Reportal Pig: Running pig script");
    PrintStream oldOutputStream = System.out;

    File tempOutputFile = new File("./temp.out");
    OutputStream tempOutputStream = new BufferedOutputStream(new FileOutputStream(tempOutputFile));
    PrintStream printStream = new PrintStream(tempOutputStream);
    System.setOut(printStream);

    PigStats stats = PigRunner.run(args, null);

    System.setOut(oldOutputStream);

    printStream.close();

    // convert pig output to csv and write it to disk
    System.out.println("Reportal Pig: Converting output");
    InputStream tempInputStream = new BufferedInputStream(new FileInputStream(tempOutputFile));
    Scanner rowScanner = new Scanner(tempInputStream);
    PrintStream csvOutputStream = new PrintStream(outputStream);
    while (rowScanner.hasNextLine()) {
      String line = rowScanner.nextLine();
      // strip all quotes, and then quote the columns
      if (line.startsWith("(")) {
        line = transformDumpLine(line);
      } else {
        line = transformDescriptionLine(line);
      }
      csvOutputStream.println(line);
    }
    rowScanner.close();
    csvOutputStream.close();

    // Flush the temp file out
    tempOutputFile.delete();

    if (!stats.isSuccessful()) {
      System.out.println("Reportal Pig: Handling errors");

      File pigLogFile = new File("./");

      File[] fileList = pigLogFile.listFiles();

      for (File file : fileList) {
        if (file.isFile() && file.getName().matches("^pig_.*\\.log$")) {
          handleError(file);
        }
      }

      // see jira ticket PIG-3313. Will remove these when we use pig binary with that patch.
      // /////////////////////
      System.out.println("Trying to do self kill, in case pig could not.");
      Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
      Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
      for (Thread t : threadArray) {
        if (!t.isDaemon() && !t.equals(Thread.currentThread())) {
          System.out.println("Killing thread " + t);
          t.interrupt();
          t.stop();
        }
      }
      System.exit(1);
      // ////////////////////

      throw new ReportalRunnerException("Pig job failed.");
    } else {
      System.out.println("Reportal Pig: Ended successfully");
    }
  }