neo4j的examples之EmbeddedNeo4jWithIndexing.java

neo4j的examples之EmbeddedNeo4jWithIndexing.java

源码:

/*
 * 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.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;
import org.neo4j.io.fs.FileUtils;

public class EmbeddedNeo4jWithIndexing
{
    private static final String DB_PATH = "target/neo4j-store";
    private static final String USERNAME_KEY = "username";
    private static GraphDatabaseService graphDb;
    private static Index<Node> nodeIndex;

    public static void main( final String[] args ) throws IOException
    {
        FileUtils.deleteRecursively( new File( DB_PATH ) );

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

        // START SNIPPET: addUsers
        try ( Transaction tx = graphDb.beginTx() )
        {
            nodeIndex = graphDb.index().forNodes( "nodes" );
            // Create some users and index their names with the IndexService
            for ( int id = 0; id < 100; id++ )
            {
                createAndIndexUser( idToUserName( id ) );
            }
            // END SNIPPET: addUsers

            // Find a user through the search index
            // START SNIPPET: findUser
            int idToFind = 45;
            String userName = idToUserName( idToFind );
            Node foundUser = nodeIndex.get( USERNAME_KEY, userName ).getSingle();

            System.out.println( "The username of user " + idToFind + " is "
                + foundUser.getProperty( USERNAME_KEY ) );
            // END SNIPPET: findUser

            // Delete the persons and remove them from the index
            for ( Node user : nodeIndex.query( USERNAME_KEY, "*" ) )
            {
                nodeIndex.remove(  user, USERNAME_KEY,
                        user.getProperty( USERNAME_KEY ) );
                user.delete();
            }
            tx.success();
        }
        shutdown();
    }

    private static void shutdown()
    {
        graphDb.shutdown();
    }

    // START SNIPPET: helperMethods
    private static String idToUserName( final int id )
    {
        return "user" + id + "@neo4j.org";
    }

    private static Node createAndIndexUser( final String username )
    {
        Node node = graphDb.createNode();
        node.setProperty( USERNAME_KEY, username );
        nodeIndex.add( node, USERNAME_KEY, username );
        return node;
    }
    // END SNIPPET: helperMethods

    private static void registerShutdownHook()
    {
        // Registers a shutdown hook for the Neo4j and index service instances
        // so that it shuts down nicely when the VM exits (even if you
        // "Ctrl-C" the running example before it's completed)
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                shutdown();
            }
        } );
    }
}

运行结果:

The username of user 45 is user45@neo4j.org

不删除索引:

/*
 * 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
 *不删除创建的索引
 * 
 * */
import java.io.File;
import java.io.IOException;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.Index;
import org.neo4j.io.fs.FileUtils;

public class EmbeddedNeo4jWithIndexing
{
    private static final String DB_PATH = "target/neo4j-store";
    private static final String USERNAME_KEY = "username";
    private static GraphDatabaseService graphDb;
    private static Index<Node> nodeIndex;

    public static void main( final String[] args ) throws IOException
    {
        FileUtils.deleteRecursively( new File( DB_PATH ) );

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

        // START SNIPPET: addUsers
        try ( Transaction tx = graphDb.beginTx() )
        {
            nodeIndex = graphDb.index().forNodes( "nodes" );
            // Create some users and index their names with the IndexService
            for ( int id = 0; id < 100; id++ )
            {
                createAndIndexUser( idToUserName( id ) );
            }
            // END SNIPPET: addUsers

            // Find a user through the search index
            // START SNIPPET: findUser
            int idToFind = 45;
            String userName = idToUserName( idToFind );
            Node foundUser = nodeIndex.get( USERNAME_KEY, userName ).getSingle();

            System.out.println( "The username of user " + idToFind + " is "
                + foundUser.getProperty( USERNAME_KEY ) );
            // END SNIPPET: findUser

            // Delete the persons and remove them from the index
            for ( Node user : nodeIndex.query( USERNAME_KEY, "*" ) )
            {
//                nodeIndex.remove(  user, USERNAME_KEY,
//                        user.getProperty( USERNAME_KEY ) );
//                user.delete();
            }
            tx.success();
        }
        shutdown();
    }

    private static void shutdown()
    {
        graphDb.shutdown();
    }

    // START SNIPPET: helperMethods
    private static String idToUserName( final int id )
    {
        return "user" + id + "@neo4j.org";
    }

    private static Node createAndIndexUser( final String username )
    {
        Node node = graphDb.createNode();
        node.setProperty( USERNAME_KEY, username );
        nodeIndex.add( node, USERNAME_KEY, username );
        return node;
    }
    // END SNIPPET: helperMethods

    private static void registerShutdownHook()
    {
        // Registers a shutdown hook for the Neo4j and index service instances
        // so that it shuts down nicely when the VM exits (even if you
        // "Ctrl-C" the running example before it's completed)
        Runtime.getRuntime().addShutdownHook( new Thread()
        {
            @Override
            public void run()
            {
                shutdown();
            }
        } );
    }
}

查询:

/*
 * 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.cypher.ExecutionEngine;
import org.neo4j.cypher.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.logging.NullLog;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;

public class searchByCypher
{
    private static final String DB_PATH = "target/neo4j-store";
    private static GraphDatabaseService graphDb;

    private static class MyCustomLogProvider implements LogProvider
    {
        public MyCustomLogProvider( Object output )
        {
        }

        @Override
        public Log getLog( Class loggingClass )
        {
            return NullLog.getInstance();
        }

        @Override
        public Log getLog( String context )
        {
            return NullLog.getInstance();
        }
    }

    public static void main( final String[] args ) throws IOException
    {
        //FileUtils.deleteRecursively( new File( DB_PATH ) );

        Object output = new Object();

        // START SNIPPET: startDbWithLogProvider
        LogProvider logProvider = new MyCustomLogProvider( output );
        graphDb = new GraphDatabaseFactory().setUserLogProvider( logProvider ).newEmbeddedDatabase( DB_PATH );
        // END SNIPPET: startDbWithLogProvider
        System.out.println("hello neo4j log");
        //Node firstNode;
        //firstNode = graphDb.createNode();
        //firstNode.setProperty( "message", "Hello,scala " );

        ExecutionEngine engine =new ExecutionEngine(graphDb,  logProvider);
         ExecutionResult result = engine.execute("start n=node(*)  return n;");  
          System.out.println(result.dumpToString());  

          ExecutionResult result2 = engine.execute("start n=node(*) match (n)-[r]->() return r;");  
          System.out.println(result2.dumpToString());  
        shutdown();
    }

    private static void shutdown()
    {
        graphDb.shutdown();
    }
}

返回结果:

hello neo4j log
+---------------------------------------+
| n                                     |
+---------------------------------------+
| Node[0]{username:"user0@neo4j.org"}   |
| Node[1]{username:"user1@neo4j.org"}   |
| Node[2]{username:"user2@neo4j.org"}   |
| Node[3]{username:"user3@neo4j.org"}   |
| Node[4]{username:"user4@neo4j.org"}   |
| Node[5]{username:"user5@neo4j.org"}   |
| Node[6]{username:"user6@neo4j.org"}   |
| Node[7]{username:"user7@neo4j.org"}   |
| Node[8]{username:"user8@neo4j.org"}   |
| Node[9]{username:"user9@neo4j.org"}   |
| Node[10]{username:"user10@neo4j.org"} |
| Node[11]{username:"user11@neo4j.org"} |
| Node[12]{username:"user12@neo4j.org"} |
| Node[13]{username:"user13@neo4j.org"} |
| Node[14]{username:"user14@neo4j.org"} |
| Node[15]{username:"user15@neo4j.org"} |
| Node[16]{username:"user16@neo4j.org"} |
| Node[17]{username:"user17@neo4j.org"} |
| Node[18]{username:"user18@neo4j.org"} |
| Node[19]{username:"user19@neo4j.org"} |
| Node[20]{username:"user20@neo4j.org"} |
| Node[21]{username:"user21@neo4j.org"} |
| Node[22]{username:"user22@neo4j.org"} |
| Node[23]{username:"user23@neo4j.org"} |
| Node[24]{username:"user24@neo4j.org"} |
| Node[25]{username:"user25@neo4j.org"} |
| Node[26]{username:"user26@neo4j.org"} |
| Node[27]{username:"user27@neo4j.org"} |
| Node[28]{username:"user28@neo4j.org"} |
| Node[29]{username:"user29@neo4j.org"} |
| Node[30]{username:"user30@neo4j.org"} |
| Node[31]{username:"user31@neo4j.org"} |
| Node[32]{username:"user32@neo4j.org"} |
| Node[33]{username:"user33@neo4j.org"} |
| Node[34]{username:"user34@neo4j.org"} |
| Node[35]{username:"user35@neo4j.org"} |
| Node[36]{username:"user36@neo4j.org"} |
| Node[37]{username:"user37@neo4j.org"} |
| Node[38]{username:"user38@neo4j.org"} |
| Node[39]{username:"user39@neo4j.org"} |
| Node[40]{username:"user40@neo4j.org"} |
| Node[41]{username:"user41@neo4j.org"} |
| Node[42]{username:"user42@neo4j.org"} |
| Node[43]{username:"user43@neo4j.org"} |
| Node[44]{username:"user44@neo4j.org"} |
| Node[45]{username:"user45@neo4j.org"} |
| Node[46]{username:"user46@neo4j.org"} |
| Node[47]{username:"user47@neo4j.org"} |
| Node[48]{username:"user48@neo4j.org"} |
| Node[49]{username:"user49@neo4j.org"} |
| Node[50]{username:"user50@neo4j.org"} |
| Node[51]{username:"user51@neo4j.org"} |
| Node[52]{username:"user52@neo4j.org"} |
| Node[53]{username:"user53@neo4j.org"} |
| Node[54]{username:"user54@neo4j.org"} |
| Node[55]{username:"user55@neo4j.org"} |
| Node[56]{username:"user56@neo4j.org"} |
| Node[57]{username:"user57@neo4j.org"} |
| Node[58]{username:"user58@neo4j.org"} |
| Node[59]{username:"user59@neo4j.org"} |
| Node[60]{username:"user60@neo4j.org"} |
| Node[61]{username:"user61@neo4j.org"} |
| Node[62]{username:"user62@neo4j.org"} |
| Node[63]{username:"user63@neo4j.org"} |
| Node[64]{username:"user64@neo4j.org"} |
| Node[65]{username:"user65@neo4j.org"} |
| Node[66]{username:"user66@neo4j.org"} |
| Node[67]{username:"user67@neo4j.org"} |
| Node[68]{username:"user68@neo4j.org"} |
| Node[69]{username:"user69@neo4j.org"} |
| Node[70]{username:"user70@neo4j.org"} |
| Node[71]{username:"user71@neo4j.org"} |
| Node[72]{username:"user72@neo4j.org"} |
| Node[73]{username:"user73@neo4j.org"} |
| Node[74]{username:"user74@neo4j.org"} |
| Node[75]{username:"user75@neo4j.org"} |
| Node[76]{username:"user76@neo4j.org"} |
| Node[77]{username:"user77@neo4j.org"} |
| Node[78]{username:"user78@neo4j.org"} |
| Node[79]{username:"user79@neo4j.org"} |
| Node[80]{username:"user80@neo4j.org"} |
| Node[81]{username:"user81@neo4j.org"} |
| Node[82]{username:"user82@neo4j.org"} |
| Node[83]{username:"user83@neo4j.org"} |
| Node[84]{username:"user84@neo4j.org"} |
| Node[85]{username:"user85@neo4j.org"} |
| Node[86]{username:"user86@neo4j.org"} |
| Node[87]{username:"user87@neo4j.org"} |
| Node[88]{username:"user88@neo4j.org"} |
| Node[89]{username:"user89@neo4j.org"} |
| Node[90]{username:"user90@neo4j.org"} |
| Node[91]{username:"user91@neo4j.org"} |
| Node[92]{username:"user92@neo4j.org"} |
| Node[93]{username:"user93@neo4j.org"} |
| Node[94]{username:"user94@neo4j.org"} |
| Node[95]{username:"user95@neo4j.org"} |
| Node[96]{username:"user96@neo4j.org"} |
| Node[97]{username:"user97@neo4j.org"} |
| Node[98]{username:"user98@neo4j.org"} |
| Node[99]{username:"user99@neo4j.org"} |
+---------------------------------------+
100 rows

+---+
| r |
+---+
+---+
0 row

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