赞
踩
官方介绍
https://neo4j.com/docs/developer-manual/current/cypher/clauses/
https://neo4j.com/developer/data-modeling/
When using Maven, add the following block to your pom.xml file. Note the placeholder for the driver version. You will have to find out the exact patch version that you wish to install.
- <dependencies>
- <dependency>
- <groupId>org.neo4j.driver</groupId>
- <artifactId>neo4j-java-driver</artifactId>
- <version>$JAVA_DRIVER_VERSION</version>
- </dependency>
- </dependencies>
- import org.neo4j.driver.v1.*;
-
- import static org.neo4j.driver.v1.Values.parameters;
-
-
- public class HelloWorldExample implements AutoCloseable
- {
- private final Driver driver;
-
- public HelloWorldExample( String uri, String user, String password )
- {
- driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
- }
-
- @Override
- public void close() throws Exception
- {
- driver.close();
- }
-
- public void printGreeting( final String message )
- {
- try ( Session session = driver.session() )
- {
- String greeting = session.writeTransaction( new TransactionWork<String>()
- {
- @Override
- public String execute( Transaction tx )
- {
- StatementResult result = tx.run( "CREATE (a:Greeting) " +
- "SET a.message = $message " +
- "RETURN a.message + ', from node ' + id(a)",
- parameters( "message", message ) );
- return result.single().get( 0 ).asString();
- }
- } );
- System.out.println( greeting );
- }
- }
-
- public static void main( String... args ) throws Exception
- {
- try ( HelloWorldExample greeter = new HelloWorldExample( "bolt://localhost:7687", "neo4j", "password" ) )
- {
- greeter.printGreeting( "hello, world" );
- }
- }
- }
- public boolean addItem()
- {
- try ( Session session = driver.session() )
- {
- return session.writeTransaction( new TransactionWork<Boolean>()
- {
- @Override
- public Boolean execute( Transaction tx )
- {
- tx.run( "CREATE (a:Item)" );
- return true;
- }
- } );
- }
- catch ( ServiceUnavailableException ex )
- {
- return false;
- }
- }
- public void addPerson(String name)
- {
- try ( Session session = driver.session() )
- {
- session.run("CREATE (a:Person {name: $name})", parameters( "name", name ) );
- }
- }
- public void addPerson( final String name )
- {
- try ( Session session = driver.session() )
- {
- session.writeTransaction( new TransactionWork<Integer>()
- {
- @Override
- public Integer execute( Transaction tx )
- {
- return createPersonNode( tx, name );
- }
- } );
- }
- }
-
- private static int createPersonNode( Transaction tx, String name )
- {
- tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
- return 1;
- }
- public List<String> getPeople()
- {
- try ( Session session = driver.session() )
- {
- return session.readTransaction( new TransactionWork<List<String>>()
- {
- @Override
- public List<String> execute( Transaction tx )
- {
- return matchPersonNodes( tx );
- }
- } );
- }
- }
-
- private static List<String> matchPersonNodes( Transaction tx )
- {
- List<String> names = new ArrayList<>();
- StatementResult result = tx.run( "MATCH (a:Person) RETURN a.name ORDER BY a.name" );
- while ( result.hasNext() )
- {
- names.add( result.next().get( 0 ).asString() );
- }
- return names;
- }
- public int addEmployees( final String companyName )
- {
- try ( Session session = driver.session() )
- {
- int employees = 0;
- List<Record> persons = session.readTransaction( new TransactionWork<List<Record>>()
- {
- @Override
- public List<Record> execute( Transaction tx )
- {
- return matchPersonNodes( tx );
- }
- } );
- for ( final Record person : persons )
- {
- employees += session.writeTransaction( new TransactionWork<Integer>()
- {
- @Override
- public Integer execute( Transaction tx )
- {
- tx.run( "MATCH (emp:Person {name: $person_name}) " +
- "MERGE (com:Company {name: $company_name}) " +
- "MERGE (emp)-[:WORKS_FOR]->(com)",
- parameters( "person_name", person.get( "name" ).asString(), "company_name",
- companyName ) );
- return 1;
- }
- } );
- }
- return employees;
- }
- }
-
- private static List<Record> matchPersonNodes( Transaction tx )
- {
- return tx.run( "MATCH (a:Person) RETURN a.name AS name" ).list();
- }
【学习创建方式】
- class TwitterNeo4jWriter {
- static String STATEMENT = "UNWIND {tweets} AS t\n" +
- "WITH t,\n" +
- " t.entities AS e,\n" +
- " t.user AS u,\n" +
- " t.retweeted_status AS retweet\n" +
- "WHERE t.id is not null " +
- "MERGE (tweet:Tweet {id:t.id})\n" +
- "SET tweet.text = t.text,\n" +
- " tweet.created = t.created_at,\n" +
- " tweet.favorites = t.favorite_count\n" +
- "MERGE (user:User {screen_name:u.screen_name})\n" +
- "SET user.name = u.name,\n" +
- " user.location = u.location,\n" +
- " user.followers = u.followers_count,\n" +
- " user.following = u.friends_count,\n" +
- " user.statuses = u.statuses_count,\n" +
- " user.profile_image_url = u.profile_image_url\n" +
- "MERGE (user)-[:POSTED]->(tweet)\n" +
- "FOREACH (h IN e.hashtags |\n" +
- " MERGE (tag:Tag {name:LOWER(h.text)})\n" +
- " MERGE (tag)<-[:TAGGED]-(tweet)\n" +
- ")\n" +
- "FOREACH (u IN [u IN e.urls WHERE u.expanded_url IS NOT NULL] |\n" +
- " MERGE (url:Link {url:u.expanded_url})\n" +
- " MERGE (tweet)-[:LINKED]->(url)\n" +
- ")\n" +
- "FOREACH (m IN e.user_mentions |\n" +
- " MERGE (mentioned:User {screen_name:m.screen_name})\n" +
- " ON CREATE SET mentioned.name = m.name\n" +
- " MERGE (tweet)-[:MENTIONED]->(mentioned)\n" +
- ")\n" +
- "FOREACH (r IN [r IN [t.in_reply_to_status_id] WHERE r IS NOT NULL] |\n" +
- " MERGE (reply_tweet:Tweet {id:r})\n" +
- " MERGE (tweet)-[:REPLIED_TO]->(reply_tweet)\n" +
- ")\n" +
- "FOREACH (retweet_id IN [x IN [retweet.id] WHERE x IS NOT NULL] |\n" +
- " MERGE (retweet_tweet:Tweet {id:retweet_id})\n" +
- " MERGE (tweet)-[:RETWEETED]->(retweet_tweet)\n" +
- ")";
- private Driver driver;
-
- public TwitterNeo4jWriter(String neo4jUrl) throws URISyntaxException {
- URI boltUri = new URI(neo4jUrl);
- String[] authInfo = boltUri.getUserInfo().split(":");
- driver = GraphDatabase.driver(boltUri, AuthTokens.basic(authInfo[0], authInfo[1]));
- }
-
- public void init() {
- try (Session session = driver.session()) {
- session.run("CREATE CONSTRAINT ON (t:Tweet) ASSERT t.id IS UNIQUE");
- session.run("CREATE CONSTRAINT ON (u:User) ASSERT u.screen_name IS UNIQUE");
- session.run("CREATE CONSTRAINT ON (t:Tag) ASSERT t.name IS UNIQUE");
- session.run("CREATE CONSTRAINT ON (l:Link) ASSERT l.url IS UNIQUE");
- }
- }
-
- public void close() {
- driver.close();
- }
-
- public int insert(List<String> tweets, int retries) {
- while (retries > 0) {
- try (Session session = driver.session()) {
- Gson gson = new Gson();
- List<Map> statuses = tweets.stream().map((s) -> gson.fromJson(s, Map.class)).collect(toList());
- long time = System.nanoTime();
-
- ResultSummary result = session.run(STATEMENT, Values.parameters("tweets", statuses)).consume();
- int created = result.counters().nodesCreated();
-
- System.out.println(created+" in "+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-time)+" ms");
- System.out.flush();
-
- return created;
- } catch (Exception e) {
- System.err.println(e.getClass().getSimpleName() + ":" + e.getMessage()+" retries left "+retries);
- retries--;
- }
- }
- return -1;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。