win7安装使用neo4j

编程

1、打开官网下载地址: https://neo4j.com/download/ ,点击“Download”进入下载页面,填写相关信息后点击“Download Desktop”下载,要把右侧出现的拷贝下来。

2、安装过程,略。安装成功后,双击桌面快捷图标,打开neo4j,界面如下:

3、点击“Add Database”->"Create a local graph"(如果提示输入key,把之前保存的key复制过来)。

graph name就是图数据库的名称,这里只能输入密码,不能输入用户名,用户名默认是neo4j。

4、点击“create”创建graph成功。点击新建好的graph的“start”,略等一会启动成功,点击“open”按钮,打开“Neo4j Browser”:

5、上图的效果,和在浏览器地址栏输入localhost:7474的效果是一样的,在顶部的输入框里,就可以按照官网的例子进行操作了,比如打开名为“movie”的graph

:play movie graph

下边后出现8页操作提示,根据提示一步一步操作就可以了。

6、java连接neo4j:

pom.xml分别引入bolt连接的jar包和http连接的jar包:

       <dependency>
            <groupId>org.neo4j.driver</groupId>
            <artifactId>neo4j-java-driver</artifactId>
            <version>4.0.0</version>
        </dependency>
        
         <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-jdbc-driver</artifactId>
            <version>4.0.0</version>
        </dependency> 

版本号可以不是4.0.0,但是这两个jar包的版本号最好一致,至少保证这两个jar包不能相互冲突。

连接neo4j的代码如下:

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Iterator;

import java.util.List;

import org.neo4j.driver.AuthTokens;

import org.neo4j.driver.Driver;

import org.neo4j.driver.GraphDatabase;

import org.neo4j.driver.Record;

import org.neo4j.driver.Result;

import org.neo4j.driver.Session;

import org.neo4j.driver.Transaction;

import org.neo4j.driver.Value;

import org.neo4j.driver.types.Node;

import org.neo4j.jdbc.Neo4jConnection;

public class AppTest

{

public static void main(String[] args) throws Exception {

try{

testBolt();

System.out.println("bolt is ok");

testJdbc();

System.out.println("jdbc is ok");

}catch(Exception e){

e.printStackTrace();

}

}

public static void testBolt() throws Exception {

String boltUri = "bolt://localhost:7687";

String user = "neo4j";

String password = "neo4j";

Driver driver = GraphDatabase.driver(boltUri, AuthTokens.basic(user, password));

Session session = driver.session();

Transaction tx = session.beginTransaction();

Result result = tx.run("MATCH (p:Person) RETURN p limit 3");

List<Record> list=result.list();

for(int i=0;i<list.size();i++){

Record r = list.get(i);

List<String> keys = r.keys();

for(String key: keys){

Value val = r.get(key);

Node node = val.asNode();

Iterator<String> it = node.keys().iterator();

while(it.hasNext()){

String field = it.next();

System.out.print(",field=" + field + ",value=" + node.get(field));

}

}

}

tx.close();

session.close();

driver.close();

}

public static void testJdbc() throws Exception {

Class.forName("org.neo4j.jdbc.Driver").newInstance();

Neo4jConnection con = (Neo4jConnection) DriverManager.getConnection("jdbc:neo4j:http://localhost:7474", "neo4j",

"neo4j");

try (Statement stmt = con.createStatement()) {

ResultSet rs = stmt.executeQuery("match (p:Person) return p limit 10");

while (rs.next()) {

System.out.println(rs.getString(1));

}

}

con.close();

}

}

引入的类和包名根据jar包版本自行修改。如果版本不一致可能会出现以下问题:

org.neo4j.driver.v1.exceptions.ServiceUnavailableException: SSL Connection terminated while receiving data. This can happen due to network instabilities, or due to restarts of the database.

at org.neo4j.driver.internal.security.TLSSocketChannel.channelRead(TLSSocketChannel.java:170)

at org.neo4j.driver.internal.security.TLSSocketChannel.unwrap(TLSSocketChannel.java:229)

at org.neo4j.driver.internal.security.TLSSocketChannel.runHandshake(TLSSocketChannel.java:132)

at org.neo4j.driver.internal.security.TLSSocketChannel.create(TLSSocketChannel.java:81)

at org.neo4j.driver.internal.security.TLSSocketChannel.create(TLSSocketChannel.java:73)

