Neo4j embedded例子

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

直接从本地导入数据,相当于直接写文件了,速度非常快。
使用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自动生成的。配置文件第一句就有提到。

这里写图片描述

目录
相关文章
|
4月前
Neo4j Neo.TransientError.General.MemoryPoolOutOfMemoryError
Neo4j Neo.TransientError.General.MemoryPoolOutOfMemoryError
59 0
|
4月前
|
Java 知识图谱
知识图谱(Knowledge Graph)- Neo4j 5.10.0 使用 - Java SpringBoot 操作 Neo4j
知识图谱(Knowledge Graph)- Neo4j 5.10.0 使用 - Java SpringBoot 操作 Neo4j
206 0
|
数据库管理 Windows
【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
40 0
|
SQL 安全 Java
6. 成功解决:Driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' could not be found, make sure the 'MS SQL Server (Native)' driver (jar file) is installed.
在使用 Kettle(Spoon) 工具创建 SQL Server 数据库连接时,提示:Driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' could not be found, make sure the 'MS SQL Server (Native)' driver (jar file) is installed. com.microsoft.sqlserver.jdbc.SQLServerDriver
1532 1
|
机器学习/深度学习 SQL 数据库
NEO4J的入门和一些简单的操作
> 持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第29天,[点击查看活动详情](https://juejin.cn/post/7147654075599978532 "https://juejin.cn/post/7147654075599978532") # 引言 今天我们继续学习NEO4J. # 创建 创建语句我们一般会使用create指令 我们首先在控制台上输入`neo4j.bat console`启动neo4j 然后打开你的浏览器,然后在浏览器地址栏中输入 `http://localhost:7687 - Neo4j Browser](http:
Neo4j语法2
Neo4j语法
83 0
|
数据库 数据库管理
Neo4j语法1
Neo4j语法
126 0
Neo4j的一些命令
delete命令
105 0
|
数据库管理
Neo4j一些命令
Neo4j的CREATE命令
87 0
neo4j的一些命令
return命令
107 0