Search in sources :

Example 1 with JcrQueryResult

use of com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult in project kylo by Teradata.

the class DebugController method queryJcr.

/**
 * Prints the nodes of the JCR path given, for debugging.
 *
 * @param query the jcr query
 * @return a printout of the JCR tree
 */
@GET
@Path("jcr-sql")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public JcrQueryResult queryJcr(@QueryParam("query") final String query) {
    return metadata.read(() -> {
        this.accessController.checkPermission(AccessController.SERVICES, MetadataAccessControl.ADMIN_METADATA);
        List<List<String>> rows = new ArrayList<>();
        Long startTime = System.currentTimeMillis();
        JcrQueryResult jcrQueryResult = new JcrQueryResult();
        try {
            Session session = JcrMetadataAccess.getActiveSession();
            Workspace workspace = (Workspace) session.getWorkspace();
            String explainPlain = JcrQueryUtil.explainPlain(session, query);
            // start the timer now:
            startTime = System.currentTimeMillis();
            QueryResult result = JcrQueryUtil.query(session, query);
            jcrQueryResult.setExplainPlan(explainPlain);
            RowIterator rowItr = result.getRows();
            List<JcrQueryResultColumn> columns = new ArrayList<>();
            String colsStr = StringUtils.substringAfter(query.toLowerCase(), "select");
            colsStr = StringUtils.substringBefore(colsStr, "from");
            if (StringUtils.isNotBlank(colsStr)) {
                colsStr = colsStr.trim();
                columns = Arrays.asList(colsStr.split(",")).stream().map(c -> {
                    String columnName = c;
                    if (c.contains("as ")) {
                        columnName = StringUtils.substringAfter(c, "as ");
                    } else if (c.contains(" ")) {
                        columnName = StringUtils.substringAfter(c, " ");
                    }
                    return new JcrQueryResultColumn(columnName);
                }).collect(Collectors.toList());
            }
            jcrQueryResult.setColumns(columns);
            while (rowItr.hasNext()) {
                Row row = rowItr.nextRow();
                Value[] rowValues = row.getValues();
                if (rowValues != null) {
                    if (rowValues.length != columns.size()) {
                        columns = IntStream.range(0, rowValues.length).mapToObj(i -> new JcrQueryResultColumn("Column " + i)).collect(Collectors.toList());
                        jcrQueryResult.setColumns(columns);
                    }
                    JcrQueryResultRow jcrQueryResultRow = new JcrQueryResultRow();
                    jcrQueryResult.addRow(jcrQueryResultRow);
                    List<JcrQueryResultColumnValue> jcrQueryResultColumnValues = Arrays.asList(rowValues).stream().map(v -> {
                        try {
                            String value = v.getString();
                            return new JcrQueryResultColumnValue(value);
                        } catch (Exception e) {
                            return new JcrQueryResultColumnValue("ERROR: " + e.getMessage());
                        }
                    }).collect(Collectors.toList());
                    jcrQueryResultRow.setColumnValues(jcrQueryResultColumnValues);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        long totalTime = System.currentTimeMillis() - startTime;
        jcrQueryResult.setQueryTime(totalTime);
        return jcrQueryResult;
    });
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) JcrQueryResultRow(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultRow) Metric(com.thinkbiganalytics.metadata.sla.api.Metric) JcrTools(org.modeshape.jcr.api.JcrTools) JcrQueryResult(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult) Path(javax.ws.rs.Path) QueryResult(javax.jcr.query.QueryResult) FeedPrecondition(com.thinkbiganalytics.metadata.rest.model.feed.FeedPrecondition) Property(javax.jcr.Property) StringUtils(org.apache.commons.lang3.StringUtils) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) JcrQueryResultColumn(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumn) QueryParam(javax.ws.rs.QueryParam) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) JcrQueryUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrQueryUtil) IndexDefinition(org.modeshape.jcr.api.index.IndexDefinition) Consumes(javax.ws.rs.Consumes) WithinSchedule(com.thinkbiganalytics.metadata.api.sla.WithinSchedule) JcrQueryResultColumnValue(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumnValue) Map(java.util.Map) AccessController(com.thinkbiganalytics.security.AccessController) DefaultValue(javax.ws.rs.DefaultValue) ModeshapeIndexUtil(com.thinkbiganalytics.metadata.modeshape.support.ModeshapeIndexUtil) Collector(java.util.stream.Collector) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) ParseException(java.text.ParseException) DELETE(javax.ws.rs.DELETE) PrintWriter(java.io.PrintWriter) JcrPropertyUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrPropertyUtil) Context(javax.ws.rs.core.Context) Session(javax.jcr.Session) MetadataAccessControl(com.thinkbiganalytics.metadata.api.security.MetadataAccessControl) Set(java.util.Set) PropertyType(javax.jcr.PropertyType) MetadataEventService(com.thinkbiganalytics.metadata.api.event.MetadataEventService) Collectors(java.util.stream.Collectors) JcrIndexDefinition(com.thinkbiganalytics.metadata.rest.model.jcr.JcrIndexDefinition) List(java.util.List) Row(javax.jcr.query.Row) UriInfo(javax.ws.rs.core.UriInfo) JcrMetadataAccess(com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess) IntStream(java.util.stream.IntStream) PathParam(javax.ws.rs.PathParam) FeedOperationStatusEvent(com.thinkbiganalytics.metadata.api.event.feed.FeedOperationStatusEvent) GET(javax.ws.rs.GET) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Strings(com.google.common.base.Strings) PathNotFoundException(javax.jcr.PathNotFoundException) RestResponseStatus(com.thinkbiganalytics.rest.model.RestResponseStatus) RepositoryException(javax.jcr.RepositoryException) RowIterator(javax.jcr.query.RowIterator) Node(javax.jcr.Node) FeedExecutedSinceFeed(com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceFeed) Api(io.swagger.annotations.Api) Tag(io.swagger.annotations.Tag) OperationStatus(com.thinkbiganalytics.metadata.api.event.feed.OperationStatus) FeedOperation(com.thinkbiganalytics.metadata.api.op.FeedOperation) FeedExecutedSinceSchedule(com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceSchedule) POST(javax.ws.rs.POST) Datasource(com.thinkbiganalytics.metadata.rest.model.data.Datasource) StringWriter(java.io.StringWriter) HiveTableDatasource(com.thinkbiganalytics.metadata.rest.model.data.HiveTableDatasource) Component(org.springframework.stereotype.Component) Value(javax.jcr.Value) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition) ApiResponse(io.swagger.annotations.ApiResponse) Workspace(org.modeshape.jcr.api.Workspace) Collections(java.util.Collections) JcrQueryResultRow(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultRow) ArrayList(java.util.ArrayList) JcrQueryResultColumn(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumn) ParseException(java.text.ParseException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) JcrQueryResult(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult) QueryResult(javax.jcr.query.QueryResult) JcrQueryResult(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResult) RowIterator(javax.jcr.query.RowIterator) JcrQueryResultColumnValue(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumnValue) DefaultValue(javax.ws.rs.DefaultValue) Value(javax.jcr.Value) JcrQueryResultColumnValue(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultColumnValue) List(java.util.List) ArrayList(java.util.ArrayList) JcrQueryResultRow(com.thinkbiganalytics.metadata.rest.model.jcr.JcrQueryResultRow) Row(javax.jcr.query.Row) Session(javax.jcr.Session) Workspace(org.modeshape.jcr.api.Workspace) Path(javax.ws.rs.Path) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

