neo4j的examples之EmbeddedNeo4j.java

neo4j的examples之EmbeddedNeo4j.java

neo2.3.0 代码
下载:github

/*
 * Licensed to Neo Technology under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo Technology licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.neo4j.examples;

import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;

public class EmbeddedNeo4j
{
    private static final String DB_PATH = "target/neo4j-hello-db";

    public String greeting;

    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;
    // END SNIPPET: vars

    // START SNIPPET: createReltype
    private static enum RelTypes implements RelationshipType
    {
        KNOWS
    }
    // END SNIPPET: createReltype

    public static void main( final String[] args ) throws IOException
    {
        EmbeddedNeo4j hello = new EmbeddedNeo4j();
        hello.createDb();
        hello.removeData();
        hello.shutDown();
    }

    void createDb() throws IOException
    {
        FileUtils.deleteRecursively( new File( DB_PATH ) );

        // START SNIPPET: startDb
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
        registerShutdownHook( graphDb );
        // END SNIPPET: startDb

        // START SNIPPET: transaction
        try ( Transaction tx = graphDb.beginTx() )
        {
            // Database operations go here
            // END SNIPPET: transaction
            // START SNIPPET: addData
            firstNode = graphDb.createNode();
            firstNode.setProperty( "message", "Hello, " );
            secondNode = graphDb.createNode();
            secondNode.setProperty( "message", "World!" );

            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
            relationship.setProperty( "message", "brave Neo4j " );
            // END SNIPPET: addData

            // START SNIPPET: readData
            System.out.print( firstNode.getProperty( "message" ) );
            System.out.print( relationship.getProperty( "message" ) );
            System.out.print( secondNode.getProperty( "message" ) );
            // END SNIPPET: readData

            greeting = ( (String) firstNode.getProperty( "message" ) )
                       + ( (String) relationship.getProperty( "message" ) )
                       + ( (String) secondNode.getProperty( "message" ) );

            // START SNIPPET: transaction
            tx.success();
        }
        // END SNIPPET: transaction
    }

    void removeData()
    {
        try ( Transaction tx = graphDb.beginTx() )
        {
            // START SNIPPET: removingData
            // let's remove the data
            firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
            firstNode.delete();
            secondNode.delete();
            // END SNIPPET: removingData

            tx.success();
        }
    }

    void shutDown()
    {
        System.out.println();
        System.out.println( "Shutting down database ..." );
        // START SNIPPET: shutdownServer
        graphDb.shutdown();
        // END SNIPPET: shutdownServer
    }

    // START SNIPPET: shutdownHook
    private static void registerShutdownHook( final GraphDatabaseService graphDb )
    {
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running application).
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                graphDb.shutdown();
            }
        } );
    }
    // END SNIPPET: shutdownHook
}

运行结果:

Hello, brave Neo4j World!
Shutting down database ...

自己稍作修改用于测试:

/*
 * Licensed to Neo Technology under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Neo Technology licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package n1;
/**
 * @author xubo601450868
 * 创建节点relationship等,创建后不删除
 * */
import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;

public class EmbeddedNeo4j
{
    private static final String DB_PATH = "target/neo4j-hello-db";

    public String greeting;

    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;
    // END SNIPPET: vars

    // START SNIPPET: createReltype
    private static enum RelTypes implements RelationshipType
    {
        KNOWS,friend
    }
    // END SNIPPET: createReltype

    public static void main( final String[] args ) throws IOException
    {
        EmbeddedNeo4j hello = new EmbeddedNeo4j();
        hello.createDb();
        hello.removeData();
        hello.shutDown();
    }

    void createDb() throws IOException
    {
        FileUtils.deleteRecursively( new File( DB_PATH ) );

        // START SNIPPET: startDb
        graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
        registerShutdownHook( graphDb );
        // END SNIPPET: startDb

        // START SNIPPET: transaction
        try ( Transaction tx = graphDb.beginTx() )
        {
            // Database operations go here
            // END SNIPPET: transaction
            // START SNIPPET: addData
            firstNode = graphDb.createNode();
            firstNode.setProperty( "message", "Hello,scala " );
            secondNode = graphDb.createNode();
            secondNode.setProperty( "message", "hello World!" );
            for (int i=1;i<=100;i++){
            Node firstNode1=graphDb.createNode();
            firstNode1.setProperty("message", "hello World!"+i);
            }
            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
            relationship.setProperty( "message", "brave Neo4j " );
            // END SNIPPET: addData

            // START SNIPPET: readData
            System.out.print( firstNode.getProperty( "message" ) );
            System.out.print( relationship.getProperty( "message" ) );
            System.out.print( secondNode.getProperty( "message" ) );
            System.out.println("\n"+firstNode+":"+firstNode.getId());
            System.out.println(relationship);
            System.out.println(secondNode);
            // END SNIPPET: readData

            greeting = "greet:"+( (String) firstNode.getProperty( "message" ) )
                       + ( (String) relationship.getProperty( "message" ) )
                       + ( (String) secondNode.getProperty( "message" ) );

            // START SNIPPET: transaction
            System.out.println(greeting);
            tx.success();
        }
        // END SNIPPET: transaction
    }

    void removeData()
    {
        try ( Transaction tx = graphDb.beginTx() )
        {
            // START SNIPPET: removingData
            // let's remove the data
//            firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
//            firstNode.delete();
//            secondNode.delete();
            // END SNIPPET: removingData

            tx.success();
        }
    }

    void shutDown()
    {
        System.out.println();
        System.out.println( "Shutting down database ..." );
        // START SNIPPET: shutdownServer
        graphDb.shutdown();
        // END SNIPPET: shutdownServer
    }

    // START SNIPPET: shutdownHook
    private static void registerShutdownHook( final GraphDatabaseService graphDb )
    {
        // Registers a shutdown hook for the Neo4j instance so that it
        // shuts down nicely when the VM exits (even if you "Ctrl-C" the
        // running application).
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                graphDb.shutdown();
            }
        } );
    }
    // END SNIPPET: shutdownHook
}

运行结果:

Hello,scala brave Neo4j hello World!
Node[0]:0
Relationship[0]
Node[1]
greet:Hello,scala brave Neo4j hello World!

Shutting down database ...

查询:

package n1;
/**
 * @author xubo601450868
 * 查询
 * 可以修改
 * */
import java.io.IOException;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

//import n1.EmbeddedNeo4j.RelTypes;

public class neo4j3 {
     private static final String DB_PATH = "target/neo4j-hello-db";
     //target/neo4j-hello-db
     //D:/1win81/java/neo4jdatabase
     GraphDatabaseService graphDb;
     Node firstNode;
     Node secondNode;
     Relationship relationship;
     private static enum RelTypes implements RelationshipType
     {
         KNOWS,friend
     }

    public static void main(String[] args) throws IOException{
        System.out.println("hello neo4j3");
        neo4j3 hello = new neo4j3();
         hello.createDb();
         hello.removeData();
         hello.shutDown();
    }
      void createDb() throws IOException
        {
          graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH) ;
          registerShutdownHook( graphDb );

          try ( Transaction tx = graphDb.beginTx() )
            {
                // Database operations go here
                // END SNIPPET: transaction
                // START SNIPPET: addData
              int i;
              for(i=0;i<=100;i++){
//               graphDb.
              if(graphDb.isAvailable(i)==true){
                  firstNode=graphDb.getNodeById(i);
                  //System.out.println(firstNode);
                  System.out.println(firstNode+":"+firstNode.getProperty( "message" ));

              }else{
                  break;
                  }
             // System.out.println(graphDb.isAvailable(i)); 
              }
//              firstNode = graphDb.createNode();
//              firstNode.setProperty( "message", "Hello,scala " );
//              secondNode = graphDb.createNode();
//              secondNode.setProperty( "message", "hello World!" );
//
//              relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
//              relationship.setProperty( "message", "brave Neo4j " );
//              // END SNIPPET: addData
//
//              // START SNIPPET: readData
//              System.out.print( firstNode.getProperty( "message" ) );
//              System.out.print( relationship.getProperty( "message" ) );
//              System.out.print( secondNode.getProperty( "message" ) );
//              System.out.println("\n"+firstNode+":"+firstNode.getId());
//              System.out.println(relationship);
//              System.out.println(secondNode);
                // END SNIPPET: readData

                tx.success();
            }

        }
      void removeData()
        {
          try ( Transaction tx = graphDb.beginTx() )
            {
                // START SNIPPET: removingData
                // let's remove the data
//              firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
//              firstNode.delete();
//              secondNode.delete();
                // END SNIPPET: removingData

                tx.success();
            }
        }

      void shutDown()
        {
            System.out.println();
            System.out.println( "Shutting down database ..." );
            // START SNIPPET: shutdownServer
            graphDb.shutdown();
            // END SNIPPET: shutdownServer
        }
      private static void registerShutdownHook( final GraphDatabaseService graphDb )
        {
            // Registers a shutdown hook for the Neo4j instance so that it
            // shuts down nicely when the VM exits (even if you "Ctrl-C" the
            // running application).
            Runtime.getRuntime().addShutdownHook( new Thread()
            {
                @Override
                public void run()
                {
                    graphDb.shutdown();
                }
            } );
        }
}


