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

The following examples show how to use org.apache.zeppelin.interpreter.InterpreterResult#code() . 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: PythonInterpreter.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
protected void bootstrapInterpreter(String resourceName) throws IOException {
  LOGGER.info("Bootstrap interpreter via " + resourceName);
  String bootstrapCode =
      IOUtils.toString(getClass().getClassLoader().getResourceAsStream(resourceName));
  try {
    // Add hook explicitly, otherwise python will fail to execute the statement
    InterpreterResult result = interpret(bootstrapCode + "\n" + "__zeppelin__._displayhook()",
        InterpreterContext.get());
    if (result.code() != Code.SUCCESS) {
      throw new IOException("Fail to run bootstrap script: " + resourceName + "\n" + result);
    } else {
      LOGGER.debug("Bootstrap python successfully.");
    }
  } catch (InterpreterException e) {
    throw new IOException(e);
  }
}
 
Example 3
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 4
Source File: LivySparkSQLInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws InterpreterException {
  this.sparkInterpreter = getInterpreterInTheSameSessionByClassName(LivySparkInterpreter.class);
  // As we don't know whether livyserver use spark2 or spark1, so we will detect SparkSession
  // to judge whether it is using spark2.
  try {
    InterpreterContext context = InterpreterContext.builder()
        .setInterpreterOut(new InterpreterOutput(null))
        .build();
    InterpreterResult result = sparkInterpreter.interpret("spark", context);
    if (result.code() == InterpreterResult.Code.SUCCESS &&
        result.message().get(0).getData().contains("org.apache.spark.sql.SparkSession")) {
      LOGGER.info("SparkSession is detected so we are using spark 2.x for session {}",
          sparkInterpreter.getSessionInfo().id);
      isSpark2 = true;
    } else {
      // spark 1.x
      result = sparkInterpreter.interpret("sqlContext", context);
      if (result.code() == InterpreterResult.Code.SUCCESS) {
        LOGGER.info("sqlContext is detected.");
      } else if (result.code() == InterpreterResult.Code.ERROR) {
        // create SqlContext if it is not available, as in livy 0.2 sqlContext
        // is not available.
        LOGGER.info("sqlContext is not detected, try to create SQLContext by ourselves");
        result = sparkInterpreter.interpret(
            "val sqlContext = new org.apache.spark.sql.SQLContext(sc)\n"
                + "import sqlContext.implicits._", context);
        if (result.code() == InterpreterResult.Code.ERROR) {
          throw new LivyException("Fail to create SQLContext," +
              result.message().get(0).getData());
        }
      }
    }
  } catch (LivyException e) {
    throw new RuntimeException("Fail to Detect SparkVersion", e);
  }
}
 
Example 5
Source File: JDBCInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public InterpreterResult executePrecode(InterpreterContext interpreterContext)
        throws InterpreterException {
  InterpreterResult interpreterResult = null;
  for (String propertyKey : basePropertiesMap.keySet()) {
    String precode = getProperty(String.format("%s.precode", propertyKey));
    if (StringUtils.isNotBlank(precode)) {
      interpreterResult = executeSql(propertyKey, precode, interpreterContext);
      if (interpreterResult.code() != Code.SUCCESS) {
        break;
      }
    }
  }

  return interpreterResult;
}
 
Example 6
Source File: NotebookRestApi.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
/**
 * Run synchronously a paragraph REST API.
 *
 * @param noteId      - noteId
 * @param paragraphId - paragraphId
 * @param message     - JSON with params if user wants to update dynamic form's value
 *                    null, empty string, empty json if user doesn't want to update
 * @return JSON with status.OK
 * @throws IOException
 * @throws IllegalArgumentException
 */
@POST
@Path("run/{noteId}/{paragraphId}")
@ZeppelinApi
public Response runParagraphSynchronously(@PathParam("noteId") String noteId,
                                          @PathParam("paragraphId") String paragraphId,
                                          String message)
    throws IOException, IllegalArgumentException {
  LOG.info("run paragraph synchronously {} {} {}", noteId, paragraphId, message);

  Note note = notebook.getNote(noteId);
  checkIfNoteIsNotNull(note);
  Paragraph paragraph = note.getParagraph(paragraphId);
  checkIfParagraphIsNotNull(paragraph);

  Map<String, Object> params = new HashMap<>();
  if (!StringUtils.isEmpty(message)) {
    ParametersRequest request =
        ParametersRequest.fromJson(message);
    params = request.getParams();
  }

  if (notebookService.runParagraph(noteId, paragraphId, paragraph.getTitle(),
      paragraph.getText(), params,
      new HashMap<>(), false, true, getServiceContext(), new RestServiceCallback<>())) {
    note = notebookService.getNote(noteId, getServiceContext(), new RestServiceCallback<>());
    Paragraph p = note.getParagraph(paragraphId);
    InterpreterResult result = p.getReturn();
    if (result.code() == InterpreterResult.Code.SUCCESS) {
      return new JsonResponse<>(Status.OK, result).build();
    } else {
      return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR, result).build();
    }
  } else {
    return new JsonResponse<>(Status.INTERNAL_SERVER_ERROR, "Fail to run paragraph").build();
  }
}
 
Example 7
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));
  }
}
 
Example 8
Source File: CredentialInjector.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
public InterpreterResult hidePasswords(InterpreterResult ret) {
  if (ret == null) {
    return null;
  }
  return new InterpreterResult(ret.code(), replacePasswords(ret.message()));
}
 
Example 9
Source File: RemoteInterpreterServer.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public InterpreterResult jobRun() throws Throwable {
  ClassLoader currentThreadContextClassloader = Thread.currentThread().getContextClassLoader();
  try {
    InterpreterContext.set(context);
    // clear the result of last run in frontend before running this paragraph.
    context.out.clear();

    InterpreterResult result = null;

    // Open the interpreter instance prior to calling interpret().
    // This is necessary because the earliest we can register a hook
    // is from within the open() method.
    LazyOpenInterpreter lazy = (LazyOpenInterpreter) interpreter;
    if (!lazy.isOpen()) {
      lazy.open();
      result = lazy.executePrecode(context);
    }

    if (result == null || result.code() == Code.SUCCESS) {
      // Add hooks to script from registry.
      // note scope first, followed by global scope.
      // Here's the code after hooking:
      //     global_pre_hook
      //     note_pre_hook
      //     script
      //     note_post_hook
      //     global_post_hook
      processInterpreterHooks(context.getNoteId());
      processInterpreterHooks(null);
      LOGGER.debug("Script after hooks: " + script);
      result = interpreter.interpret(script, context);
    }

    // data from context.out is prepended to InterpreterResult if both defined
    context.out.flush();
    List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();

    for (InterpreterResultMessage resultMessage : result.message()) {
      // only add non-empty InterpreterResultMessage
      if (!StringUtils.isBlank(resultMessage.getData())) {
        resultMessages.add(resultMessage);
      }
    }

    List<String> stringResult = new ArrayList<>();
    for (InterpreterResultMessage msg : resultMessages) {
      if (msg.getType() == InterpreterResult.Type.IMG) {
        LOGGER.debug("InterpreterResultMessage: IMAGE_DATA");
      } else {
        LOGGER.debug("InterpreterResultMessage: " + msg.toString());
      }
      stringResult.add(msg.getData());
    }
    // put result into resource pool
    if (context.getLocalProperties().containsKey("saveAs")) {
      if (stringResult.size() == 1) {
        LOGGER.info("Saving result into ResourcePool as single string: " +
                context.getLocalProperties().get("saveAs"));
        context.getResourcePool().put(
                context.getLocalProperties().get("saveAs"), stringResult.get(0));
      } else {
        LOGGER.info("Saving result into ResourcePool as string list: " +
                context.getLocalProperties().get("saveAs"));
        context.getResourcePool().put(
                context.getLocalProperties().get("saveAs"), stringResult);
      }
    }
    return new InterpreterResult(result.code(), resultMessages);
  } catch (Throwable e) {
    return new InterpreterResult(Code.ERROR, ExceptionUtils.getStackTrace(e));
  } finally {
    Thread.currentThread().setContextClassLoader(currentThreadContextClassloader);
    InterpreterContext.remove();
  }
}