Package com.dianping.cosmos.hive.server.queryengine

Examples of com.dianping.cosmos.hive.server.queryengine.HiveQueryOutput


  @Override
  public HiveQueryOutputBo getQueryResult(HiveQueryInputBo input) {
    if (input == null)
      return null;
    HiveQueryOutput result = queryEngine.getQueryResult(new HiveQueryInput(
        input));
   
    // insert query history DB
    String resultLocation = "";
    if (result.getStoreFileLocation() != null){
      resultLocation = result.getStoreFileLocation();
    }
   
    QueryHistory history = new QueryHistory();
    history.setHql(input.getHql());
    history.setUsername(input.getUsername());
    history.setAddtime(new Date(input.getTimestamp()));
    history.setFilename(resultLocation);
    queryHistoryService.insertQueryHistory(history);
   
    HiveQueryOutputBo bo = result.toHiveQueryOutputBo();
    return bo;
  }
View Full Code Here


    }
  }

  public HiveQueryOutput getResult(){
    try {
      HiveQueryOutput result = OutputParser.getInstance().parse(
          BasicUtils.openInputStream(new File(outFileLocation), gzip), limit);
      result.setStoreFileLocation(outFileLocation);
      return result;
    } catch (IOException e) {
      s_logger.error("Exception occurs in parsing output:", e);
      return null;
    }
View Full Code Here

  public static OutputParser getInstance(){
    return s_instance;
  }
 
  public HiveQueryOutput parse(InputStream is, int limit) throws IOException{
    HiveQueryOutput result = new HiveQueryOutput();
    LineIterator it = IOUtils.lineIterator(is, BasicUtils.ENCODING);
    try{
      //the first line is column names
      result.setTitleList(parseOneLine(it.next().toString()));
      int lineNum = 0;
      while(it.hasNext() && lineNum < limit){
        result.addRow(parseOneLine(it.next().toString()));
        lineNum ++;
      }
    }finally{
      it.close();
    }
View Full Code Here

  }

  public HiveQueryOutput getQueryResult(String tokenid, String username,
      String database, String hql, int resultLimit, Boolean isStoreFile,
      long timestamp) {
    HiveQueryOutput hqo = new HiveQueryOutput();
    UserGroupInformation ugi = ugiCache.getIfPresent(tokenid);
    Connection conn = getConnection(ugi);
    Statement stmt;
    try {
      stmt = conn.createStatement();
      stmt.executeQuery("use " + database);
      ResultSet rs = stmt.executeQuery(hql);
      ResultSetMetaData rsm = rs.getMetaData();
      int columnCount = rsm.getColumnCount();
      List<String> columnNames = new ArrayList<String>(columnCount);

      StringBuilder sb = new StringBuilder(200);
      for (int i = 1; i <= columnCount; i++) {
        columnNames.add(rsm.getColumnName(i));
        if (logger.isDebugEnabled()) {
          sb.append(rsm.getColumnName(i)).append("\t")
              .append(rsm.getColumnTypeName(i)).append("\n");
        }
      }

      if (logger.isDebugEnabled()) {
        logger.debug("resultset meta data is:" + sb.toString());
      }

      hqo.setTitleList(columnNames);
      int maxShowRowCount = resultLimit < USER_SHOW_ROW_MAXIMUM_COUNT ? resultLimit
          : USER_SHOW_ROW_MAXIMUM_COUNT;
      int currentRow = 0;

      String storeFilePath = "";
      BufferedWriter bw = null;
      if (isStoreFile) {
        storeFilePath = DataFileStore.getStoreFilePath(tokenid,
            username, database, hql, timestamp);
        bw = DataFileStore.openOutputStream(storeFilePath);
      }
      logger.info("isStoreFile:" + isStoreFile + " storeFilePath:"
          + storeFilePath);

      if (!StringUtils.isBlank(storeFilePath)) {
        hqo.setStoreFileLocation(storeFilePath);
      }

      while (rs.next()
          && currentRow < DataFileStore.FILE_STORE_LINE_LIMIT) {
        if (currentRow < maxShowRowCount) {
          List<String> oneRowData = new ArrayList<String>();
          for (int i = 1; i <= columnCount; i++) {
            String value = rs.getString(i) == null ? "" : rs
                .getString(i);
            oneRowData.add(value);
            if (isStoreFile) {
              bw.write(value);
              if (i < columnCount)
                bw.write('\t');
            }
          }
          hqo.addRow(oneRowData);

          if (isStoreFile) {
            bw.write('\n');
          }
        } else if (isStoreFile) {
          for (int i = 1; i <= columnCount; i++) {
            bw.write(rs.getString(i));
            if (i < columnCount)
              bw.write('\t');
          }
          bw.write('\n');
        } else {
          break;
        }
        currentRow++;
      }
      if (isStoreFile && bw != null) {
        bw.flush();
        IOUtils.closeQuietly(bw);
      }
      rs.close();
      stmt.close();
    } catch (Exception e) {
      hqo.setErrorMessage(e.toString());
      logger.error("getQueryResult failed, db:" + database + " hql:"
          + hql, e);
    } finally {
      try {
        conn.close();
View Full Code Here

TOP

Related Classes of com.dianping.cosmos.hive.server.queryengine.HiveQueryOutput

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.