import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.neo4j.graphdb.Direction; import org.neo4j.graphdb.DynamicLabel; import org.neo4j.graphdb.DynamicRelationshipType; import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Label; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Relationship; import org.neo4j.graphdb.RelationshipType; import org.neo4j.graphdb.Transaction; import org.neo4j.graphdb.factory.GraphDatabaseFactory; import org.neo4j.unsafe.batchinsert.BatchInserter; import org.neo4j.unsafe.batchinsert.BatchInserters; import org.neo4j.io.fs.DefaultFileSystemAbstraction; import org.neo4j.io.fs.FileUtils; /** * * In this program a separate inserter is used for each file; This is not * required for small datasets like this sample program But larger datasets * might result in memory overruns if a single inserter is used for the entire * dataset * */ public class LoadData { BufferedReader breader; Map movieMap = new HashMap(); Map theaterMap = new HashMap(); Map screenMap = new HashMap(); public void loadDB() throws IOException { deleteDataDir(new File(CONSTANTS.dbPath)); loadMovies(); loadTheaters(); loadScreens(); loadTheaterScreenRel(); loadScreenShowsRel(); System.out.println("Done loading..Shutting down...."); } public void loadMovies() throws IOException { BatchInserter inserter = null; try { inserter = BatchInserters.inserter(CONSTANTS.dbPath); // indexes are only creatred for the id attribute of the movie. // Indexes are dependent on the query. // So create as many indexes as the queries demand inserter.createDeferredSchemaIndex(MyLabels.MOVIE).on("id").create(); Map properties = new HashMap<>(); String line; // read movies breader = new BufferedReader( new FileReader(CONSTANTS.inputPath + CONSTANTS.movieFile)); while ((line = breader.readLine()) != null) { String[] parts = line.split(":"); properties.put("id", parts[0]); properties.put("name", parts[1]); properties.put("country", parts[2]); properties.put("year", parts[3]); long node = inserter.createNode(properties, MyLabels.MOVIE); movieMap.put(parts[0], node); } } finally { if (inserter != null) { inserter.shutdown(); } } } public void loadTheaters() throws IOException { BatchInserter inserter = null; try { inserter = BatchInserters.inserter(CONSTANTS.dbPath); // here an index is not created for the city attribute even though // it is required forn the query // experiment query times with and without indexes Map properties = new HashMap<>(); breader = new BufferedReader( new FileReader(CONSTANTS.inputPath + CONSTANTS.theaterFile)); String line; while ((line = breader.readLine()) != null) { String[] parts = line.split(":"); properties.put("id", parts[0]); properties.put("name", parts[1]); properties.put("city", parts[2]); long node = inserter.createNode(properties, MyLabels.THEATER); theaterMap.put(parts[0], node); } } finally { if (inserter != null) { inserter.shutdown(); } } } public void loadScreens() throws IOException { BatchInserter inserter = null; try { inserter = BatchInserters.inserter(CONSTANTS.dbPath); Map properties = new HashMap<>(); breader = new BufferedReader( new FileReader(CONSTANTS.inputPath + CONSTANTS.screensFile)); String line; while ((line = breader.readLine()) != null) { String[] parts = line.split(":"); properties.put("id", parts[0]); properties.put("name", parts[1]); long node = inserter.createNode(properties, MyLabels.SCREEN); screenMap.put(parts[0], node); } } finally { if (inserter != null) { inserter.shutdown(); } } } public void loadTheaterScreenRel() throws IOException { BatchInserter inserter = null; try { inserter = BatchInserters.inserter(CONSTANTS.dbPath); breader = new BufferedReader( new FileReader(CONSTANTS.inputPath + CONSTANTS.theaterscreensFile)); String line; while ((line = breader.readLine()) != null) { String[] parts = line.split(":"); Long theater = theaterMap.get(parts[0]); Long screen = screenMap.get(parts[1]); inserter.createRelationship(theater, screen, RelTypes.HAS_SCREEN, null); } } finally { if (inserter != null) { inserter.shutdown(); } } } public void loadScreenShowsRel() throws IOException { BatchInserter inserter = null; try { inserter = BatchInserters.inserter(CONSTANTS.dbPath); breader = new BufferedReader( new FileReader(CONSTANTS.inputPath + CONSTANTS.showsFile)); String line; while ((line = breader.readLine()) != null) { Map properties = new HashMap(); String[] parts = line.split(":"); Long screen = screenMap.get(parts[0]); Long movie = movieMap.get(parts[1]); properties.put("date", parts[2]); properties.put("time", parts[3]); properties.put("numOfTkts", Integer.parseInt(parts[4])); properties.put("price", Integer.parseInt(parts[5])); inserter.createRelationship(screen, movie, RelTypes.PLAYS_MOVIE, properties); } } finally { if (inserter != null) { inserter.shutdown(); } } } public void deleteDataDir(File dir) { for (File file : dir.listFiles()) file.delete(); } }