org.apache.pig.tools.pigstats.PigStats Java Examples

The following examples show how to use org.apache.pig.tools.pigstats.PigStats. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestEmptyInputDir.java    From spork with Apache License 2.0 8 votes vote down vote up
@Test
public void testSkewedJoin() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("A = load '" + INPUT_FILE + "';");
    w.println("B = load '" + EMPTY_DIR + "';");
    w.println("C = join B by $0, A by $0 using 'skewed';");
    w.println("store C into '" + OUTPUT_FILE + "';");
    w.close();
    
    try {
        String[] args = { PIG_FILE };
        PigStats stats = PigRunner.run(args, null);
 
        assertTrue(stats.isSuccessful());
        // the sampler job has zero maps
        MRJobStats js = (MRJobStats)stats.getJobGraph().getSources().get(0);
        
        // This assert fails on 205 due to MAPREDUCE-3606
        if (!Util.isHadoop205()&&!Util.isHadoop1_x())
            assertEquals(0, js.getNumberMaps()); 
        
        FileSystem fs = cluster.getFileSystem();
        FileStatus status = fs.getFileStatus(new Path(OUTPUT_FILE));
        assertTrue(status.isDir());
        assertEquals(0, status.getLen());
        // output directory isn't empty
        assertTrue(fs.listStatus(status.getPath()).length > 0);
    } finally {
        new File(PIG_FILE).delete();
        Util.deleteFile(cluster, OUTPUT_FILE);
    }
}
 
Example #2
Source File: PigServer.java    From spork with Apache License 2.0 6 votes vote down vote up
public PigServer(PigContext context, boolean connect) throws ExecException {
    this.pigContext = context;
    currDAG = new Graph(false);

    jobName = pigContext.getProperties().getProperty(
            PigContext.JOB_NAME,
            PigContext.JOB_NAME_PREFIX + ":DefaultJobName");

    if (connect) {
        pigContext.connect();
    }

    this.filter = new BlackAndWhitelistFilter(this);

    addJarsFromProperties();
    markPredeployedJarsFromProperties();

    if (ScriptState.get() == null) {
        // If Pig was started via command line, ScriptState should have been
        // already initialized in Main. If so, we should not overwrite it.
        ScriptState.start(pigContext.getExecutionEngine().instantiateScriptState());
    }
    PigStats.start(pigContext.getExecutionEngine().instantiatePigStats());

}
 
Example #3
Source File: TestPigRunner.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void classLoaderTest() throws Exception {
    // Skip in hadoop 23 test, see PIG-2449
    if (org.apache.pig.impl.util.Utils.isHadoop23() || org.apache.pig.impl.util.Utils.isHadoop2())
        return;
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("register test/org/apache/pig/test/data/pigtestloader.jar");
    w.println("A = load '" + INPUT_FILE + "' using org.apache.pig.test.PigTestLoader();");
    w.println("store A into '" + OUTPUT_FILE + "';");
    w.close();

    try {
        String[] args = { "-x", execType, PIG_FILE };
        PigStats stats = PigRunner.run(args, new TestNotificationListener(execType));
        assertTrue(stats.isSuccessful());
    } finally {
        new File(PIG_FILE).delete();
        Util.deleteFile(cluster, OUTPUT_FILE);
    }
}
 
Example #4
Source File: JsScriptEngine.java    From spork with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, List<PigStats>> main(PigContext pigContext,
        String scriptFile) throws IOException {

    File f = new File(scriptFile);

    if (!f.canRead()) {
        throw new IOException("Can't read file: " + scriptFile);
    }

    registerFunctions(scriptFile, null, pigContext);    

    // run main
    jsEval("main", "main();");

    return getPigStatsMap();
}
 
Example #5
Source File: TestPigRunner.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void fsCommandTest() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("fs -mv nonexist.file dummy.file");
    w.close();

    try {
        String[] args = { "-x", execType, PIG_FILE };
        PigStats stats = PigRunner.run(args, new TestNotificationListener(execType));

        assertTrue(!stats.isSuccessful());
        assertTrue(stats.getReturnCode() == PigRunner.ReturnCode.IO_EXCEPTION);
    } finally {
        new File(PIG_FILE).delete();
    }
}
 
