src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/web/NamenodeAnalyticsMethods.java
@POST
  @Path("/sql")
  public Response sql(MultivaluedMap<String, String> formData) {
    try {
      final NameNodeLoader nameNodeLoader = (NameNodeLoader) context.getAttribute(NNA_NN_LOADER);
      before();

      if (!nameNodeLoader.isInit()) {
        return Response.ok()
            .entity(new StringEntity("not_loaded"))
            .type(MediaType.TEXT_PLAIN_TYPE)
            .build();
      }

      String sqlStatement = request.getParameter("sqlStatement");
      if (Strings.isNullOrEmpty(sqlStatement)) {
        sqlStatement = formData.getFirst("sqlStatement");
      }

      SqlParser sqlParser = new SqlParser();

      if (sqlStatement.toUpperCase().contains("SHOW TABLES")) {
        String jsonShowTables = sqlParser.showTables();
        return Response.ok(jsonShowTables, MediaType.APPLICATION_JSON).build();
      }
      if (sqlStatement.contains("DESCRIBE")) {
        String jsonDescription = sqlParser.describeInJson(sqlStatement);
        return Response.ok(jsonDescription, MediaType.APPLICATION_JSON).build();
      }

      sqlParser.parse(sqlStatement);
      boolean isHistogram = !Strings.isNullOrEmpty(sqlParser.getType());
      boolean hasSumOrFind =
          !Strings.isNullOrEmpty(sqlParser.getSum()) || !Strings.isNullOrEmpty(sqlParser.getFind());

      String set = sqlParser.getINodeSet();
      String[] filters = Helper.parseFilters(sqlParser.getFilters());
      String[] filterOps = Helper.parseFilterOps(sqlParser.getFilters());
      String type = sqlParser.getType();
      String sum = sqlParser.getSum();
      String find = sqlParser.getFind();
      Integer limit = sqlParser.getLimit();
      int parentDirDepth = sqlParser.getParentDirDepth();
      String timeRange = sqlParser.getTimeRange();
      Boolean sortAscending = sqlParser.getSortAscending();
      Boolean sortDescending = sqlParser.getSortDescending();
      Integer top = (limit != null && sortDescending != null && sortDescending) ? limit : null;
      Integer bottom = (limit != null && sortAscending != null && sortAscending) ? limit : null;

      if (isHistogram) {
        QueryChecker.isValidQuery(set, filters, type, sum, filterOps, find);
        Stream<INode> filteredINodes = Helper.setFilters(nameNodeLoader, set, filters, filterOps);
        HistogramInvoker histogramInvoker =
            new HistogramInvoker(
                    nameNodeLoader.getQueryEngine(),
                    type,
                    sum,
                    parentDirDepth,
                    timeRange,
                    find,
                    filteredINodes,
                    null,
                    null,
                    top,
                    bottom,
                    sortAscending,
                    sortDescending)
                .invoke();
        Map<String, Long> histogram = histogramInvoker.getHistogram();
        String histogramJson = Histograms.toJson(histogram);
        return Response.ok(histogramJson, MediaType.APPLICATION_JSON).build();
      } else {
        Collection<INode> filteredINodes =
            Helper.performFilters(nameNodeLoader, set, filters, filterOps, find);
        if (hasSumOrFind) {
          long sumValue = nameNodeLoader.getQueryEngine().sum(filteredINodes, sum);
          return Response.ok(sumValue, MediaType.TEXT_PLAIN).build();
        } else {
          nameNodeLoader.getQueryEngine().dumpINodePaths(filteredINodes, limit, response);
          return Response.ok().type(MediaType.TEXT_PLAIN).build();
        }
      }
    } catch (RuntimeException rtex) {
      return handleException(rtex);
    } catch (Exception ex) {
      return handleException(ex);
    } finally {
      after();
    }
  }
/**
   * SQL is a POST only call that only READER users can access. It takes a form document as an
   * argument that specifies an SQL query to perform on NNA.
   */
