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自动生成的。配置文件第一句就有提到。

这里写图片描述

目录
相关文章
|
10月前
|
SQL NoSQL 算法
Neo4j极简教程
图数据库是NoSQL类数据库的一大典型代表,在国内图数据库属于新兴事物,其优异的复杂关系解决方案引起了国内众多大型互联网公司及IT开发者的关注,而Neo4j是目前图形化数据库中最为出色、最为成熟的产品。
616 0
Neo4j极简教程
|
11月前
Neo4j语法2
Neo4j语法
60 0
|
11月前
|
数据库 数据库管理
Neo4j语法1
Neo4j语法
92 0
|
11月前
|
数据库管理
Neo4j一些命令
Neo4j的CREATE命令
68 0
|
11月前
neo4j的一些命令
return命令
76 0
|
11月前
Neo4j的一些命令
delete命令
79 0
|
12月前
|
NoSQL 数据可视化 Java
我的Neo4j探索之旅 - 初识Neo4j(一)
neo4j 这个东西在国内用的很少,目前能百度的资料也是很早之前的几篇了,我针对neo4j 3.5 的版本进行一次学习和记录,以及实际的工作需求我也遇到了,后续会开源一个剔除业务的开源项目,有兴趣的读者可以了解一下图数据库的中间件,还是蛮有意思的。
229 0
|
机器学习/深度学习 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:
|
atlas 算法框架/工具 Caffe
caffe cmake错误 Could NOT find Atlas (missing: Atlas_LAPACK_LIBRARY)
caffe cmake错误 Could NOT find Atlas (missing: Atlas_LAPACK_LIBRARY)
|
算法 搜索推荐
Neo4j-APOC扩展与使用(下)
Neo4j-APOC扩展与使用 1.APOC简介与安装 1.1 APOC简介 1.2安装APOC
Neo4j-APOC扩展与使用(下)