xufei 3 years ago
parent
commit
eda8c6869f
2 changed files with 137 additions and 0 deletions
  1. 22 0
      pom.xml
  2. 115 0
      src/test/java/com/winhc/CompanyIdUpdate.java

+ 22 - 0
pom.xml

@@ -153,6 +153,28 @@
             <artifactId>commons-io</artifactId>
             <version>2.5</version>
         </dependency>
+
+        <dependency>
+            <groupId>org.elasticsearch</groupId>
+            <artifactId>elasticsearch</artifactId>
+            <version>5.6.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-client</artifactId>
+            <version>5.6.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-high-level-client</artifactId>
+            <version>5.6.0</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.elasticsearch.client</groupId>
+                    <artifactId>elasticsearch-rest-client</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
     </dependencies>
 
     <dependencyManagement>

+ 115 - 0
src/test/java/com/winhc/CompanyIdUpdate.java

@@ -0,0 +1,115 @@
+package com.winhc;
+
+import cn.hutool.core.io.FileUtil;
+import com.alibaba.fastjson.JSON;
+import com.winhc.utils.CompanyUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.LineIterator;
+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.beans.factory.annotation.Qualifier;
+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:
+ * @date 2021/2/26 13:53
+ */
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+@Slf4j
+public class CompanyIdUpdate {
+
+    @Autowired
+    @Qualifier("DriverV1")
+    Driver driver;
+
+    @Test
+    public void parseCompanyIdCSV() throws IOException {
+        String path = "D:\\data\\opt\\export\\error_companyid_all.csv";
+        if (!CompanyUtils.isWindows()) {
+            path = "/data/opt/export/error_companyid_all.csv";
+        }
+        LineIterator it = FileUtils.lineIterator(FileUtil.file(path), "UTF-8");
+        try {
+            int i = 0;
+            List<Map<String, Object>> batch_list = new ArrayList<>();
+            while (it.hasNext()) {
+                String line = it.nextLine();
+                ++i;
+                //if (i == 1) continue;
+                Map<String, Object> map = new HashMap<>();
+                List<String> list = Arrays.asList(line.split(",", -1)).stream()
+                        .map(x -> x.replaceAll("\"", "")).collect(Collectors.toList());
+                //if (list.size() < 2) continue;
+                map.put("company_id", list.get(0));
+                String message = JSON.toJSONString(map);
+                //System.out.println(message);
+                batch_list.add(map);
+                if (batch_list.size() == 2000) {
+                    setCompanyIdLabel(batch_list, "企业", "deleted_company_20210625", "update_20210625");
+                    System.out.println("size: " + i);
+                    batch_list.clear();
+                }
+            }
+            if (batch_list.size() > 0) {
+                setCompanyIdLabel(batch_list, "企业", "deleted_company_20210625", "update_20210625");
+                System.out.println("size: " + i);
+                batch_list.clear();
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            System.out.println(e.getMessage());
+        } finally {
+            LineIterator.closeQuietly(it);
+        }
+    }
+
+
+    public void setCompanyIdLabel(List<Map<String, Object>> batch_list, String fromLabel, String updateCompanyLabel, String toLabel) {
+        long start = System.currentTimeMillis();
+        Session session = driver.session();
+
+        final String cql = "\nWITH  {batch_list} AS batch_list \n" +
+                "UNWIND batch_list AS row \n" +
+                "MATCH(n:" + fromLabel + "{company_id:row.company_id}) \n" +
+                "set n.deleted_name = n.name\n" +
+                "set n:" + updateCompanyLabel + "\n" +
+                "remove n.name\n" +
+                "with n\n" +
+                "MATCH (n)-[r]-(m)\n" +
+                "WHERE  ID(n) <> ID(m) \n" +
+                "set m:" + toLabel + "\n" +
+                "DELETE r";
+        CompanyUtils.runNeo4j(session, cql, new HashMap<String, Object>() {{
+            put("batch_list", batch_list);
+        }});
+        log.info("consumer size: {}, cost: {}, cql:{}", batch_list.size(), (System.currentTimeMillis() - start), cql);
+        session.close();
+    }
+
+    public void setCompanyIdLabel2(List<Map<String, Object>> batch_list, String fromLabel, String toLabel) {
+        long start = System.currentTimeMillis();
+        Session session = driver.session();
+
+        final String cql = "\nWITH  {batch_list} AS batch_list \n" +
+                "UNWIND batch_list AS row \n" +
+                "MATCH(s:" + fromLabel + "{company_id:row.company_id}) \n" +
+                "SET s:" + toLabel + " \n";
+        CompanyUtils.runNeo4j(session, cql, new HashMap<String, Object>() {{
+            put("batch_list", batch_list);
+        }});
+        log.info("consumer size: {}, cost: {}, cql:{}", batch_list.size(), (System.currentTimeMillis() - start), cql);
+        session.close();
+    }
+}