Search in sources :

Example 6 with PigStats

use of org.apache.pig.tools.pigstats.PigStats in project oozie by apache.

the class PigMain method runPigJob.

/**
 * Runs the pig script using PigRunner. Embedded pig within python is also supported.
 *
 * @param args pig command line arguments
 * @param pigLog pig log file
 * @param resetSecurityManager specify if need to reset security manager
 * @param retrieveStats specify if stats are to be retrieved
 * @throws Exception in case of error during Pig execution
 */
protected void runPigJob(String[] args, String pigLog, boolean resetSecurityManager, boolean retrieveStats) throws Exception {
    PigStats stats = PigRunner.run(args, null);
    String jobIds = getHadoopJobIds(stats);
    if (jobIds != null && !jobIds.isEmpty()) {
        System.out.println("Hadoop Job IDs executed by Pig: " + jobIds);
        File f = new File(System.getProperty(EXTERNAL_CHILD_IDS));
        writeExternalData(jobIds, f);
    }
    if (!stats.isSuccessful()) {
        if (pigLog != null) {
            handleError(pigLog);
        }
        throw new LauncherMainException(PigRunner.ReturnCode.FAILURE);
    } else {
        // return
        if (resetSecurityManager) {
            return;
        }
        // configuration
        if (retrieveStats) {
            ActionStats pigStats;
            String JSONString;
            try {
                pigStats = new OoziePigStats(stats);
                JSONString = pigStats.toJSON();
            } catch (UnsupportedOperationException uoe) {
                throw new UnsupportedOperationException("Pig stats are not supported for this type of operation", uoe);
            }
            File f = new File(System.getProperty(EXTERNAL_ACTION_STATS));
            writeExternalData(JSONString, f);
        }
    }
}
Also used : PigStats(org.apache.pig.tools.pigstats.PigStats) File(java.io.File)

Example 7 with PigStats

use of org.apache.pig.tools.pigstats.PigStats in project oozie by apache.

the class PigMain method getHadoopJobIds.

/**
 * Get Hadoop Ids through PigStats API
 *
 * @param pigStats stats object obtained through PigStats API
 * @return comma-separated String
 */
protected String getHadoopJobIds(PigStats pigStats) {
    StringBuilder sb = new StringBuilder(STRING_BUFFER_SIZE);
    String separator = ",";
    // comma separated string
    try {
        PigStats.JobGraph jobGraph = pigStats.getJobGraph();
        for (JobStats jobStats : jobGraph) {
            String hadoopJobId = jobStats.getJobId();
            if (StringUtils.isEmpty(hadoopJobId) || hadoopJobId.trim().equalsIgnoreCase("NULL")) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append(separator);
            }
            sb.append(hadoopJobId);
        }
    }// Return null if Pig API's are not supported
     catch (UnsupportedOperationException uoe) {
        return null;
    }
    return sb.toString();
}
Also used : PigStats(org.apache.pig.tools.pigstats.PigStats) JobStats(org.apache.pig.tools.pigstats.JobStats)

Example 8 with PigStats

use of org.apache.pig.tools.pigstats.PigStats in project oozie by apache.

the class PigTestCase method resetPigStats.

/**
 * PigStats is a singleton, so it gets persisted between unit tests that run different Pig jobs that run PigMain. Unfortunetly,
 * there isn't a clean way to reset it.  Pig v 0.9 has a set method which we can pass null to in order to reset the PigStats.
 * However, this was changed in Pig v 0.13 to the start method.  In either case, they're both package private, so we have to use
 * reflection to find the existing method for whichever version of Pig we're using and make it public.
 *
 * This should be called when tearing down any unit test that runs PigMain.
 *
 * @throws Exception
 */
static void resetPigStats() throws Exception {
    Method m = null;
    try {
        System.out.println("Attempting to reset PigStats via 'set' method");
        m = PigStats.class.getDeclaredMethod("set", PigStats.class);
    } catch (NoSuchMethodException e1) {
        try {
            System.out.println("Attempting to reset PigStats via 'start' method");
            m = PigStats.class.getDeclaredMethod("start", PigStats.class);
        } catch (NoSuchMethodException e2) {
            System.out.println("WARNING: Unable to reset PigStats. This may cause test failures.");
        }
    }
    if (m != null) {
        m.setAccessible(true);
        m.invoke(null, (PigStats) null);
    }
}
Also used : PigStats(org.apache.pig.tools.pigstats.PigStats) Method(java.lang.reflect.Method)

Aggregations

PigStats (org.apache.pig.tools.pigstats.PigStats)8 File (java.io.File)4 JobStats (org.apache.pig.tools.pigstats.JobStats)3 IOException (java.io.IOException)2 FrontendException (org.apache.pig.impl.logicalLayer.FrontendException)2 ParseException (org.apache.pig.tools.pigscript.parser.ParseException)2 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)2 VespaCounters (com.yahoo.vespa.hadoop.mapreduce.util.VespaCounters)1 FileWriter (java.io.FileWriter)1 PrintStream (java.io.PrintStream)1 PrintWriter (java.io.PrintWriter)1 RandomAccessFile (java.io.RandomAccessFile)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 FileSystem (org.apache.hadoop.fs.FileSystem)1 Path (org.apache.hadoop.fs.Path)1 Counters (org.apache.hadoop.mapred.Counters)1 HCatBaseTest (org.apache.hive.hcatalog.mapreduce.HCatBaseTest)1 PigServer (org.apache.pig.PigServer)1