故障兜底方案

一、文件处理

    public static void readCsv() throws ParseException {
        public static final String file_path = "/Users/mac/Downloads/645292114-1689663974599.csv";
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        String line = "";
        try (BufferedReader br = new BufferedReader(new FileReader(file_path))) {
            while ((line = br.readLine()) != null) {
                //line todo something
                //辅助工具:正则匹配、分隔字符串等
                // Pattern patternIdMath2 = Pattern.compile("some\"\":\\d+");
                // group.split(":")[1]
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }


    public static void readHttp() throws ParseException {
        String token = ""; //token中不包含空格或换行符。日志中心前端提供的token可能因为bug包含空格或换行符
        RestClientBuilder builder = RestClient.builder(new HttpHost("es.data.xxx.com", 80, "http"));
        Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "Token "+ token)};
        builder.setDefaultHeaders(defaultHeaders);
        RestHighLevelClient client = new RestHighLevelClient(builder.build());

        String index_name = "log.com.xxx_all";
        // 时间戳根据实际情况配置
        QueryBuilder timeRanges = new RangeQueryBuilder("es_timestamp").from("2023/06/13 16:28:30 +0800").to("2023/06/13 16:29:00 +0800");
        QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery("metaMap AND id");

        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(timeRanges);
        boolQueryBuilder.must(queryStringQueryBuilder);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder);
        // size最大不超过10000
        searchSourceBuilder.size(5000);

        Scroll scroll = new Scroll(TimeValue.timeValueMinutes(1L));


        SearchRequest searchRequest = new SearchRequest(index_name);
        searchRequest.source(searchSourceBuilder);
        searchRequest.scroll(scroll);
        // 发送查询请求
        SearchResponse searchResponse = client.search(searchRequest);

        // 根据实际使用情况修改
        String outPath = "/Users/mac/Desktop/xxx.log";


        try (FileWriter writer = new FileWriter(outPath)){
            do {
                for(SearchHit hit:searchResponse.getHits().getHits()) {
                    writer.write(hit.getSourceAsMap().get("message").toString());
                }
                writer.flush();
                // 单次结果超出size限制时,使用scroll继续查询直到获取所有符合条件结果
                searchResponse = client.searchScroll(new SearchScrollRequest(searchResponse.getScrollId()).scroll(scroll));
            } while(searchResponse.getHits().getHits().length!=0);
        }
    }


    public static void anaCsv() throws ParseException {
        public static final String file_path = "/Users/xxxxx/Downloads/314217302.csv";

        private static Logger log = LoggerFactory.getLogger(SCAppkeyDepartMentInfo.class);


        public static void anCsv() {

            String line = "";

            List list = Lists.newArrayList();
            try (BufferedReader br = new BufferedReader(new FileReader(file_path))) {
                while ((line = br.readLine()) != null) {
                    String lines[] = line.split("\"");
                    list.add(Long.valueOf(lines[1]));
                }
            } catch (Exception e) {
                log.error("解析csv异常", e);
            }

            for (List list2 : Lists.partition(list, 1500)) {
                System.out.println(String.format("update xxxx set utime = utime +1 where id in (%s)", StringUtils.join(list2, ",")));
            }
        }
    }

二、binlog分析

https://blog.csdn.net/doujiayun3939/article/details/106918946
https://blog.csdn.net/vhomes/article/details/8082734
https://blog.csdn.net/weixin_46866730/article/details/125676947

你可能感兴趣的:(架构)