SQL is a POST only call that only READER users can access. It takes a form document as an
argument that specifies an SQL query to perform on NNA.
SQL is a POST only call that only READER users can access . It takes a form document as an argument that specifies an SQL query to perform on NNA .
@POST
  @Path("/sql")
  public Response sql(MultivaluedMap<String, String> formData) {
    try {
      final NameNodeLoader nameNodeLoader = (NameNodeLoader) context.getAttribute(NNA_NN_LOADER);
      before();
      if (!nameNodeLoader.isInit()) {
        return Response.ok()
            .entity(new StringEntity("not_loaded"))
            .type(MediaType.TEXT_PLAIN_TYPE)
            .build();
      }
      String sqlStatement = request.getParameter("sqlStatement");
      if (Strings.isNullOrEmpty(sqlStatement)) {
        sqlStatement = formData.getFirst("sqlStatement");
      }
      SqlParser sqlParser = new SqlParser();
      if (sqlStatement.toUpperCase().contains("SHOW TABLES")) {
        String jsonShowTables = sqlParser.showTables();
        return Response.ok(jsonShowTables, MediaType.APPLICATION_JSON).build();
      }
      if (sqlStatement.contains("DESCRIBE")) {
        String jsonDescription = sqlParser.describeInJson(sqlStatement);
        return Response.ok(jsonDescription, MediaType.APPLICATION_JSON).build();
      }
      sqlParser.parse(sqlStatement);
      boolean isHistogram = !Strings.isNullOrEmpty(sqlParser.getType());
      boolean hasSumOrFind =
          !Strings.isNullOrEmpty(sqlParser.getSum()) || !Strings.isNullOrEmpty(sqlParser.getFind());
      String set = sqlParser.getINodeSet();
      String[] filters = Helper.parseFilters(sqlParser.getFilters());
      String[] filterOps = Helper.parseFilterOps(sqlParser.getFilters());
      String type = sqlParser.getType();
      String sum = sqlParser.getSum();
      String find = sqlParser.getFind();
      Integer limit = sqlParser.getLimit();
      int parentDirDepth = sqlParser.getParentDirDepth();
      String timeRange = sqlParser.getTimeRange();
      Boolean sortAscending = sqlParser.getSortAscending();
      Boolean sortDescending = sqlParser.getSortDescending();
      Integer top = (limit != null && sortDescending != null && sortDescending) ? limit : null;
      Integer bottom = (limit != null && sortAscending != null && sortAscending) ? limit : null;
      if (isHistogram) {
        QueryChecker.isValidQuery(set, filters, type, sum, filterOps, find);
        Stream<INode> filteredINodes = Helper.setFilters(nameNodeLoader, set, filters, filterOps);
        HistogramInvoker histogramInvoker =
            new HistogramInvoker(
                    nameNodeLoader.getQueryEngine(),
                    type,
                    sum,
                    parentDirDepth,
                    timeRange,
                    find,
                    filteredINodes,
                    null,
                    null,
                    top,
                    bottom,
                    sortAscending,
                    sortDescending)
                .invoke();
        Map<String, Long> histogram = histogramInvoker.getHistogram();
        String histogramJson = Histograms.toJson(histogram);
        return Response.ok(histogramJson, MediaType.APPLICATION_JSON).build();
      } else {
        Collection<INode> filteredINodes =
            Helper.performFilters(nameNodeLoader, set, filters, filterOps, find);
        if (hasSumOrFind) {
          long sumValue = nameNodeLoader.getQueryEngine().sum(filteredINodes, sum);
          return Response.ok(sumValue, MediaType.TEXT_PLAIN).build();
        } else {
          nameNodeLoader.getQueryEngine().dumpINodePaths(filteredINodes, limit, response);
          return Response.ok().type(MediaType.TEXT_PLAIN).build();
        }
      }
    } catch (RuntimeException rtex) {
      return handleException(rtex);
    } catch (Exception ex) {
      return handleException(ex);
    } finally {
      after();
    }
  }
