org.apache.zeppelin.interpreter.InterpreterResultMessage Java Examples

The following examples show how to use org.apache.zeppelin.interpreter.InterpreterResultMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseLivyInterpreter.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private InterpreterResult appendSessionExpireDead(InterpreterResult result,
                                                  boolean sessionExpired,
                                                  boolean sessionDead) {
  InterpreterResult result2 = new InterpreterResult(result.code());
  if (sessionExpired) {
    result2.add(InterpreterResult.Type.HTML,
        "<font color=\"red\">Previous livy session is expired, new livy session is created. " +
            "Paragraphs that depend on this paragraph need to be re-executed!</font>");

  }
  if (sessionDead) {
    result2.add(InterpreterResult.Type.HTML,
        "<font color=\"red\">Previous livy session is dead, new livy session is created. " +
            "Paragraphs that depend on this paragraph need to be re-executed!</font>");
  }

  for (InterpreterResultMessage message : result.message()) {
    result2.add(message.getType(), message.getData());
  }
  return result2;
}
 
Example #2
Source File: IPythonKernelTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #3
Source File: InterpreterResultTableData.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public InterpreterResultTableData(InterpreterResultMessage msg) {
  this.msg = msg;

  String[] lines = msg.getData().split("\n");
  if (lines == null || lines.length == 0) {
    columnDef = null;
  } else {
    String[] headerRow = lines[0].split("\t");
    columnDef = new ColumnDef[headerRow.length];
    for (int i = 0; i < headerRow.length; i++) {
      columnDef[i] = new ColumnDef(headerRow[i], ColumnDef.TYPE.STRING);
    }

    for (int r = 1; r < lines.length; r++) {
      Object [] row = lines[r].split("\t");
      rows.add(new Row(row));
    }
  }
}
 
Example #4
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testStatementPrecode() throws IOException, InterpreterException {
  Properties properties = new Properties();
  properties.setProperty("default.driver", "org.h2.Driver");
  properties.setProperty("default.url", getJdbcConnection());
  properties.setProperty("default.user", "");
  properties.setProperty("default.password", "");
  properties.setProperty(DEFAULT_STATEMENT_PRECODE, "set @v='statement'");
  JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties);
  jdbcInterpreter.open();

  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\nstatement\n", resultMessages.get(0).getData());
}
 
Example #5
Source File: RemoteInterpreterServer.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private RemoteInterpreterResult convert(InterpreterResult result,
                                        Map<String, Object> config, GUI gui, GUI noteGui) {

  List<RemoteInterpreterResultMessage> msg = new LinkedList<>();
  for (InterpreterResultMessage m : result.message()) {
    msg.add(new RemoteInterpreterResultMessage(
        m.getType().name(),
        m.getData()));
  }

  return new RemoteInterpreterResult(
      result.code().name(),
      msg,
      gson.toJson(config),
      gui.toJson(),
      noteGui.toJson());
}
 
Example #6
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectQueryMaxResult() throws IOException, InterpreterException {
  Properties properties = new Properties();
  properties.setProperty("common.max_count", "1");
  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 * from test_table";

  InterpreterResult interpreterResult = t.interpret(sqlQuery, context);

  assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());

  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
  assertEquals("ID\tNAME\na\ta_name\n", resultMessages.get(0).getData());
  assertEquals(InterpreterResult.Type.HTML, resultMessages.get(1).getType());
  assertTrue(resultMessages.get(1).getData().contains("Output is truncated"));
}
 
Example #7
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSelectQueryWithNull() 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 * from test_table WHERE ID = 'c'";

  InterpreterResult interpreterResult = t.interpret(sqlQuery, context);

  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
  assertEquals("ID\tNAME\nc\tnull\n", resultMessages.get(0).getData());
}
 
Example #8
Source File: InterpreterResultTableDataTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
  InterpreterResultMessage msg = new InterpreterResultMessage(
      InterpreterResult.Type.TABLE,
      "key\tvalue\nsun\t100\nmoon\t200\n");
  InterpreterResultTableData table = new InterpreterResultTableData(msg);

  ColumnDef[] cols = table.columns();
  assertEquals(2, cols.length);

  assertEquals("key", cols[0].name());
  assertEquals("value", cols[1].name());

  Iterator<Row> it = table.rows();
  Row row = it.next();
  assertEquals(2, row.get().length);
  assertEquals("sun", row.get()[0]);
  assertEquals("100", row.get()[1]);

  row = it.next();
  assertEquals("moon", row.get()[0]);
  assertEquals("200", row.get()[1]);

  assertFalse(it.hasNext());
}
 
