java连接neo4j
呼、博客要落灰了
记录一下Java连接neo4j的问题。
首先是neo4j的下载和配置,基本参考https://blog.csdn.net/appleyk/article/details/79091898
1、下载jdk
2、下载neo4j的community版本或者desktop版本 下载地址:https://neo4j.com/download-center/
3、配置环境变量 NEO4J_HOME=解压地址、path=%NEO4J_HOME%\bin;
4、验证是否安装成功:在cmd中运行neo4j.bat console
5、注册neo4j服务:neo4j install-service
6、注册后就可以使用neo4j start 来启动服务。
然后是在eclipse中新建maven项目
1、利用maven导入neo4j和java连接的包neo4j-java-driver
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.recom</groupId> <artifactId>recommender</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.neo4j.driver/neo4j-java-driver --> <dependency> <groupId>org.neo4j.driver</groupId> <artifactId>neo4j-java-driver</artifactId> <version>1.7.2</version> </dependency> </dependencies> </project>
2、编写类测试连接
public class App { public static void main(String[] args) { Driver driver=GraphDatabase.driver("bolt://localhost:7687",AuthTokens.basic("neo4j", "neo4j")); try(Session session=driver.session()){ try(Transaction transaction=session.beginTransaction()){ transaction.run("create(n:A1{NAME:{NAME},TITLE:{TITLE}})", Values.parameters("NAME","james","TITLE","King")); transaction.success(); } try(Transaction tx=session.beginTransaction()) { StatementResult result=tx.run("match(a:A1) WHERE a.NAME = {NAME} RETURN a.NAME AS NAME,a.TITLE AS TITLE", Values.parameters("NAME","james")); while (result.hasNext()) { Record record=result.next(); System.out.println(String.format("%s %s",record.get("TITLE").asString(),record.get("NAME").asString())); } } } driver.close(); } }
在测试时发现报以下错误 “The client is unauthorized due to authentication failure.”
解决方法:修改neo4j.conf配置文件,取消验证机制,修改如下:
将配置文件中的dbms.security.auth_enabled=false前的注释符号去掉