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
This commit is contained in:
Ali Bajwa 2015-10-06 22:02:18 -07:00 committed by Jeff Zhang
parent 2586336e44
commit c28beb5889
4 changed files with 198 additions and 12 deletions

View file

@ -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;
```

View file

@ -51,6 +51,12 @@
<version>${pig.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View file

@ -40,24 +40,25 @@ import org.slf4j.LoggerFactory;
/**
* 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 class PigInterpreter extends Interpreter {
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 class PigInterpreter extends Interpreter {
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() {

View file

@ -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");
}
}