Cypher 描述性的映像查詢語言

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   使用   io   

  1 /**  2       * 使用cypherParser語言  3       * @author 裴東輝  4       * @since 2014-8-4  下午3:22:25  5       */  6      public void userCypherParser(){  7         //執行一個Cypher Query的查詢    8         GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( "db/music.db" );  9         try(Transaction tx = graphDb.beginTx()){  10             /*API org.neo4j.cypher.javacompat.ExecutionEngine:To run a Cypher query, use this class. 11                  ExecutionEngine(GraphDatabaseService database) Creates an execution engine around the give graph database 12                  Method: 13                      ExecutionResult    execute(String query) 14                         Executes a query and returns an iterable that contains the result set 15              */ 16             ExecutionEngine engine = new ExecutionEngine(graphDb);   17             //使用cypher語言進行查詢-----一個描述性的圖形查詢語言 18             //你參考部分的模式時,需要通過命名完成。定義的不同的命名部分就被稱為標識符。 19             //得到所有節點可以通過星號(*),同樣對於關係也適用。 20             System.out.println("----------------------------nodes----------------------------"); 21             ExecutionResult result = engine.execute( "start n=node(*) return n" );   22             //擷取指定列的結果集   23             Iterator<Node> nodeIta = result.columnAs("n"); 24             Node node=null; 25             while(nodeIta.hasNext()){ 26                 node=nodeIta.next(); 27                 info(node+"-;node name="+node.getProperty("name"));         28             } 29              30             //關係資料 31             System.out.println("----------------------------relationship----------------------------"); 32             //得到所有節點可以通過星號(*),同樣對於關係也適用。 33             result = engine.execute( "start n=relationship(*) return n" );   34             //擷取指定列的結果集   35             Iterator<Relationship> relationshipIta = result.columnAs("n"); 36             Relationship relationship=null; 37             while(relationshipIta.hasNext()){ 38                 relationship=relationshipIta.next(); 39                 info(relationship+"-;start node:"+relationship.getStartNode().getProperty("name"));         40             } 41              42             //通過索引查詢擷取節點 43             /*如果開始節點可以通過索引查詢得到,可以如此來寫: 44                 node:indexName(key=”value”)。 45                 查詢: START n=node:nodes(name = "A") RETURN n 索引中命名為A的節點將被返回。 46             */ 47             System.out.println("----------------------------索引查詢節點----------------------------"); 48             //在此列子中存在一個節點索引叫nodes。 49              result = engine.execute( "start n=node:nodes(name=‘歌手 1的專輯 1‘) return n" );   50             //擷取指定列的結果集   51             nodeIta = result.columnAs("n"); 52             while(nodeIta.hasNext()){ 53                 node=nodeIta.next(); 54                 info(node+"-;node name="+node.getProperty("name"));         55             } 56              57             //通過索引擷取關係 58             System.out.println("----------------------------索引查詢關係----------------------------"); 59             //得到所有節點可以通過星號(*),同樣對於關係也適用。 60             result = engine.execute( "start n=relationship:relationships(publishtime=‘2014-10-10‘) return n" );   61             //擷取指定列的結果集   62             relationshipIta = result.columnAs("n"); 63             while(relationshipIta.hasNext()){ 64                 relationship=relationshipIta.next(); 65                 info(relationship+"-;start node:"+relationship.getStartNode().getProperty("name"));         66             } 67              68             //Match--http://www.uml.org.cn/sjjm/201203063.asp 69             /*Match語句 70                 在一個查詢的匹配(match)部分申明圖形(模式)。模式的申明導致一個或多個以逗號隔開的路徑(path)。 71                 節點標識符可以使用或者不是用圓括弧。使用圓括弧與不使用圓括弧完全對等,如: 72                 MATCH(a)-->(b) 與 MATCH a-->b 匹配模式完全相同。 73                 模式的所有部分都直接或者間接地綁定到開始點上。 74                 可選關係是一個可選描述模式的方法,但在真正圖中可能沒有匹配(節點可能沒有或者沒有此類別關係時),將被估值為null。 75                 與SQL中的外連接類似,如果Cypher發現一個或者多個匹配,將會全部返回。如果沒有匹配,Cypher將返回null。 76             */ 77             System.out.println("----------------------------Match查詢與節點--相關節點----------------------------"); 78             //在此列子中存在一個節點索引叫nodes。 79             //符號—意味著相關性,不需要關心方向和類型。 80             //當對關係的方向感興趣時,可以使用-->或<--符號,如: 81              result = engine.execute( "start n=node(0) match (n)-->(x) return x" );   82             //擷取指定列的結果集   83             nodeIta = result.columnAs("x"); 84             while(nodeIta.hasNext()){ 85                 node=nodeIta.next(); 86                 info(node+"-;node name="+node.getProperty("name"));         87             } 88              89             //定向關係和標識符  90             /*如果需要關係的標識符,為了過濾關係的屬性或為了返回關係,可如下例使用標識符。  91                 查詢:  92                 START n=node(3) MATCH (n)-[r]->() RETURN r 93                 所有從節點A接出的關係將被返回。 94             */ 95             System.out.println("----------------------------Match查詢與節點--這個節點所有點擊的關係----------------------------"); 96             //得到所有節點可以通過星號(*),同樣對於關係也適用。 97             result = engine.execute( "start n=node(0) match (n)-[r]->() return r" );   98             //擷取指定列的結果集   99             relationshipIta = result.columnAs("r");100             while(relationshipIta.hasNext()){101                 relationship=relationshipIta.next();102                 info(relationship+"-;start node:"+relationship.getStartNode().getProperty("name"));        103             }104             105             //通過關聯類型匹配到從這個節點發出的某個關係的所有節點106             System.out.println("----------------------------Match查詢通過關聯類型匹配到從這個節點發出的某個關係的所有節點----------------------------");107              result = engine.execute( "start n=node(0) match (n)-[:PUBLISH]->(x) return x" );  108             //擷取指定列的結果集  109             nodeIta = result.columnAs("x");110             while(nodeIta.hasNext()){111                 node=nodeIta.next();112                 info(node+"-;node name="+node.getProperty("name"));        113             }114             115             //通過關聯類型匹配和使用標識符116             System.out.println("----------------------------Match通過關聯類型匹配和使用標識符----------------------------");117             result = engine.execute( "start n=node(0) match (n)-[r:PUBLISH]->() return r" );  118             //擷取指定列的結果集  119             relationshipIta = result.columnAs("r");120             while(relationshipIta.hasNext()){121                 relationship=relationshipIta.next();122                 info(relationship+"-;start node:"+relationship.getStartNode().getProperty("name"));        123             }124             125             //多重關係---這一下就解決了這個歌手的所有歌曲的資訊126             System.out.println("----------------------------Match 多重關係----------------------------");127              result = engine.execute( "start n=node(0) match (n)-[:PUBLISH]->(a)-[:CONTAIN]->(x) return x" );  128             //擷取指定列的結果集  129             nodeIta = result.columnAs("x");130             while(nodeIta.hasNext()){131                 node=nodeIta.next();132                 info(node+"-;node name="+node.getProperty("name"));        133             }134             135         }finally{136             graphDb.shutdown();137         }138      }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.