Example #9
Source File: IPythonInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws InterpreterException {
  Properties properties = initIntpProperties();
  startInterpreter(properties);

  InterpreterContext context = getInterpreterContext();
  InterpreterResult result = interpreter.interpret("import sys\nsys.version_info.major", context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  try {
    List<InterpreterResultMessage> messages = context.out.toInterpreterResultMessage();
    if (messages.get(0).getData().equals("2")) {
      isPython2 = true;
    } else {
      isPython2 = false;
    }
  } catch (IOException e) {
    throw new InterpreterException(e);
  }

}
 
Example #10
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #11
Source File: PySparkInterpreterMatplotlibTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
/**
 * This code is mainly copied from RemoteInterpreterServer.java which
 * normally handles this in real use cases.
 */
@Override
public InterpreterResult interpret(String st, InterpreterContext context) throws InterpreterException {
  context.out.clear();
  InterpreterResult result = super.interpret(st, context);
  List<InterpreterResultMessage> resultMessages = null;
  try {
    context.out.flush();
    resultMessages = context.out.toInterpreterResultMessage();
  } catch (IOException e) {
    e.printStackTrace();
  }
  resultMessages.addAll(result.message());

  return new InterpreterResult(result.code(), resultMessages);
}
 
Example #12
Source File: FlinkStreamSqlInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleStreamSql() throws IOException, InterpreterException {
  String initStreamScalaScript = getInitStreamScript(100);
  InterpreterContext context = getInterpreterContext();
  InterpreterResult result = flinkInterpreter.interpret(initStreamScalaScript, context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  context = getInterpreterContext();
  context.getLocalProperties().put("type", "single");
  context.getLocalProperties().put("template", "Total Count: {1} <br/> {0}");
  result = sqlInterpreter.interpret("select max(rowtime), count(1) " +
          "from log", context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Type.ANGULAR, resultMessages.get(0).getType());
  assertTrue(resultMessages.toString(),
          resultMessages.get(0).getData().contains("Total Count"));
}
 
Example #13
Source File: FlinkStreamSqlInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateStreamSql() throws IOException, InterpreterException {
  String initStreamScalaScript = getInitStreamScript(100);
  InterpreterResult result = flinkInterpreter.interpret(initStreamScalaScript,
          getInterpreterContext());
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  InterpreterContext context = getInterpreterContext();
  context.getLocalProperties().put("type", "update");
  result = sqlInterpreter.interpret("select url, count(1) as pv from " +
          "log group by url", context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
  assertTrue(resultMessages.toString(),
          resultMessages.get(0).getData().contains("url\tpv\n"));
}
 
Example #14
Source File: FlinkStreamSqlInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testAppendStreamSql() throws IOException, InterpreterException {
  String initStreamScalaScript = getInitStreamScript(100);
  InterpreterResult result = flinkInterpreter.interpret(initStreamScalaScript,
          getInterpreterContext());
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  InterpreterContext context = getInterpreterContext();
  context.getLocalProperties().put("type", "append");
  result = sqlInterpreter.interpret("select TUMBLE_START(rowtime, INTERVAL '5' SECOND) as " +
          "start_time, url, count(1) as pv from log group by " +
          "TUMBLE(rowtime, INTERVAL '5' SECOND), url", context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
  assertTrue(resultMessages.toString(),
          resultMessages.get(0).getData().contains("url\tpv\n"));
}
 
Example #15
Source File: FlinkStreamSqlInterpreterTest.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
@Test
public void testResumeStreamSqlFromInvalidSavePointPath() throws IOException, InterpreterException, InterruptedException, TimeoutException {
  String initStreamScalaScript = getInitStreamScript(1000);
  InterpreterResult result = flinkInterpreter.interpret(initStreamScalaScript,
          getInterpreterContext());
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  File savepointPath = FileUtils.getTempDirectory();
  InterpreterContext context = getInterpreterContext();
  context.getLocalProperties().put("type", "update");
  context.getLocalProperties().put("savepointPath", savepointPath.getAbsolutePath());
  context.getLocalProperties().put("parallelism", "1");
  context.getLocalProperties().put("maxParallelism", "10");
  InterpreterResult result2 = sqlInterpreter.interpret("select url, count(1) as pv from " +
          "log group by url", context);

  // due to invalid savepointPath, failed to submit job and throw exception
  assertEquals(InterpreterResult.Code.ERROR, result2.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertTrue(resultMessages.toString().contains("Failed to submit job."));

}
 
Example #16
Source File: CredentialInjector.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private List<InterpreterResultMessage> replacePasswords(List<InterpreterResultMessage> original) {
  List<InterpreterResultMessage> replaced = new ArrayList<>();
  for (InterpreterResultMessage msg : original) {
    switch(msg.getType()) {
      case HTML:
      case TEXT:
      case TABLE: {
        String replacedMessages = replacePasswords(msg.getData());
        replaced.add(new InterpreterResultMessage(msg.getType(), replacedMessages));
        break;
      }
      default:
        replaced.add(msg);
    }
  }
  return replaced;
}
 
Example #17
Source File: AbstractInterpreter.java    From submarine with Apache License 2.0 6 votes vote down vote up
@Override
public InterpreterResult interpret(String code) throws InterpreterException {
  InterpreterResult interpreterResult = null;
  try {
    org.apache.zeppelin.interpreter.InterpreterResult zeplInterpreterResult
            = this.zeppelinInterpreter.interpret(code, getIntpContext());
    interpreterResult = new InterpreterResult(zeplInterpreterResult);

    List<InterpreterResultMessage> interpreterResultMessages =
            getIntpContext().out.toInterpreterResultMessage();

    for (org.apache.zeppelin.interpreter.InterpreterResultMessage message : interpreterResultMessages) {
      interpreterResult.add(message);
    }
  } catch (org.apache.zeppelin.interpreter.InterpreterException | IOException e) {
    LOG.error(e.getMessage(), e);
    throw new InterpreterException(e);
  }

  return interpreterResult;
}
 
Example #18
Source File: SqlInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidSql() throws InterpreterException, IOException {

  InterpreterContext context = getInterpreterContext();
  InterpreterResult result = sqlInterpreter.interpret("Invalid sql", context);
  assertEquals(Code.ERROR, result.code());
  List<InterpreterResultMessage> 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("Invalid Sql statement: Invalid sql"));
  assertTrue(resultMessages.get(0).getData(),
          resultMessages.get(0).getData().contains("The following commands are available"));
}
 
Example #19
Source File: IPythonInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testGrpcFrameSize() throws InterpreterException, IOException {
  tearDown();

  Properties properties = initIntpProperties();
  properties.setProperty("zeppelin.jupyter.kernel.grpc.message_size", "4000");

  startInterpreter(properties);

  // to make this test can run under both python2 and python3
  InterpreterResult result =
      interpreter.interpret("from __future__ import print_function", getInterpreterContext());
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  InterpreterContext context = getInterpreterContext();
  result = interpreter.interpret("print('1'*4000)", context);
  assertEquals(InterpreterResult.Code.ERROR, result.code());
  List<InterpreterResultMessage> interpreterResultMessages =
      context.out.toInterpreterResultMessage();
  assertEquals(1, interpreterResultMessages.size());
  assertTrue(interpreterResultMessages.get(0).getData().contains("exceeds maximum size 4000"));

  // next call continue work
  result = interpreter.interpret("print(1)", context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  tearDown();

  // increase framesize to make it work
  properties.setProperty("zeppelin.ipython.grpc.message_size", "5000");
  startInterpreter(properties);
  // to make this test can run under both python2 and python3
  result =
      interpreter.interpret("from __future__ import print_function", getInterpreterContext());
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  context = getInterpreterContext();
  result = interpreter.interpret("print('1'*3000)", context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
}
 
Example #20
Source File: TableDataProxyTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testProxyTable() {
  InterpreterResultMessage msg = new InterpreterResultMessage(
      InterpreterResult.Type.TABLE,
      "key\tvalue\nsun\t100\nmoon\t200\n");
  InterpreterResultTableData table = new InterpreterResultTableData(msg);

  pool.put("table", table);
  TableDataProxy proxy = new TableDataProxy(pool.get("table"));

  ColumnDef[] cols = proxy.columns();
  assertEquals(2, cols.length);

  assertEquals("key", cols[0].name());
  assertEquals("value", cols[1].name());

  Iterator<Row> it = proxy.rows();
  Row row = it.next();
  assertEquals(2, row.get().length);
  assertEquals("sun", row.get()[0]);
  assertEquals("100", row.get()[1]);

  row = it.next();
  assertEquals("moon", row.get()[0]);
  assertEquals("200", row.get()[1]);

  assertFalse(it.hasNext());
}
 
Example #21
Source File: FlinkStreamSqlInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testInsertInto() throws InterpreterException, IOException {
  hiveShell.execute("create table source_table (id int, name string)");

  File destDir = Files.createTempDirectory("flink_test").toFile();
  FileUtils.deleteDirectory(destDir);
  InterpreterResult result = sqlInterpreter.interpret(
          "CREATE TABLE dest_table (\n" +
                  "id int,\n" +
                  "name string" +
                  ") WITH (\n" +
                  "'format.field-delimiter'=',',\n" +
                  "'connector.type'='filesystem',\n" +
                  "'format.derive-schema'='true',\n" +
                  "'connector.path'='" + destDir.getAbsolutePath() + "',\n" +
                  "'format.type'='csv'\n" +
                  ");", getInterpreterContext());
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  result = sqlInterpreter.interpret(
          "insert into dest_table select * from source_table",
          getInterpreterContext());

  assertEquals(InterpreterResult.Code.SUCCESS, result.code());

  // after these select queries, `show tables` should still show only one source table,
  // other temporary tables should not be displayed.
  InterpreterContext context = getInterpreterContext();
  result = sqlInterpreter.interpret("show tables", context);
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  assertEquals(1, resultMessages.size());
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
  assertEquals(resultMessages.get(0).toString(),
          "table\ndest_table\nsource_table\n", resultMessages.get(0).getData());
}
 
Example #22
Source File: RemoteInterpreterEventClient.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private List<org.apache.zeppelin.interpreter.thrift.RemoteInterpreterResultMessage>
      convertToThrift(List<InterpreterResultMessage> messages) {
  List<org.apache.zeppelin.interpreter.thrift.RemoteInterpreterResultMessage> thriftMessages =
      new ArrayList<>();
  for (InterpreterResultMessage message : messages) {
    thriftMessages.add(
        new org.apache.zeppelin.interpreter.thrift.RemoteInterpreterResultMessage(
            message.getType().name(), message.getData()));
  }
  return thriftMessages;
}
 
Example #23
Source File: FlinkStreamSqlInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTableWithWaterMark() throws InterpreterException, IOException {
  // create table
  InterpreterContext context = getInterpreterContext();
  InterpreterResult result = sqlInterpreter.interpret(
          "CREATE TABLE sink_kafka (\n" +
                  "    status  STRING,\n" +
                  "    direction STRING,\n" +
                  "    event_ts TIMESTAMP(3),\n" +
                  "    WATERMARK FOR event_ts AS event_ts - INTERVAL '5' SECOND\n" +
                  ") WITH (\n" +
                  "  'connector.type' = 'kafka',       \n" +
                  "  'connector.version' = 'universal',    \n" +
                  "  'connector.topic' = 'generated.events2',\n" +
                  "  'connector.properties.zookeeper.connect' = 'localhost:2181',\n" +
                  "  'connector.properties.bootstrap.servers' = 'localhost:9092',\n" +
                  "  'connector.properties.group.id' = 'testGroup',\n" +
                  "  'format.type'='json',\n" +
                  "  'update-mode' = 'append'\n" +
                  ")\n",
          context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(1, resultMessages.size());
  assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
  assertEquals("Table has been created.\n", resultMessages.get(0).getData());
}
 
Example #24
Source File: RemoteInterpreterEventClient.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public void onInterpreterOutputUpdateAll(
    String noteId, String paragraphId, List<InterpreterResultMessage> messages) {
  try {
    callRemoteFunction(client -> {
      client.updateAllOutput(
              new OutputUpdateAllEvent(noteId, paragraphId, convertToThrift(messages)));
      return null;
    });

  } catch (Exception e) {
    LOGGER.warn("Fail to updateAllOutput", e);
  }
}
 
Example #25
Source File: FlinkInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testZShow() throws InterpreterException, IOException {
  // show dataset
  InterpreterContext context = getInterpreterContext();
  InterpreterResult result = interpreter.interpret(
          "val data = benv.fromElements((1, \"jeff\"), (2, \"andy\"), (3, \"james\"))",
          context);
  assertEquals(InterpreterResult.Code.SUCCESS, result.code());
  context = getInterpreterContext();
  result = interpreter.interpret("z.show(data)", context);
  assertEquals(context.out.toString(), InterpreterResult.Code.SUCCESS, result.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(0).getType());
  assertEquals("_1\t_2\n1\tjeff\n2\tandy\n3\tjames\n", resultMessages.get(0).getData());
}
 
Example #26
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testSplitSqlQueryWithComments() 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("default.splitQueries", "true");
  JDBCInterpreter t = new JDBCInterpreter(properties);
  t.open();

  String sqlQuery = "/* ; */\n" +
      "-- /* comment\n" +
      "--select * from test_table\n" +
      "select * from test_table; /* some comment ; */\n" +
      "/*\n" +
      "select * from test_table;\n" +
      "*/\n" +
      "-- a ; b\n" +
      "select * from test_table WHERE ID = ';--';\n" +
      "select * from test_table WHERE ID = '/*'; -- test";

  InterpreterResult interpreterResult = t.interpret(sqlQuery, context);
  assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(3, resultMessages.size());
}
 
Example #27
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@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(null))
      .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());
}
 
Example #28
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrecodeWithAnotherPrefix() 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(PRECODE_KEY_TEMPLATE, "anotherPrefix"),
          "create table test_precode_2 (id int); insert into test_precode_2 values (2);");
  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(null))
      .setLocalProperties(localProperties)
      .build();
  jdbcInterpreter.executePrecode(context);

  String sqlQuery = "select * from test_precode_2";

  InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, context);

  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());

  assertEquals(3, resultMessages.size());
  assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
  assertEquals("Query executed successfully. Affected rows : 0\n",
          resultMessages.get(0).getData());
  assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(1).getType());
  assertEquals("Query executed successfully. Affected rows : 1\n",
          resultMessages.get(1).getData());
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(2).getType());
  assertEquals("ID\n2\n", resultMessages.get(2).getData());
}
 
Example #29
Source File: Paragraph.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
/**
 * Save the buffered output to InterpreterResults. So that open another tab or refresh
 * note you can see the latest checkpoint's output.
 */
public void checkpointOutput() {
  LOGGER.info("Checkpoint Paragraph output for paragraph: " + getId());
  this.results = new InterpreterResult(Code.SUCCESS);
  for (InterpreterResultMessage buffer : outputBuffer) {
    results.add(buffer);
  }
}
 
Example #30
Source File: JDBCInterpreterTest.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrecode() throws IOException, InterpreterException {
  Properties properties = new Properties();
  properties.setProperty("default.driver", "org.h2.Driver");
  properties.setProperty("default.url", getJdbcConnection());
  properties.setProperty("default.user", "");
  properties.setProperty("default.password", "");
  properties.setProperty(DEFAULT_PRECODE,
          "create table test_precode (id int); insert into test_precode values (1);");
  JDBCInterpreter jdbcInterpreter = new JDBCInterpreter(properties);
  jdbcInterpreter.open();
  jdbcInterpreter.executePrecode(context);

  String sqlQuery = "select * from test_precode";

  InterpreterResult interpreterResult = jdbcInterpreter.interpret(sqlQuery, context);

  assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code());
  List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
  assertEquals(3, resultMessages.size());
  assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(0).getType());
  assertEquals("Query executed successfully. Affected rows : 0\n",
          resultMessages.get(0).getData());
  assertEquals(InterpreterResult.Type.TEXT, resultMessages.get(1).getType());
  assertEquals("Query executed successfully. Affected rows : 1\n",
          resultMessages.get(1).getData());
  assertEquals(InterpreterResult.Type.TABLE, resultMessages.get(2).getType());
  assertEquals("ID\n1\n", resultMessages.get(2).getData());
}