| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package com.uas.search.console.util;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.HashSet;
- import java.util.Set;
- import org.springframework.util.StringUtils;
- import com.alibaba.fastjson.JSONObject;
- import com.uas.search.console.core.util.FastjsonUtils;
- import com.uas.search.console.model.ComponentSimpleInfo;
- import com.uas.search.console.model.PropertyValue;
- /**
- * 用于对从数据库导出的器件和属性值两个文本文件进行处理,将每个器件所拥有的属性值提取出来,之后每个器件作为一行,以json的形式写入多个文件中
- *
- * @author sunyj
- * @since 2016年8月8日 下午9:00:31
- */
- public class MergeComponentData {
- // 单个文件存储的最大数据数目
- public static final int SIMGLE_FILE_MAX_SIZE = 100000;
- // 器件和属性值两个文本文件的路径
- private static final String DATA_DIR = "C:\\Users\\sunyj-pc\\Desktop\\data";
- /**
- * 将从本地文件读取的一行字符串数据解析为器件对象
- *
- * @param data
- * 格式为"CMP_ID" "CMP_BRID" "CMP_CODE" "CMP_KIID",如1 149
- * "10120-5212PC" 295
- * @return
- */
- private static ComponentSimpleInfo parseComponent(String data) {
- ComponentSimpleInfo component = new ComponentSimpleInfo();
- String[] strs = data.split("\t");
- component.setId(Long.parseLong(strs[0]));
- component.setBrandid(Long.parseLong(strs[1]));
- component.setCode(strs[2].substring(1, strs[2].length() - 1));
- component.setKindid(Long.parseLong(strs[3]));
- return component;
- }
- /**
- * 将从本地文件读取的一行字符串数据解析为属性值对象
- *
- * @param data
- * 格式为"PV_COMPONENTID" "PV_PROPERTYID" "PV_VALUE",如1 394
- * "Copper Alloy"
- * @return
- */
- private static PropertyValue parsePropertyValue(String data) {
- PropertyValue propertyValue = new PropertyValue();
- String[] strs = data.split("\t");
- propertyValue.setComponentid(Long.parseLong(strs[0]));
- propertyValue.setPropertyid(Long.parseLong(strs[1]));
- propertyValue.setValue(strs[2].substring(1, strs[2].length() - 1));
- return propertyValue;
- }
- public static void merge() {
- BufferedReader componentReader = null;
- BufferedReader propertyValueReader = null;
- PrintWriter printWriter = null;
- // 分成多个文件存储
- int fileIndex = 1;
- try {
- componentReader = new BufferedReader(new FileReader(DATA_DIR + "\\Component.txt"));
- propertyValueReader = new BufferedReader(new FileReader(DATA_DIR + "\\PropertyValue.txt"));
- printWriter = new PrintWriter(DATA_DIR + "\\merged\\ComponentWithProperty_" + fileIndex + ".txt");
- // 第一行内容为列名,不做解析
- componentReader.readLine();
- propertyValueReader.readLine();
- String componentLine = null;
- String propertyValueLine = null;
- PropertyValue propertyValue = null;
- int dataCount = 0;
- while (!StringUtils.isEmpty(componentLine = componentReader.readLine())) {
- dataCount++;
- System.out.println(dataCount);
- // 一个文件存放100000条数据,一旦超过,写入新的文件
- if (dataCount > SIMGLE_FILE_MAX_SIZE) {
- System.out.println("--------------------------------new file");
- dataCount = 1;
- printWriter.flush();
- printWriter.close();
- fileIndex++;
- printWriter = new PrintWriter(DATA_DIR + "\\merged\\ComponentWithProperty_" + fileIndex + ".txt");
- }
- ComponentSimpleInfo component = parseComponent(componentLine);
- Set<PropertyValue> properties = new HashSet<>();
- if (propertyValue == null && !StringUtils.isEmpty(propertyValueLine = propertyValueReader.readLine())) {
- propertyValue = parsePropertyValue(propertyValueLine);
- }
- while (propertyValue.getComponentid().equals(component.getId())) {
- propertyValue.setComponentid(null);
- properties.add(propertyValue);
- // 读到文件最后一行
- if (StringUtils.isEmpty(propertyValueLine = propertyValueReader.readLine())) {
- break;
- }
- propertyValue = parsePropertyValue(propertyValueLine);
- }
- component.setProperties(properties);
- System.out.println(component);
- printWriter.println(FastjsonUtils.toJson(component));
- }
- printWriter.flush();
- } catch (FileNotFoundException e) {
- System.out.println("File not found!");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- componentReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- propertyValueReader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- printWriter.close();
- }
- }
- private static void readData() {
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(DATA_DIR + "\\merged\\ComponentWithProperty_1.txt"));
- String line = br.readLine();
- System.out.println(JSONObject.parseObject(line, ComponentSimpleInfo.class));
- } catch (FileNotFoundException e) {
- System.out.println("File not found!");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- merge();
- // readData();
- }
- }
|