Strings (com.google.common.base.Strings)1 MetadataAccess (com.thinkbiganalytics.metadata.api.MetadataAccess)1 MetadataEventService (com.thinkbiganalytics.metadata.api.event.MetadataEventService)1 FeedOperationStatusEvent (com.thinkbiganalytics.metadata.api.event.feed.FeedOperationStatusEvent)1 OperationStatus (com.thinkbiganalytics.metadata.api.event.feed.OperationStatus)1 FeedOperation (com.thinkbiganalytics.metadata.api.op.FeedOperation)1 MetadataAccessControl (com.thinkbiganalytics.metadata.api.security.MetadataAccessControl)1 FeedExecutedSinceFeed (com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceFeed)1 FeedExecutedSinceSchedule (com.thinkbiganalytics.metadata.api.sla.FeedExecutedSinceSchedule)1 WithinSchedule (com.thinkbiganalytics.metadata.api.sla.WithinSchedule)1 JcrMetadataAccess (com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess)1 JcrPath (com.thinkbiganalytics.metadata.modeshape.support.JcrPath)1 JcrPropertyUtil (com.thinkbiganalytics.metadata.modeshape.support.JcrPropertyUtil)1 JcrQueryUtil (com.thinkbiganalytics.metadata.modeshape.support.JcrQueryUtil)1 JcrTool (com.thinkbiganalytics.metadata.modeshape.support.JcrTool)1 ModeshapeIndexUtil (com.thinkbiganalytics.metadata.modeshape.support.ModeshapeIndexUtil)1 Datasource (com.thinkbiganalytics.metadata.rest.model.data.Datasource)1 HiveTableDatasource (com.thinkbiganalytics.metadata.rest.model.data.HiveTableDatasource)1 FeedPrecondition (com.thinkbiganalytics.metadata.rest.model.feed.FeedPrecondition)1 JcrIndexDefinition (com.thinkbiganalytics.metadata.rest.model.jcr.JcrIndexDefinition)1