neo4j-jersey分嵌入式和服务式连接图形数据库
原文载自:http://blog.csdn.net/yidian815/article/details/12887259
嵌入式:
引入neo4j依赖
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>1.9.4</version>
</dependency>
创建一个neo4j.properties(数据库的配置文件)
# Default values for the low-level graph engine
#neostore.nodestore.db.mapped_memory=25M
#neostore.relationshipstore.db.mapped_memory=50M
#neostore.propertystore.db.mapped_memory=90M
#neostore.propertystore.db.strings.mapped_memory=130M
#neostore.propertystore.db.arrays.mapped_memory=130M # Autoindexing # Enable auto-indexing for nodes, default is false
#node_auto_indexing=true # The node property keys to be auto-indexed, if enabled
#node_keys_indexable=name,age # Enable auto-indexing for relationships, default is false
#relationship_auto_indexing=true # The relationship property keys to be auto-indexed, if enabled
#relationship_keys_indexable=name,age # Keep logical logs, needed for online backups to work
keep_logical_logs=true # Enable online backups to be taken from this database.
online_backup_enabled=true # Uncomment and specify these lines for running Neo4j in High Availability mode.
# ha.server_id is a unique integer for each instance of the Neo4j database in the cluster.
# (as opposed to the coordinator instance IDs)
# example: ha.server_id=1
#ha.server_id= # ha.coordinators is a comma-separated list (without spaces) of the host:port of where to
# find one or more of the Neo4j coordinator servers.
# Avoid localhost due to IP resolution issues on some systems.
# example: ha.coordinators=localhost:2181,1.2.3.4:4321
#ha.coordinators=localhost:2181 # You can also, optionally, configure the ha.cluster_name. This is the name of the cluster this
# instance is supposed to join. Accepted characters are alphabetical, numerical, dot and dash.
# This configuration is useful if you have multiple Neo4j HA clusters managed by the same
# Coordinator cluster.
# Example: ha.cluster_name = my.neo4j.ha.cluster
#ha.cluster_name = # IP and port for this instance to bind to to communicate data with the
# other neo4j instances in the cluster. This is broadcasted to the other
# cluster members, so different members can have different communication ports.
# Optional if the members are on different machines so the IP is different for every member.
#ha.server = localhost:6001 # The interval at which slaves will pull updates from the master. Comment out
# the option to disable periodic pulling of updates. Unit is seconds.
ha.pull_interval = 10 # The session timeout for the zookeeper client. Lower values make new master
# election happen closer to the master loosing connection but also more sensitive
# to zookeeper quorum hiccups. If experiencing master switches without reason
# consider increasing this value. Unit is seconds
#ha.zk_session_timeout = 5 # Amount of slaves the master will try to push a transaction to upon commit (default is 1).
# The master will optimistically continue and not fail the transaction even if it fails to
# reach the push factor. Setting this to 0 will increase write performance when writing
# through master but could potentially lead to branched data (or loss of transaction)
# if the master goes down.
#ha.tx_push_factor=1 # Strategy the master will use when pushing data to slaves (if the push factor is greater than 0).
# There are two options available "fixed" (default) or "round_robin". Fixed will start by
# pushing to slaves ordered by server id (highest first) improving performance since the
# slaves only have to cache up one transaction at a time.
#ha.tx_push_strategy=fixed # Enable this to be able to upgrade a store from 1.4 -> 1.5 or 1.4 -> 1.6
#allow_store_upgrade=true # Enable this to specify a parser other than the default one. 1.5, 1.6, 1.7 are available
#cypher_parser_version=1.6
java文件(neo4j示例文件修改而来)
package org.easypoint; import java.io.File;
import java.io.IOException;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
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;
import org.neo4j.kernel.impl.util.FileUtils; public class Learn1
{
private static final String DB_PATH = "target/neo4j-hello-db";
String greeting;
// START SNIPPET: vars
GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;
// END SNIPPET: vars // START SNIPPET: createReltype
private static enum RelTypes implements RelationshipType
{
KNOWS
}
// END SNIPPET: createReltype public static void main( final String[] args )
{
Learn1 hello = new Learn1();
hello.createDb();
hello.removeData();
hello.shutDown();
} void createDb()
{
clearDb();
// START SNIPPET: startDb
graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder( "target/database/learn1" )
.loadPropertiesFromFile(Learn1.class.getResource("/").getPath()+"neo4j.properties" )
.newGraphDatabase(); registerShutdownHook( graphDb );
// END SNIPPET: startDb // START SNIPPET: transaction
Transaction tx = graphDb.beginTx();
try
{
// Updating operations go here
// END SNIPPET: transaction
// START SNIPPET: addData
firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" ); relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
// END SNIPPET: addData // START SNIPPET: readData
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );
// END SNIPPET: readData greeting = ( (String) firstNode.getProperty( "message" ) )
+ ( (String) relationship.getProperty( "message" ) )
+ ( (String) secondNode.getProperty( "message" ) ); // START SNIPPET: transaction
tx.success();
}
finally
{
tx.finish();
}
// END SNIPPET: transaction
} private void clearDb()
{
try
{
FileUtils.deleteRecursively( new File( DB_PATH ) );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
} void removeData()
{
Transaction tx = graphDb.beginTx();
try
{
// START SNIPPET: removingData
// let's remove the data
firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
firstNode.delete();
secondNode.delete();
// END SNIPPET: removingData tx.success();
}
finally
{
tx.finish();
}
} 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
}
运行java文件,可以看到在target/database/下创建了一个learn1的数据库。

从java代码我们也可以看出,neo4j数据库主要依靠node,relationship和property来存储数据,利用relationship将各个node链接起来。
服务式:
1.简单暴力连接(使用jdbc):http://www.cnblogs.com/hwaggLee/p/5956541.html
2.使用JERSEY
添加依赖jar
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-project</artifactId>
<version>1.17</version>
</dependency> <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency> <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.17</version>
</dependency> <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.17</version>
</dependency> <dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17</version>
</dependency>
新建java类Learn1Rest
package org.easypoint; import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.Client;
import java.net.URI;
import javax.ws.rs.core.MediaType;
/**
* 服务式neo4j连接
*/
public class Learn1Rest {
public static void main(String args[]){
Learn1Rest lr = new Learn1Rest();
URI firstNode = lr.createNode();
lr.addProperty( firstNode, "name", "Joe Strummer" );
URI secondNode = lr.createNode();
lr.addProperty( secondNode, "band", "The Clash" ); } public URI createNode(){
String SERVER_ROOT_URI = "http://61.xxx.xxx.xx:7474/db/data/";
final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
WebResource resource = Client.create().resource(nodeEntryPointUri); ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
.type(MediaType.APPLICATION_JSON)
.entity("{}")
.post(ClientResponse.class);
final URI location = response.getLocation();
System.out.println( String.format("POST to [%s], status code [%d], location header [%s]",
nodeEntryPointUri, response.getStatus(), location.toString() ) );
response.close();
return location; } public void addProperty(URI nodeUri,String propertyName, String propertyValue){
String propertyUri = nodeUri.toString() + "/properties/" + propertyName;
WebResource resource = Client.create()
.resource( propertyUri );
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
.type( MediaType.APPLICATION_JSON )
.entity( "\"" + propertyValue + "\"" )
.put( ClientResponse.class );
System.out.println( String.format( "PUT to [%s], status code [%d]",
propertyUri, response.getStatus() ) );
response.close(); }
}
执行成功

数据库查询:

neo4j-jersey分嵌入式和服务式连接图形数据库的更多相关文章
- Android应用程序窗口(Activity)与WindowManagerService服务的连接过程分析
在前两文中,我们分析了Activity组件的窗口对象和视图对象的创建过程.Activity组件在其窗口对象和视图对象创建完成之后,就会请求与WindowManagerService建立一个连接,即请求 ...
- Android应用程序与SurfaceFlinger服务的连接过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/7857163 前文在描述Android应用程序和 ...
- 原创:Equinox OSGi应用嵌入Jersey框架搭建REST服务
一.环境 eclipse版本:eclipse-luna 4.4 jre版本:1.8 二.Equinox OSGi应用嵌入Jersey框架搭建REST服务 1.新建插件工程HelloWebOSGI a. ...
- DLL模块例2:使用__declspec(dllexport)导出函数,extern "C"规范修饰名称,隐式连接调用dll中函数
以下内容,我看了多篇文章,整合在一起,写的一个例子,关于dll工程的创建,请参考博客里另一篇文章:http://www.cnblogs.com/pingge/articles/3153571.html ...
- CSS的三种样式:内联式,嵌入式,外部式以及他们的优先级
从CSS 样式代码插入的形式来看基本能够分为下面3种:内联式.嵌入式和外部式三种. 1:内联式css样式表就是把css代码直接写在现有的HTML标签中,如以下代码: <p style=" ...
- 打印机威胁:嵌入式Web服务有安全问题
现在大多数打印机.扫描仪,以及VoIP系统等设备都会内建嵌入式的Web服务,这主要是为了方便管理.然而不幸的是,这些设备大多会由于设置问题而处在无保护状态下.有些服务甚至可以使用默认的帐号和密码访问, ...
- ssh服务突然连接不了案例总结
ssh服务突然连接不了案例总结 一台Oracle数据库服务器(Linux版本为Oracle Linux Server release 5.7)今天中午突然出现短暂的ssh连接不上的情况,ssh连接 ...
- Equinox OSGi应用嵌入Jersey框架搭建REST服务
原文地址:https://www.cnblogs.com/kira2will/p/5040264.html 一.环境 eclipse版本:eclipse-luna 4.4 jre版本:1.8 二.Eq ...
- springcloud服务安全连接
Spring Cloud可以增加HTTP Basic认证来增加服务连接的安全性. 1.加入security启动器 在maven配置文件中加入Spring Boot的security启动器. <d ...
随机推荐
- jQuery Lightbox图片放大预览
简介:jQuery Lightbox图片放大预览代码是一款可以在用户点击页面中的小图片时,将该图片的高清版本以Lightbox的方式放大显示在页面的中间,提高用户的体验度. 效果展示 http://h ...
- PHP数组详解
作为一名C++程序员,在转做PHP开发的过程中,对PHP数组产生了一些混淆,与C++数组有相似的地方,也有一些不同,下面就全面地分析一下PHP的数组及其与C++中相应数据类型的区别和联系. 数组的分类 ...
- PHP基础知识第一趴
今天来贴一贴我的一张部分php基础知识的思维导图.未完,待续......慢慢'补枪'(为了让引号内的期望输出内容<strong>变成</strong>现实,应该使用双引号?那就 ...
- 浮动清除、before&after
::before 和 ::after属于伪元素,而 :before 和 :after属于伪类(CSS3 中为了区别伪元素和伪类为伪元素使用了双冒号)因此如果使用了 display 或者 width 等 ...
- Java接口响应超时监控
为什么要监控 服务化接口是提供服务的,接口正确性.稳定性是最最重要的,在保证正确的同时需要尽量提高接口响应时间. 有的团队会有专门的工具来对系统响应时间.吞吐量做监控,但如果团队没有这种"待 ...
- Java全角、半角字符的关系以及转换
如果搞明白了Java中全角字符和半角字符之间的关系,那他们之间的转换就不是个麻烦事儿.你只需要对这个关系有那么一个印象就足够了. 全角字符与半角字符的关系 通过下面的代码能看到Java中所有字符以及对 ...
- [dedecms]后台不显示验证码
原因:某个加载文件的开始处有一个标点,去掉就可显示 // 文件地址 /include/vdimgck.php @session_start(); $_SESSION['securimage_code_ ...
- Java设计模式——线程安全的单件模式
单件模式,也称单例模式,用以创建独一无二的.只能有一个实例的对象. 单件模式的类图是所有模式的类图中最简单的--只有一个类.尽管从类设计的视角来看单件模式很简单,但是实现上还是会遇到一些问题,本文着重 ...
- 浅谈时钟的生成(js手写代码)
在生成时钟的过程中自己想到布置表盘的写法由这么几种: 当然利用那种模式都可以实现,所以我们要用一个最好理解,代码有相对简便的方法实现 1.利用三角函数 用js在三角函数布置表盘的过程中有遇见到这种情况 ...
- java web 之客户关系管理系统
这个周末真的是觉得自己学会了一个比较高大上的本领,为什么这么觉得呢?那是因为星期六的时候觉得自己可以看看源码能做出来,可是让我头疼的是花费了一上午的时间还是没有弄出来,还好上天给了我机会,要是没有老师 ...