Exemplo n.º 1
0
 /**
  * Explain and prepare.
  *
  * @param sql the sql
  * @param queryName the query name
  * @return the string
  * @throws UnsupportedEncodingException the unsupported encoding exception
  * @throws LensAPIException
  */
 @CliCommand(
     value = "prepQuery explain",
     help = "Explain and prepare query <query-string>. Can optionally provide <query-name>")
 public String explainAndPrepare(
     @CliOption(
             key = {"", "query"},
             mandatory = true,
             help = "<query-string>")
         String sql,
     @CliOption(
             key = {"name"},
             mandatory = false,
             help = "<query-name>")
         String queryName)
     throws UnsupportedEncodingException, LensAPIException {
   PrettyPrintable cliOutput;
   try {
     QueryPlan plan = getClient().explainAndPrepare(sql, queryName).getData();
     return plan.getPlanString() + "\n" + "Prepare handle:" + plan.getPrepareHandle();
   } catch (final LensAPIException e) {
     BriefError briefError = new BriefError(e.getLensAPIErrorCode(), e.getLensAPIErrorMessage());
     cliOutput =
         new IdBriefErrorTemplate(
             IdBriefErrorTemplateKey.REQUEST_ID, e.getLensAPIRequestId(), briefError);
   } catch (final LensBriefErrorException e) {
     cliOutput = e.getIdBriefErrorTemplate();
   }
   return cliOutput.toPrettyString();
 }
Exemplo n.º 2
0
  /**
   * Execute query.
   *
   * @param sql the sql
   * @param async the asynch
   * @param queryName the query name
   * @return the string
   */
  @CliCommand(
      value = "query execute",
      help = "Execute query <query-string>. " + ASYNC_DOC + " " + QUERY_NAME_DOC)
  public String executeQuery(
      @CliOption(
              key = {"", "query"},
              mandatory = true,
              help = "<query-string>")
          String sql,
      @CliOption(
              key = {"async"},
              mandatory = false,
              unspecifiedDefaultValue = "false",
              specifiedDefaultValue = "true",
              help = "<async>")
          boolean async,
      @CliOption(
              key = {"name"},
              mandatory = false,
              help = "<query-name>")
          String queryName) {

    PrettyPrintable cliOutput;

    try {
      if (async) {
        QueryHandle queryHandle = getClient().executeQueryAsynch(sql, queryName).getData();
        return queryHandle.getHandleIdString();
      } else {
        return formatResultSet(getClient().getResults(sql, queryName));
      }
    } catch (final LensAPIException e) {

      BriefError briefError = new BriefError(e.getLensAPIErrorCode(), e.getLensAPIErrorMessage());
      cliOutput =
          new IdBriefErrorTemplate(
              IdBriefErrorTemplateKey.REQUEST_ID, e.getLensAPIRequestId(), briefError);

    } catch (final LensBriefErrorException e) {
      cliOutput = e.getIdBriefErrorTemplate();
    }

    return cliOutput.toPrettyString();
  }
Exemplo n.º 3
0
  /**
   * Explain query.
   *
   * @param sql the sql
   * @return the string
   * @throws LensAPIException
   * @throws UnsupportedEncodingException the unsupported encoding exception
   */
  @CliCommand(
      value = "query explain",
      help =
          "Explain execution plan of query <query-string>. "
              + "Can optionally save the plan to a file by providing <save_location>")
  public String explainQuery(
      @CliOption(
              key = {"", "query"},
              mandatory = true,
              help = "<query-string>")
          String sql,
      @CliOption(
              key = {"save_location"},
              mandatory = false,
              help = "<save_location>")
          final File path)
      throws IOException, LensAPIException {
    PrettyPrintable cliOutput;

    try {
      QueryPlan plan = getClient().getQueryPlan(sql).getData();
      if (path != null && StringUtils.isNotBlank(path.getPath())) {
        String validPath = getValidPath(path, false, false);
        try (OutputStreamWriter osw =
            new OutputStreamWriter(new FileOutputStream(validPath), Charset.defaultCharset())) {
          osw.write(plan.getPlanString());
        }
        return "Saved to " + validPath;
      }
      return plan.getPlanString();
    } catch (final LensAPIException e) {
      BriefError briefError = new BriefError(e.getLensAPIErrorCode(), e.getLensAPIErrorMessage());
      cliOutput =
          new IdBriefErrorTemplate(
              IdBriefErrorTemplateKey.REQUEST_ID, e.getLensAPIRequestId(), briefError);
    } catch (final LensBriefErrorException e) {
      cliOutput = e.getIdBriefErrorTemplate();
    }
    return cliOutput.toPrettyString();
  }