MergeComponentData.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package com.uas.search.console.util;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.HashSet;
  8. import java.util.Set;
  9. import org.springframework.util.StringUtils;
  10. import com.alibaba.fastjson.JSONObject;
  11. import com.uas.search.console.core.util.FastjsonUtils;
  12. import com.uas.search.console.model.ComponentSimpleInfo;
  13. import com.uas.search.console.model.PropertyValue;
  14. /**
  15. * 用于对从数据库导出的器件和属性值两个文本文件进行处理,将每个器件所拥有的属性值提取出来,之后每个器件作为一行,以json的形式写入多个文件中
  16. *
  17. * @author sunyj
  18. * @since 2016年8月8日 下午9:00:31
  19. */
  20. public class MergeComponentData {
  21. // 单个文件存储的最大数据数目
  22. public static final int SIMGLE_FILE_MAX_SIZE = 100000;
  23. // 器件和属性值两个文本文件的路径
  24. private static final String DATA_DIR = "C:\\Users\\sunyj-pc\\Desktop\\data";
  25. /**
  26. * 将从本地文件读取的一行字符串数据解析为器件对象
  27. *
  28. * @param data
  29. * 格式为"CMP_ID" "CMP_BRID" "CMP_CODE" "CMP_KIID",如1 149
  30. * "10120-5212PC" 295
  31. * @return
  32. */
  33. private static ComponentSimpleInfo parseComponent(String data) {
  34. ComponentSimpleInfo component = new ComponentSimpleInfo();
  35. String[] strs = data.split("\t");
  36. component.setId(Long.parseLong(strs[0]));
  37. component.setBrandid(Long.parseLong(strs[1]));
  38. component.setCode(strs[2].substring(1, strs[2].length() - 1));
  39. component.setKindid(Long.parseLong(strs[3]));
  40. return component;
  41. }
  42. /**
  43. * 将从本地文件读取的一行字符串数据解析为属性值对象
  44. *
  45. * @param data
  46. * 格式为"PV_COMPONENTID" "PV_PROPERTYID" "PV_VALUE",如1 394
  47. * "Copper Alloy"
  48. * @return
  49. */
  50. private static PropertyValue parsePropertyValue(String data) {
  51. PropertyValue propertyValue = new PropertyValue();
  52. String[] strs = data.split("\t");
  53. propertyValue.setComponentid(Long.parseLong(strs[0]));
  54. propertyValue.setPropertyid(Long.parseLong(strs[1]));
  55. propertyValue.setValue(strs[2].substring(1, strs[2].length() - 1));
  56. return propertyValue;
  57. }
  58. public static void merge() {
  59. BufferedReader componentReader = null;
  60. BufferedReader propertyValueReader = null;
  61. PrintWriter printWriter = null;
  62. // 分成多个文件存储
  63. int fileIndex = 1;
  64. try {
  65. componentReader = new BufferedReader(new FileReader(DATA_DIR + "\\Component.txt"));
  66. propertyValueReader = new BufferedReader(new FileReader(DATA_DIR + "\\PropertyValue.txt"));
  67. printWriter = new PrintWriter(DATA_DIR + "\\merged\\ComponentWithProperty_" + fileIndex + ".txt");
  68. // 第一行内容为列名,不做解析
  69. componentReader.readLine();
  70. propertyValueReader.readLine();
  71. String componentLine = null;
  72. String propertyValueLine = null;
  73. PropertyValue propertyValue = null;
  74. int dataCount = 0;
  75. while (!StringUtils.isEmpty(componentLine = componentReader.readLine())) {
  76. dataCount++;
  77. System.out.println(dataCount);
  78. // 一个文件存放100000条数据,一旦超过,写入新的文件
  79. if (dataCount > SIMGLE_FILE_MAX_SIZE) {
  80. System.out.println("--------------------------------new file");
  81. dataCount = 1;
  82. printWriter.flush();
  83. printWriter.close();
  84. fileIndex++;
  85. printWriter = new PrintWriter(DATA_DIR + "\\merged\\ComponentWithProperty_" + fileIndex + ".txt");
  86. }
  87. ComponentSimpleInfo component = parseComponent(componentLine);
  88. Set<PropertyValue> properties = new HashSet<>();
  89. if (propertyValue == null && !StringUtils.isEmpty(propertyValueLine = propertyValueReader.readLine())) {
  90. propertyValue = parsePropertyValue(propertyValueLine);
  91. }
  92. while (propertyValue.getComponentid().equals(component.getId())) {
  93. propertyValue.setComponentid(null);
  94. properties.add(propertyValue);
  95. // 读到文件最后一行
  96. if (StringUtils.isEmpty(propertyValueLine = propertyValueReader.readLine())) {
  97. break;
  98. }
  99. propertyValue = parsePropertyValue(propertyValueLine);
  100. }
  101. component.setProperties(properties);
  102. System.out.println(component);
  103. printWriter.println(FastjsonUtils.toJson(component));
  104. }
  105. printWriter.flush();
  106. } catch (FileNotFoundException e) {
  107. System.out.println("File not found!");
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. } finally {
  111. try {
  112. componentReader.close();
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. try {
  117. propertyValueReader.close();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121. printWriter.close();
  122. }
  123. }
  124. private static void readData() {
  125. BufferedReader br = null;
  126. try {
  127. br = new BufferedReader(new FileReader(DATA_DIR + "\\merged\\ComponentWithProperty_1.txt"));
  128. String line = br.readLine();
  129. System.out.println(JSONObject.parseObject(line, ComponentSimpleInfo.class));
  130. } catch (FileNotFoundException e) {
  131. System.out.println("File not found!");
  132. } catch (IOException e) {
  133. e.printStackTrace();
  134. } finally {
  135. try {
  136. br.close();
  137. } catch (IOException e) {
  138. e.printStackTrace();
  139. }
  140. }
  141. }
  142. public static void main(String[] args) {
  143. merge();
  144. // readData();
  145. }
  146. }