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