Example #6
Source File: TestEmptyInputDir.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void testLeftOuterJoin() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("A = load '" + INPUT_FILE + "' as (x:int);");
    w.println("B = load '" + EMPTY_DIR + "' as (x:int);");
    w.println("C = join B by $0 left outer, A by $0;");
    w.println("store C into '" + OUTPUT_FILE + "';");
    w.close();
    
    try {
        String[] args = { PIG_FILE };
        PigStats stats = PigRunner.run(args, null);
 
        assertTrue(stats.isSuccessful());               
        assertEquals(0, stats.getNumberRecords(OUTPUT_FILE));                  
    } finally {
        new File(PIG_FILE).delete();
        Util.deleteFile(cluster, OUTPUT_FILE);
    }
}
 
Example #7
Source File: TestPigRunner.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test // PIG-2006
public void testEmptyFile() throws IOException {
    File f1 = new File(PIG_FILE);

    FileWriter fw1 = new FileWriter(f1);
    fw1.close();

    try {
       String[] args = { "-x", "local", "-c", PIG_FILE };
       PigStats stats = PigRunner.run(args, null);

       assertTrue(stats.isSuccessful());
       assertEquals( 0, stats.getReturnCode() );
    } finally {
        new File(PIG_FILE).delete();
        Util.deleteFile(cluster, OUTPUT_FILE);
    }
}
 
Example #8
Source File: TestQueryParser.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test // test error message with file name
public void testNagative7() throws IOException {
    File f1 = new File("myscript.pig");
    f1.deleteOnExit();
    
    FileWriter fw1 = new FileWriter(f1);
    fw1.append("A = loadd '1.txt';");
    fw1.close();
    
    String[] args = { "-x", "local", "-c", "myscript.pig" };
    PigStats stats = PigRunner.run(args, null);
   
    Assert.assertFalse(stats.isSuccessful());
    
    String expected = "<file myscript.pig, line 1, column 0>";
    String msg = stats.getErrorMessage();
    
    Assert.assertFalse(msg == null);
    Assert.assertTrue(msg.startsWith(expected));
}
 
Example #9
Source File: TestTezLauncher.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void testRun1() throws Exception {
    String query =
            "a = load '" + INPUT_FILE + "' as (x:int, y:chararray);" +
            "b = filter a by x > 100;" +
            "c = foreach b generate y;" +
            "d = group c all;" +
            "store d into '" + OUTPUT_FILE + "';";

    PhysicalPlan pp = Util.buildPp(pigServer, query);
    TezLauncher launcher = new TezLauncher();
    PigStats pigStats = launcher.launchPig(pp, "testRun1", pc);
    assertTrue(pigStats.isSuccessful());

    String[] output = Util.readOutput(cluster.getFileSystem(), OUTPUT_FILE);
    for (int i = 0; i < output.length; i++) {
        assertEquals(OUTPUT_RECORDS[i], output[i]);
    }

    assertEquals(1, pigStats.getInputStats().size());
    assertEquals(INPUT_FILE, pigStats.getInputStats().get(0).getName());

    assertEquals(1, pigStats.getOutputStats().size());
    assertEquals(OUTPUT_FILE, pigStats.getOutputStats().get(0).getName());
}
 
Example #10
Source File: TestGroupConstParallel.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * Test parallelism for group all
 * @throws Exception
 */
@Test
public void testGroupAllWithParallel() throws Exception {
    PigServer pigServer = new PigServer(cluster.getExecType(), cluster
            .getProperties());
    
    
    pigServer.registerQuery("A = LOAD '" + INPUT_FILE + "' as (x:chararray);");
    pigServer.registerQuery("B = group A all parallel 5;");
    {
        Iterator<Tuple> iter = pigServer.openIterator("B");
        List<Tuple> expectedRes = 
            Util.getTuplesFromConstantTupleStrings(
                    new String[] {
                            "('all',{('one'),('two'),('two')})"
                    });
        Util.checkQueryOutputsAfterSort(iter, expectedRes);
        
        JobGraph jGraph = PigStats.get().getJobGraph();
        checkGroupAllWithParallelGraphResult(jGraph);
    }
}
 
Example #11
Source File: BoundScript.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * Run a pipeline on Hadoop.  
 * If there are no stores in this pipeline then nothing will be run.  
 * @param prop Map of properties that Pig should set when running the script.
 * This is intended for use with scripting languages that do not support
 * the Properties object.
 * @return {@link PigStats}, null if there is no bound query to run.
 * @throws IOException
 */
