/**
* SQL 联想搜索返回随机的16个
* @param sql
* @return
*/
@Override
public String sqlLenovo(String sql, String db) {
List<Map<String, Object>> result = new ArrayList<>();
MySQLUtil mySQLUtil = new MySQLUtil(
configUtil.readConfig("hive.meta.url"),
configUtil.readConfig("hive.meta.username"),
configUtil.readConfig("hive.meta.password"));
if (sql.endsWith(" ")) {
return returnResultModel.generateResult(Integer.parseInt(configUtil.readConfig("return.success")),
configUtil.readConfig("return.msg.success"),
result);
}
if (sql.trim().equals("") || sql.trim().equals(".")) {
return returnResultModel.generateResult(Integer.parseInt(configUtil.readConfig("return.success")),
configUtil.readConfig("return.msg.success"),
result);
}
String key = sql.trim().split(" ")[sql.trim().split(" ").length - 1];
List<String> keyList = new ArrayList<>(Arrays.asList(key.split("\\.")));
if (key.contains(".") && keyList.size() > 1) {
key = key.split("\\.")[1];
LOG.info(key);
} else if (key.contains(".") && keyList.size() == 1){
key = key.split("\\.")[0];
LOG.info(key);
}
if (!key.isEmpty()) {
if (configUtil.readConfig("data.engine.hive").equals(db)) {
// 匹配关键字
List<String> keyWordTemp = new ArrayList<>(Arrays.asList(configUtil.readConfig("hive.keywords").split(",")));
if (!keyWordTemp.isEmpty()) {
int i = 0;
for (String keyTemp : keyWordTemp) {
if (i == 4) break;
if (keyTemp.startsWith(key.toUpperCase())) {
Map<String, Object> mapKeyWord = Maps.newHashMap();
mapKeyWord.put("key", i);
mapKeyWord.put("name", keyTemp);
mapKeyWord.put("type", "KeyWord");
result.add(mapKeyWord);
i += 1;
}
}
}
// 匹配表名
String mysqlTable = "select TBL_NAME from TBLS where TBL_NAME like \'" + key.toLowerCase() + "%\' limit 4";
List<List<String>> tableData = mySQLUtil.executeQuery(mysqlTable);
if (!tableData.isEmpty()) {
int j = 5;
for (List<String> dataTemp : tableData) {
for (String dat : dataTemp) {
Map<String, Object> mapTable = Maps.newHashMap();
mapTable.put("name", dat);
mapTable.put("key", j);
mapTable.put("type", "Table");
result.add(mapTable);
j += 1;
}
}
}
// 字段
String mysqlCol = "select COLUMN_NAME from COLUMNS_V2 where COLUMN_NAME like \'" + key.toLowerCase() + "%\' limit 4";
List<List<String>> colData = mySQLUtil.executeQuery(mysqlCol);
if (!colData.isEmpty()) {
int count = 10;
for (List<String> dataTemp : colData) {
for (String dat : dataTemp) {
Map<String, Object> mapColumn = Maps.newHashMap();
mapColumn.put("name", dat);
mapColumn.put("key", count);
mapColumn.put("type", "Column");
result.add(mapColumn);
count += 1;
}
}
}
// 匹配聚合函数
List<String> funcTemp = new ArrayList<>(Arrays.asList(configUtil.readConfig("hive.func").split(",")));
if (!funcTemp.isEmpty()) {
int k = 15;
for (String funcT : funcTemp) {
if (k == 19) break;
if (funcT.startsWith(key.toUpperCase())) {
Map<String, Object> mapFunc = Maps.newHashMap();
mapFunc.put("key", k);
mapFunc.put("name", funcT);
mapFunc.put("type", "Func");
result.add(mapFunc);
k += 1;
}
}
}
}
}