use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class SqlInterpreterTest method testView.
@Test
public void testView() throws InterpreterException, IOException {
// create table
InterpreterContext context = getInterpreterContext();
InterpreterResult result = sqlInterpreter.interpret("CREATE TABLE source_table (int_col INT, double_col double, " + "varchar_col varchar, bool_col boolean)" + " WITH (\n" + "'format.field-delimiter'='\\n',\n" + "'connector.type'='filesystem',\n" + "'format.derive-schema'='true',\n" + "'connector.path'='hdfs:///tmp/bank.csv',\n" + "'format.type'='csv'\n" + ");", context);
assertEquals(Code.SUCCESS, result.code());
List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(Type.TEXT, resultMessages.get(0).getType());
assertEquals("Table has been created.\n", resultMessages.get(0).getData());
// create view
context = getInterpreterContext();
result = sqlInterpreter.interpret("create view my_view as select int_col from source_table", context);
assertEquals(Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(Type.TEXT, resultMessages.get(0).getType());
assertEquals("View has been created.\n", resultMessages.get(0).getData());
// create same view again
context = getInterpreterContext();
result = sqlInterpreter.interpret("create view my_view as select int_col from source_table", context);
assertEquals(Code.ERROR, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(Type.TEXT, resultMessages.get(0).getType());
assertTrue(resultMessages.get(0).getData(), resultMessages.get(0).getData().contains("already exists"));
// show view
context = getInterpreterContext();
result = sqlInterpreter.interpret("show tables", context);
assertEquals(Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(Type.TABLE, resultMessages.get(0).getType());
assertEquals("table\nmy_view\nsource_table\n", resultMessages.get(0).getData());
// drop view
context = getInterpreterContext();
result = sqlInterpreter.interpret("drop view my_view", context);
assertEquals(Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals("View has been dropped.\n", resultMessages.get(0).getData());
// show tables again
context = getInterpreterContext();
result = sqlInterpreter.interpret("show tables", context);
assertEquals(Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(Type.TABLE, resultMessages.get(0).getType());
assertEquals("table\nsource_table\n", resultMessages.get(0).getData());
// create temporary view
context = getInterpreterContext();
result = sqlInterpreter.interpret("create temporary view my_temp_view as select int_col from source_table", context);
assertEquals(result.toString(), Code.SUCCESS, result.code());
resultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, resultMessages.size());
assertEquals(Type.TEXT, resultMessages.get(0).getType());
assertEquals("View has been created.\n", resultMessages.get(0).getData());
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class InfluxDBInterpeterTest method testMultiTable.
@Test
public void testMultiTable() throws InterpreterException {
InfluxDBInterpreter t = new InfluxDBInterpreter(properties);
t.open();
// just for testing with real influxdb (not used in mock)
String flux = "from(bucket: \"my-bucket\")\n" + " |> range(start: -1h)\n" + " |> filter(fn: (r) => r._measurement == \"cpu\")\n" + " |> filter(fn: (r) => r._field == \"usage_user\")\n" + " |> aggregateWindow(every: 1m, fn: mean)\n" + " |> limit(n:5, offset: 0)";
InterpreterContext context = InterpreterContext.builder().setAuthenticationInfo(new AuthenticationInfo("testUser")).build();
mockServer.enqueue(createResponse(MULTI_TABLE_RESPONSE));
InterpreterResult interpreterResult = t.interpret(flux, context);
// if prefix not found return ERROR and Prefix not found.
if (InterpreterResult.Code.ERROR.equals(interpreterResult.code())) {
Assert.fail(interpreterResult.toString());
}
assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
List<InterpreterResultMessage> message = interpreterResult.message();
Assert.assertEquals(9, message.size());
message.forEach(m -> Assert.assertEquals(InterpreterResult.Type.TABLE, m.getType()));
Assert.assertEquals("result\ttable\t_start\t_stop\t_field\t_measurement\tcpu\thost\t_value\t_time\n" + "_result\t0\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu-total\tmacek.local\t12.381414297598637\t2020-01-24T09:28:00Z\n" + "_result\t0\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu-total\tmacek.local\t18.870254041431455\t2020-01-24T09:29:00Z\n" + "_result\t0\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu-total\tmacek.local\t26.64080311971415\t2020-01-24T09:30:00Z\n" + "_result\t0\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu-total\tmacek.local\t11.644120979499911\t2020-01-24T09:31:00Z\n" + "_result\t0\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu-total\tmacek.local\t16.046354351571846\t2020-01-24T09:32:00Z\n", message.get(0).getData());
Assert.assertEquals("result\ttable\t_start\t_stop\t_field\t_measurement\tcpu\thost\t_value" + "\t_time\n" + "_result\t8\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu7\tmacek.local\t3.4507517507517504\t2020-01-24T09:28:00Z\n" + "_result\t8\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu7\tmacek.local\t8.817554700888033\t2020-01-24T09:29:00Z\n" + "_result\t8\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu7\tmacek.local\t16.957243048909714\t2020-01-24T09:30:00Z\n" + "_result\t8\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu7\tmacek.local\t3.408601950268617\t2020-01-24T09:31:00Z\n" + "_result\t8\t2020-01-24T09:27:44.845218500Z\t2020-01-24T10:27:44.845218500Z\tusage_user" + "\tcpu\tcpu7\tmacek.local\t10.672760839427506\t2020-01-24T09:32:00Z\n", message.get(8).getData());
t.close();
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class InfluxDBInterpeterTest method testSigleTable.
@Test
public void testSigleTable() throws InterpreterException {
InfluxDBInterpreter t = new InfluxDBInterpreter(properties);
t.open();
// just for testing with real influxdb (not used in mock)
String flux = "from(bucket: \"my-bucket\")\n" + " |> range(start:-1m)\n" + " |> filter(fn: (r) => r._measurement == \"cpu\")\n" + " |> filter(fn: (r) => r._field == \"usage_user\")\n" + " |> filter(fn: (r) => r.cpu == \"cpu-total\")\n" + " |> limit(n:5, offset: 0)" + " |> keep(columns: [\"_field\", \"_value\", \"_time\"])";
InterpreterContext context = InterpreterContext.builder().setAuthenticationInfo(new AuthenticationInfo("testUser")).build();
mockServer.enqueue(createResponse(SINGLE_TABLE_RESPONSE));
InterpreterResult interpreterResult = t.interpret(flux, context);
// if prefix not found return ERROR and Prefix not found.
assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
List<InterpreterResultMessage> message = interpreterResult.message();
Assert.assertEquals(1, message.size());
Assert.assertEquals(InterpreterResult.Type.TABLE, message.get(0).getType());
Assert.assertEquals("result\ttable\t_time\t_value\t_field\n" + "_result\t0\t2020-01-24T10:23:56Z\t12.114014251781473\tusage_user\n" + "_result\t0\t2020-01-24T10:23:57Z\t12.048493938257717\tusage_user\n" + "_result\t0\t2020-01-24T10:24:06Z\t12.715678919729932\tusage_user\n" + "_result\t0\t2020-01-24T10:24:07Z\t11.876484560570072\tusage_user\n" + "_result\t0\t2020-01-24T10:24:16Z\t10.044977511244378\tusage_user\n" + "_result\t0\t2020-01-24T10:24:17Z\t10.594702648675662\tusage_user\n" + "_result\t0\t2020-01-24T10:24:26Z\t12.092034512942353\tusage_user\n" + "_result\t0\t2020-01-24T10:24:27Z\t12.131065532766383\tusage_user\n" + "_result\t0\t2020-01-24T10:24:36Z\t14.332125452955141\tusage_user\n" + "_result\t0\t2020-01-24T10:24:37Z\t15.153788447111777\tusage_user\n", message.get(0).getData());
t.close();
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class BasePythonInterpreterTest 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());
if (interpreter instanceof IPythonInterpreter) {
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertTrue(interpreterResultMessages.get(0).getData().contains("name 'unknown' is not defined"));
} else if (interpreter instanceof PythonInterpreter) {
assertTrue(result.message().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());
if (interpreter instanceof IPythonInterpreter) {
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertTrue(interpreterResultMessages.get(0).getData().contains("ZeroDivisionError"));
} else if (interpreter instanceof PythonInterpreter) {
assertTrue(result.message().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());
}
use of org.apache.zeppelin.interpreter.InterpreterResultMessage in project zeppelin by apache.
the class BasePythonInterpreterTest method testZeppelinContext.
@Test
public void testZeppelinContext() throws InterpreterException, InterruptedException, IOException {
// TextBox
InterpreterContext context = getInterpreterContext();
InterpreterResult result = interpreter.interpret("z.input(name='text_1', defaultValue='value_1')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
List<InterpreterResultMessage> interpreterResultMessages = context.out.toInterpreterResultMessage();
assertTrue(interpreterResultMessages.get(0).getData().contains("'value_1'"));
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("text_1") instanceof TextBox);
TextBox textbox = (TextBox) context.getGui().getForms().get("text_1");
assertEquals("text_1", textbox.getName());
assertEquals("value_1", textbox.getDefaultValue());
// Password
context = getInterpreterContext();
result = interpreter.interpret("z.password(name='pwd_1')", context);
Thread.sleep(100);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertTrue(context.getGui().getForms().get("pwd_1") instanceof Password);
Password password = (Password) context.getGui().getForms().get("pwd_1");
assertEquals("pwd_1", password.getName());
// Select
context = getInterpreterContext();
result = interpreter.interpret("z.select(name='select_1'," + " options=[('value_1', 'name_1'), ('value_2', 'name_2')])", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("select_1") instanceof Select);
Select select = (Select) context.getGui().getForms().get("select_1");
assertEquals("select_1", select.getName());
assertEquals(2, select.getOptions().length);
assertEquals("name_1", select.getOptions()[0].getDisplayName());
assertEquals("value_1", select.getOptions()[0].getValue());
// CheckBox
context = getInterpreterContext();
result = interpreter.interpret("z.checkbox(name='checkbox_1'," + "options=[('value_1', 'name_1'), ('value_2', 'name_2')])", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
assertEquals(1, context.getGui().getForms().size());
assertTrue(context.getGui().getForms().get("checkbox_1") instanceof CheckBox);
CheckBox checkbox = (CheckBox) context.getGui().getForms().get("checkbox_1");
assertEquals("checkbox_1", checkbox.getName());
assertEquals(2, checkbox.getOptions().length);
assertEquals("name_1", checkbox.getOptions()[0].getDisplayName());
assertEquals("value_1", checkbox.getOptions()[0].getValue());
// Pandas DataFrame
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3], 'name':['a\ta','b\\nb','c\\r\\nc']})\nz.show(df)", context);
assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("id\tname\n1\ta a\n2\tb b\n3\tc c\n", interpreterResultMessages.get(0).getData());
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3,4], 'name':['a','b','c', 'd']})\nz.show(df)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(2, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("id\tname\n1\ta\n2\tb\n3\tc\n", interpreterResultMessages.get(0).getData());
assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(1).getType());
assertEquals("<font color=red>Results are limited by 3.</font>\n", interpreterResultMessages.get(1).getData());
// z.show(df, show_index=True)
context = getInterpreterContext();
result = interpreter.interpret("import pandas as pd\n" + "df = pd.DataFrame({'id':[1,2,3], 'name':['a','b','c']})\n" + "z.show(df, show_index=True)", context);
assertEquals(context.out.toInterpreterResultMessage().toString(), InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.TABLE, interpreterResultMessages.get(0).getType());
assertEquals("\tid\tname\n" + "%html <strong>0</strong>\t1\ta\n" + "%html <strong>1</strong>\t2\tb\n" + "%html <strong>2</strong>\t3\tc\n", interpreterResultMessages.get(0).getData());
// z.show(matplotlib)
context = getInterpreterContext();
result = interpreter.interpret("import matplotlib.pyplot as plt\n" + "data=[1,1,2,3,4]\nplt.figure()\nplt.plot(data)\nz.show(plt)", context);
assertEquals(InterpreterResult.Code.SUCCESS, result.code());
interpreterResultMessages = context.out.toInterpreterResultMessage();
assertEquals(1, interpreterResultMessages.size());
assertEquals(InterpreterResult.Type.HTML, interpreterResultMessages.get(0).getType());
// clear output
context = getInterpreterContext();
result = interpreter.interpret("import time\nprint(\"Hello\")\n" + "time.sleep(0.5)\nz.getInterpreterContext().out().clear()\nprint(\"world\")\n", context);
assertEquals("%text world\n", context.out.getCurrentOutput().toString());
}
Aggregations