@ POST @ Path ( "/sql" ) public Response sql ( MultivaluedMap < String , String > formData ) { try { final NameNodeLoader nameNodeLoader = ( NameNodeLoader ) context . getAttribute ( NNA_NN_LOADER ) ; before ( ) ; if ( ! nameNodeLoader . isInit ( ) ) { return Response . ok ( ) . entity ( new StringEntity ( "not_loaded" ) ) . type ( MediaType . TEXT_PLAIN_TYPE ) . build ( ) ; } String sqlStatement = request . getParameter ( "sqlStatement" ) ; if ( Strings . isNullOrEmpty ( sqlStatement ) ) { sqlStatement = formData . getFirst ( "sqlStatement" ) ; } SqlParser sqlParser = new SqlParser ( ) ; if ( sqlStatement . toUpperCase ( ) . contains ( "SHOW TABLES" ) ) { String jsonShowTables = sqlParser . showTables ( ) ; return Response . ok ( jsonShowTables , MediaType . APPLICATION_JSON ) . build ( ) ; } if ( sqlStatement . contains ( "DESCRIBE" ) ) { String jsonDescription = sqlParser . describeInJson ( sqlStatement ) ; return Response . ok ( jsonDescription , MediaType . APPLICATION_JSON ) . build ( ) ; } sqlParser . parse ( sqlStatement ) ; boolean isHistogram = ! Strings . isNullOrEmpty ( sqlParser . getType ( ) ) ; boolean hasSumOrFind = ! Strings . isNullOrEmpty ( sqlParser . getSum ( ) ) || ! Strings . isNullOrEmpty ( sqlParser . getFind ( ) ) ; String set = sqlParser . getINodeSet ( ) ; String [ ] filters = Helper . parseFilters ( sqlParser . getFilters ( ) ) ; String [ ] filterOps = Helper . parseFilterOps ( sqlParser . getFilters ( ) ) ; String type = sqlParser . getType ( ) ; String sum = sqlParser . getSum ( ) ; String find = sqlParser . getFind ( ) ; Integer limit = sqlParser . getLimit ( ) ; int parentDirDepth = sqlParser . getParentDirDepth ( ) ; String timeRange = sqlParser . getTimeRange ( ) ; Boolean sortAscending = sqlParser . getSortAscending ( ) ; Boolean sortDescending = sqlParser . getSortDescending ( ) ; Integer top = ( limit != null && sortDescending != null && sortDescending ) ? limit : null ; Integer bottom = ( limit != null && sortAscending != null && sortAscending ) ? limit : null ; if ( isHistogram ) { QueryChecker . isValidQuery ( set , filters , type , sum , filterOps , find ) ; Stream < INode > filteredINodes = Helper . setFilters ( nameNodeLoader , set , filters , filterOps ) ; HistogramInvoker histogramInvoker = new HistogramInvoker ( nameNodeLoader . getQueryEngine ( ) , type , sum , parentDirDepth , timeRange , find , filteredINodes , null , null , top , bottom , sortAscending , sortDescending ) . invoke ( ) ; Map < String , Long > histogram = histogramInvoker . getHistogram ( ) ; String histogramJson = Histograms . toJson ( histogram ) ; return Response . ok ( histogramJson , MediaType . APPLICATION_JSON ) . build ( ) ; } else { Collection < INode > filteredINodes = Helper . performFilters ( nameNodeLoader , set , filters , filterOps , find ) ; if ( hasSumOrFind ) { long sumValue = nameNodeLoader . getQueryEngine ( ) . sum ( filteredINodes , sum ) ; return Response . ok ( sumValue , MediaType . TEXT_PLAIN ) . build ( ) ; } else { nameNodeLoader . getQueryEngine ( ) . dumpINodePaths ( filteredINodes , limit , response ) ; return Response . ok ( ) . type ( MediaType . TEXT_PLAIN ) . build ( ) ; } } } catch ( RuntimeException rtex ) { return handleException ( rtex ) ; } catch ( Exception ex ) { return handleException ( ex ) ; } finally { after ( ) ; } }
SQL is a POST only call that only READER users can access.
SQL is a POST only call that only READER users can access .