public PigStats runSingle(Properties prop) throws IOException {
    if (queries.size() > 1) {
        throw new IOException(
                "This pipeline contains multiple queries. Use run() method instead");
    }
    if (queries.isEmpty()) {
        LOG.info("No bound query to run");
        return null;
    }
    if (prop != null) {
        scriptContext.getPigContext().getProperties().putAll(prop);
    }
    PigStats ret = exec(queries.get(0)); 
    setPigStats(ret);
    return ret;
}
 
Example #12
Source File: TestPigRunner.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void testRegisterExternalJar() throws Exception {
    String jarName = Util.findPigJarName();

    String[] args = { "-Dpig.additional.jars=" + jarName,
            "-Dmapred.job.queue.name=default", "-x", execType,
            "-e", "A = load '" + INPUT_FILE + "';store A into '" + OUTPUT_FILE + "';\n" };
    PigStats stats = PigRunner.run(args, new TestNotificationListener(execType));

    Util.deleteFile(cluster, OUTPUT_FILE);
    PigContext ctx = stats.getPigContext();

    assertNotNull(ctx);

    assertTrue(ctx.extraJars.contains(ClassLoader.getSystemResource(jarName)));
    assertTrue("default", ctx.getProperties().getProperty(MRConfiguration.JOB_QUEUE_NAME) != null
            && ctx.getProperties().getProperty(MRConfiguration.JOB_QUEUE_NAME).equals("default")
            || ctx.getProperties().getProperty("mapreduce.job.queuename") != null
            && ctx.getProperties().getProperty("mapreduce.job.queuename").equals("default"));

}
 
Example #13
Source File: NativeTezOper.java    From spork with Apache License 2.0 6 votes vote down vote up
public void runJob(String jobStatsKey) throws JobCreationException {
    RunJarSecurityManager secMan = new RunJarSecurityManager();
    try {
        RunJar.main(getNativeTezParams());
        ((TezPigScriptStats)PigStats.get()).addTezJobStatsForNative(jobStatsKey, this, true);
    } catch (SecurityException se) {
        if(secMan.getExitInvoked()) {
            if(secMan.getExitCode() != 0) {
                throw new JobCreationException("Native job returned with non-zero return code");
            }
            else {
                ((TezPigScriptStats)PigStats.get()).addTezJobStatsForNative(jobStatsKey, this, true);
            }
        }
    } catch (Throwable t) {
        JobCreationException e = new JobCreationException(
                "Cannot run native tez job "+ t.getMessage(), t);
        ((TezPigScriptStats)PigStats.get()).addTezJobStatsForNative(jobStatsKey, this, false);
        throw e;
    } finally {
        secMan.retire();
    }
}
 
Example #14
Source File: TestMacroExpansion.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterSubstitutionTest() throws Exception {
    String macro = "define group_and_count (A,C) returns B, D {\n" +
        "    $B = JOIN $A BY user, $C BY user using 'replicated' partition by org.apache.pig.test.utils.SimpleCustomPartitioner parallel 5;\n" +
        "    $D = JOIN $A BY $0, $C BY $1 using 'skewed' parallel 5;\n" +
        "};\n";
    
    String script = 
        "alpha = load 'users' as (user, age, zip);\n" +
        "beta = load 'links' as (user, link, view);\n" +
        "gamma, sigma = group_and_count (alpha,beta);\n" +
        "store gamma into '$output1';\n" +
        "store sigma into '$output2';\n";
    
    File f1 = new File("myscript.pig");
    f1.deleteOnExit();
    
    FileWriter fw1 = new FileWriter(f1);
    fw1.append(macro).append(script);
    fw1.close();
    
    String[] args = { "-x", "local", "-p", "output1=byuser", "-p", "output2=byage", "-c", "myscript.pig" };
    PigStats stats = PigRunner.run(args, null);
 
    assertTrue(stats.isSuccessful());
}
 
Example #15
Source File: PigServer.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * Compile and execute the current plan.
 * @return
 * @throws IOException
 */
