java連接neo4j之rest api


neo4j連接java目前主要有嵌入式、jdbc和rest api。

以neo4j文檔的Jersey為例(實際有多種方式可以實現,目前覺得Jersey實現比較麻煩點,其他的都有封裝好請求)。

使用的lib包:jersey-bundle-1.17.jar(這個比較不好找)和Jersey提供的包

    String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
        final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
        WebResource resource = Client.create()
                .resource( nodeEntryPointUri );
        // POST {} to the node entry point URI
        //通過post請求新建一個節點
        ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
                .type( MediaType.APPLICATION_JSON )
                .entity( "{}" )
                .post( ClientResponse.class );
         
        final URI location = response.getLocation();
        System.out.println( String.format(
                "POST to [%s], status code [%d], location header [%s]",
                nodeEntryPointUri, response.getStatus(), location.toString() ) );
        response.close();
        System.out.println(location.toString());
        
        String propertyUri = location.toString() + "/properties/" + "name";
        // http://localhost:7474/db/data/node/{node_id}/properties/{property_name}
         
        WebResource propertyResource = Client.create()
                .resource( propertyUri );
        //根據拿回來的節點url,設置節點的屬性
        ClientResponse response1 = propertyResource.accept( MediaType.APPLICATION_JSON )
                .type( MediaType.APPLICATION_JSON )
                .entity( "\"Joe Strummer\"")
                .put( ClientResponse.class );
         
        System.out.println( String.format( "PUT to [%s], status code [%d]",
                propertyUri, response1.getStatus() ) );
        response.close();

jersey這種方式個人覺得不是很好使用,在創建一個節點時不能把屬性加入進去,必須是{},然后在根據拿回來的節點url加入屬性,比較繁瑣,但是根據權重計算最短路徑時,如果想使用服務式的neo4j。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM