Skip to content

Instantly share code, notes, and snippets.

@mneedham
Created March 17, 2013 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mneedham/5182948 to your computer and use it in GitHub Desktop.
Save mneedham/5182948 to your computer and use it in GitHub Desktop.
Code for importing stadiums to the football graph
package main.java;
import org.neo4j.gis.spatial.indexprovider.SpatialIndexProvider;
import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class StadiumsImport {
public static void main(String[] args) throws IOException {
List<String> lines = readFile("data/stadiums.csv");
EmbeddedGraphDatabase db = new EmbeddedGraphDatabase("neo4j-community-1.9.M04/data/graph.db");
Index<Node> stadiumsIndex = createSpatialIndex(db, "stadiumsLocation");
Transaction tx = db.beginTx();
for (String stadium : lines) {
String[] columns = stadium.split(",");
Index<Node> teamsIndex = db.index().forNodes("teams");
String team = columns[1].replaceAll("\"","");
Node teamNode = teamsIndex.get("name", team).getSingle();
if(teamNode != null) {
Node stadiumNode = db.createNode();
stadiumNode.setProperty("wkt", String.format("POINT(%s %s)", columns[4], columns[3]));
stadiumNode.setProperty("name", columns[0].replaceAll("\"",""));
stadiumsIndex.add(stadiumNode, "dummy", "value");
teamNode.createRelationshipTo(stadiumNode, DynamicRelationshipType.withName("play_at"));
}
}
tx.success();
tx.finish();
}
private static Index<Node> createSpatialIndex(EmbeddedGraphDatabase db, String indexName) {
return db.index().forNodes(indexName, SpatialIndexProvider.SIMPLE_WKT_CONFIG);
}
private static List<String> readFile(String stadiumsFile) throws IOException {
List<String> lines = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(stadiumsFile));
br.readLine();
String line = br.readLine();
while (line != null) {
lines.add(line);
line = br.readLine();
}
return lines;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment