原文链接: http://mon.annotation.YarnResource)") public void yarnResourcdPointCut(){ } /** * 检查yarn的资源是否可用 */ @Before("yarnResourcdPointCut()") public void before(){ log.info("************************************检查yarn的资源是否可用*******************************"); // yarn资源紧张 if(!YarnClient.yarnResourceOk()){ throw new InvalidStatusException(); } }}
获取yarn的资源使用数据:
@Slf4jpublic class YarnClient { /** * yarn资源不能超过多少 */ private static final int YARN_RESOURCE = 55; /** * * @return true : 表示资源正常, false: 资源紧张 */ public static boolean yarnResourceOk() { try { URL url = new URL("http://master:8088/cluster/scheduler"); HttpURLConnection conn = null; conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setUseCaches(false); // 请求超时5秒 conn.setConnectTimeout(5000); // 设置HTTP头: conn.setRequestProperty("Accept", "*/*"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"); // 连接并发送HTTP请求: conn.connect(); // 判断HTTP响应是否200: if (conn.getResponseCode() != 200) { throw new RuntimeException("bad response"); } // 获取所有响应Header: Map<String, List<String>> map = conn.getHeaderFields(); for (String key : map.keySet()) { System.out.println(key + ": " + map.get(key)); } // 获取响应内容: InputStream input = conn.getInputStream(); byte[] datas = null; try { // 从输入流中读取数据 datas = readInputStream(input); } catch (Exception e) { e.printStackTrace(); } String result = new String(datas, "UTF-8");// 将二进制流转为String Document document = Jsoup.parse(result); Elements elements = document.getElementsByClass("qstats"); String[] ratios = elements.text().split("used"); return Double.valueOf(ratios[3].replace("%", "")) < YARN_RESOURCE; } catch (IOException e) { log.error("yarn资源获取失败"); } return false; } private static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; }}在controller上通过注解@YarnResource标识:
@Controller@RequestMapping("/hero/hive")public class HiveController { /** * html 文件地址前缀 */ private String prefix = "hero"; @Autowired IUserService iUserService; @RequestMapping("") @RequiresPermissions("hero:hive:view") public String heroHive(){ return prefix + "/hive"; } @YarnResource @RequestMapping("/user") @RequiresPermissions("hero:hive:user") @ResponseBody public TableDataInfo user(UserModel userModel){ DateCheckUtils.checkInputDate(userModel); PageInfo pageInfo = iUserService.queryUser(userModel); TableDataInfo tableDataInfo = new TableDataInfo(); tableDataInfo.setTotal(pageInfo.getTotal()); tableDataInfo.setRows(pageInfo.getList()); return tableDataInfo; }}2.2 查询数据跨度不超过60天检查
这样每次请求进入controller的时候就会自动检查查询的日期是否超过60天了,防止载入数据过多,引发其它任务资源不够。
这里访问hive肯定需要切换数据源的,因为其它页面还有对mysql的数据访问,需要注意一下。
目前功能看起来很简单,没有用到什么高大上的东西,后面慢慢完善。
以上就是SpringBoot连接Hive实现自助取数的示例的详细内容,更多关于SpringBoot连接Hive的资料请关注其它相关文章!