/***
* 分析引擎查询
* @param dbSourcePageQueryReqDTO
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public List<Map<String, Object>> pageQuery(DbSourcePageQueryReqDTO dbSourcePageQueryReqDTO) throws SQLException, ClassNotFoundException {
String sql = dbSourcePageQueryReqDTO.getSql();
String dataBase = dbSourcePageQueryReqDTO.getDataBase();
Integer pageSize = dbSourcePageQueryReqDTO.getPageSize();
List<SQLStatement> sqlStatements = SQLUtils.parseStatements(sql, JdbcConstants.HIVE, true);
//暂时支持一条SQL
SQLStatement sqlStatement = sqlStatements.get(0);
//SchemaVisitor
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
sqlStatement.accept(visitor);
//只支持查询操作
if (!(sqlStatement instanceof SQLSelectStatement)) {
throw new ServiceException("error", "只支持查询语句!!");
}
// 添加库名
Map<TableStat.Name, TableStat> tables = visitor.getTables();
for (Map.Entry<TableStat.Name, TableStat> entry : tables.entrySet()) {
String tableName = entry.getKey().getName();
if (tableName.contains(".")) {
continue;
}
String expr = "\b\w*\.*" + tableName + "\b";
Pattern pattern = Pattern.compile(expr, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sql);
sql = matcher.replaceAll("`" + dataBase + "." + tableName + "`");
}
if (null == pageSize || pageSize == 0) {
pageSize = 2000;
}
sql = sql.replace(";", "");
sql = "select * from (" + sql + ")mpbaseanalyze limit " + pageSize;
log.info("分析引擎:QUERY SQL:" + sql);
String jdbc = MessageFormat.format("jdbc:hive2://{0}:{1}?mapreduce.job.queuename={2};hive.resultset.use.unique.column.names=false",
baseProperties.getHiveIp(), baseProperties.getHivePort(), baseProperties.getHiveMapreduceJobQueuename());
String user = baseProperties.getHiveUser();
String password = baseProperties.getHivePassword();
Hive2DataSource hive2DataSource = new Hive2DataSource(user, password, jdbc);
return hive2DataSource.executeQuery(MapListHandler.create(), sql);
}
最后
以上就是英俊冰棍最近收集整理的关于hive sql查询的全部内容,更多相关hive内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复