private PigStats execute() throws IOException {
    pigContext.getProperties().setProperty( PigContext.JOB_NAME, jobName );
    if( jobPriority != null ) {
        pigContext.getProperties().setProperty( PigContext.JOB_PRIORITY, jobPriority );
    }

    // In this plan, all stores in the plan will be executed. They should be ignored if the plan is reused.
    currDAG.countExecutedStores();

    currDAG.compile();

    if( currDAG.lp.size() == 0 ) {
       return PigStats.get();
    }

    pigContext.getProperties().setProperty("pig.logical.plan.signature", currDAG.lp.getSignature());

    PigStats stats = executeCompiledLogicalPlan();

    return stats;
}
 
Example #16
Source File: TestMRJobStats.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void testMedianMapReduceTime() throws Exception {
    JobClient jobClient = Mockito.mock(JobClient.class);

    // mock methods to return the predefined map and reduce task reports
    Mockito.when(jobClient.getMapTaskReports(jobID)).thenReturn(mapTaskReports);
    Mockito.when(jobClient.getReduceTaskReports(jobID)).thenReturn(reduceTaskReports);

    PigStats.JobGraph jobGraph = new PigStats.JobGraph();
    MRJobStats jobStats = createJobStats("JobStatsTest", jobGraph);
    getJobStatsMethod("setId", JobID.class).invoke(jobStats, jobID);
    jobStats.setSuccessful(true);

    getJobStatsMethod("addMapReduceStatistics", Iterator.class, Iterator.class)
        .invoke(jobStats, Arrays.asList(mapTaskReports).iterator(), Arrays.asList(reduceTaskReports).iterator());
    String msg = (String)getJobStatsMethod("getDisplayString")
        .invoke(jobStats);

    System.out.println(JobStats.SUCCESS_HEADER);
    System.out.println(msg);

    assertTrue(msg.startsWith(ASSERT_STRING));
}
 
Example #17
Source File: TestMultiQueryLocal.java    From spork with Apache License 2.0 6 votes vote down vote up
private boolean executePlan(PhysicalPlan pp) throws IOException {
    boolean failed = true;
    MapReduceLauncher launcher = new MapReduceLauncher();
    PigStats stats = null;
    try {
        stats = launcher.launchPig(pp, "execute", myPig.getPigContext());
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new IOException(e);
    }
    Iterator<JobStats> iter = stats.getJobGraph().iterator();
    while (iter.hasNext()) {
        JobStats js = iter.next();
        failed = !js.isSuccessful();
        if (failed) {
            break;
        }
    }
    return !failed;
}
 
Example #18
Source File: FetchLauncher.java    From spork with Apache License 2.0 6 votes vote down vote up
/**
 * Runs the fetch task by executing chain of calls on the PhysicalPlan from the leaf
 * up to the LoadFunc
 *
 * @param pp - Physical plan
 * @return SimpleFetchPigStats instance representing the fetched result
 * @throws IOException
 */
public PigStats launchPig(PhysicalPlan pp) throws IOException {
    try {
        POStore poStore = (POStore) pp.getLeaves().get(0);
        init(pp, poStore);

        // run fetch
        runPipeline(poStore);

        UDFFinishVisitor udfFinisher = new UDFFinishVisitor(pp,
                new DependencyOrderWalker<PhysicalOperator, PhysicalPlan>(pp));
        udfFinisher.visit();

        return PigStats.start(new EmptyPigStats(pigContext, poStore));
    }
    finally {
        UDFContext.getUDFContext().addJobConf(null);
        pigContext.getProperties().remove(PigImplConstants.CONVERTED_TO_FETCH);
    }
}
 
Example #19
Source File: Main.java    From spork with Apache License 2.0 6 votes vote down vote up
private static int runEmbeddedScript(PigContext pigContext, String file, String engine)
throws IOException {
    log.info("Run embedded script: " + engine);
    pigContext.connect();
    ScriptEngine scriptEngine = ScriptEngine.getInstance(engine);
    Map<String, List<PigStats>> statsMap = scriptEngine.run(pigContext, file);
    PigStatsUtil.setStatsMap(statsMap);

    int failCount = 0;
    int totalCount = 0;
    for (List<PigStats> lst : statsMap.values()) {
        if (lst != null && !lst.isEmpty()) {
            for (PigStats stats : lst) {
                if (!stats.isSuccessful()) failCount++;
                totalCount++;
            }
        }
    }
    return (totalCount > 0 && failCount == totalCount) ? ReturnCode.FAILURE
            : (failCount > 0) ? ReturnCode.PARTIAL_FAILURE
                    : ReturnCode.SUCCESS;
}
 
Example #20
Source File: BoundScript.java    From spork with Apache License 2.0 6 votes vote down vote up
@Override
public PigStats call() throws Exception {
    LOG.info("Query to run:\n" + query);
    PigContext pc = scriptContext.getPigContext();
    ScriptState scriptState = pc.getExecutionEngine().instantiateScriptState();
    ScriptState.start(scriptState);
    ScriptState.get().setScript(query);
    ScriptState.get().registerListener(adaptor);
    PigServer pigServer = new PigServer(ctx, true);
    pigServer.setBatchOn();
    GruntParser grunt = new GruntParser(new StringReader(query), pigServer);
    grunt.setInteractive(false);
    try {
        grunt.parseStopOnError(true);
    } catch (ParseException e) {
        throw new IOException("Failed to parse script", e);
    }
    pigServer.executeBatch();
    return PigStats.get();
}
 
Example #21
Source File: TestPigRunner.java    From spork with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorLogFile() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("A = load '" + INPUT_FILE + "' as (a0:int, a1:int, a2:int);");
    w.println("B = foreach A generate StringSize(a0);");
    w.println("store B into '" + OUTPUT_FILE + "';");
    w.close();

    try {
        String[] args = { "-x", "local", PIG_FILE };
        PigStats stats = PigRunner.run(args, null);

        assertTrue(!stats.isSuccessful());

        Properties props = stats.getPigProperties();
        String logfile = props.getProperty("pig.logfile");
        File f = new File(logfile);
        assertTrue(f.exists());
    } finally {
        new File(PIG_FILE).delete();
    }
}
 
Example #22
Source File: HJob.java    From spork with Apache License 2.0 5 votes vote down vote up
public HJob(JOB_STATUS status,
        PigContext pigContext,
        POStore store,
        String alias,
        PigStats stats) {
    this.status = status;
    this.pigContext = pigContext;
    this.poStore = store;
    this.outFileSpec = poStore.getSFile();
    this.alias = alias;
    this.stats = stats;
}
 
Example #23
Source File: PigServer.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves a list of Job objects from the PigStats object
 * @param stats
 * @return A list of ExecJob objects
 */
protected List<ExecJob> getJobs(PigStats stats) {
    LinkedList<ExecJob> jobs = new LinkedList<ExecJob>();
    if (stats instanceof EmptyPigStats) {
        HJob job = new HJob(HJob.JOB_STATUS.COMPLETED, pigContext, stats.result(null)
                .getPOStore(), null);
        jobs.add(job);
        return jobs;
    }
    JobGraph jGraph = stats.getJobGraph();
    Iterator<JobStats> iter = jGraph.iterator();
    while (iter.hasNext()) {
        JobStats js = iter.next();
        for (OutputStats output : js.getOutputs()) {
            if (js.isSuccessful()) {
                jobs.add(new HJob(HJob.JOB_STATUS.COMPLETED, pigContext, output
                        .getPOStore(), output.getAlias(), stats));
            } else {
                HJob hjob = new HJob(HJob.JOB_STATUS.FAILED, pigContext, output
                        .getPOStore(), output.getAlias(), stats);
                hjob.setException(js.getException());
                jobs.add(hjob);
            }
        }
    }
    return jobs;
}
 
Example #24
Source File: TestScriptLanguage.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
 public void testPyShouldNotFailScriptIfExitCodeIs0() throws Exception {
     String[] script = {
             "#!/usr/bin/python",
             "from org.apache.pig.scripting import *",
             "import sys",
             "if 1 == 2:",
             "   sys.exit(1)",
             "else: sys.exit(0)"
      };

     Map<String, List<PigStats>> statsMap = runScript("testPyShouldNotFailScriptIfExitCodeIs0", script);
     assertEquals(0, statsMap.size());

}
 
Example #25
Source File: TestScriptLanguage.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void NegativeTest() throws Exception {
    String[] script = {
            "#!/usr/bin/python",
            " from org.apache.pig.scripting import *",
            "Pig.fs(\"rmr simple_out\")"
    };

    PigStats stats = runPigRunner("NegativeTest", script, false);

    assertTrue(stats.getErrorCode() == 1121);
    assertTrue(stats.getReturnCode() == PigRunner.ReturnCode.PIG_EXCEPTION);
}
 
