文章目录
neo4j如何导入csv文件
以下演示的是带文件头的csv文件
1.把全部的csv文件导入到机器的/import文件夹内
2.执行导入语句可以不设置路径
Windows在bin目录的同级目录import
Linux在/var/lib/neo4j/import
--导入节点
LOAD CSV WITH HEADERS FROM "file:///person.csv" AS line CREATE(n:Person{id:line.personId,name:line.name})
--导入关系
--支持 toInt(),toFloat()函数
LOAD CSV WITH HEADERS FROM "file:///relation.csv" AS line
match (from{personId:line.targetId}),(to{personId:line.sourceId})
merge (from)-[r:关系{sourceId:line.sourceId,targetId:line.targetId,关系类型:line.关系类型,借债金额:toFloat(line.借债金额),借债时间:toInt(line.借债时间)}]->(to)
--给关系属性加新数据
match ()-[r]-() set r.开始时间 = toInt('20180101')
neo4j还支持很多的函数,可以查询官方的API
neo4j的查询
--最短路径查询{路径中不支持定义查询维度}
MATCH (n),(m) ,p=allShortestPaths((n)-[*]-(m)) where id(n)=91 and id(m)=42 RETURN p
--定义路径查询,存在即返回值,否则不返回
MATCH (n),(m) ,p=(n)-[r*..3]-(m) where id(n)=91 and id(m)=42 RETURN p
--关系筛选[以顶点xxxx开始,3度以内的全部关系满足才会返回数据]
MATCH p=(n)-[r*..3]-() where n.name = 'xxxx'
and all( x in relationships(p) where x.xxx = xxx)
return p
以上的关系筛选,基本可以满足属性筛选过滤的需求,并且返回值是无孤立点
Java查询
public class CypherNeo4jOperation {
private final static Logger logger = LoggerFactory.getLogger(CypherNeo4jOperation.class);
// NEo4j driver properties
private String URI;
private String USER;
private String PASSWORD;
private Driver driver;
private static Session session;
private StatementResult result;
private static volatile CypherNeo4jOperation instance = null;
public static Session getSession() {
return CypherNeo4jOperation.session;
}
public CypherNeo4jOperation() {
}
/**
* @param
* @return
*/
public CypherNeo4jOperation(String uri, String user, String password) {
this.URI = uri.trim();
this.USER = user.trim();
this.PASSWORD = password.trim();
this.driver = GraphDatabase.driver(URI, AuthTokens.basic(USER, PASSWORD));
this.session = driver.session();
}
/**
* @param
* @return
*/
public synchronized static final CypherNeo4jOperation getInstance(String bolt, String username, String password) {
if (instance == null) {
// 加锁
synchronized (CypherNeo4jOperation.class) {
// 这一次判断也是必须的,不然会有并发问题
if (instance == null) {
instance = new CypherNeo4jOperation(bolt, username, password);
}
}
}
logger.info("Neo4j status " + instance.toString());
return instance;
}
/**
* @param cypher neo4j查询语句
* @return String
* @Description: (返回查询结束的结果)
*/
public String cypherJson(String cypher) {
logger.info(cypher);
StatementResult result = session.run(cypher);
}