at org.neo4j.driver.internal.net.ChannelFactory.create(ChannelFactory.java:47)

at org.neo4j.driver.internal.net.SocketClient.start(SocketClient.java:124)

at org.neo4j.driver.internal.net.SocketConnection.startSocketClient(SocketConnection.java:92)

at org.neo4j.driver.internal.net.SocketConnection.<init>(SocketConnection.java:67)

at org.neo4j.driver.internal.net.SocketConnector.createConnection(SocketConnector.java:77)

at org.neo4j.driver.internal.net.SocketConnector.connect(SocketConnector.java:50)

at org.neo4j.driver.internal.net.pooling.SocketConnectionPool$ConnectionSupplier.get(SocketConnectionPool.java:204)

at org.neo4j.driver.internal.net.pooling.SocketConnectionPool$ConnectionSupplier.get(SocketConnectionPool.java:186)

at org.neo4j.driver.internal.net.pooling.BlockingPooledConnectionQueue.acquire(BlockingPooledConnectionQueue.java:96)

at org.neo4j.driver.internal.net.pooling.SocketConnectionPool.acquireConnection(SocketConnectionPool.java:137)

at org.neo4j.driver.internal.net.pooling.SocketConnectionPool.acquire(SocketConnectionPool.java:76)

at org.neo4j.driver.internal.DirectConnectionProvider.acquireConnection(DirectConnectionProvider.java:45)

at org.neo4j.driver.internal.NetworkSession.acquireConnection(NetworkSession.java:340)

at org.neo4j.driver.internal.NetworkSession.beginTransaction(NetworkSession.java:288)

at org.neo4j.driver.internal.NetworkSession.beginTransaction(NetworkSession.java:175)

Exception in thread "main" java.lang.NoSuchFieldError: NULL

at org.neo4j.driver.v1.Values.<clinit>(Values.java:82)

at org.neo4j.driver.v1.AuthTokens.basic(AuthTokens.java:66)

at org.neo4j.driver.v1.AuthTokens.basic(AuthTokens.java:48)

at com.sgcc.practice.AppTest.testBolt(AppTest.java:29)

at com.sgcc.practice.AppTest.main(AppTest.java:18)

org.neo4j.driver.v1.exceptions.ServiceUnavailableException: Connection to the database terminated. This can happen due to network instabilities, or due to restarts of the database

at org.neo4j.driver.internal.util.ErrorUtil.newConnectionTerminatedError(ErrorUtil.java:45)

at org.neo4j.driver.internal.async.HandshakeHandler.channelInactive(HandshakeHandler.java:83)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)

at org.neo4j.driver.internal.shaded.io.netty.handler.codec.ByteToMessageDecoder.channelInputClosed(ByteToMessageDecoder.java:377)

at org.neo4j.driver.internal.shaded.io.netty.handler.codec.ByteToMessageDecoder.channelInactive(ByteToMessageDecoder.java:342)

at org.neo4j.driver.internal.shaded.io.netty.handler.ssl.SslHandler.channelInactive(SslHandler.java:978)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)

at org.neo4j.driver.internal.shaded.io.netty.channel.ChannelInboundHandlerAdapter.channelInactive(ChannelInboundHandlerAdapter.java:75)

at org.neo4j.driver.internal.shaded.io.netty.handler.timeout.IdleStateHandler.channelInactive(IdleStateHandler.java:277)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.fireChannelInactive(AbstractChannelHandlerContext.java:224)

at org.neo4j.driver.internal.shaded.io.netty.channel.DefaultChannelPipeline$HeadContext.channelInactive(DefaultChannelPipeline.java:1354)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:245)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannelHandlerContext.invokeChannelInactive(AbstractChannelHandlerContext.java:231)

at org.neo4j.driver.internal.shaded.io.netty.channel.DefaultChannelPipeline.fireChannelInactive(DefaultChannelPipeline.java:917)

at org.neo4j.driver.internal.shaded.io.netty.channel.AbstractChannel$AbstractUnsafe$8.run(AbstractChannel.java:822)

at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)

at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)

at org.neo4j.driver.internal.shaded.io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)

at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)

at org.neo4j.driver.internal.shaded.io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:138)

at java.lang.Thread.run(Thread.java:745)

这些都不是代码问题,是jar包冲突导致的。

it is all.

以上是 win7安装使用neo4j 的全部内容, 来源链接: utcz.com/z/517599.html

回到顶部