/** * 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(); }
/** * Execute prepared query. * * @param phandle the phandle * @param async the asynch * @param queryName the query name * @return the string */ @CliCommand( value = "prepQuery execute", help = "Execute prepared query with handle <prepare_handle>." + " If <async> is supplied and is true, query is run in async manner and query handle is returned immediately." + " Optionally, <query-name> can be provided, though not required.") public String executePreparedQuery( @CliOption( key = {"", "prepare_handle"}, mandatory = true, help = "Prepare handle to execute") String phandle, @CliOption( key = {"async"}, mandatory = false, unspecifiedDefaultValue = "false", specifiedDefaultValue = "true", help = "<async>") boolean async, @CliOption( key = {"name"}, mandatory = false, help = "<query-name>") String queryName) { if (async) { QueryHandle handle = getClient().executePrepared(QueryPrepareHandle.fromString(phandle), queryName); return handle.getHandleId().toString(); } else { try { LensClient.LensClientResultSetWithStats result = getClient().getResultsFromPrepared(QueryPrepareHandle.fromString(phandle), queryName); return formatResultSet(result); } catch (Throwable t) { return t.getMessage(); } } }
/** * Gets the status. * * @param qh the qh * @return the status */ @CliCommand( value = "query status", help = "Fetch status of executed query having query handle <query_handle>. " + DEFAULT_QUERY_HANDLE_DESCRIPTION) public String getStatus( @CliOption( key = {"", "query_handle"}, mandatory = false, help = "<query_handle>") String qh) { qh = getOrDefaultQueryHandleString(qh); QueryHandle handle = QueryHandle.fromString(qh); QueryStatus status = getClient().getQueryStatus(handle); if (status == null) { return "Unable to find status for " + handle; } return "Query Handle: " + qh + "\n" + status.toString(); }