neo4j部分资料:https://pan.baidu.com/s/1MDYGQqZqT7fRc0pKv5KTGw
一、java 环境配置
1、openJdk卸载
rpm -qa | grep java
java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5.el6_3.x86_64
java-1.7.0-openjdk-1.7.0.9-2.3.4.1.el6_3.x86_64
rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.50.1.11.5.el6_3.x86_64
rpm -e --nodeps java-1.7.0-openjdk-1.7.0.9-2.3.4.1.el6_3.x86_64
说明:
java jdk-7u7-i586与jdl-7u7-x64区别
jdk-7u7-i586 :文件名中的i586指定了这个jdk是适用于windows 32位系统的jdk,也就是说它只能在32位系统中生效。
jdl-7u7-x64:文件名中的X64指定了这个jdk是适用于windows 64位系统的jdk,它只能在64位系统中生效。
2、JDK安装
http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
jdk-7u79-linux-x64.tar.gz 下载
3、neo4j安装
neo4j官网
企业版
neo4j-enterprise-2.2.1-unix.tar.gz
解压:
tar zxvf neo4j-enterprise-2.2.1-unix.tar.gz
修改配置文件
neo4j.properties
# 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
修改配置文件
neo4j-server.properties
# Let the webserver only listen on the specified IP. Default is localhost (only
# accept local connections). Uncomment to allow any connection. Please see the
# security section in the neo4j manual before modifying this.
org.neo4j.server.webserver.address=0.0.0.0 //ip限制
dbms.security.auth_enabled=false //是否要登陆密码
http://172.16.70.161:7474/browser/
http://172.16.70.161:7474/webadmin/
neo4j操作详解
Neo4j 远程客户端库介绍
http://www.neo4j.org.cn/2012/07/27/tutorials-rest/
https://github.com/neo4j-contrib/java-rest-binding
java-rest-binding-master
maven项目导入
在项目中新建lib目录
run ->maven build
goals输入
dependency:copy-dependencies -DoutputDirectory=lib
后运行,将maven依赖包拷贝到本地
2.3.1. 在Java中如何使用REST API
http://www.neo4j.org.cn/2012/07/30/server-java-rest-client-example/#maodian001
rest Jersey 风格连接,有密码登录
public class neo4jTest {
final static String SERVER_ROOT_URI = "http://172.16.70.161:7474/db/data/";
public static void main(String[] args) {
// START SNIPPET: checkServer
WebResource resource = Client.create().resource(SERVER_ROOT_URI);
resource.addFilter(new HTTPBasicAuthFilter("neo4j", "123456"));//数据库用户密码
ClientResponse response = resource.get(ClientResponse.class);
if (response.getStatus() == 200) {
System.out.println("连接成功!");
} else {
System.out.println("连接失败!");
}
response.close();
}
}
{
"extensions": {},
"node": "http://172.16.70.161:7474/db/data/node",
"node_index": "http://172.16.70.161:7474/db/data/index/node",
"relationship_index": "http://172.16.70.161:7474/db/data/index/relationship",
"extensions_info": "http://172.16.70.161:7474/db/data/ext",
"relationship_types": "http://172.16.70.161:7474/db/data/relationship/types",
"batch": "http://172.16.70.161:7474/db/data/batch",
"cypher": "http://172.16.70.161:7474/db/data/cypher",
"indexes": "http://172.16.70.161:7474/db/data/schema/index",
"constraints": "http://172.16.70.161:7474/db/data/schema/constraint",
"transaction": "http://172.16.70.161:7474/db/data/transaction",
"node_labels": "http://172.16.70.161:7474/db/data/labels",
"neo4j_version": "2.2.1"
}
{"extensions" : { },"metadata" : {"id" : 111,"labels" : [ ]}
图形数据库Neo4J简介
http://www.cnblogs.com/loveis715/p/5277051.html
// 创建Sally这个Person类型的结点,该结点的name属性为Sally,age属性为32
CREATE INDEX ON :Person(nodeType)
CREATE (sally:Person { name: 'Sally', age: 32,nodeType:'Person' })
// 创建John结点
CREATE (john:Person { name: 'John', age: 27,nodeType:'Person' })
CREATE INDEX person ON :Person(name)
// 创建Graph Databases一书所对应的结点
CREATE (gdb:Book { title: 'Graph Databases',
authors: ['Ian Robinson', 'Jim Webber'] ,nodeType:'Book'})
// 在Sally和John之间建立朋友关系,这里的since值应该是timestamp。请自行回忆各位的日期是如何在关系数据库中记录的~~~
CREATE (sally)-[:FRIEND_OF { since: 1357718400 }]->(john)
// 在Sally和Graph Databases一书之间建立已读关系
CREATE (sally)-[:HAS_READ { rating: 4, on: 1360396800 }]->(gdb)
// 在John和Graph Databases一书之间建立已读关系
CREATE (john)-[:HAS_READ { rating: 5, on: 1359878400 }]->(gdb)
基本查询语句(方便图形数据库数据校验用):
例一:
START root=node(0) // Start with the reference node
RETURN root // and return it.
例二:
START n=node:Person(name = "Sally")
RETURN n
START n=node:node_auto_index (name = "Sally") RETURN n
例三:
START root=node:person(key='330127199107091811') match root-[relation:fuqin|muqin|qizi|nver|erzi|gege|xiong|sao|didi|yuefu|meifu|jiefu|yuemu|jiejie|meimei|gonggong|popo|qsgx|qtqs|tonghuo|tongjian|jbxd|duyou|tongshi|tongxiang|tongxue|xiaoyou|zhanyou|xueyou|wangyou|shisheng|lingju|zhugu|hhr|kehu|lianren|qingren|qianfu|qianqi|tar|pengyou|sfu|tdi|sxiong|sdi|sjie|smei|smei|
sunzi|sunvn|wsz|wsn|dixi|dsz|dsn|zfm|qfq
]-other RETURN other,relation,root
START root=node:person(key='330127199107091811') match root-[relation:*
]-other RETURN other,relation,root
MATCH (sally:Person { name: 'Sally' })
MATCH (john:Person { name: 'John' })
MATCH (sally)-[r:FRIEND_OF]-(john)
RETURN r.since as friends_since
MATCH (people:Person)
WHERE people.name = 'John' OR people.name = 'Sally'
MATCH (people)-[r:HAS_READ]->(gdb:Book { title: 'Graph Databases' })
RETURN people.name as first_reader
ORDER BY r.on
LIMIT 1
start people=node:Person(name='John')
match root-[r]-other return people,r,other;
MATCH (people:Person)
WHERE people.name = 'John'
MATCH (people)-[r]->(gdb:Book)
RETURN people,gdb,r
ORDER BY r.on
LIMIT 1
CREATE INDEX ON :Movie(title)
start root = node(1) match root-[r]-other
delete r,other,node
match (people:Person)
MATCH (people)-[r]->(gdb:Book)
delete r
match (people:Person)
MATCH (people)-[r]->(gdb:Person)
delete r
match (people:Person)
delete people
match (people:Book)
delete people
///START root=node:Person(name='cf421303') match root-[relation]-other RETURN other,relation,root
http://172.16.70.161:7474/db/data/node/22
http://172.16.70.161:7474/db/data/schema/index/Person
http://172.16.70.161:7474/db/data/schema/index/Person
POST http://localhost:7474/db/data/index/node/favorites
Accept: application/json
Content-Type: application/json
{
"value" : "some value",
"uri" : "http://localhost:7474/db/data/node/99",
"key" : "some-key"
}
Overview
Dashboard
Explore and edit
Data browser
Power tool
Console
Add and remove
Indexes
Details
Server info
start a = node:Person(name='Test0'),b = node:Person(name='Test10') return a,b;
match (people:Person) where people.name="Sally" match (people1:Person) where people1.name="cf421303" create (people)-[r:FRIEND_OF {type:"scholl"}]->(people1) return r;
不确定路径长度查询,把通过1条到10条变连接起来的节点查出来
start a = node:Person(name='Test0'),b = node:Person(name='Test10')
match a-[r*1..10]->b
return a,b,r;
最短路径
start a = node:Person(name='Test0'),b = node:Person(name='Test10')
match p=shortestPath( a-[r*..15]->b)
return p;
参考官方的visualization
neo4j可视化
http://neo4j.com/developer/guide-data-visualization/
http://www.cnblogs.com/visoncheng/p/4138749.html
java连接neo4j之jdbc
neo4j连接java目前主要有嵌入式、jdbc和rest api。
jdbc:需要使用到的lib包:neo4j-jdbc-2.0.1-SNAPSHOT-jar-with-dependencies.jar
jdbc连接的是服务式neo4j。
目前cypher语言提供的shorestPath方法仅仅支持计算两个节点间经过的节点数最小的路径,不支持关系之间的权重计算,如果需要计算权重的最短路径,则需要使用内嵌式,或者是服务式的rest API。
选中的三种布局
design.html
plugin-layouts-fruchtermanReingold.html
http://172.16.66.37:8080/Neo/linkurious/examples/load-neo4j-cypher-query.html