neo4j获取所有节点个数和所有关系个数以及通过java的简单获取

一:获取所有关系的个数

MATCH P=()-->() RETURN COUNT(*)  AS COUNT

二:获取所有节点的个数

MATCH (n) RETURN count(*) as COUNT

三:通过java获取所有关系的个数和所有节点的个数
java代码中使用的jar包为neo4j-java-driver-1.5.1.jar版本。

Map<String,String> map=new HashMap<String, String>();
    	try {
    			Driver driver = GraphDatabase.driver( "bolt://62.120.122.11:7687", AuthTokens.basic( "neo4j", "123456" ) );
        		Session session = driver.session();       		  
                StatementResult result = session.run( "MATCH P=()-->() RETURN COUNT(P) AS COUNT");
                String gxcount="";
                while ( result.hasNext() )
                {
                    Record record = result.next();
                    gxcount= record.get( "COUNT").asInt()+"";
                    System.out.println( gxcount);
                } 
                StatementResult result1 = session.run( "MATCH (n) RETURN count(*) as COUNT");
                String jdcount="";
                while ( result1.hasNext() )
                {
                    Record record = result1.next();
                    jdcount= record.get( "COUNT").asInt()+"";
                    System.out.println( jdcount);
                } 
                session.close();
                driver.close();  
                map.put("jdcount", jdcount);
                map.put("gxcount", gxcount);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}    	
    	return  map;

备注:因业务需要最近刚刚接触使用neo4j,如有错误欢迎留言指正。

你可能感兴趣的:(neo4j,java,neo4j)