neo4j-contrib / neo4j-graph-algorithms

Efficient Graph Algorithms for Neo4j
https://github.com/neo4j/graph-data-science/
GNU General Public License v3.0
771 stars 194 forks source link

How can i use the TriangleCount/ClusteringCoefficient function in a java embedding situation? #811

Open BeCuriousCat opened 5 years ago

BeCuriousCat commented 5 years ago

I had use Neo4j 3.2.2 in my application. But when i want to compute the clustering coefficient, I find i have to use the lasted version (3.5)。 After i see the ClusteringCoefficientWikiTest.java:

graph = new GraphLoader(db)
            .withAnyLabel()
            .withAnyRelationshipType()
            .withoutRelationshipWeights()
             .withoutNodeWeights()
             .asUndirected(true)
             .load(HeavyGraphFactory.class);

But i don't have a GraphDatabaseAPI db, so i have to convert GraphDatabaseAPI db = (GraphDatabaseAPI) new GraphDatabaseFactory().newEmbeddedDatabase(database);

When i can't get the right clustering coefficient. The result is 0.0

In Debug model , i find it had computed the triangle count but can't give the right clustering coefficient image image image

jexp commented 5 years ago

If you're using embedded it should be easy to upgrade to 3.5

You could also just call the procedure via db.execute() and pass in parameters, that would be easiest.

Usually you can cast GraphDatabaseService to GraphDatabaseAPI.

I think the clustering coefficient is computed by the procedure output.

BeCuriousCat commented 5 years ago

Yes, i also think use db.execute() maybe simpler. But i just use java embedding database, i don't download the server. From your document after download the jar, i have to change the conf file like image i don't have $NEO4J_HOME dir. So i don't know how to change the conf. When i just import the jar and use db.execute(), the following error are occurred.

org.neo4j.graphdb.QueryExecutionException: There is no procedure with the name `algo.triangleCount` registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed.
Exception in thread "main" java.lang.NullPointerException
    at Statistic.main(Statistic.java:207)

Here is my code:

public void calculateCC1(){
        try{
            // neo4j statement
            //cyper statement
            String query  = "CALL algo.triangleCount('User', 'FRIEND',"
                            +"{concurrency:4, write:true, writeProperty:'triangles',clusteringCoefficientProperty:'coefficient'})"
                            +"YIELD loadMillis, computeMillis, writeMillis, nodeCount, triangleCount, averageClusteringCoefficient;";
             Result result = graphDB.execute(query);
             while ( result.hasNext() )
             {
                    List<String> columns = result.columns();
                    System.out.println( columns );
             }
            System.out.println("successfully");
        }catch(Exception e){
                System.out.println("compute clustering coefficient fail!!!");
                System.out.println(e);
        }

    }
BeCuriousCat commented 5 years ago

If you're using embedded it should be easy to upgrade to 3.5

You could also just call the procedure via db.execute() and pass in parameters, that would be easiest.

Usually you can cast GraphDatabaseService to GraphDatabaseAPI.

I think the clustering coefficient is computed by the procedure output.

And i also confused about why the clustering coefficient is zero.. Thought it had computer the triangles.

BeCuriousCat commented 5 years ago

If you're using embedded it should be easy to upgrade to 3.5 You could also just call the procedure via db.execute() and pass in parameters, that would be easiest. Usually you can cast GraphDatabaseService to GraphDatabaseAPI. I think the clustering coefficient is computed by the procedure output.

And i also confused about why the clustering coefficient is zero.. Thought it had computer the triangles.

i find the reason. Because i don't use algo.getCoefficients() before i use algo.getAverageCoefficient.

Thank you for ur answer!

dreamerlzl commented 5 years ago

Yes, i also think use db.execute() maybe simpler. But i just use java embedding database, i don't download the server. From your document after download the jar, i have to change the conf file like image i don't have $NEO4J_HOME dir. So i don't know how to change the conf. When i just import the jar and use db.execute(), the following error are occurred.

org.neo4j.graphdb.QueryExecutionException: There is no procedure with the name `algo.triangleCount` registered for this database instance. Please ensure you've spelled the procedure name correctly and that the procedure is properly deployed.
Exception in thread "main" java.lang.NullPointerException
  at Statistic.main(Statistic.java:207)

Here is my code:

public void calculateCC1(){
      try{
            // neo4j statement
            //cyper statement
            String query  = "CALL algo.triangleCount('User', 'FRIEND',"
                              +"{concurrency:4, write:true, writeProperty:'triangles',clusteringCoefficientProperty:'coefficient'})"
                              +"YIELD loadMillis, computeMillis, writeMillis, nodeCount, triangleCount, averageClusteringCoefficient;";
             Result result = graphDB.execute(query);
             while ( result.hasNext() )
             {
                      List<String> columns = result.columns();
                      System.out.println( columns );
             }
            System.out.println("successfully");
        }catch(Exception e){
              System.out.println("compute clustering coefficient fail!!!");
              System.out.println(e);
        }

    }

How did you solve this error? ("there is no procedure...")