NginxLogTest.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import cn.reghao.jutil.jdk.serializer.JsonConverter;
  2. import cn.reghao.jutil.jdk.id.SnowFlake;
  3. import cn.reghao.tnb.search.app.SearchApplication;
  4. //import cn.reghao.tnb.search.app.es.DocumentService;
  5. //import cn.reghao.tnb.search.app.es.QueryService;
  6. //import cn.reghao.tnb.search.app.es.SearchService;
  7. import cn.reghao.tnb.search.app.log.model.NginxLog;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.junit.jupiter.api.Test;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.boot.test.context.SpringBootTest;
  12. import org.springframework.test.context.ActiveProfiles;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.IOException;
  16. import java.nio.ByteBuffer;
  17. import java.nio.channels.FileChannel;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * @author reghao
  22. * @date 2025-07-03 15:56:02
  23. */
  24. @Slf4j
  25. @ActiveProfiles("dev")
  26. @SpringBootTest(classes = SearchApplication.class)
  27. public class NginxLogTest {
  28. /*@Autowired
  29. QueryService<NginxLog> queryService;
  30. @Autowired
  31. SearchService searchService;
  32. @Autowired
  33. DocumentService documentService;
  34. static SnowFlake idGenerator = new SnowFlake(1, 1);
  35. static long total = 0L;
  36. public static void readFileFileChannel(File file, String index, DocumentService documentService) {
  37. List<String> lines = new ArrayList<>();
  38. List<NginxLog> nginxLogs = new ArrayList<>();
  39. try {
  40. FileInputStream fis = new FileInputStream(file);
  41. FileChannel fileChannel = fis.getChannel();
  42. // 10MB
  43. int capacity = 10*1024*1024;
  44. ByteBuffer byteBuffer = ByteBuffer.allocate(capacity);
  45. StringBuffer buffer = new StringBuffer();
  46. while(fileChannel.read(byteBuffer) != -1) {
  47. //读取后,将位置置为0,将limit置为容量, 以备下次读入到字节缓冲中,从0开始存储
  48. byteBuffer.clear();
  49. byte[] bytes = byteBuffer.array();
  50. String str = new String(bytes);
  51. buffer.append(str);
  52. String[] strArray = buffer.toString().split(System.lineSeparator());
  53. for (int i = 0; i < strArray.length-1; i++) {
  54. try {
  55. NginxLog nginxLog = JsonConverter.jsonToObject(strArray[i], NginxLog.class);
  56. nginxLogs.add(nginxLog);
  57. } catch (Exception e) {
  58. lines.add(strArray[i]);
  59. //e.printStackTrace();
  60. }
  61. }
  62. String lastLine = strArray[strArray.length-1];
  63. if (!lastLine.endsWith("}")) {
  64. buffer = new StringBuffer();
  65. buffer.append(strArray[strArray.length-1]);
  66. }
  67. while (nginxLogs.size() > 10_000) {
  68. //nginxLogs.forEach(nginxLog -> nginxLog.setId(idGenerator.nextId()+""));
  69. //NginxLog nginxLog = nginxLogs.get(0);
  70. //documentService.update(index, nginxLog);
  71. //documentService.batchAddDocument(index, nginxLogs);
  72. //log.info("save {} nginxLogs", nginxLogs.size());
  73. total += nginxLogs.size();
  74. nginxLogs.clear();
  75. }
  76. }
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. } finally {
  80. // TODO close 处理
  81. }
  82. }
  83. void addNginxLogs(String index) {
  84. String baseDir = "/home/reghao/work/azy/data/konglog";
  85. File dir = new File(baseDir);
  86. for (File file : dir.listFiles()) {
  87. readFileFileChannel(file, index, documentService);
  88. log.info("total -> {}", total);
  89. }
  90. log.info("total -> {}", total);
  91. }
  92. @Test
  93. public void addNginxLogTest() {
  94. String index = "nginx_log";
  95. addNginxLogs(index);
  96. }
  97. @Test
  98. public void nginxLogTest() throws Exception {
  99. String index = "nginx_log";
  100. int ps = 1000;
  101. int pn = 1;
  102. String queryString = "content";
  103. //Page<NginxLog> list = queryService.queryWithHighlight(index, queryString, pn, ps, NginxLog.class);
  104. //searchService.searchAll(index);
  105. searchService.aggregate(index);
  106. System.out.println();
  107. }*/
  108. }