neo4j的examples之EmbeddedNeo4jWithCustomLogging.java

neo4j的examples之EmbeddedNeo4jWithCustomLogging.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.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 EmbeddedNeo4jWithCustomLogging
{
    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

        shutdown();
    }

    private static void shutdown()
    {
        graphDb.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
 * 不删除,读取http://blog.csdn.net/xubo245/article/details/50033549创建的信息
 * 查询所有节点和所有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 EmbeddedNeo4jWithCustomLogging
{
    private static final String DB_PATH = "target/neo4j-hello-db";
    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]{message:"Hello,scala "}      |
| Node[1]{message:"hello World!"}      |
| Node[2]{message:"hello World!1"}     |
| Node[3]{message:"hello World!2"}     |
| Node[4]{message:"hello World!3"}     |
| Node[5]{message:"hello World!4"}     |
| Node[6]{message:"hello World!5"}     |
| Node[7]{message:"hello World!6"}     |
| Node[8]{message:"hello World!7"}     |
| Node[9]{message:"hello World!8"}     |
| Node[10]{message:"hello World!9"}    |
| Node[11]{message:"hello World!10"}   |
| Node[12]{message:"hello World!11"}   |
| Node[13]{message:"hello World!12"}   |
| Node[14]{message:"hello World!13"}   |
| Node[15]{message:"hello World!14"}   |
| Node[16]{message:"hello World!15"}   |
| Node[17]{message:"hello World!16"}   |
| Node[18]{message:"hello World!17"}   |
| Node[19]{message:"hello World!18"}   |
| Node[20]{message:"hello World!19"}   |
| Node[21]{message:"hello World!20"}   |
| Node[22]{message:"hello World!21"}   |
| Node[23]{message:"hello World!22"}   |
| Node[24]{message:"hello World!23"}   |
| Node[25]{message:"hello World!24"}   |
| Node[26]{message:"hello World!25"}   |
| Node[27]{message:"hello World!26"}   |
| Node[28]{message:"hello World!27"}   |
| Node[29]{message:"hello World!28"}   |
| Node[30]{message:"hello World!29"}   |
| Node[31]{message:"hello World!30"}   |
| Node[32]{message:"hello World!31"}   |
| Node[33]{message:"hello World!32"}   |
| Node[34]{message:"hello World!33"}   |
| Node[35]{message:"hello World!34"}   |
| Node[36]{message:"hello World!35"}   |
| Node[37]{message:"hello World!36"}   |
| Node[38]{message:"hello World!37"}   |
| Node[39]{message:"hello World!38"}   |
| Node[40]{message:"hello World!39"}   |
| Node[41]{message:"hello World!40"}   |
| Node[42]{message:"hello World!41"}   |
| Node[43]{message:"hello World!42"}   |
| Node[44]{message:"hello World!43"}   |
| Node[45]{message:"hello World!44"}   |
| Node[46]{message:"hello World!45"}   |
| Node[47]{message:"hello World!46"}   |
| Node[48]{message:"hello World!47"}   |
| Node[49]{message:"hello World!48"}   |
| Node[50]{message:"hello World!49"}   |
| Node[51]{message:"hello World!50"}   |
| Node[52]{message:"hello World!51"}   |
| Node[53]{message:"hello World!52"}   |
| Node[54]{message:"hello World!53"}   |
| Node[55]{message:"hello World!54"}   |
| Node[56]{message:"hello World!55"}   |
| Node[57]{message:"hello World!56"}   |
| Node[58]{message:"hello World!57"}   |
| Node[59]{message:"hello World!58"}   |
| Node[60]{message:"hello World!59"}   |
| Node[61]{message:"hello World!60"}   |
| Node[62]{message:"hello World!61"}   |
| Node[63]{message:"hello World!62"}   |
| Node[64]{message:"hello World!63"}   |
| Node[65]{message:"hello World!64"}   |
| Node[66]{message:"hello World!65"}   |
| Node[67]{message:"hello World!66"}   |
| Node[68]{message:"hello World!67"}   |
| Node[69]{message:"hello World!68"}   |
| Node[70]{message:"hello World!69"}   |
| Node[71]{message:"hello World!70"}   |
| Node[72]{message:"hello World!71"}   |
| Node[73]{message:"hello World!72"}   |
| Node[74]{message:"hello World!73"}   |
| Node[75]{message:"hello World!74"}   |
| Node[76]{message:"hello World!75"}   |
| Node[77]{message:"hello World!76"}   |
| Node[78]{message:"hello World!77"}   |
| Node[79]{message:"hello World!78"}   |
| Node[80]{message:"hello World!79"}   |
| Node[81]{message:"hello World!80"}   |
| Node[82]{message:"hello World!81"}   |
| Node[83]{message:"hello World!82"}   |
| Node[84]{message:"hello World!83"}   |
| Node[85]{message:"hello World!84"}   |
| Node[86]{message:"hello World!85"}   |
| Node[87]{message:"hello World!86"}   |
| Node[88]{message:"hello World!87"}   |
| Node[89]{message:"hello World!88"}   |
| Node[90]{message:"hello World!89"}   |
| Node[91]{message:"hello World!90"}   |
| Node[92]{message:"hello World!91"}   |
| Node[93]{message:"hello World!92"}   |
| Node[94]{message:"hello World!93"}   |
| Node[95]{message:"hello World!94"}   |
| Node[96]{message:"hello World!95"}   |
| Node[97]{message:"hello World!96"}   |
| Node[98]{message:"hello World!97"}   |
| Node[99]{message:"hello World!98"}   |
| Node[100]{message:"hello World!99"}  |
| Node[101]{message:"hello World!100"} |
+--------------------------------------+
102 rows

+-----------------------------------+
| r                                 |
+-----------------------------------+
| :KNOWS[0]{message:"brave Neo4j "} |
+-----------------------------------+
1 row

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