🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.bazy-danych
3 messages
3 total messages Started by Borneq Sun, 22 May 2016 21:35
Zrozumieć język Cypher
#194978
Author: Borneq
Date: Sun, 22 May 2016 21:35
77 lines
3694 bytes
Są przykłady oprogramowania bazy neo4j, zwłaszcza w Javie:
http://neo4j.com/developer/
Najpierw mamy listę list i
               String insertQuery = "UNWIND {pairs} as pair " +
                "MERGE (p1:Person {name:pair[0]}) " +
                "MERGE (p2:Person {name:pair[1]}) " +
                "MERGE (p1)-[:KNOWS]-(p2);";
Co dokładnie robi?


import org.neo4j.driver.v1.*;
import static org.neo4j.driver.v1.Values.parameters;

import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonMap;

public class Social {

     public static void main(String...args) {

         Config noSSL = 
Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig();
         Driver driver = 
GraphDatabase.driver("bolt://localhost",AuthTokens.basic("neo4j","test"),noSSL); 
// <password>
         try (Session session = driver.session()) {

               List data =
 
asList(asList("Jim","Mike"),asList("Jim","Billy"),asList("Anna","Jim"),
 
asList("Anna","Mike"),asList("Sally","Anna"),asList("Joe","Sally"),
               asList("Joe","Bob"),asList("Bob","Sally"));

               String insertQuery = "UNWIND {pairs} as pair " +
                "MERGE (p1:Person {name:pair[0]}) " +
                "MERGE (p2:Person {name:pair[1]}) " +
                "MERGE (p1)-[:KNOWS]-(p2);";

 
session.run(insertQuery,singletonMap("pairs",data)).consume();

                StatementResult result;

                String foafQuery =
                " MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) "+
                " WHERE person.name = {name} " +
                "   AND NOT (person)-[:KNOWS]-(foaf) " +
                " RETURN foaf.name AS name ";
                result = session.run(foafQuery, parameters("name","Joe"));
                while (result.hasNext()) 
System.out.println(result.next().get("name"));

                String commonFriendsQuery =
                "MATCH 
(user:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person) " +
                " WHERE user.name = {from} AND foaf.name = {to} " +
                " RETURN friend.name AS friend";
                result = session.run(commonFriendsQuery, 
parameters("from","Joe","to","Sally"));
                while (result.hasNext()) 
System.out.println(result.next().get("friend"));

                String connectingPathsQuery =
                "MATCH path = 
shortestPath((p1:Person)-[:KNOWS*..6]-(p2:Person)) " +
                " WHERE p1.name = {from} AND p2.name = {to} " +
                " RETURN [n IN nodes(path) | n.name] as names";
                result = session.run(connectingPathsQuery, 
parameters("from","Joe","to","Billy"));
                while (result.hasNext()) 
System.out.println(result.next().get("names"));

         }
     }
}
Re: Zrozumieć język Cypher
#194979
Author: artiun
Date: Mon, 23 May 2016 12:17
86 lines
4763 bytes
W dniu 2016-05-22 o 23:35, Borneq pisze:
> Są przykłady oprogramowania bazy neo4j, zwłaszcza w Javie:
> http://neo4j.com/developer/
> Najpierw mamy listę list i
>               String insertQuery = "UNWIND {pairs} as pair " +
>                "MERGE (p1:Person {name:pair[0]}) " +
>                "MERGE (p2:Person {name:pair[1]}) " +
>                "MERGE (p1)-[:KNOWS]-(p2);";
> Co dokładnie robi?
>
>
> import org.neo4j.driver.v1.*;
> import static org.neo4j.driver.v1.Values.parameters;
>
> import java.util.List;
> import static java.util.Arrays.asList;
> import static java.util.Collections.singletonMap;
>
> public class Social {
>
>     public static void main(String...args) {
>
>         Config noSSL =
> Config.build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig();
>         Driver driver =
> GraphDatabase.driver("bolt://localhost",AuthTokens.basic("neo4j","test"),noSSL);
> // <password>
>         try (Session session = driver.session()) {
>
>               List data =
>
> asList(asList("Jim","Mike"),asList("Jim","Billy"),asList("Anna","Jim"),
>
> asList("Anna","Mike"),asList("Sally","Anna"),asList("Joe","Sally"),
>               asList("Joe","Bob"),asList("Bob","Sally"));
>
>               String insertQuery = "UNWIND {pairs} as pair " +
>                "MERGE (p1:Person {name:pair[0]}) " +
>                "MERGE (p2:Person {name:pair[1]}) " +
>                "MERGE (p1)-[:KNOWS]-(p2);";
>
>
> session.run(insertQuery,singletonMap("pairs",data)).consume();
>
>                StatementResult result;
>
>                String foafQuery =
>                " MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) "+
>                " WHERE person.name = {name} " +
>                "   AND NOT (person)-[:KNOWS]-(foaf) " +
>                " RETURN foaf.name AS name ";
>                result = session.run(foafQuery, parameters("name","Joe"));
>                while (result.hasNext())
> System.out.println(result.next().get("name"));
>
>                String commonFriendsQuery =
>                "MATCH (user:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person)
> " +
>                " WHERE user.name = {from} AND foaf.name = {to} " +
>                " RETURN friend.name AS friend";
>                result = session.run(commonFriendsQuery,
> parameters("from","Joe","to","Sally"));
>                while (result.hasNext())
> System.out.println(result.next().get("friend"));
>
>                String connectingPathsQuery =
>                "MATCH path =
> shortestPath((p1:Person)-[:KNOWS*..6]-(p2:Person)) " +
>                " WHERE p1.name = {from} AND p2.name = {to} " +
>                " RETURN [n IN nodes(path) | n.name] as names";
>                result = session.run(connectingPathsQuery,
> parameters("from","Joe","to","Billy"));
>                while (result.hasNext())
> System.out.println(result.next().get("names"));
>
>         }
>     }
> }
Nie pomyliło Ci się coś?
Nie ta grupa - NTG
A jeśli już coś to nie zajmuj się czymś o ile nie masz o tym pojęcia. Choć 
skromny manual przeczytaj!


-- 
Artur
Re: Zrozumieć język Cypher
#194980
Author: Borneq
Date: Mon, 23 May 2016 20:13
7 lines
1178 bytes
W dniu 23.05.2016 o 14:17, artiun pisze:
> Nie pomyliło Ci się coś?
> Nie ta grupa - NTG
> A jeśli już coś to nie zajmuj się czymś o ile nie masz o tym pojęcia.
> Choć skromny manual przeczytaj!

Jak to NTG? to jest baza danych
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads