neo4j 简单使用

docker run -d --name container_name \  //-d表示容器后台运行 --name指定容器名字
	-p 7474:7474 -p 7687:7687 \  //映射容器的端口号到宿主机的端口号
	-v /home/neo4j/data:/data \  //把容器内的数据目录挂载到宿主机的对应目录下
	-v /home/neo4j/logs:/logs \  //挂载日志目录
	-v /home/neo4j/conf:/var/lib/neo4j/conf   //挂载配置目录
	-v /home/neo4j/import:/var/lib/neo4j/import \  //挂载数据导入目录
	--env NEO4J_AUTH=neo4j/password \  //设定数据库的名字的访问密码
	neo4j //指定使用的镜像

一个可以直接复制粘贴到终端执行的代码模板

docker run -d --name myneo4j -p 7474:7474 -p 7687:7687 -v /home/neo4j/data:/data -v /home/neo4j/logs:/logs -v /home/neo4j/conf:/var/lib/neo4j/conf -v /home/neo4j/import:/var/lib/neo4j/import --env NEO4J_AUTH=neo4j/password neo4j

其中container_name可以自己指定,挂载在根目录下的子目录可以根据你自己的实际情况进行替换,我这里是/home。另外NEO4J_AUTH也是你自己来进行设置。

neo4j

将数据复制到容器内部:

docker cp ./api.csv 67aaabf4f905:/var/lib/neo4j/import/

简单导入数据

zcy.csv, herber.csv,r_contain.csv

image-20211002171727319

image-20211002171743811

image-20211002171801162

method name playground apiresponse apiparams category apiroute apidescription content methodname
##导入实体
LOAD CSV WITH HEADERS  FROM "file:///api.csv" AS line
MERGE (z:api{name: line.name, method:line.method, playground: line.playground, apiresponse: line.apiresponse, apiparams: line.apiparams, category: line.category, apiroute: line.apiroute, apidescription: line.description, content: line.content, methodname: line.methodname})

##导入实体
LOAD CSV WITH HEADERS  FROM "file:///relations.csv" AS line
MERGE (z:relations{agrs1:line.relation1 , args2:line.rel ation2})

##导入关系第一种方法:
LOAD CSV WITH HEADERS FROM "file:///api_relation.csv" AS row  
match (from:api{name:row.api_name1}),(to:api{name:row.api_name2})  
merge (from)-[r:call{relation:row.relation , result :row.reslut}]->(to)

//存在空值时,使用create

删除节点

match (n:qianfu) delete n

删除关系

MATCH (Raul)-[r:rel_name]->(It)
DELETE r

关系建立

语法:

CREATE  
   (<node1-label-name>:<node1-name>)-
   [<relationship-label-name>:<relationship-name>]->
   (<node1-label-name>:<node1-name>)
RETURN <relationship-label-name>

案例:

CREATE (fb1:FaceBookProfile1)-[like:LIKES]->(fb2:FaceBookProfile2) 

查询

普通查询匹配

MATCH (a:api_name)-[r:call]->(b:api_name) where a.name contains 'amounts' RETURN a,r,b

match p=(a:api_name)-[*]-() where a.name contains 'am' return p

节点多深度关系

match p=(a:api_name)-[*..7]-(c:api_name) where a.name contains 'amou' return p

neo4j快速手册

https://www.jianshu.com/p/a217a0b5fb83

java 操作neo4j

package neo4j;/*
 * @Author ljc
 * @Description //TODO $
 * @Param $
 * @return $
 **/


import org.neo4j.driver.*;
import org.neo4j.driver.types.Entity;
import org.neo4j.driver.types.Node;
import org.neo4j.driver.types.Path;
import org.neo4j.driver.types.Relationship;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Hello world!
 *
 */
public class Neo4j
{
	public static void main(String[] args) throws Exception {
		try{
			System.out.println(testBolt("Da"));
			System.out.println("bolt is ok");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public static List<String> testBolt(String ask) throws Exception {
		List<String> arrayList = new ArrayList<>();
		List<String> myList ;
		String boltUri = "bolt://yourwebsite:7687";
		String user = "neo4j";
		String password = "admin";
		Driver driver = GraphDatabase.driver(boltUri, AuthTokens.basic(user, password));
		Session session = driver.session();
		Transaction tx = session.beginTransaction();

		//Result result = tx.run("match p=(a:api_name)-[*]-() where a.name contains '" + ask+ "' return distinct p");
		//match p=(a:api_name)-[*..4]-(b:api_name) where a.name contains 'Da'  RETURN DISTINCT p
		Result result = tx.run("MATCH p=(a:api_name)-[*..4]-(b:api_name) where a.name contains '" + ask+ "' return p");
		List<Record> list=result.list();
		System.out.println(list.size());
		for (Record r : list) {
			List<String> keys = r.keys();

			for (String key : keys) {
				Value val = r.get(key);
				System.out.println(val);
				Path path = val.asPath();
				System.out.println("-                    -");
				for (Path.Segment segment : path) {
					System.out.println(segment.start().get("name"));
					arrayList.add(segment.start().get("name").toString());
					System.out.println(segment.end().get("name"));
					arrayList.add(segment.end().get("name").toString());

				}
				System.out.println("------------  ---------");

			}
		}

		tx.close();
		session.close();
		driver.close();
		myList = arrayList.stream().distinct().collect(Collectors.toList());
		return myList;
	}

}

java neo4j所需依赖

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.neo4j.driver/neo4j-java-driver -->
        <dependency>
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>4.1.1</version>
        </dependency>

    </dependencies>
posted @ 2021-10-26 22:00  ament  阅读(179)  评论(0编辑  收藏  举报