Java Code Examples for org.apache.zeppelin.interpreter.InterpreterResult#add()

The following examples show how to use org.apache.zeppelin.interpreter.InterpreterResult#add() . 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: RemoteInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private InterpreterResult convert(RemoteInterpreterResult result) {
  InterpreterResult r = new InterpreterResult(
      InterpreterResult.Code.valueOf(result.getCode()));

  for (RemoteInterpreterResultMessage m : result.getMsg()) {
    r.add(InterpreterResult.Type.valueOf(m.getType()), m.getData());
  }

  return r;
}
 
Example 3
Source File: LivySparkSQLInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public InterpreterResult interpret(String line, InterpreterContext context) {
  try {
    if (StringUtils.isEmpty(line)) {
      return new InterpreterResult(InterpreterResult.Code.SUCCESS, "");
    }

    // use triple quote so that we don't need to do string escape.
    String sqlQuery = null;
    if (isSpark2) {
      sqlQuery = "spark.sql(\"\"\"" + line + "\"\"\").show(" + maxResult + ", " +
          truncate + ")";
    } else {
      sqlQuery = "sqlContext.sql(\"\"\"" + line + "\"\"\").show(" + maxResult + ", " +
          truncate + ")";
    }
    InterpreterResult result = sparkInterpreter.interpret(sqlQuery, context);
    if (result.code() == InterpreterResult.Code.SUCCESS) {
      InterpreterResult result2 = new InterpreterResult(InterpreterResult.Code.SUCCESS);
      for (InterpreterResultMessage message : result.message()) {
        // convert Text type to Table type. We assume the text type must be the sql output. This
        // assumption is correct for now. Ideally livy should return table type. We may do it in
        // the future release of livy.
        if (message.getType() == InterpreterResult.Type.TEXT) {
          List<String> rows = parseSQLOutput(message.getData());
          result2.add(InterpreterResult.Type.TABLE, StringUtils.join(rows, "\n"));
          if (rows.size() >= (maxResult + 1)) {
            result2.add(ResultMessages.getExceedsLimitRowsMessage(maxResult,
                ZEPPELIN_LIVY_SPARK_SQL_MAX_RESULT));
          }
        } else {
          result2.add(message.getType(), message.getData());
        }
      }
      return result2;
    } else {
      return result;
    }
  } catch (Exception e) {
    LOGGER.error("Exception in LivySparkSQLInterpreter while interpret ", e);
    return new InterpreterResult(InterpreterResult.Code.ERROR,
        InterpreterUtils.getMostRelevantMessage(e));
  }
}