From ef2223abab92a452b48372ac0b4c50b70646f984 Mon Sep 17 00:00:00 2001 From: Ali Bajwa Date: Mon, 5 Oct 2015 22:40:06 -0700 Subject: [PATCH 1/3] initial commit of pig interpreter --- conf/zeppelin-site.xml.template | 2 +- pig/pom.xml | 143 ++++++++++++++++++ .../apache/zeppelin/pig/PigInterpreter.java | 137 +++++++++++++++++ pom.xml | 1 + .../zeppelin/conf/ZeppelinConfiguration.java | 1 + 5 files changed, 283 insertions(+), 1 deletion(-) create mode 100644 pig/pom.xml create mode 100644 pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java diff --git a/conf/zeppelin-site.xml.template b/conf/zeppelin-site.xml.template index 90989478f97..841875563e0 100755 --- a/conf/zeppelin-site.xml.template +++ b/conf/zeppelin-site.xml.template @@ -85,7 +85,7 @@ zeppelin.interpreters - org.apache.zeppelin.spark.SparkInterpreter,org.apache.zeppelin.spark.PySparkInterpreter,org.apache.zeppelin.spark.SparkSqlInterpreter,org.apache.zeppelin.spark.DepInterpreter,org.apache.zeppelin.markdown.Markdown,org.apache.zeppelin.angular.AngularInterpreter,org.apache.zeppelin.shell.ShellInterpreter,org.apache.zeppelin.hive.HiveInterpreter,org.apache.zeppelin.tajo.TajoInterpreter,org.apache.zeppelin.flink.FlinkInterpreter,org.apache.zeppelin.lens.LensInterpreter,org.apache.zeppelin.ignite.IgniteInterpreter,org.apache.zeppelin.ignite.IgniteSqlInterpreter,org.apache.zeppelin.cassandra.CassandraInterpreter,org.apache.zeppelin.geode.GeodeOqlInterpreter,org.apache.zeppelin.postgresql.PostgreSqlInterpreter,org.apache.zeppelin.phoenix.PhoenixInterpreter,org.apache.zeppelin.kylin.KylinInterpreter + org.apache.zeppelin.spark.SparkInterpreter,org.apache.zeppelin.spark.PySparkInterpreter,org.apache.zeppelin.spark.SparkSqlInterpreter,org.apache.zeppelin.spark.DepInterpreter,org.apache.zeppelin.markdown.Markdown,org.apache.zeppelin.angular.AngularInterpreter,org.apache.zeppelin.shell.ShellInterpreter,org.apache.zeppelin.hive.HiveInterpreter,org.apache.zeppelin.tajo.TajoInterpreter,org.apache.zeppelin.flink.FlinkInterpreter,org.apache.zeppelin.lens.LensInterpreter,org.apache.zeppelin.ignite.IgniteInterpreter,org.apache.zeppelin.ignite.IgniteSqlInterpreter,org.apache.zeppelin.cassandra.CassandraInterpreter,org.apache.zeppelin.geode.GeodeOqlInterpreter,org.apache.zeppelin.postgresql.PostgreSqlInterpreter,org.apache.zeppelin.phoenix.PhoenixInterpreter,org.apache.zeppelin.kylin.KylinInterpreter,org.apache.zeppelin.pig.PigInterpreter Comma separated interpreter configurations. First interpreter become a default diff --git a/pig/pom.xml b/pig/pom.xml new file mode 100644 index 00000000000..62e953adedb --- /dev/null +++ b/pig/pom.xml @@ -0,0 +1,143 @@ + + + + + 4.0.0 + + + zeppelin + org.apache.zeppelin + 0.6.0-incubating-SNAPSHOT + + + org.apache.zeppelin + zeppelin-pig + jar + 0.6.0-incubating-SNAPSHOT + Zeppelin: Apache Pig Interpreter + Zeppelin interprter for Apache Pig + http://zeppelin.incubator.apache.org + + + 0.15.0 + + + + + org.apache.zeppelin + zeppelin-interpreter + ${project.version} + provided + + + + org.apache.pig + pig + ${pig.version} + + + + junit + junit + test + + + + org.mockito + mockito-all + 1.9.5 + test + + + + com.mockrunner + mockrunner-jdbc + 1.0.8 + test + + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.7 + + true + + + + + maven-enforcer-plugin + 1.3.1 + + + enforce + none + + + + + + maven-dependency-plugin + 2.8 + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/../../interpreter/pig + false + false + true + runtime + + + + copy-artifact + package + + copy + + + ${project.build.directory}/../../interpreter/pig + false + false + true + runtime + + + ${project.groupId} + ${project.artifactId} + ${project.version} + ${project.packaging} + + + + + + + + + + diff --git a/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java b/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java new file mode 100644 index 00000000000..6b9a73216e9 --- /dev/null +++ b/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.zeppelin.pig; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.Properties; +import java.util.ArrayList; +import java.util.Arrays; +import org.apache.commons.exec.CommandLine; +import org.apache.commons.exec.DefaultExecutor; +import org.apache.commons.exec.ExecuteException; +import org.apache.commons.exec.ExecuteWatchdog; +import org.apache.commons.exec.PumpStreamHandler; +import org.apache.zeppelin.interpreter.Interpreter; +import org.apache.zeppelin.interpreter.InterpreterContext; +import org.apache.zeppelin.interpreter.InterpreterResult; +import org.apache.zeppelin.interpreter.InterpreterResult.Code; +import org.apache.zeppelin.interpreter.InterpreterPropertyBuilder; +import org.apache.zeppelin.scheduler.Scheduler; +import org.apache.zeppelin.scheduler.SchedulerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Pig interpreter for Zeppelin. + * + * @author abajwa-hw + * + */ +public class PigInterpreter extends Interpreter { + Logger logger = LoggerFactory.getLogger(PigInterpreter.class); + int commandTimeOut = 600000; + + static final String PIG_START_ARGS = "pig.start.args"; + static final String DEFAULT_START_ARGS = "-useHCatalog -exectype tez"; + + static { + Interpreter.register( + "pig", + "pig", + PigInterpreter.class.getName(), + new InterpreterPropertyBuilder() + .add(PIG_START_ARGS, DEFAULT_START_ARGS, "Starting arguments") + .build() + ); + } + + public PigInterpreter(Properties property) { + super(property); + } + + @Override + public void open() {} + + @Override + public void close() {} + + + @Override + public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) { + + CommandLine cmdLine = CommandLine.parse("pig"); + + // add any arguments specified + String startArgs = getProperty((PIG_START_ARGS).trim()); + if (startArgs.length() > 0){ + logger.info("Start arguments passed to pig: " + startArgs); + List argList = Arrays.asList(startArgs.split("\\s+")); + for (String arg : argList) { + cmdLine.addArgument(arg, false); + } + } + + logger.info("Run pig command '" + cmd + "'"); + long start = System.currentTimeMillis(); + cmdLine.addArgument("-e", false); + cmdLine.addArgument(cmd, false); + DefaultExecutor executor = new DefaultExecutor(); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + executor.setStreamHandler(new PumpStreamHandler(outputStream)); + + executor.setWatchdog(new ExecuteWatchdog(commandTimeOut)); + try { + int exitValue = executor.execute(cmdLine); + return new InterpreterResult(InterpreterResult.Code.SUCCESS, outputStream.toString()); + } catch (ExecuteException e) { + logger.error("Can not run " + cmd, e); + return new InterpreterResult(Code.ERROR, e.getMessage()); + } catch (IOException e) { + logger.error("Can not run " + cmd, e); + return new InterpreterResult(Code.ERROR, e.getMessage()); + } + } + + @Override + public void cancel(InterpreterContext context) {} + + @Override + public FormType getFormType() { + return FormType.SIMPLE; + } + + @Override + public int getProgress(InterpreterContext context) { + return 0; + } + + @Override + public Scheduler getScheduler() { + return SchedulerFactory.singleton().createOrGetFIFOScheduler( + PigInterpreter.class.getName() + this.hashCode()); + } + + @Override + public List completion(String buf, int cursor) { + return null; + } + +} + diff --git a/pom.xml b/pom.xml index 02f161fbc7f..9f9040db3db 100755 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,7 @@ shell hive phoenix + pig geode postgresql tajo diff --git a/zeppelin-zengine/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java b/zeppelin-zengine/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java index 1027f3d5d47..f157f053ed6 100755 --- a/zeppelin-zengine/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java +++ b/zeppelin-zengine/src/main/java/org/apache/zeppelin/conf/ZeppelinConfiguration.java @@ -402,6 +402,7 @@ public static enum ConfVars { + "org.apache.zeppelin.shell.ShellInterpreter," + "org.apache.zeppelin.hive.HiveInterpreter," + "org.apache.zeppelin.phoenix.PhoenixInterpreter," + + "org.apache.zeppelin.pig.PigInterpreter," + "org.apache.zeppelin.tajo.TajoInterpreter," + "org.apache.zeppelin.flink.FlinkInterpreter," + "org.apache.zeppelin.ignite.IgniteInterpreter," From 656329d00a766570b25970506aa9e7a95edd8f8a Mon Sep 17 00:00:00 2001 From: Ali Bajwa Date: Mon, 5 Oct 2015 23:25:47 -0700 Subject: [PATCH 2/3] exposed timeout and pig executable via interpreter and added comments --- .../apache/zeppelin/pig/PigInterpreter.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java b/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java index 6b9a73216e9..3d43c80c7c2 100644 --- a/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java +++ b/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.util.List; import java.util.Properties; -import java.util.ArrayList; import java.util.Arrays; import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; @@ -40,24 +39,34 @@ /** * Pig interpreter for Zeppelin. - * + * Closely follows code for shell interpreter * @author abajwa-hw * */ public class PigInterpreter extends Interpreter { Logger logger = LoggerFactory.getLogger(PigInterpreter.class); - int commandTimeOut = 600000; + //Executable name used to start grunt shell + static final String PIG_START_EXE = "pig.executable"; + static final String DEFAULT_START_EXE = "pig"; + + //Arguments to start pig with. More details available via 'pig -help' static final String PIG_START_ARGS = "pig.start.args"; static final String DEFAULT_START_ARGS = "-useHCatalog -exectype tez"; + //How long to wait before timing out (ms) + static final String PIG_TIMEOUT_MS = "pig.timeout.ms"; + static final String DEFAULT_TIMEOUT_MS = "600000"; + static { Interpreter.register( "pig", "pig", PigInterpreter.class.getName(), new InterpreterPropertyBuilder() + .add(PIG_START_EXE, DEFAULT_START_EXE, "Pig executable used to start grunt shell") .add(PIG_START_ARGS, DEFAULT_START_ARGS, "Starting arguments") + .add(PIG_TIMEOUT_MS, DEFAULT_TIMEOUT_MS, "Timeout (ms)") .build() ); } @@ -75,11 +84,12 @@ public void close() {} @Override public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) { + // use commandline to store string corresponding to pig shell command + // start with pig exectable name (or full path if provided)... + CommandLine cmdLine = CommandLine.parse(getProperty(PIG_START_EXE).trim()); - CommandLine cmdLine = CommandLine.parse("pig"); - - // add any arguments specified - String startArgs = getProperty((PIG_START_ARGS).trim()); + // ...add any CLI arguments specified by user in interpreter settings + String startArgs = getProperty(PIG_START_ARGS).trim(); if (startArgs.length() > 0){ logger.info("Start arguments passed to pig: " + startArgs); List argList = Arrays.asList(startArgs.split("\\s+")); @@ -87,15 +97,18 @@ public InterpreterResult interpret(String cmd, InterpreterContext contextInterpr cmdLine.addArgument(arg, false); } } - + // ...finally add contents of pig cell after the -e flag logger.info("Run pig command '" + cmd + "'"); long start = System.currentTimeMillis(); cmdLine.addArgument("-e", false); cmdLine.addArgument(cmd, false); + + // execute command and return success/failure based on its exit value DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStream)); + int commandTimeOut = Integer.parseInt(getProperty(PIG_TIMEOUT_MS)); executor.setWatchdog(new ExecuteWatchdog(commandTimeOut)); try { int exitValue = executor.execute(cmdLine); From 9c88100ee920b3caaccffc0da6771880fc7f15d9 Mon Sep 17 00:00:00 2001 From: Ali Bajwa Date: Tue, 6 Oct 2015 22:02:18 -0700 Subject: [PATCH 3/3] Updated based on comments: 1. Documentation: added pig.md with interpreter documentation and added pig entry to index.md 2. Added test junit test based on passwd file parsing example here https://pig.apache.org/docs/r0.10.0/start.html#run 3. Removed author tag from comment (this was copied from shell interpreter https://github.com/apache/incubator-zeppelin/blob/master/shell/src/main/java/org/apache/zeppelin/shell/ShellInterpreter.java#L42) 4. Implemented cancel functionality 5. Display output stream in case of error --- docs/docs/index.md | 2 + docs/docs/interpreter/pig.md | 49 +++++++ pig/pom.xml | 6 + .../apache/zeppelin/pig/PigInterpreter.java | 30 +++-- .../zeppelin/pig/PigInterpreterTest.java | 125 ++++++++++++++++++ 5 files changed, 200 insertions(+), 12 deletions(-) create mode 100644 docs/docs/interpreter/pig.md create mode 100644 pig/src/test/java/org/apache/zeppelin/pig/PigInterpreterTest.java diff --git a/docs/docs/index.md b/docs/docs/index.md index d243c41be18..66020a4e8dc 100644 --- a/docs/docs/index.md +++ b/docs/docs/index.md @@ -29,6 +29,7 @@ group: nav-right * [tajo](../docs/pleasecontribute.html) * [postgresql, hawq](./interpreter/postgresql.html) * [geode](./interpreter/geode.html) +* [pig](./interpreter/pig.html) ### Display System @@ -53,3 +54,4 @@ group: nav-right * [How to contribute (website)](./development/howtocontributewebsite.html) + diff --git a/docs/docs/interpreter/pig.md b/docs/docs/interpreter/pig.md new file mode 100644 index 00000000000..34428be8577 --- /dev/null +++ b/docs/docs/interpreter/pig.md @@ -0,0 +1,49 @@ +--- +layout: page +title: "Pig Interpreter" +description: "" +group: manual +--- +{% include JB/setup %} + + +## Pig interpreter for Apache Zeppelin +[Apache Pig](https://pig.apache.org/) is a platform for analyzing large data sets that consists of a high-level language for expressing data analysis programs, coupled with infrastructure for evaluating these programs. The salient property of Pig programs is that their structure is amenable to substantial parallelization, which in turns enables them to handle very large data sets. + +- Supported operations through Zeppelin + - Play button: Run multiple lines of pig latin (delimited by semi-colon) entered into Zeppelin cell + - Pause button: Cancel the exection of pig + - Output: Output of jobs are displayed in Zeppelin (for both jobs that were sucessful and those that failed) + +- Unsupported operations + - Progress bar + +### How to setup Pig +Install Pig as you would normally do on the same node where Zeppelin is running - see [documentation](https://pig.apache.org/). If installing through Ambari, you just need to install the client on the Zeppelin node. + +### How to configure interpreter +At the "Interpreters" menu, you have to create a new Pig interpreter and provide next properties: + +property | value | Description +---------|----------|----- +executable | pig | Path to pig executable. If pig is part of PATH, then just pig will work +args | -useHCatalog -exectype local | Arguments to pass pig when starting. Launch 'pig -help' for list of supported options. For example, the options for exectype: local|mapreduce|tez +timeout | 600000 | Time (in milliseconds) to wait for pig job before timing out + +### How to test it's working + +Run below example taken from [this tutorial](http://hortonworks.com/hadoop-tutorial/how-to-use-basic-pig-commands/) + +``` +%sh +rm -f infochimps_dataset_4778_download_16677-csv.zip +wget https://s3.amazonaws.com/hw-sandbox/tutorial1/infochimps_dataset_4778_download_16677-csv.zip -O /tmp/infochimps_dataset_4778_download_16677-csv.zip +unzip /tmp/infochimps_dataset_4778_download_16677-csv.zip -d /tmp +``` +``` +%pig +DIV_A = LOAD 'file:///tmp/infochimps_dataset_4778_download_16677/NYSE/NYSE_dividends_A.csv' using PigStorage(',') AS (exchange:chararray, symbol:chararray, date:chararray, dividend:float); +B = FILTER DIV_A BY symbol=='AZZ'; +C = GROUP B BY dividend; +dump C; +``` diff --git a/pig/pom.xml b/pig/pom.xml index 62e953adedb..7cb8dcc3223 100644 --- a/pig/pom.xml +++ b/pig/pom.xml @@ -51,6 +51,12 @@ ${pig.version} + + org.apache.commons + commons-io + 1.3.2 + + junit junit diff --git a/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java b/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java index 3d43c80c7c2..3576b4a8780 100644 --- a/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java +++ b/pig/src/main/java/org/apache/zeppelin/pig/PigInterpreter.java @@ -40,24 +40,25 @@ /** * Pig interpreter for Zeppelin. * Closely follows code for shell interpreter - * @author abajwa-hw - * */ public class PigInterpreter extends Interpreter { Logger logger = LoggerFactory.getLogger(PigInterpreter.class); + private static final String NEWLINE = "\n"; + //Executable name used to start grunt shell - static final String PIG_START_EXE = "pig.executable"; - static final String DEFAULT_START_EXE = "pig"; + public static final String PIG_START_EXE = "pig.executable"; + public static final String DEFAULT_START_EXE = "pig"; //Arguments to start pig with. More details available via 'pig -help' - static final String PIG_START_ARGS = "pig.start.args"; - static final String DEFAULT_START_ARGS = "-useHCatalog -exectype tez"; + public static final String PIG_START_ARGS = "pig.start.args"; + public static final String DEFAULT_START_ARGS = "-useHCatalog -exectype local"; //How long to wait before timing out (ms) - static final String PIG_TIMEOUT_MS = "pig.timeout.ms"; - static final String DEFAULT_TIMEOUT_MS = "600000"; + public static final String PIG_TIMEOUT_MS = "pig.timeout.ms"; + public static final String DEFAULT_TIMEOUT_MS = "600000"; + DefaultExecutor executor = null; static { Interpreter.register( "pig", @@ -104,7 +105,7 @@ public InterpreterResult interpret(String cmd, InterpreterContext contextInterpr cmdLine.addArgument(cmd, false); // execute command and return success/failure based on its exit value - DefaultExecutor executor = new DefaultExecutor(); + executor = new DefaultExecutor(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStream)); @@ -115,15 +116,20 @@ public InterpreterResult interpret(String cmd, InterpreterContext contextInterpr return new InterpreterResult(InterpreterResult.Code.SUCCESS, outputStream.toString()); } catch (ExecuteException e) { logger.error("Can not run " + cmd, e); - return new InterpreterResult(Code.ERROR, e.getMessage()); + return new InterpreterResult(Code.ERROR, e.getMessage() + NEWLINE + outputStream.toString()); } catch (IOException e) { logger.error("Can not run " + cmd, e); - return new InterpreterResult(Code.ERROR, e.getMessage()); + return new InterpreterResult(Code.ERROR, e.getMessage() + NEWLINE + outputStream.toString()); } } @Override - public void cancel(InterpreterContext context) {} + public void cancel(InterpreterContext context) { + if (executor != null) { + executor.getWatchdog().destroyProcess(); + } + } + @Override public FormType getFormType() { diff --git a/pig/src/test/java/org/apache/zeppelin/pig/PigInterpreterTest.java b/pig/src/test/java/org/apache/zeppelin/pig/PigInterpreterTest.java new file mode 100644 index 00000000000..03bef545226 --- /dev/null +++ b/pig/src/test/java/org/apache/zeppelin/pig/PigInterpreterTest.java @@ -0,0 +1,125 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.zeppelin.pig; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Properties; +import java.io.PrintWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.io.IOException; +import java.io.StringWriter; +import org.apache.commons.io.FileUtils; +import static org.apache.zeppelin.pig.PigInterpreter.*; +import org.apache.zeppelin.interpreter.InterpreterContext; +import org.apache.zeppelin.interpreter.InterpreterResult; +import org.apache.zeppelin.interpreter.InterpreterResult.Code; +import static org.junit.Assert.*; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class PigInterpreterTest { + + private static PigInterpreter pig; + private static InterpreterContext context; + private static final String PASSWD_FILE = "/tmp/tmp_zeppelin_dummypasswd"; + private static final String USERS_FILE = "/tmp/tmp_zeppelin_dummyusers"; + + @BeforeClass + public static void setUp() { + Properties properties = new Properties(); + properties.put(PIG_START_EXE, DEFAULT_START_EXE); + properties.put(PIG_START_ARGS, DEFAULT_START_ARGS); + properties.put(PIG_TIMEOUT_MS, DEFAULT_TIMEOUT_MS); + + pig = new PigInterpreter(properties); + pig.open(); + + context = new InterpreterContext(null, null, null, null, null, null, null, null); + } + + @AfterClass + public static void tearDown() { + pig.close(); + pig.destroy(); + try { + org.apache.commons.io.FileUtils.forceDelete(new File(PASSWD_FILE)); + org.apache.commons.io.FileUtils.forceDelete(new File(USERS_FILE)); + } catch (IOException e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + fail("Unable to cleanup temp pig files:\n" + sw.toString()); + } + } + + /** + * Extract users from a dummy passwd file + * https://pig.apache.org/docs/r0.10.0/start.html#run + */ + @Test + public void testExtractUsers() { + PrintWriter out = null; + StringWriter sw = new StringWriter(); + + try { + out = new PrintWriter(new File(PASSWD_FILE)); + out.println("user1:pass1"); + out.println("user2:pass2"); + out.println("user3:pass3"); + out.println("user4:pass4"); + out.flush(); + } catch (FileNotFoundException e) { + e.printStackTrace(new PrintWriter(sw)); + fail("Unable to write to "+PASSWD_FILE+":\n" + sw.toString()); + } finally { + if (out != null){ + out.close(); + } + } + + try { + FileUtils.deleteDirectory(new File(USERS_FILE)); + } catch (IOException e) { + e.printStackTrace(new PrintWriter(sw)); + fail("Unable to delete: "+USERS_FILE+". Error: " + sw.toString()); + } + String pigScript = "A = load 'file://"+PASSWD_FILE+"' using PigStorage(':');"; + pigScript += "B = foreach A generate $0 as id;"; + pigScript += "store B into 'file://"+USERS_FILE+"';"; + + InterpreterResult result = pig.interpret(pigScript, context); + + String readusers = null; + try { + readusers = new String(Files.readAllBytes(Paths.get(USERS_FILE+"/part-m-00000"))); + } catch (IOException e) { + e.printStackTrace(new PrintWriter(sw)); + fail("Unable read output of pig job from: "+USERS_FILE+"/part-m-00000. Error:\n" + sw.toString()); + } + assertEquals(readusers, "user1\nuser2\nuser3\nuser4\n"); + + } + + +}