Skip to content

Instantly share code, notes, and snippets.

@choenes
Created July 21, 2010 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choenes/484135 to your computer and use it in GitHub Desktop.
Save choenes/484135 to your computer and use it in GitHub Desktop.
package com.tynt.xiang;
import static org.junit.Assert.assertEquals;
import org.apache.pig.data.Tuple;
import org.junit.Before;
import org.junit.Test;
import com.tynt.xiang.helper.Helper;
import com.tynt.xiang.helper.PigRunner;
public class ExampleTest {
private static final String BASE_DIR = "../scripts/example/";
private static final String SCRIPT_FILE = BASE_DIR + "example.pig";
private static final String OUT_FILE = "./target/pig-output";
private static final String IN_FILE = "./target/test-classes/example-input.log";
private PigRunner runner;
@Before
public void setUp() throws Exception {
Helper.delete(OUT_FILE);
runner = new PigRunner();
}
@Test
public void testRecordCount() throws Exception {
runner.execute(SCRIPT_FILE, "in_file="+IN_FILE,"out_file="+OUT_FILE);
List<Tuple> tuples = runner.getTuples("some_pig_alias");
assertEquals(41, tuples.size());
}
}
package com.tynt.xiang.helper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import org.apache.pig.ExecType;
import org.apache.pig.PigServer;
import org.apache.pig.data.Tuple;
import org.apache.pig.tools.parameters.ParameterSubstitutionPreprocessor;
import org.apache.pig.tools.parameters.ParseException;
public class PigRunner {
private PigServer pigServer;
public PigServer getPigServer() {
return pigServer;
}
public PigRunner() throws Exception {
pigServer = new PigServer(ExecType.LOCAL);
}
public void execute(String scriptFile, String... params) throws Exception {
String substFile = preprocess(scriptFile, params);
pigServer.registerScript(substFile);
}
public List<Tuple> getTuples(String aliasName) throws IOException {
List<Tuple> list = new ArrayList<Tuple>();
Iterator<Tuple> tuples = pigServer.openIterator(aliasName);
while(tuples.hasNext()) {
Tuple t = tuples.next();
list.add(t);
}
return list;
}
private String preprocess(String scriptFile, String... params)
throws IOException, ParseException {
ParameterSubstitutionPreprocessor psp = new ParameterSubstitutionPreprocessor(
50);
String substFile = scriptFile + ".subst";
BufferedWriter writer = new BufferedWriter(new FileWriter(substFile));
try {
// String content = FileUtils.readFileToString(new File(script));
// System.out.println(content);
psp.genSubstitutedFile(new BufferedReader(
new FileReader(scriptFile)), writer, params, null);
} catch (org.apache.pig.tools.parameters.ParseException pex) {
throw new ParseException(pex.getMessage());
}
return substFile;
}
}
@choenes
Copy link
Author

choenes commented Jul 21, 2010

Run entire pig scripts from your JUnit tests just like you'll run them from the cluster/cmd line.

@zjffdu
Copy link

zjffdu commented Jul 21, 2010

There's three new API in trunk for pig script which has parameters
registerScript(String fileName, Map<String,String> params)
registerScript(String fileName, List paramsFiles)
registerScript(String fileName, Map<String,String> params,List paramsFiles)

You can use that to replace your preprocess when you upgrade your pig

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment