The fourth part of Apache Zeppelin series of tutorials - analysis of JDBCInterpreter principle

Interpreter is actually the core of the whole project, and the code operation is executed in it. First, let's look at the abstract class of Interpreter

Taking jdbc-Interpreter as an example, you can refer to the test code (in this code, you can directly test the sql execution process and data return process of jdbc)

 For parameters and introductions, please refer to the official documentation: Apache Zeppelin 0.10.1 Documentation: Generic JDBC Interpreter for Apache Zeppelin

  @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"));
  }

The execution process of JDBCInterpreter class internalInterpret is mainly:

(1) execute sql

(2) Result processing:

getResults judges whether the set number of lines will be exceeded, context.out.write in InterpreterOutput writes in bytes and judges whether the number of bytes is exceeded

(3) Call this method to return the result

List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();

Guess you like

Origin blog.csdn.net/weixin_43291055/article/details/130598976