|
@@ -0,0 +1,126 @@
|
|
|
|
+package com.winhc;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.winhc.utils.CompanyUtils;
|
|
|
|
+import com.winhc.utils.DateUtil;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.commons.io.LineIterator;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.junit.runner.RunWith;
|
|
|
|
+import org.neo4j.driver.Driver;
|
|
|
|
+import org.neo4j.driver.Session;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author π
|
|
|
|
+ * @Description:解析 merge CSV
|
|
|
|
+ * @date 2021/2/26 13:53
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
|
+@SpringBootTest
|
|
|
|
+@Slf4j
|
|
|
|
+public class ParseMergeCSV {
|
|
|
|
+ @Autowired
|
|
|
|
+ Driver driver;
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void parseCSV() throws IOException {
|
|
|
|
+ String path = "D:\\data\\opt\\export\\merge-person-20210219.csv";
|
|
|
|
+ String flag = "0";
|
|
|
|
+
|
|
|
|
+ LineIterator it = FileUtils.lineIterator(FileUtil.file(path), "UTF-8");
|
|
|
|
+ try {
|
|
|
|
+ int i = 0;
|
|
|
|
+ Set<String> set = new HashSet<String>();
|
|
|
|
+ while (it.hasNext()) {
|
|
|
|
+ String line = it.nextLine();
|
|
|
|
+ ++i;
|
|
|
|
+ if (i == 1) continue;
|
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
|
+ List<String> list = Arrays.asList(line.split("\",\"", -1)).stream()
|
|
|
|
+ .map(x -> x.replaceAll("\"", "")).collect(Collectors.toList());
|
|
|
|
+ if (list.size() < 2) continue;
|
|
|
|
+ String time = DateUtil.formatDate_YYYY_MM_DD_HH_MM_SS(new Date());
|
|
|
|
+ map.put("create_time", time);
|
|
|
|
+ map.put("update_time", time);
|
|
|
|
+ map.put("person_id", list.get(0));
|
|
|
|
+ map.put("person_name", list.get(1));
|
|
|
|
+ if ("0".equals(flag)) {//合并
|
|
|
|
+ map.put("company_id", list.get(2));
|
|
|
|
+ map.put("deleted", list.get(3));
|
|
|
|
+ map.put("label", list.get(4));
|
|
|
|
+ } else {//删除
|
|
|
|
+ map.put("company_id", "");
|
|
|
|
+ map.put("deleted", "9");
|
|
|
|
+ map.put("label", "");
|
|
|
|
+ }
|
|
|
|
+ String message = JSON.toJSONString(map);
|
|
|
|
+ if (StringUtils.isBlank(map.get("deleted"))) {
|
|
|
|
+ System.out.println(message);
|
|
|
|
+ }
|
|
|
|
+ set.add(map.get("person_id"));
|
|
|
|
+ //System.out.println(message);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ System.out.println(set.size());
|
|
|
|
+
|
|
|
|
+ List<Map<String, Object>> batch_list = new ArrayList<>();
|
|
|
|
+
|
|
|
|
+ for (String s : set) {
|
|
|
|
+ HashMap<String, Object> m2 = new HashMap<>();
|
|
|
|
+ m2.put("person_id", s);
|
|
|
|
+ batch_list.add(m2);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ setLabel(batch_list, "个人", "合并20210219");
|
|
|
|
+ } finally {
|
|
|
|
+ LineIterator.closeQuietly(it);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void setLabel(List<Map<String, Object>> batch_list, String fromLabel, String toLabel) {
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ Session session = driver.session();
|
|
|
|
+ final String cql = "WITH {batch_list} AS batch_list \n" +
|
|
|
|
+ "UNWIND batch_list AS row \n" +
|
|
|
|
+ "MATCH(s:" + fromLabel + "{person_id:row.person_id}) \n" +
|
|
|
|
+ "SET s:" + toLabel + " \n";
|
|
|
|
+ log.info("consumer size: {}, cost: {}, cql:{}", batch_list.size(), (System.currentTimeMillis() - start), cql);
|
|
|
|
+ CompanyUtils.runNeo4j(session, cql, new HashMap<String, Object>() {{
|
|
|
|
+ put("batch_list", batch_list);
|
|
|
|
+ }});
|
|
|
|
+ session.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void combineLabel() throws IOException {
|
|
|
|
+// String[] fromLabels = {"合并20210219", "合并20210220", "合并20210221", "合并20210222", "合并20210223"
|
|
|
|
+// , "合并20210224", "合并20210225", "合并20210226", "合并20210227", "合并20210301"};
|
|
|
|
+ String[] fromLabels = {"合并20210302", "合并20210303", "合并20210304", "合并20210304"};
|
|
|
|
+ String toLabel = "更新20210301";
|
|
|
|
+ for (String fromLabel : fromLabels) {
|
|
|
|
+ combineLabel(fromLabel, toLabel);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void combineLabel(String fromLabel, String toLabel) {
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ Session session = driver.session();
|
|
|
|
+ final String cql = "MATCH (m:" + fromLabel + ") \n" +
|
|
|
|
+ "SET m:" + toLabel + " \n";
|
|
|
|
+ log.info("consumer cost: {}, cql:{}", (System.currentTimeMillis() - start), cql);
|
|
|
|
+ CompanyUtils.runNeo4j(session, cql, null);
|
|
|
|
+ session.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|