View Javadoc
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       * The logger
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       * Constructor.
34       *
35       * @param dbPath
36       * @param neo4jConfiguration
37       * @param importConfiguration
38       * @param nodes
39       * @param relationships
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       * Execute the import.
51       *
52       * @throws IOException
53       */
54      public void execute() throws IOException {
55          List<String> params = new ArrayList<>();
56  
57          // Adding dbPath
58          params.add("--into");
59          params.add(dbPath);
60  
61          // Adding neo4j config file
62          params.add("--db-config");
63          params.add(this.createNeo4jConfigFile());
64  
65          // Adding advanced configuration
66          for (Map.Entry<String, String> entry : importConfiguration.entrySet()) {
67              params.add("--" + entry.getKey());
68              params.add(entry.getValue());
69          }
70  
71          // Adding node files
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          // Adding relationships files
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          // execute the import tool
86          ImportTool.main(params.toArray(new String[params.size()]));
87      }
88  
89      /**
90       * Create the specified header file.
91       *
92       * @param header Content of the file
93       * @return Full path of the generated file
94       * @throws IOException
95       */
96      protected String createHeaderFile(String header) throws IOException {
97          // create file
98          Path path = Files.createTempFile("importToolHeader", ".csv");
99  
100         // write header into it
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      * Create Neo4j configuration file.
111      *
112      * @return Full path of the generated file
113      * @throws IOException
114      */
115     protected String createNeo4jConfigFile() throws IOException {
116         // create file
117         Path path = Files.createTempFile("neo4jConfig", ".properties");
118 
119         // write header into it
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 }