org.neo4j.tooling.GlobalGraphOperations Java Examples

The following examples show how to use org.neo4j.tooling.GlobalGraphOperations. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
public double getGraphWeightSum()
{
    int edgeCount = 0;

    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            edgeCount = IteratorUtil.count(GlobalGraphOperations.at(neo4jGraph).getAllRelationships());
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to get graph weight sum", e);
        }
    }

    return (double) edgeCount;
}
 
Example #2
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
public void initCommunityProperty()
{
    int communityCounter = 0;

    // maybe commit changes every 1000 transactions?
    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            for (Node n : GlobalGraphOperations.at(neo4jGraph).getAllNodes())
            {
                n.setProperty(NODE_COMMUNITY, communityCounter);
                n.setProperty(COMMUNITY, communityCounter);
                communityCounter++;
            }
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to initialize community property", e);
        }
    }
}
 
Example #3
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 6 votes vote down vote up
@Override
public int getNodeCount()
{
    int nodeCount = 0;
    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            nodeCount = IteratorUtil.count(GlobalGraphOperations.at(neo4jGraph).getAllNodes());
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to get node count", e);
        }
    }

    return nodeCount;
}
 
Example #4
Source File: VectorUtil.java    From graphify with Apache License 2.0 6 votes vote down vote up
private static List<Integer> getFeatureIndexList(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> patterns = new ArrayList<>();
    GlobalGraphOperations.at(db)
            .getAllNodesWithLabel(DynamicLabel.label("Pattern"))
            .forEach(a -> patterns.add(a));

    Collections.sort(patterns, (a, b) -> ((Integer)b.getProperty("threshold")).compareTo(((Integer)a.getProperty("threshold"))));

    List<Integer> patternIds = patterns.stream()
            .filter(a -> getFeatureMatchDistribution(db, a.getId()) > CONFIDENCE_INTERVAL)
            .map(a -> ((Long)a.getId()).intValue()).collect(Collectors.toList());

    tx.success();
    tx.close();

    return patternIds;
}
 
Example #5
Source File: PartitionedAnalysis.java    From neo4j-mazerunner with Apache License 2.0 6 votes vote down vote up
public void analyzePartitions() {
    // Query the nodes by label, which each set becomes a partition for analysis
    Transaction tx = graphDatabaseService.beginTx();

    Iterator<Node> partitions = GlobalGraphOperations.at(graphDatabaseService)
            .getAllNodesWithLabel(DynamicLabel.label(label))
            .iterator();

    // For each partition, query the target relationships between the partition
    partitions.forEachRemaining(partition -> {
        PartitionDescription partitionDescription = new PartitionDescription(partition.getId(), label);
        partitionDescription.setGroupRelationship(groupRelationship);
        partitionDescription.setTargetRelationship(targetRelationship);
        try {
            Path pt = Writer.exportPartitionToHDFSParallel(graphDatabaseService, partition, partitionDescription);
            if(pt != null)
                Writer.dispatchPartitionedJob(graphDatabaseService, type, partitionDescription, pt);
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        }
    });

    tx.success();
    tx.close();
}
 
Example #6
Source File: GraphAlgoEngine.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public void execute(DirectedModularity modularity) {
    try (Transaction tx = graph.beginTx()) {
        for (Node n : GlobalGraphOperations.at(graph).getAllNodes()) {
            modularity.compute(n);
        }
        for (Relationship r : GlobalGraphOperations.at(graph).getAllRelationships()) {
            modularity.compute(r);
        }
        tx.success();
    }
}
 
Example #7
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
@Override
public int reInitializeCommunities()
{
    Map<Integer, Integer> initCommunities = new HashMap<Integer, Integer>();
    int communityCounter = 0;

    try (final Transaction tx = beginUnforcedTransaction())
    {
        try
        {
            for (Node n : GlobalGraphOperations.at(neo4jGraph).getAllNodes())
            {
                Integer communityId = (Integer) (n.getProperty(COMMUNITY));
                if (!initCommunities.containsKey(communityId))
                {
                    initCommunities.put(communityId, communityCounter);
                    communityCounter++;
                }
                int newCommunityId = initCommunities.get(communityId);
                n.setProperty(COMMUNITY, newCommunityId);
                n.setProperty(NODE_COMMUNITY, newCommunityId);
            }
            tx.success();
        }
        catch (Exception e)
        {
            tx.failure();
            throw new BenchmarkingException("unable to reinitialize communities", e);
        }
    }

    return communityCounter;
}
 
Example #8
Source File: VectorUtil.java    From graphify with Apache License 2.0 5 votes vote down vote up
private static List<Node> getAllClasses(GraphDatabaseService db) {
    Transaction tx = db.beginTx();
    // Get classes using Java API
    final List<Node> finalClasses = new ArrayList<>();
    GlobalGraphOperations.at(db)
            .getAllNodesWithLabel(DynamicLabel.label("Class"))
            .forEach(a -> finalClasses.add(a));
    tx.success();
    tx.close();

    return finalClasses.stream().map(a -> a).collect(Collectors.toList());
}
 
Example #9
Source File: VectorUtil.java    From graphify with Apache License 2.0 5 votes vote down vote up
public static int getDocumentSize(GraphDatabaseService db)
{
    int documentSize;
    String cacheKey = "GLOBAL_DOCUMENT_SIZE";
    if(vectorSpaceModelCache.getIfPresent(cacheKey) == null) {
        documentSize = IteratorUtil.count(GlobalGraphOperations.at(db).getAllNodesWithLabel(DynamicLabel.label("Class")));
        vectorSpaceModelCache.put(cacheKey, documentSize);
    }
    else
    {
        documentSize = (Integer)vectorSpaceModelCache.getIfPresent(cacheKey);
    }
    return documentSize;
}
 
Example #10
Source File: NeoGraph.java    From neo4jena with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the grpah is empty or not
 */
@Override
public boolean isEmpty() {
	Transaction tx = graphdb.beginTx();
	Iterable<org.neo4j.graphdb.Node> nodes= GlobalGraphOperations.at(graphdb).getAllNodes();
	boolean empty = nodes.iterator().hasNext();
	tx.success();
	return !empty;
}
 
Example #11
Source File: Neo4JDb.java    From knowledge-extraction with Apache License 2.0 5 votes vote down vote up
public void writeOutContent(String filename) {
	GlobalGraphOperations ops = GlobalGraphOperations.at(graphDb);
	ExecutionEngine engine = new ExecutionEngine(graphDb);

	try (FileWriter writer = new FileWriter(filename);
			Transaction tx = graphDb.beginTx()) {
		for (Node n : ops.getAllNodes()) {
			writer.write("[" + n.getId() + "," + n.getProperty(PROP_NAME)
					+ ",[");
			Iterator<Node> connected = engine.execute(
					"START s=node(" + n.getId()
							+ ") MATCH s-[r]->n RETURN n").columnAs("n");
			for (Node e : IteratorUtil.asIterable(connected)) {
				Iterator<String> rel = engine.execute(
						"START s=node(" + n.getId() + "), e=node("
								+ e.getId()
								+ ") MATCH s-[r]->e RETURN r.value")
						.columnAs("r.value");
				String relVal = rel.hasNext()? rel.next() : "";
				writer.write("[" + e.getId() + ","
						+ relVal + "],");
			}
			writer.write("]]\n");
		}
		tx.success();
	} catch (IOException e1) {
		e1.printStackTrace();
	}
}
 
Example #12
Source File: DataImporterTest.java    From neo4j-rdbms-import with GNU General Public License v3.0 5 votes vote down vote up
private static String assertImport() {
    GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(STORE_DIR);

    try (Transaction tx = db.beginTx()) {
        int nodes = IteratorUtil.count(db.getAllNodes());
        Assert.assertEquals(USERS, nodes);
        int rels = IteratorUtil.count(GlobalGraphOperations.at(db).getAllRelationships());
        Assert.assertEquals(FRIENDSHIPS, rels);
        return "Imported nodes " + nodes + " rels " + rels;
    } finally {
        db.shutdown();
    }
}
 
Example #13
Source File: DataImporterEmployeeTest.java    From neo4j-rdbms-import with GNU General Public License v3.0 5 votes vote down vote up
private static String importInfo() {
    GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(STORE_DIR);

    try (Transaction tx = db.beginTx()) {
        int nodes = IteratorUtil.count(db.getAllNodes());
        int rels = IteratorUtil.count(GlobalGraphOperations.at(db).getAllRelationships());
        return "Imported nodes " + nodes + " rels " + rels;
    } finally {
        db.shutdown();
    }
}
 
Example #14
Source File: DataImporterNorthwindTest.java    From neo4j-rdbms-import with GNU General Public License v3.0 5 votes vote down vote up
private static String importInfo() {
    GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(STORE_DIR);
    try (Transaction tx = db.beginTx()) {
        int nodes = IteratorUtil.count(db.getAllNodes());
        int rels = IteratorUtil.count(GlobalGraphOperations.at(db).getAllRelationships());
        return "Imported nodes " + nodes + " rels " + rels;
    } finally {
        db.shutdown();
    }
}
 
Example #15
Source File: PageRank.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public PageRank(GraphDatabaseService g) {
	rankMap = new Long2DoubleOpenHashMap();
	try(Transaction tx = g.beginTx()) {
		for(@SuppressWarnings("unused") Node n : GlobalGraphOperations.at(g).getAllNodes())
			nodeCount += 1;
		tx.success();
	}
	this.firstMember = ( 1.0 - this.dampingFactor ) / this.nodeCount;
}
 
Example #16
Source File: Louvain.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public void clean() {
    try (Transaction tx = g.beginTx()) {
        for (Node n : GlobalGraphOperations.at(g).getAllNodes()) {
            n.removeProperty(communityProperty);
        }
        tx.success();
    }
}
 
Example #17
Source File: DataImporterSakilaTest.java    From neo4j-rdbms-import with GNU General Public License v3.0 5 votes vote down vote up
private static String importInfo() {
    GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase(STORE_DIR);

    try (Transaction tx = db.beginTx()) {
        int nodes = IteratorUtil.count(db.getAllNodes());
        int rels = IteratorUtil.count(GlobalGraphOperations.at(db).getAllRelationships());
        return "Imported nodes " + nodes + " rels " + rels;
    } finally {
        db.shutdown();
    }
}
 
Example #18
Source File: GraphAlgoEngine.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public void execute(SingleNodeScanAlgorithm algo) {
    try (Transaction tx = graph.beginTx()) {
        for (Node n : GlobalGraphOperations.at(graph).getAllNodes()) {
            algo.compute(n);
        }
    }
}
 
Example #19
Source File: GraphAlgoEngine.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
protected void initPhase(VertexAlgorithm algorithm) {
    Timer.timer().start();
    for (Node n : GlobalGraphOperations.at(graph).getAllNodes())
        algorithm.init(n);
    Timer.timer().stop();
    log.info("Init: " + Timer.timer().totalTime());
}
 
Example #20
Source File: GraphAlgoEngine.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public void clean(VertexAlgorithm algo) {
    try (Transaction tx = graph.beginTx()) {
        for (Node n : GlobalGraphOperations.at(graph).getAllNodes()) {
            String attrName = algo.getAttributeName();
            if (n.hasProperty(attrName))
                n.removeProperty(attrName);
        }
        tx.success();
    }
}
 
Example #21
Source File: GraphAlgoEngine.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public void collectResult(VertexAlgorithm algorithm) {
    Timer.timer().start();
    for (Node n : GlobalGraphOperations.at(graph).getAllNodes()) {
        algorithm.collectResult(n);
    }
    Timer.timer().stop();
    log.info("Collect: " + Timer.timer().totalTime());
}
 
Example #22
Source File: GraphAlgoEngine.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
protected void main(VertexAlgorithm algorithm) {
    Timer.timer().start();
    for (int it = 0; it < algorithm.getMaxIterations(); it++) {
        for (Node n : GlobalGraphOperations.at(graph).getAllNodes()) {
            algorithm.apply(n);
        }
    }
    Timer.timer().stop();
    log.info("Main: " + Timer.timer().totalTime());
}
 
Example #23
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<Relationship> getAllEdges()
{
    return GlobalGraphOperations.at(neo4jGraph).getAllRelationships().iterator();
}
 
Example #24
Source File: Neo4jGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<Node> getVertexIterator()
{
    return GlobalGraphOperations.at(neo4jGraph).getAllNodes().iterator();
}
 
Example #25
Source File: Neo4jSNAMain.java    From Neo4jSNA with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
       String zipFile = "data/cineasts_12k_movies_50k_actors_2.1.6.zip";
       String path = "data/cineasts_12k_movies_50k_actors.db/";

       try {
           FileUtils.deleteRecursively(new File(path));
           extractFolder(zipFile);
       } catch (Exception e) {
           e.printStackTrace();
           System.exit(1);
       }

       long nodeCount, relsCount;
	
	// Open a database instance
       GraphDatabaseService g = new GraphDatabaseFactory()
               .newEmbeddedDatabaseBuilder(path)
               .setConfig(GraphDatabaseSettings.allow_store_upgrade, "true")
               .newGraphDatabase();
       try (Transaction tx = g.beginTx() ) {
		nodeCount = IteratorUtil.count( GlobalGraphOperations.at(g).getAllNodes() );
		relsCount = IteratorUtil.count( GlobalGraphOperations.at(g).getAllRelationships() );
		tx.success();
	}
	
	System.out.println("Node count: "+nodeCount);
       System.out.println("Rel count: " + relsCount);

       // Declare the GraphAlgoEngine on the database instance
	GraphAlgoEngine engine = new GraphAlgoEngine(g);
	if( args.length > 1 && args[1].equals("off") )
		engine.disableLogging();

       Louvain louvain = new Louvain(g);
       louvain.execute();
       LouvainResult result = louvain.getResult();
       for (int layer : result.layers()) {
           System.out.println("Layer " + layer + ": " + result.layer(layer).size() + " nodes");
       }

       LabelPropagation lp = new LabelPropagation();
       // Starts the algorithm on the given graph g
	engine.execute(lp);
	Long2LongMap communityMap = lp.getResult();
	long totCommunities = new LongOpenHashSet( communityMap.values() ).size();
       System.out.println("There are " + totCommunities + " communities according to Label Propagation");

	DirectedModularity modularity = new DirectedModularity(g);
	engine.execute(modularity);
       System.out.println("The directed modularity of this network is " + modularity.getResult());

       UndirectedModularity umodularity = new UndirectedModularity(g);
	engine.execute(umodularity);
       System.out.println("The undirected modularity of this network is " + umodularity.getResult());

       engine.clean(lp); // Now you can clean Label propagation results

       TriangleCount tc = new TriangleCount();
	engine.execute(tc);
	Long2LongMap triangleCount = tc.getResult();
	Optional<Long> totalTriangles = triangleCount.values().stream().reduce( (x, y) -> x + y );
	System.out.println("There are "+totalTriangles.get()+" triangles");

	PageRank pr = new PageRank(g);
	engine.execute(pr);
	Long2DoubleMap ranks = pr.getResult();
	engine.clean(pr);
	Optional<Double> res = ranks.values().parallelStream().reduce( (x, y) -> x + y );
	System.out.println("Check PageRank sum is 1.0: "+ res.get());

	ConnectedComponents cc = new ConnectedComponents();
	engine.execute(cc);
	Long2LongMap components = cc.getResult();
	engine.clean(cc);
	int totalComponents = new LongOpenHashSet( components.values() ).size();
	System.out.println("There are "+ totalComponents+ " different connected components");
	
	StronglyConnectedComponents scc = new StronglyConnectedComponents();
	engine.execute(scc);
	components = scc.getResult();
	engine.clean(scc);
	totalComponents = new LongOpenHashSet( components.values() ).size();
	System.out.println("There are "+ totalComponents+ " different strongly connected components");
	
	// Don't forget to shutdown the database
	g.shutdown();
}
 
Example #26
Source File: GraphUtils.java    From Neo4jSNA with Apache License 2.0 4 votes vote down vote up
public static long getNodeCount(GraphDatabaseService db) {
    long count = 0;
    for (Node _ : GlobalGraphOperations.at(db).getAllNodes())
        count++;
    return count;
}
 
Example #27
Source File: GraphUtils.java    From Neo4jSNA with Apache License 2.0 4 votes vote down vote up
public static long getEdgeCount(GraphDatabaseService db) {
    long count = 0;
    for (Relationship _ : GlobalGraphOperations.at(db).getAllRelationships())
        count++;
    return count;
}
 
Example #28
Source File: Writer.java    From neo4j-mazerunner with Apache License 2.0 4 votes vote down vote up
public static Path exportSubgraphToHDFS(GraphDatabaseService db) throws IOException, URISyntaxException {
    FileSystem fs = FileUtil.getHadoopFileSystem();
    Path pt = new Path(ConfigurationLoader.getInstance().getHadoopHdfsUri() + EDGE_LIST_RELATIVE_FILE_PATH.replace("/{job_id}", ""));
    BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(pt)));

    Transaction tx = db.beginTx();

    // Get all nodes in the graph
    Iterable<Node> nodes = GlobalGraphOperations.at(db)
            .getAllNodes();

    br.write("# Adacency list" + "\n");

    int nodeTotal = IteratorUtil.count(nodes);
    final int[] nodeCount = {0};
    final int[] pathCount = {0};
    int pathCountBlocks = 10000;

    int size = IteratorUtil.count(nodes.iterator());

    //System.out.println(nodes.spliterator().trySplit().estimateSize());

    // Fork join

    nodes.iterator().forEachRemaining(n -> {
        // Filter nodes by all paths connected by the relationship type described in the configuration properties
        Iterable<org.neo4j.graphdb.Path> nPaths = db.traversalDescription()
                .depthFirst()
                .relationships(withName(ConfigurationLoader.getInstance().getMazerunnerRelationshipType()), Direction.OUTGOING)
                .evaluator(Evaluators.fromDepth(1))
                .evaluator(Evaluators.toDepth(1))
                .traverse(n);

        for (org.neo4j.graphdb.Path path : nPaths) {
            try {
                String line = path.startNode().getId() + " " + path.endNode().getId();
                br.write(line + "\n");
                pathCount[0]++;
                if (pathCount[0] > pathCountBlocks) {
                    pathCount[0] = 0;
                    System.out.println("Mazerunner Export Status: " + MessageFormat.format("{0,number,#%}", ((double) nodeCount[0] / (double) nodeTotal)));
                }
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }
        nodeCount[0]++;
    });

    System.out.println("Mazerunner Export Status: " + MessageFormat.format("{0,number,#.##%}", 1.0));

    br.flush();
    br.close();
    tx.success();
    tx.close();

    return pt;
}
 
Example #29
Source File: Writer.java    From neo4j-mazerunner with Apache License 2.0 4 votes vote down vote up
public static Path exportSubgraphToHDFSParallel(GraphDatabaseService db) throws IOException, URISyntaxException {
    FileSystem fs = FileUtil.getHadoopFileSystem();
    Path pt = new Path(ConfigurationLoader.getInstance().getHadoopHdfsUri() + EDGE_LIST_RELATIVE_FILE_PATH.replace("{job_id}", ""));
    BufferedWriter br = new BufferedWriter(new OutputStreamWriter(fs.create(pt)));

    Integer reportBlockSize = 20000;
    Transaction tx = db.beginTx();

    // Get all nodes in the graph
    Iterable<Node> nodes = GlobalGraphOperations.at(db)
            .getAllNodes();

    br.write("# Adacency list" + "\n");

    List<Spliterator<Node>> spliteratorList = new ArrayList<>();
    boolean hasSpliterator = true;
    Spliterator<Node> nodeSpliterator = nodes.spliterator();

    while (hasSpliterator) {
        Spliterator<Node> localSpliterator = nodeSpliterator.trySplit();
        hasSpliterator = localSpliterator != null;
        if (hasSpliterator)
            spliteratorList.add(localSpliterator);
    }

    tx.success();
    tx.close();

    counter = 0;

    if (spliteratorList.size() > 4) {
        // Fork join
        ParallelWriter parallelWriter = new ParallelWriter<Node>(spliteratorList.toArray(new Spliterator[spliteratorList.size()]),
                new GraphWriter(0, spliteratorList.size(), br, spliteratorList.size(), reportBlockSize, db, ConfigurationLoader.getInstance().getMazerunnerRelationshipType()));
        ForkJoinPool pool = new ForkJoinPool();
        pool.invoke(parallelWriter);
    } else {
        // Sequential
        spliteratorList.forEach(sl -> sl.forEachRemaining(n -> {
            try {
                writeBlockForNode(n, db, br, reportBlockSize, ConfigurationLoader.getInstance().getMazerunnerRelationshipType());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }));
    }

    System.out.println("Mazerunner Export Status: " + MessageFormat.format("{0,number,#.##%}", 1.0));

    br.flush();
    br.close();

    return pt;
}
 
Example #30
Source File: DirectedModularity.java    From Neo4jSNA with Apache License 2.0 4 votes vote down vote up
public DirectedModularity(GraphDatabaseService g) {
	try( Transaction tx = g.beginTx() ) {
		for( @SuppressWarnings("unused") Relationship r : GlobalGraphOperations.at(g).getAllRelationships() ) divisor += 1.0;
		tx.success();
	}
}