import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider;
import org.neo4j.unsafe.batchinsert.BatchInserter;
import org.neo4j.unsafe.batchinsert.BatchInserterIndex;
import org.neo4j.unsafe.batchinsert.BatchInserterIndexProvider;
import org.neo4j.unsafe.batchinsert.BatchInserters;
public class Neo4jMassiveInsertion implements Insertion {
private BatchInserter inserter = null;
private BatchInserterIndexProvider indexProvider = null;
private BatchInserterIndex nodes = null;
private static enum RelTypes implements RelationshipType {
SIMILAR
}
public static void main(String args[]) {
Neo4jMassiveInsertion test = new Neo4jMassiveInsertion();
test.startup("data/neo4j");
test.createGraph("data/youtubeEdges.txt");
test.shutdown();
}
/**
* Start neo4j database and configure for massive insertion
* @param neo4jDBDir
*/
public void startup(String neo4jDBDir) {
System.out.println("The Neo4j database is now starting . . . .");
Map<String, String> config = new HashMap<String, String>();
config.put("cache_type", "none");
config.put("use_memory_mapped_buffers", "true");
config.put("neostore.nodestore.db.mapped_memory", "200M");
config.put("neostore.relationshipstore.db.mapped_memory", "1000M");
config.put("neostore.propertystore.db.mapped_memory", "250M");
config.put("neostore.propertystore.db.strings.mapped_memory", "250M");
inserter = BatchInserters.inserter(neo4jDBDir, config);
indexProvider = new LuceneBatchInserterIndexProvider(inserter);
nodes = indexProvider.nodeIndex("nodes", MapUtil.stringMap("type", "exact"));
}
public void shutdown() {
System.out.println("The Neo4j database is now shuting down . . . .");
if(inserter != null) {
indexProvider.shutdown();
inserter.shutdown();
indexProvider = null;
inserter = null;
}
}
public void createGraph(String datasetDir) {
System.out.println("Creating the Neo4j database . . . .");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(datasetDir)));
String line;
int lineCounter = 1;
Map<String, Object> properties;
IndexHits<Long> cache;
long srcNode, dstNode;
while((line = reader.readLine()) != null) {
if(lineCounter > 4) {
String[] parts = line.split("\t");
cache = nodes.get("nodeId", parts[0]);
if(cache.hasNext()) {
srcNode = cache.next();
}
else {
properties = MapUtil.map("nodeId", parts[0]);
srcNode = inserter.createNode(properties);
nodes.add(srcNode, properties);
nodes.flush();
}
cache = nodes.get("nodeId", parts[1]);
if(cache.hasNext()) {
dstNode = cache.next();
}
else {
properties = MapUtil.map("nodeId", parts[1]);
dstNode = inserter.createNode(properties);
nodes.add(dstNode, properties);
nodes.flush();
}
inserter.createRelationship(srcNode, dstNode, RelTypes.SIMILAR, null);
}
lineCounter++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public void createGraph(String datasetDir) {
System.out.println("Creating the Neo4j database . . . .");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(datasetDir)));
String line;
int lineCounter = 1;
Map<String, Object> properties;
List<Long> index = new ArrayList<Long>();
long srcNode, dstNode;
while((line = reader.readLine()) != null) {
if(lineCounter > 4) {
String[] parts = line.split("\t");
if(index.contains(Long.valueOf(parts[0]))) {
srcNode = Long.valueOf(parts[0]);
}
else {
properties = MapUtil.map("nodeId", parts[0]);
srcNode = inserter.createNode(properties);
//nodes.add(srcNode, properties);
index.add(srcNode);
}
if(index.contains(Long.valueOf(parts[1]))) {
dstNode = Long.valueOf(parts[1]);
}
else {
properties = MapUtil.map("nodeId", parts[1]);
dstNode = inserter.createNode(properties);
//nodes.add(dstNode, properties);
index.add(dstNode);
}
inserter.createRelationship(srcNode, dstNode, RelTypes.SIMILAR, null);
}
lineCounter++;
}
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/uZqRgCBc9lg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSetting;
import org.neo4j.graphdb.index.Index;
public class Neo4jSingleInsertion implements Insertion {
public static String INSERTION_TIMES_OUTPUT_PATH = "data/neo4j.insertion.times";
private static int count;
private GraphDatabaseService neo4jGraph = null;
private Index<Node> nodeIndex = null;
private static enum RelTypes implements RelationshipType {
SIMILAR
}
public static void main(String args[]) {
Neo4jSingleInsertion test = new Neo4jSingleInsertion();
test.startup("data/neo4j");
test.createGraph("data/enronEdges.txt");
test.shutdown();
}
public void startup(String neo4jDBDir) {
System.out.println("The Neo4j database is now starting . . . .");
neo4jGraph = new GraphDatabaseFactory().newEmbeddedDatabase(neo4jDBDir);
nodeIndex = neo4jGraph.index().forNodes("nodes");
}
public void shutdown() {
System.out.println("The Neo4j database is now shuting down . . . .");
if(neo4jGraph != null) {
neo4jGraph.shutdown();
nodeIndex = null;
}
}
public void createGraph(String datasetDir) {
count++;
System.out.println("Incrementally creating the Neo4j database . . . .");
List<Double> insertionTimes = new ArrayList<Double>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(datasetDir)));
String line;
int nodesCounter = 0;
int lineCounter = 1;
Transaction tx = null;
long start = System.currentTimeMillis();
long duration;
while((line = reader.readLine()) != null) {
if(lineCounter > 4) {
String[] parts = line.split("\t");
Node srcNode = nodeIndex.get("nodeId", parts[0]).getSingle();
if(srcNode == null) {
tx = neo4jGraph.beginTx();
srcNode = neo4jGraph.createNode();
srcNode.setProperty("nodeId", parts[0]);
nodeIndex.add(srcNode, "nodeId", parts[0]);
tx.success();
tx.finish();
nodesCounter++;
}
if(nodesCounter == 1000) {
duration = System.currentTimeMillis() - start;
insertionTimes.add((double) duration);
nodesCounter = 0;
start = System.currentTimeMillis();
}
Node dstNode = nodeIndex.get("nodeId", parts[1]).getSingle();
if(dstNode == null) {
tx = neo4jGraph.beginTx();
dstNode = neo4jGraph.createNode();
dstNode.setProperty("nodeId", parts[1]);
nodeIndex.add(dstNode, "nodeId", parts[1]);
tx.success();
tx.finish();
nodesCounter++;
}
tx = neo4jGraph.beginTx();
srcNode.createRelationshipTo(dstNode, RelTypes.SIMILAR);
tx.success();
tx.finish();
if(nodesCounter == 1000) {
duration = System.currentTimeMillis() - start;
insertionTimes.add((double) duration);
nodesCounter = 0;
start = System.currentTimeMillis();
}
}
lineCounter++;
}
duration = System.currentTimeMillis() - start;
insertionTimes.add((double) duration);
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
Utils utils = new Utils();
utils.writeTimes(insertionTimes, Neo4jSingleInsertion.INSERTION_TIMES_OUTPUT_PATH+"."+count);
}
}Transaction tx = ((GraphDatabaseAPI)neo4jGraph).tx().unforced().begin();
You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/uZqRgCBc9lg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "Neo4j" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/neo4j/uZqRgCBc9lg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to neo4j+un...@googlegroups.com.
db = Cadet::BatchInserter::Session.open("neo4j-community-2.0.1/data/graph.db")db.constraint :Legislator, :namel = db.get_node(:Legislator, :thomas_id, leg["id"]["thomas"].to_i)gender = db.get_node(:Gender, :name, leg["bio"]["gender"])l.outgoing(:gender) << genderdb.close