Search in sources :

Example 1 with InterpreterResultMessage

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

the class SparkShinyInterpreterTest method testSparkShinyApp.

@Test
public void testSparkShinyApp() throws IOException, InterpreterException, InterruptedException, UnirestException {
    /**
     **************** Launch Shiny app with default app name ****************************
     */
    InterpreterContext context = getInterpreterContext();
    context.getLocalProperties().put("type", "ui");
    InterpreterResult result = interpreter.interpret(IOUtils.toString(getClass().getResource("/spark_ui.R")), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    context = getInterpreterContext();
    context.getLocalProperties().put("type", "server");
    result = interpreter.interpret(IOUtils.toString(getClass().getResource("/spark_server.R")), context);
    assertEquals(InterpreterResult.Code.SUCCESS, result.code());
    final InterpreterContext context2 = getInterpreterContext();
    context2.getLocalProperties().put("type", "run");
    Thread thread = new Thread(() -> {
        try {
            interpreter.interpret("", context2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    thread.start();
    // wait for the shiny app start
    Thread.sleep(5 * 1000);
    // extract shiny url
    List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage();
    assertEquals(resultMessages.toString(), 1, resultMessages.size());
    assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType());
    String resultMessageData = resultMessages.get(0).getData();
    assertTrue(resultMessageData, resultMessageData.contains("<iframe"));
    Pattern urlPattern = Pattern.compile(".*src=\"(http\\S*)\".*", Pattern.DOTALL);
    Matcher matcher = urlPattern.matcher(resultMessageData);
    if (!matcher.matches()) {
        fail("Unable to extract url: " + resultMessageData);
    }
    String shinyURL = matcher.group(1);
    // verify shiny app via calling its rest api
    HttpResponse<String> response = Unirest.get(shinyURL).asString();
    assertEquals(200, response.getStatus());
    assertTrue(response.getBody(), response.getBody().contains("Spark Version"));
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) UnirestException(com.mashape.unirest.http.exceptions.UnirestException) IOException(java.io.IOException) InterpreterException(org.apache.zeppelin.interpreter.InterpreterException) Test(org.junit.Test) ShinyInterpreterTest(org.apache.zeppelin.r.ShinyInterpreterTest)

Example 2 with InterpreterResultMessage

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

the class JDBCInterpreterInterpolationTest method testEnableDisableProperty.

@Test
public void testEnableDisableProperty() 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", "");
    resourcePool.put("zid", "mem");
    String sqlQuery = "select * from test_table where id = '{zid}'";
    // 
    // Empty result expected because "zeppelin.jdbc.interpolation" is false by default ...
    // 
    JDBCInterpreter t = new JDBCInterpreter(properties);
    t.open();
    InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext);
    assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
    List<InterpreterResultMessage> resultMessages = interpreterContext.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
    assertEquals("ID\tNAME\n", resultMessages.get(0).getData());
    // 
    // 1 result expected because "zeppelin.jdbc.interpolation" set to "true" ...
    // 
    properties.setProperty("zeppelin.jdbc.interpolation", "true");
    t = new JDBCInterpreter(properties);
    t.open();
    interpreterContext = getInterpreterContext();
    interpreterResult = t.interpret(sqlQuery, interpreterContext);
    assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
    resultMessages = interpreterContext.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
    assertEquals("ID\tNAME\nmem\tmemory\n", resultMessages.get(0).getData());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Properties(java.util.Properties) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Example 3 with InterpreterResultMessage

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

the class JDBCInterpreterInterpolationTest method testNormalQueryInterpolation.

@Test
public void testNormalQueryInterpolation() 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", "");
    properties.setProperty("zeppelin.jdbc.interpolation", "true");
    JDBCInterpreter t = new JDBCInterpreter(properties);
    t.open();
    // 
    // Empty result expected because "kbd" is not defined ...
    // 
    String sqlQuery = "select * from test_table where id = '{kbd}'";
    InterpreterResult interpreterResult = t.interpret(sqlQuery, interpreterContext);
    assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
    List<InterpreterResultMessage> resultMessages = interpreterContext.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
    assertEquals("ID\tNAME\n", resultMessages.get(0).getData());
    resourcePool.put("itemId", "key");
    // 
    // 1 result expected because z-variable 'item' is 'key' ...
    // 
    sqlQuery = "select * from test_table where id = '{itemId}'";
    interpreterContext = getInterpreterContext();
    interpreterResult = t.interpret(sqlQuery, interpreterContext);
    assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
    resultMessages = interpreterContext.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
    assertEquals("ID\tNAME\nkey\tkeyboard\n", resultMessages.get(0).getData());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) Properties(java.util.Properties) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Example 4 with InterpreterResultMessage

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

the class SqlInterpreterTest method testDatabases.

@Test
public void testDatabases() throws InterpreterException, IOException {
    // show databases
    InterpreterContext context = getInterpreterContext();
    InterpreterResult result = sqlInterpreter.interpret("show databases", context);
    assertEquals(Code.SUCCESS, result.code());
    List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals(Type.TABLE, resultMessages.get(0).getType());
    assertEquals("database\ndefault\ntest_db\n", resultMessages.get(0).getData());
    // create database
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("create database db1", context);
    assertEquals(Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(Type.TEXT, resultMessages.get(0).getType());
    assertEquals("Database has been created.\n", resultMessages.get(0).getData());
    // use database
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("use db1", context);
    assertEquals(Code.SUCCESS, result.code());
    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\n", resultMessages.get(0).getData());
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("CREATE TABLE source (msg INT) with ('connector'='print')", context);
    assertEquals(Code.SUCCESS, result.code());
    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\n", resultMessages.get(0).getData());
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("describe db1.source", context);
    assertEquals(Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(Type.TABLE, resultMessages.get(0).getType());
    assertEquals("Column\tType\n" + "msg\tINT\n", resultMessages.get(0).getData());
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("use default", context);
    assertEquals(Code.SUCCESS, result.code());
    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\n", resultMessages.get(0).getData());
    // fail to drop database if there's tables under this database
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("drop database db1", context);
    assertEquals(Code.ERROR, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertTrue(resultMessages.get(0).getData(), resultMessages.get(0).getData().contains("is not empty"));
    // drop table first then drop db
    result = sqlInterpreter.interpret("drop table db1.source", getInterpreterContext());
    assertEquals(Code.SUCCESS, result.code());
    result = sqlInterpreter.interpret("drop database db1", getInterpreterContext());
    assertEquals(Code.SUCCESS, result.code());
    // verify database is dropped
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("show databases", context);
    assertEquals(Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(Type.TABLE, resultMessages.get(0).getType());
    assertEquals("database\ndefault\ntest_db\n", resultMessages.get(0).getData());
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Example 5 with InterpreterResultMessage

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

the class SqlInterpreterTest method testTable.

@Test
public void testTable() 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 ('connector'='print')", 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());
    // describe table
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("describe source_table", context);
    assertEquals(Code.SUCCESS, result.code());
    assertEquals(1, resultMessages.size());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(Type.TABLE, resultMessages.get(0).getType());
    assertEquals("Column\tType\n" + "int_col\tINT\n" + "double_col\tDOUBLE\n" + "varchar_col\tSTRING\n" + "bool_col\tBOOLEAN\n", resultMessages.get(0).getData());
    // describe unknown table
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("describe unknown_table", context);
    assertEquals(Code.ERROR, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertTrue(resultMessages.toString(), resultMessages.get(0).getData().contains("Table `unknown_table` was not found."));
    // drop unknown table
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("drop table unknown_table", context);
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(Code.ERROR, result.code());
    assertEquals(1, resultMessages.size());
    assertTrue(resultMessages.toString(), resultMessages.get(0).getData().contains("does not exist"));
    // drop table
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("drop table source_table", context);
    assertEquals(Code.SUCCESS, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertEquals("Table has been dropped.\n", resultMessages.get(0).getData());
    // describe the dropped table
    context = getInterpreterContext();
    result = sqlInterpreter.interpret("describe source_table", context);
    assertEquals(Code.ERROR, result.code());
    resultMessages = context.out.toInterpreterResultMessage();
    assertEquals(1, resultMessages.size());
    assertTrue(resultMessages.get(0).getData(), resultMessages.get(0).getData().contains("Table `source_table` was not found"));
}
Also used : InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult) InterpreterContext(org.apache.zeppelin.interpreter.InterpreterContext) InterpreterResultMessage(org.apache.zeppelin.interpreter.InterpreterResultMessage) Test(org.junit.Test)

Aggregations

InterpreterResultMessage (org.apache.zeppelin.interpreter.InterpreterResultMessage)80 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)78 Test (org.junit.Test)60 InterpreterContext (org.apache.zeppelin.interpreter.InterpreterContext)55 Properties (java.util.Properties)17 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)15 IOException (java.io.IOException)13 File (java.io.File)8 TimeoutException (java.util.concurrent.TimeoutException)7 Waiter (net.jodah.concurrentunit.Waiter)7 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)6 Test (org.junit.jupiter.api.Test)6 UnirestException (com.mashape.unirest.http.exceptions.UnirestException)3 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 Map (java.util.Map)2 AngularObjectRegistry (org.apache.zeppelin.display.AngularObjectRegistry)2 CheckBox (org.apache.zeppelin.display.ui.CheckBox)2 Select (org.apache.zeppelin.display.ui.Select)2