当前位置: 首页 > news >正文

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

这里写图片描述

相关文章:

  • win10编译caffe跑faster-rcnn(cuda7.5)
  • iOS计步器实例
  • 多线程
  • SmoOne——开源免费的企业移动OA应用,基于.Net
  • Configuring Zookeeper Cluster
  • Kubernetes集群测试环境搭建
  • 51 N QUEENS
  • 浏览器部分UA汇总
  • Java基础/Socket.io双向通信
  • java web项目流程小结
  • linux下查看文件编码及修改编码
  • nginx https配置
  • 挨踢部落故事汇(7): 结缘51CTO志在高远
  • canvas做的桌面
  • 多个极路由配置桥接模式共同ssid上网
  • 0x05 Python数据分析,Anaconda八斩刀
  • co模块的前端实现
  • JavaScript函数式编程(一)
  • JavaScript新鲜事·第5期
  • learning koa2.x
  • mysql 5.6 原生Online DDL解析
  • Phpstorm怎样批量删除空行?
  • python_bomb----数据类型总结
  • Python连接Oracle
  • Vim Clutch | 面向脚踏板编程……
  • vue-cli在webpack的配置文件探究
  • 记一次和乔布斯合作最难忘的经历
  • 前嗅ForeSpider采集配置界面介绍
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 数据结构java版之冒泡排序及优化
  • 我有几个粽子,和一个故事
  • 新手搭建网站的主要流程
  • 用jquery写贪吃蛇
  • 白色的风信子
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • C# - 为值类型重定义相等性
  • Hibernate主键生成策略及选择
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​虚拟化系列介绍(十)
  • # 透过事物看本质的能力怎么培养?
  • #ifdef 的技巧用法
  • #ubuntu# #git# repository git config --global --add safe.directory
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • #每天一道面试题# 什么是MySQL的回表查询
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (求助)用傲游上csdn博客时标签栏和网址栏一直显示袁萌 的头像
  • (三)终结任务
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .bat批处理(四):路径相关%cd%和%~dp0的区别