use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class JDBCInterpreterTest method testStatementPrecodeWithAnotherPrefix.
@Test
public void testStatementPrecodeWithAnotherPrefix() throws IOException, InterpreterException {
Properties properties = new Properties();
properties.setProperty("anotherPrefix.driver", "org.h2.Driver");
properties.setProperty("anotherPrefix.url", getJdbcConnection());
properties.setProperty("anotherPrefix.user", "");
properties.setProperty("anotherPrefix.password", "");
properties.setProperty(String.format(STATEMENT_PRECODE_KEY_TEMPLATE, "anotherPrefix"), "set @v='statementAnotherPrefix'");
JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties);
jdbcInterpreter.open();
Map<String, String> localProperties = new HashMap<>();
localProperties.put("db", "anotherPrefix");
InterpreterContext context = InterpreterContext.builder().setAuthenticationInfo(new AuthenticationInfo("testUser")).setInterpreterOut(new InterpreterOutput()).setLocalProperties(localProperties).build();
String sqlQuery = "select @v";
InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, context);
assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
assertEquals("@V\nstatementAnotherPrefix\n", resultMessages.get(0).getData());
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class JDBCInterpreterTest method testColumnAliasQuery.
@Test
public void testColumnAliasQuery() throws IOException, InterpreterException {
Properties properties = new Properties();
properties.setProperty("common.max_count", "1000");
properties.setProperty("common.max_retry", "3");
properties.setProperty("default.driver", "org.h2.Driver");
properties.setProperty("default.url", getJdbcConnection());
properties.setProperty("default.user", "");
properties.setProperty("default.password", "");
JDBCInterpreter t = new JDBCInterpreter(properties);
t.open();
String sqlQuery = "select NAME as SOME_OTHER_NAME from test_table limit 1";
InterpreterResult interpreterResult = t.interpret(sqlQuery, context);
List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
assertEquals(interpreterResult.toString(), InterpreterResult.Code.SUCCESS, interpreterResult.code());
assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
assertEquals("SOME_OTHER_NAME\na_name\n", resultMessages.get(0).getData());
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class IRKernelTest method testIRInterpreter.
@Test
public void testIRInterpreter() throws InterpreterException, IOException {
InterpreterContext context = getInterpreterContext();
InterpreterResult result = interpreter.interpret("1+1", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(resultMessages.toString(), InterpreterResult.Type.HTML, resultMessages.get(0).getType());
assertEquals(resultMessages.toString(), "2", resultMessages.get(0).getData());
// error
context = getInterpreterContext();
result = interpreter.interpret("unknown_var", context);
assertEquals(InterpreterResult.Code.ERROR, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(result.toString(), InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
assertTrue(resultMessages.toString(), resultMessages.get(0).getData().contains("object 'unknown_var' not found"));
context = getInterpreterContext();
result = interpreter.interpret("foo <- TRUE\n" + "print(foo)\n" + "bare <- c(1, 2.5, 4)\n" + "print(bare)\n" + "double <- 15.0\n" + "print(double)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(result.toString(), InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
assertTrue(resultMessages.toString(), resultMessages.get(0).getData().contains("[1] TRUE\n" + "[1] 1.0 2.5 4.0\n" + "[1] 15\n"));
// plotting
context = getInterpreterContext();
result = interpreter.interpret("hist(mtcars$mpg)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(resultMessages.toString(), InterpreterResult.Type.IMG, resultMessages.get(0).getType());
// ggplot2
result = interpreter.interpret("library(ggplot2)\n" + "ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()", getInterpreterContext());
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(resultMessages.toString(), InterpreterResult.Type.IMG, resultMessages.get(0).getType());
// TODO(zjffdu) It is weird that googlevis doesn't work with spark 2.2
if (ENABLE_GOOGLEVIS_TEST) {
context = getInterpreterContext();
result = interpreter.interpret("library(googleVis)\n" + "df=data.frame(country=c(\"US\", \"GB\", \"BR\"), \n" + " val1=c(10,13,14), \n" + " val2=c(23,12,32))\n" + "Bar <- gvisBarChart(df)\n" + "print(Bar, tag = 'chart')", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(2, resultMessages.size());
assertEquals(resultMessages.toString(), InterpreterResult.Type.HTML, resultMessages.get(1).getType());
assertTrue(resultMessages.get(1).getData(), resultMessages.get(1).getData().contains("javascript"));
}
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class IPythonKernelTest method testUpdateOutput.
@Test
public void testUpdateOutput() throws IOException, InterpreterException {
InterpreterContext context = getInterpreterContext();
String st = "import sys\n" + "import time\n" + "from IPython.display import display, clear_output\n" + "for i in range(10):\n" + " time.sleep(0.25)\n" + " clear_output(wait=True)\n" + " print(i)\n" + " sys.stdout.flush()";
InterpreterResult result = interpreter.interpret(st, context);
List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals("9\n", interpreterResultMessages.get(0).getData());
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class IPythonKernelTest method testPythonBasics.
@Test
public void testPythonBasics() throws InterpreterException, InterruptedException, IOException {
InterpreterContext context = getInterpreterContext();
InterpreterResult result = interpreter.interpret("import sys\nprint(sys.version[0])", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
Thread.sleep(100);
List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
// single output without print
context = getInterpreterContext();
result = interpreter.interpret("'hello world'", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("'hello world'", interpreterResultMessages.get(0).getData().trim());
// unicode
context = getInterpreterContext();
result = interpreter.interpret("print(u'你好')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("你好\n", interpreterResultMessages.get(0).getData());
// only the last statement is printed
context = getInterpreterContext();
result = interpreter.interpret("'hello world'\n'hello world2'", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("'hello world2'", interpreterResultMessages.get(0).getData().trim());
// single output
context = getInterpreterContext();
result = interpreter.interpret("print('hello world')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("hello world\n", interpreterResultMessages.get(0).getData());
// multiple output
context = getInterpreterContext();
result = interpreter.interpret("print('hello world')\nprint('hello world2')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("hello world\nhello world2\n", interpreterResultMessages.get(0).getData());
// assignment
context = getInterpreterContext();
result = interpreter.interpret("abc=1", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(0, interpreterResultMessages.size());
// if block
context = getInterpreterContext();
result = interpreter.interpret("if abc > 0:\n\tprint('True')\nelse:\n\tprint('False')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("True\n", interpreterResultMessages.get(0).getData());
// for loop
context = getInterpreterContext();
result = interpreter.interpret("for i in range(3):\n\tprint(i)", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("0\n1\n2\n", interpreterResultMessages.get(0).getData());
// syntax error
context = getInterpreterContext();
result = interpreter.interpret("print(unknown)", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.ERROR, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertTrue(interpreterResultMessages.get(0).getData().contains("name 'unknown' is not defined"));
// raise runtime exception
context = getInterpreterContext();
result = interpreter.interpret("1/0", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.ERROR, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertTrue(interpreterResultMessages.get(0).getData().contains("ZeroDivisionError"));
// ZEPPELIN-1133
context = getInterpreterContext();
result = interpreter.interpret("from __future__ import print_function\n" + "def greet(name):\n" + " print('Hello', name)\n" + "greet('Jack')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("Hello Jack\n", interpreterResultMessages.get(0).getData());
// ZEPPELIN-1114
context = getInterpreterContext();
result = interpreter.interpret("print('there is no Error: ok')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("there is no Error: ok\n", interpreterResultMessages.get(0).getData());
// ZEPPELIN-3687
context = getInterpreterContext();
result = interpreter.interpret("# print('Hello')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(0, interpreterResultMessages.size());
context = getInterpreterContext();
result = interpreter.interpret("# print('Hello')\n# print('How are u?')\n# time.sleep(1)", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(0, interpreterResultMessages.size());
// multiple text output
context = getInterpreterContext();
result = interpreter.interpret("for i in range(1,4):\n" + "\tprint(i)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals("1\n2\n3\n", interpreterResultMessages.get(0).getData());
}
Aggregations