neo4j的理解

1.neo4j的安装

安装前先要装jdk

(1)neo4j-community-2.1.7-unix.tar.gz  解压即用

(2)neo4j-community-2.1.7/bin  

修改conf/neo4j-server.properties配置文件,将org.neo4j.server.webserver.address=0.0.0.0注释字符去掉

# With default configuration Neo4j only accepts local connections.
# To accept non-local connections, uncomment this line:
dbms.connectors.default_listen_address=0.0.0.0

 

启动: ./neo4j start

 

停止: ./neo4j stop

 

注:启动报错的解决办法:

http://neo4j.com/docs/1.6.2/configuration-linux-notes.html

 

2.neo4j服务器的配置

 

http://neo4j.com/docs/stable/server-configuration.html

 

3.neo4j常用cyhper语句

http://neo4j.com/docs/stable/query-functions-collection.html

 

4.遇见的常见问题

(1)程序中neo4j read timeout,一条cyhper语句耗时40000多ms,

(2)如何怎样改服务器的配置都不行,因为程序所导入的neo4j-rest-graphdb.jar中程序写死了getTimeout("connect_timeout", 30);

所以只能优化cyhper语句。

 

5.解决过的复杂cypher:如何查询某个节点下面的所有路径中,每条路径上的最近的渠道节点

start a=node:unique_node(uid={uid}) match p=a-[*]->b  where b.isChannel=true return extract(n in nodes(p) | n.uid) as path

结果:

[620806,621744]
[620806,621744,625098]
[620806,621744,625098,667825]

start a=node:unique_node(uid={uid}) match a-[*]->b  where b.isChannel=true return b.uid as uid

结果:

[621744,625098,667825]

 

根据上面两条结果,程序去处理,就能得到想到的结果 621744,思路是:每条路径中不能包括除自己之外的其它渠道结点。

 

从关系数据库中导入数据到neo4j

(1)先将数据从关系数据库中导出为cvs的格式(2)

(2)新建一个import_cvs.cypher的命令文件

格式如下:

USING PERIODIC COMMIT


LOAD CSV WITH HEADERS FROM "file:pmmerinf.csv" AS row


CREATE (:PMMERINF {MERCID:row.MERCID,MERCNAM:row.MERCNAM,MERCNUM:row.MERCNUM,
MERPHONENUMBER:row.MERPHONENUMBER,RECOMMENDATION:row.RECOMMENDATION,NOCARDAGTID:row.NOCARDAGTID});


CREATE INDEX ON :PMMERINF(MERCID);
CREATE INDEX ON :PMMERINF(MERPHONENUMBER);


USING PERIODIC COMMIT


LOAD CSV WITH HEADERS FROM "file:pmmerinf.csv" AS row


MATCH (pmerinf:PMMERINF {MERPHONENUMBER: row.MERPHONENUMBER})


MATCH (recommandtion:PMMERINF {RECOMMENDATION: row.RECOMMENDATION})


MERGE (recommandtion)-[:RECOMMEND]->(pmerinf);

 

(3)在neo4j-community-2.1.7/bin目录下执行如下命令即可

./neo4j-shell -path graph.db -file import_csv.cypher

 

 

(4)删除节点及关系

MATCH (a)-[r:`MAC_RECOMMEND`]->(b) delete a,b,r;

 

MATCH (a)-[r:`BIT_RECOMMEND`]->(b) WHERE b.uid=46 delete b,r

 

(5)创建两个节点

create (a:user{uid=12})-[:MAC_RECOMMEND]->(b:user{uid=13});

create (b:user{uid=12})-[:MAC_RECOMMEND]->(b:user{uid=13});

(6)创建关系

match (a:user),(b:user) where a.uid=12 and b.uid=13 create (a)-[r:MAC_RECOMMEND]->(b) return r;

你可能感兴趣的:(java)