Search in sources :

Example 21 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class PigQueryInterpreterTest method testBasics.

@Test
public void testBasics() throws IOException {
    String content = "andy\tmale\t10\n" + "peter\tmale\t20\n" + "amy\tfemale\t14\n";
    File tmpFile = File.createTempFile("zeppelin", "test");
    FileWriter writer = new FileWriter(tmpFile);
    IOUtils.write(content, writer);
    writer.close();
    // run script in PigInterpreter
    String pigscript = "a = load '" + tmpFile.getAbsolutePath() + "' as (name, gender, age);\n" + "a2 = load 'invalid_path' as (name, gender, age);\n" + "dump a;";
    InterpreterResult result = pigInterpreter.interpret(pigscript, context);
    assertEquals(InterpreterResult.Type.TEXT, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertTrue(result.message().get(0).getData().contains("(andy,male,10)\n(peter,male,20)\n(amy,female,14)"));
    // run single line query in PigQueryInterpreter
    String query = "foreach a generate name, age;";
    result = pigQueryInterpreter.interpret(query, context);
    assertEquals(InterpreterResult.Type.TABLE, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("name\tage\nandy\t10\npeter\t20\namy\t14\n", result.message().get(0).getData());
    // run multiple line query in PigQueryInterpreter
    query = "b = group a by gender;\nforeach b generate group as gender, COUNT($1) as count;";
    result = pigQueryInterpreter.interpret(query, context);
    assertEquals(InterpreterResult.Type.TABLE, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("gender\tcount\nmale\t2\nfemale\t1\n", result.message().get(0).getData());
    // generate alias with unknown schema
    query = "b = group a by gender;\nforeach b generate group, COUNT($1);";
    result = pigQueryInterpreter.interpret(query, context);
    assertEquals(InterpreterResult.Type.TABLE, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertEquals("group\tcol_1\nmale\t2\nfemale\t1\n", result.message().get(0).getData());
    // syntax error in PigQueryInterpereter
    query = "b = group a by invalid_column;\nforeach b generate group as gender, COUNT($1) as count;";
    result = pigQueryInterpreter.interpret(query, context);
    assertEquals(InterpreterResult.Type.TEXT, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.ERROR, result.code());
    assertTrue(result.message().get(0).getData().contains("Projected field [invalid_column] does not exist in schema"));
    // execution error in PigQueryInterpreter
    query = "foreach a2 generate name, age;";
    result = pigQueryInterpreter.interpret(query, context);
    assertEquals(InterpreterResult.Type.TEXT, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.ERROR, result.code());
    assertTrue(result.message().get(0).getData().contains("Input path does not exist"));
}
Also used : FileWriter(java.io.FileWriter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) File(java.io.File) Test(org.junit.Test)

Example 22 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class PigQueryInterpreterTest method testMaxResult.

@Test
public void testMaxResult() throws IOException {
    StringBuilder content = new StringBuilder();
    for (int i = 0; i < 30; ++i) {
        content.append(i + "\tname_" + i + "\n");
    }
    File tmpFile = File.createTempFile("zeppelin", "test");
    FileWriter writer = new FileWriter(tmpFile);
    IOUtils.write(content, writer);
    writer.close();
    // run script in PigInterpreter
    String pigscript = "a = load '" + tmpFile.getAbsolutePath() + "' as (id, name);";
    InterpreterResult result = pigInterpreter.interpret(pigscript, context);
    assertEquals(0, result.message().size());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    // run single line query in PigQueryInterpreter
    String query = "foreach a generate id;";
    result = pigQueryInterpreter.interpret(query, context);
    assertEquals(InterpreterResult.Type.TABLE, result.message().get(0).getType());
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    assertTrue(result.message().get(0).getData().contains("id\n0\n1\n2"));
    assertTrue(result.message().get(0).getData().contains("Results are limited by 20"));
}
Also used : FileWriter(java.io.FileWriter) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) File(java.io.File) Test(org.junit.Test)

Example 23 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class PythonInterpreterMatplotlibTest method testClose.

@Test
public // Test for when configuration is set to auto-close figures after show().
void testClose() throws IOException {
    InterpreterResult ret;
    InterpreterResult ret1;
    InterpreterResult ret2;
    ret = python.interpret("import matplotlib.pyplot as plt", context);
    ret = python.interpret("z.configure_mpl(interactive=False)", context);
    ret = python.interpret("plt.plot([1, 2, 3])", context);
    ret1 = python.interpret("plt.show()", context);
    // Second call to show() should print nothing, and Type should be TEXT.
    // This is because when close=True, there should be no living instances
    // of FigureManager, causing show() to return before setting the output
    // type to HTML.
    ret = python.interpret("plt.show()", context);
    assertEquals(new String(out.getOutputAt(0).toByteArray()), InterpreterResult.Code.SUCCESS, ret.code());
    assertEquals(0, ret.message().size());
    // Now test that new plot is drawn. It should be identical to the
    // previous one.
    ret = python.interpret("plt.plot([1, 2, 3])", context);
    String msg1 = new String(out.getOutputAt(0).toByteArray());
    InterpreterResult.Type type1 = out.getOutputAt(0).getType();
    ret2 = python.interpret("plt.show()", context);
    String msg2 = new String(out.getOutputAt(0).toByteArray());
    InterpreterResult.Type type2 = out.getOutputAt(0).getType();
    assertEquals(msg1, msg2);
    assertEquals(type1, type2);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 24 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class PythonInterpreterMatplotlibTest method testNoClose.

@Test
public // Test for when configuration is set to not auto-close figures after show().
void testNoClose() throws IOException {
    InterpreterResult ret;
    InterpreterResult ret1;
    InterpreterResult ret2;
    ret = python.interpret("import matplotlib.pyplot as plt", context);
    ret = python.interpret("z.configure_mpl(interactive=False, close=False)", context);
    ret = python.interpret("plt.plot([1, 2, 3])", context);
    ret1 = python.interpret("plt.show()", context);
    // Second call to show() should print nothing, and Type should be HTML.
    // This is because when close=False, there should be living instances
    // of FigureManager, causing show() to set the output
    // type to HTML even though the figure is inactive.
    ret = python.interpret("plt.show()", context);
    String msg1 = new String(out.getOutputAt(0).toByteArray());
    assertNotSame("", msg1);
    // Now test that plot can be reshown if it is updated. It should be
    // different from the previous one because it will plot the same line
    // again but in a different color.
    ret = python.interpret("plt.plot([1, 2, 3])", context);
    msg1 = new String(out.getOutputAt(1).toByteArray());
    ret2 = python.interpret("plt.show()", context);
    String msg2 = new String(out.getOutputAt(1).toByteArray());
    assertNotSame(msg1, msg2);
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Example 25 with InterpreterResult

use of org.apache.zeppelin.interpreter.InterpreterResult in project zeppelin by apache.

the class PythonInterpreterMatplotlibTest method dependenciesAreInstalled.

@Test
public void dependenciesAreInstalled() {
    // matplotlib
    InterpreterResult ret = python.interpret("import matplotlib", context);
    assertEquals(ret.message().toString(), InterpreterResult.Code.SUCCESS, ret.code());
    // inline backend
    ret = python.interpret("import backend_zinline", context);
    assertEquals(ret.message().toString(), InterpreterResult.Code.SUCCESS, ret.code());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Test(org.junit.Test)

Aggregations

InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)159 Test (org.junit.Test)68 Properties (java.util.Properties)17 File (java.io.File)10 IOException (java.io.IOException)9 AlluxioURI (alluxio.AlluxioURI)8 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)7 Theory (org.junit.experimental.theories.Theory)7 FileWriter (java.io.FileWriter)6 PrintStream (java.io.PrintStream)5 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)5 Code (org.apache.zeppelin.interpreter.InterpreterResult.Code)5 InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)5 ActionResponse (org.apache.zeppelin.elasticsearch.action.ActionResponse)4 FileInStream (alluxio.client.file.FileInStream)3 JsonObject (com.google.gson.JsonObject)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3