Neo4j embedded例子

直接从本地导入数据,相当于直接写文件了,速度非常快。
使用neo4j 3.1.0社区版
1.neo4j-community-3.1.0-unix.tar.gz 解压后修改相关配置

# Bolt connector
dbms.connector.bolt.enabled=true
dbms.connector.bolt.tls_level=OPTIONAL
dbms.connector.bolt.listen_address=0.0.0.0:7687

# HTTP Connector. There must be exactly one HTTP connector.
dbms.connector.http.enabled=true
dbms.connector.http.listen_address=0.0.0.0:7474

# HTTPS Connector. There can be zero or one HTTPS connectors.
dbms.connector.https.enabled=true
dbms.connector.https.listen_address=0.0.0.0:7473

2.新建工程。新建lib文件夹。把$NEO4J_HOME/lib下的拷贝到工程的lib文件夹下。

/*
 * 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 cn.integritytech;

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

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
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;

public class EmbeddedNeo4j2
{

    // START SNIPPET: vars
    GraphDatabaseService graphDb;
    Node firstNode;
    Node secondNode;
    Relationship relationship;
    File testDirectory = new File("/usr/local/neo4j-community-3.1.0/data/databases/graph.db");
    String pathToConfig = "/usr/local/neo4j-community-3.1.0/conf/";
    // END SNIPPET: vars

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

    public static void main( final String[] args ) throws IOException
    {
        System.out.println("2开始导入了...............................");
        EmbeddedNeo4j2 hello = new EmbeddedNeo4j2();
        hello.createDb();
        hello.shutDown();
        System.out.println("2导入完毕...............................");
    }

    void createDb() throws IOException
    {
        // 读配置文件创建graphDb实例
        graphDb = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder( testDirectory )
                .loadPropertiesFromFile( pathToConfig + "neo4j.conf" )
                .newGraphDatabase();
        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.addLabel(Label.label("Swordsman"));
            firstNode.setProperty( "name", "郭靖" );

            secondNode = graphDb.createNode();
            secondNode.addLabel(Label.label("Swordsman"));
            secondNode.setProperty( "name", "黄蓉" );

            relationship = firstNode.createRelationshipTo( secondNode, RelTypes.MARRIES );
            relationship.setProperty( "childrenNumber", "3" );
            // END SNIPPET: addData

            // START SNIPPET: readData
            System.out.print( firstNode.getProperty( "name" ) +"\t");
            System.out.print( secondNode.getProperty( "name" ) +"\t");
            System.out.print( relationship.getProperty( "childrenNumber" ) );
            // END SNIPPET: readData

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

    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
}

3.导出jar。右键工程》导出》Runnable Jar》
Launch configuration选择 main函数所在的类
Library handling选择 Copy required libraries into a sub-folder next to the generated JAR

假如导出的结果是一个embedded2.jar embedded2_lib。将他们放在同一个目录下。
运行: java -jar embedded2.jar

注意这里数据库的存储路径使用默认的路径。就是neo4j server启动起来之后默认生成的。 NEO4JHOME/data/databases/graph.db使 NEO4J_HOME/conf/neo4j.conf

graph.db是当前活动的数据库,是neo4j server自动生成的。配置文件第一句就有提到。

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值