运行结果:

hello neo4j3
Node[0]:Hello,scala 
Node[1]:hello World!
Node[2]:hello World!1
Node[3]:hello World!2
Node[4]:hello World!3
Node[5]:hello World!4
Node[6]:hello World!5
Node[7]:hello World!6
Node[8]:hello World!7
Node[9]:hello World!8
Node[10]:hello World!9
Node[11]:hello World!10
Node[12]:hello World!11
Node[13]:hello World!12
Node[14]:hello World!13
Node[15]:hello World!14
Node[16]:hello World!15
Node[17]:hello World!16
Node[18]:hello World!17
Node[19]:hello World!18
Node[20]:hello World!19
Node[21]:hello World!20
Node[22]:hello World!21
Node[23]:hello World!22
Node[24]:hello World!23
Node[25]:hello World!24
Node[26]:hello World!25
Node[27]:hello World!26
Node[28]:hello World!27
Node[29]:hello World!28
Node[30]:hello World!29
Node[31]:hello World!30
Node[32]:hello World!31
Node[33]:hello World!32
Node[34]:hello World!33
Node[35]:hello World!34
Node[36]:hello World!35
Node[37]:hello World!36
Node[38]:hello World!37
Node[39]:hello World!38
Node[40]:hello World!39
Node[41]:hello World!40
Node[42]:hello World!41
Node[43]:hello World!42
Node[44]:hello World!43
Node[45]:hello World!44
Node[46]:hello World!45
Node[47]:hello World!46
Node[48]:hello World!47
Node[49]:hello World!48
Node[50]:hello World!49
Node[51]:hello World!50
Node[52]:hello World!51
Node[53]:hello World!52
Node[54]:hello World!53
Node[55]:hello World!54
Node[56]:hello World!55
Node[57]:hello World!56
Node[58]:hello World!57
Node[59]:hello World!58
Node[60]:hello World!59
Node[61]:hello World!60
Node[62]:hello World!61
Node[63]:hello World!62
Node[64]:hello World!63
Node[65]:hello World!64
Node[66]:hello World!65
Node[67]:hello World!66
Node[68]:hello World!67
Node[69]:hello World!68
Node[70]:hello World!69
Node[71]:hello World!70
Node[72]:hello World!71
Node[73]:hello World!72
Node[74]:hello World!73
Node[75]:hello World!74
Node[76]:hello World!75
Node[77]:hello World!76
Node[78]:hello World!77
Node[79]:hello World!78
Node[80]:hello World!79
Node[81]:hello World!80
Node[82]:hello World!81
Node[83]:hello World!82
Node[84]:hello World!83
Node[85]:hello World!84
Node[86]:hello World!85
Node[87]:hello World!86
Node[88]:hello World!87
Node[89]:hello World!88
Node[90]:hello World!89
Node[91]:hello World!90
Node[92]:hello World!91
Node[93]:hello World!92
Node[94]:hello World!93
Node[95]:hello World!94
Node[96]:hello World!95
Node[97]:hello World!96
Node[98]:hello World!97
Node[99]:hello World!98
Node[100]:hello World!99

Shutting down database ...
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页