1
votes
Following is the java program which connects to NEO4J . it is not properly conneting executing cypher queries. Your help at the earliest

is appreciated.

        import java.util.Iterator;
        import java.util.Map;
        import java.util.Map.Entry;

        import javax.xml.soap.Node;

        import org.neo4j.graphdb.PropertyContainer; 
        import org.neo4j.graphdb.Transaction;

        import org.apache.struts2.interceptor.SessionAware;
        import org.neo4j.cypher.CypherParser;
        import org.neo4j.cypher.ExecutionEngine;
        import org.neo4j.cypher.javacompat.ExecutionResult;
        import org.neo4j.graphdb.GraphDatabaseService;
        import org.neo4j.graphdb.factory.GraphDatabaseFactory;
        import org.neo4j.kernel.EmbeddedGraphDatabase;

        import com.opensymphony.xwork2.ActionSupport;

        public class arch extends ActionSupport implements SessionAware
        {
        String S;

        private static final String DB_PATH = "/home/mkgs/Desktop/placement/";
        GraphDatabaseService graphDb;

        private static final long serialVersionUID = 1L;


        private String pwd;
        public String getPwd() {
          return pwd;
        }

        public void setPwd(String pwd) {
          this.pwd = pwd;
        }

        public String getUsername() {
          return username;
        }

        public void setUsername(String username) {
          this.username = username;
        }

        private String username;

        Map session;


        public String save() throws Exception
        {
          System.out.println("inside save");

          System.out.println("username:"+getUsername());

          System.out.println("pwd:"+getPwd());


          graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
          Transaction transaction = (Transaction) graphDb.beginTx();
          try {

              ExecutionEngine engine = new ExecutionEngine(graphDb, null);
              ExecutionResult result = (ExecutionResult) engine.execute("START     n=node(*) MATCH n-[r]-m RETURN type(r),

count() ORDER BY count() desc"); System.out.println(result);

              Iterator<Node> columnAs = result.columnAs("n");
              while(columnAs.hasNext())
              {
                  Node n = (Node)columnAs.next();
                  for (String key : ((PropertyContainer) n).getPropertyKeys()) 
                  {
                      System.out.println("{ " + key + " : " + ((PropertyContainer) n).getProperty(key)+ " } ");
                  }

              }
          }

          finally {
              ((org.neo4j.graphdb.Transaction) transaction).finish();
          }
          return "l";         
        }


        @Override
        public void setSession(Map arg0) {
          // TODO Auto-generated method stub

        }


        }

This is im getting during execution of above code using struts.......


Apr 02, 2014 5:53:27 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the

java.library.path: /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/server:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386:/usr/lib/jvm/java-7-openjdk-i386/jre/../lib/i386:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client:/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386::/usr/java/packages/lib/i386:/usr/lib/i386-linux-gnu/jni:/lib/i386-linux-gnu:/usr/lib/i386-linux-gnu:/usr/lib/jni:/lib:/usr/lib Apr 02, 2014 5:53:27 PM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:placementportal' did not find a matching property. Apr 02, 2014 5:53:27 PM org.apache.coyote.http11.Http11Protocol init INFO: Initializing Coyote HTTP/1.1 on http-8080 Apr 02, 2014 5:53:27 PM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 645 ms Apr 02, 2014 5:53:27 PM org.apache.catalina.core.StandardService start INFO: Starting service Catalina Apr 02, 2014 5:53:27 PM org.apache.catalina.core.StandardEngine start INFO: Starting Servlet Engine: Apache Tomcat/6.0.39 Apr 02, 2014 5:53:27 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(/home/mkgs/workspace/proj/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/placementportal/WEB-INF/lib/servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class Apr 02, 2014 5:53:29 PM org.apache.coyote.http11.Http11Protocol start INFO: Starting Coyote HTTP/1.1 on http-8080 Apr 02, 2014 5:53:29 PM org.apache.jk.common.ChannelSocket init INFO: JK: ajp13 listening on /0.0.0.0:8009 Apr 02, 2014 5:53:29 PM org.apache.jk.server.JkMain start INFO: Jk running ID=0 time=0/29 config=null Apr 02, 2014 5:53:29 PM org.apache.catalina.startup.Catalina start INFO: Server startup in 1958 ms

2
If you want help with an exception, then you really need to provide the exception error message and stack trace in your question.cybersam
So, can you please provide the exception error message and stack trace?cybersam
also create the graphdatabase and executionengine only once, and keep them around otherwise you don't utilize the caches. still make sure to shut it down correctly at the end of your applicationMichael Hunger

2 Answers

1
votes

you should be calling transaction.success() and then transaction.close(). Transaction.success() is what commits the transaction. Not sure what method you are actually calling with your cast.

The following code is a cut and paste from one of my projects for accessing an embedding GDB.

Transaction tx = graphDb.beginTx();
try{
    ExecutionEngine engine = new ExecutionEngine(graphDb);
    ExecutionResult result = engine.execute("MATCH (n) RETURN n");
    System.out.println(result.dumpToString());
    tx.success();
} finally {
    tx.close();
}
1
votes

Your query is

START n=node(*)
MATCH n-[r]-m
RETURN type(r), count(*)
ORDER BY count(*) DESC

but you're trying to access column 'n' and you don't mention it in your RETURN clause. You might want to change your query to the one below, but that depends on your use case.

START n=node(*)
MATCH n-[r]-m
RETURN n, type(r), count(*)
ORDER BY count(*) DESC