1 package org.neo4j.talend;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.neo4j.tooling.ImportTool;
6
7 import java.io.BufferedWriter;
8 import java.io.FileWriter;
9 import java.io.IOException;
10 import java.nio.file.Files;
11 import java.nio.file.Path;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15
16 public class Neo4jImportTool {
17
18
19
20
21 private final static Logger log = Logger.getLogger(Neo4jImportTool.class);
22
23 protected final static String HEADERS_KEY = "HEADERS";
24 protected final static String FILE_KEY = "FILE";
25
26 private String dbPath;
27 private Map<String, String> neo4jConfiguration;
28 private Map<String, String> importConfiguration;
29 private List<Map<String, String>> nodes;
30 private List<Map<String, String>> relationships;
31
32
33
34
35
36
37
38
39
40
41 public Neo4jImportTool(String dbPath, Map<String, String> neo4jConfiguration, Map<String, String> importConfiguration, List<Map<String, String>> nodes, List<Map<String, String>> relationships) {
42 this.dbPath = dbPath;
43 this.neo4jConfiguration = neo4jConfiguration;
44 this.importConfiguration = importConfiguration;
45 this.nodes = nodes;
46 this.relationships = relationships;
47 }
48
49
50
51
52
53
54 public void execute() throws IOException {
55 List<String> params = new ArrayList<>();
56
57
58 params.add("--into");
59 params.add(dbPath);
60
61
62 params.add("--db-config");
63 params.add(this.createNeo4jConfigFile());
64
65
66 for (Map.Entry<String, String> entry : importConfiguration.entrySet()) {
67 params.add("--" + entry.getKey());
68 params.add(entry.getValue());
69 }
70
71
72 for (Map<String, String> node : nodes) {
73 params.add("--nodes");
74 params.add(this.createHeaderFile(node.get(HEADERS_KEY)) + "," + node.get(FILE_KEY));
75 }
76
77
78 for (Map<String, String> relationship : relationships) {
79 params.add("--relationships");
80 params.add(this.createHeaderFile(relationship.get(HEADERS_KEY)) + "," + relationship.get(FILE_KEY));
81 }
82
83 log.trace("Launch import tool with args : " + StringUtils.join(params, " "));
84
85
86 ImportTool.main(params.toArray(new String[params.size()]));
87 }
88
89
90
91
92
93
94
95
96 protected String createHeaderFile(String header) throws IOException {
97
98 Path path = Files.createTempFile("importToolHeader", ".csv");
99
100
101 BufferedWriter bw = new BufferedWriter(new FileWriter(path.toFile()));
102 bw.write(header);
103 bw.close();
104
105
106 return path.toFile().getAbsolutePath();
107 }
108
109
110
111
112
113
114
115 protected String createNeo4jConfigFile() throws IOException {
116
117 Path path = Files.createTempFile("neo4jConfig", ".properties");
118
119
120 BufferedWriter bw = new BufferedWriter(new FileWriter(path.toFile()));
121 for (Map.Entry<String, String> entry : neo4jConfiguration.entrySet()) {
122 bw.write(entry.getKey() + "=" + entry.getValue());
123 bw.newLine();
124 }
125 bw.close();
126
127 return path.toFile().getAbsolutePath();
128 }
129 }