Example #26
Source File: PigServer.java    From spork with Apache License 2.0 5 votes vote down vote up
private PigStats executeCompiledLogicalPlan() throws ExecException,
        FrontendException {
    // discover pig features used in this script
    ScriptState.get().setScriptFeatures(currDAG.lp);
    currDAG.lp.optimize(pigContext);

    return launchPlan(currDAG.lp, "job_pigexec_");
}
 
Example #27
Source File: TestEmptyInputDir.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeJoin() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("A = load '" + INPUT_FILE + "';");
    w.println("B = load '" + EMPTY_DIR + "';");
    w.println("C = join A by $0, B by $0 using 'merge';");
    w.println("store C into '" + OUTPUT_FILE + "';");
    w.close();
    
    try {
        String[] args = { PIG_FILE };
        PigStats stats = PigRunner.run(args, null);
 
        assertTrue(stats.isSuccessful());    
        // the indexer job has zero maps
        MRJobStats js = (MRJobStats)stats.getJobGraph().getSources().get(0);
        
        // This assert fails on 205 due to MAPREDUCE-3606
        if (!Util.isHadoop205()&&!Util.isHadoop1_x())
            assertEquals(0, js.getNumberMaps()); 
        
        FileSystem fs = cluster.getFileSystem();
        FileStatus status = fs.getFileStatus(new Path(OUTPUT_FILE));
        assertTrue(status.isDir());
        assertEquals(0, status.getLen());
        
        // output directory isn't empty
        assertTrue(fs.listStatus(status.getPath()).length > 0);            
    } finally {
        new File(PIG_FILE).delete();
        Util.deleteFile(cluster, OUTPUT_FILE);
    }
}
 
Example #28
Source File: TestPigRunner.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorLogFile2() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("A = load '" + INPUT_FILE + "' as (a0:int, a1:int, a2:int);");
    w.println("B = foreach A generate StringSize(a0);");
    w.println("store B into '" + OUTPUT_FILE + "';");
    w.close();

    try {
        String[] args = { "-M", "-x", "local", PIG_FILE };
        PigStats stats = PigRunner.run(args, null);

        assertTrue(!stats.isSuccessful());

        Properties props = stats.getPigProperties();
        // If test on nfs, the pig script complaining "output" exists
        // and does not actually launch the job. This could due to a mapreduce
        // bug which removing file before closing it.
        // If this happens, props is null because we only set pigContext before
        // launching job.
        if (props!=null) {
            String logfile = props.getProperty("pig.logfile");
            File f = new File(logfile);
            assertTrue(f.exists());
        }
    } finally {
        new File(PIG_FILE).delete();
    }
}
 
Example #29
Source File: TestPigRunner.java    From spork with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleTest() throws Exception {
    PrintWriter w = new PrintWriter(new FileWriter(PIG_FILE));
    w.println("A = load '" + INPUT_FILE + "' as (a0:int, a1:int, a2:int);");
    w.println("B = group A by a0;");
    w.println("C = foreach B generate group, COUNT(A);");
    w.println("store C into '" + OUTPUT_FILE + "';");
    w.close();

    try {
        String[] args = { "-Dstop.on.failure=true", "-Dopt.multiquery=false", "-Dopt.fetch=false", "-Daggregate.warning=false", "-x", execType, PIG_FILE };
        PigStats stats = PigRunner.run(args, new TestNotificationListener(execType));

        assertTrue(stats.isSuccessful());

        assertEquals(1, stats.getNumberJobs());
        String name = stats.getOutputNames().get(0);
        assertEquals(OUTPUT_FILE, name);
        assertEquals(12, stats.getBytesWritten());
        assertEquals(3, stats.getRecordWritten());

        assertEquals("A,B,C",
                ((JobStats)stats.getJobGraph().getSinks().get(0)).getAlias());

        Configuration conf = ConfigurationUtil.toConfiguration(stats.getPigProperties());
        assertTrue(conf.getBoolean("stop.on.failure", false));
        assertTrue(!conf.getBoolean("aggregate.warning", true));
        assertTrue(!conf.getBoolean(PigConfiguration.PIG_OPT_MULTIQUERY, true));
        assertTrue(!conf.getBoolean("opt.fetch", true));
    } finally {
        new File(PIG_FILE).delete();
        Util.deleteFile(cluster, OUTPUT_FILE);
    }
}
 
Example #30
Source File: TestMain.java    From spork with Apache License 2.0 5 votes vote down vote up
@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");
    }
}