吴旭华 发表于 2024-9-18 01:36:27

如何将neo4j,4.x版本摆设到服务器上



一. 简介

当我们利用neo4j构建知识图谱时,我们希望让别人能和我们共用neo4j进行知识图谱的构建,我们的方法之一就是将neo4j摆设到我们的服务器上,然后将7474,7687端口暴袒露来,如许就可以通过访问服务器公网IP的7474端口来操作我们的数据库。
二. 服务器的设置

1. 起首我们须要有一个自己的服务器

推荐利用2核4G内存的服务器,因为在现实操作过程中B哥因为贪便宜利用2核2G的服务器踩了大坑——服务器内存完全不够用,纵然我仅仅创建了一个 47 节点的图谱,但是由于大量进程占用导致剩余空闲内存很少:https://i-blog.csdnimg.cn/direct/a8705a854e1e445ca71c7fc2a888209d.png
然后就会导致neo4j频繁的停止运行。如果我们利用2核4G的服务器来运行:https://i-blog.csdnimg.cn/direct/28daaa7063d14ed3a6d3256755046265.png
可以看到空闲内存云云之富裕,简直就是稳稳地幸福。固然作为赛博垃圾佬,购置服务器前B哥发起大家可以先去小黄鱼碰碰运气。
2. 服务器设置

然后系统什么的CentOS,Ubuntu随便选一个Linux的就行了,然后利用
java --version 查看jdk版本https://i-blog.csdnimg.cn/direct/4617451fd5414fc999450d87cf8b78ef.png
然后根据jdk版本来下载对应的neo4j版本:
jdk 8 - neo4j 3.x
jdk 11 - neo4j 4.x
其他的的忘记了自行去官网搜索。
3. 然后我们就可以下载neo4j了


[*]更新软件包 sudo apt update
sudo apt upgrade -y

[*]下载neo4j服务器版本安装包(因为是Linux操作系统) wget -O - https://debian.neo4j.com/neotechnology.gpg.key | sudo apt-key add -
echo 'deb https://debian.neo4j.com stable 4.4' | sudo tee /etc/apt/sources.list.d/neo4j.list
sudo apt update

[*]安装neo4j sudo apt install neo4j

4. 接下来我们须要去服务器安全组放行7474端口和7687端口https://i-blog.csdnimg.cn/direct/8c5e91f06b4a4d2a9e95681f973b47e6.png

三. neo4j设置

我们须要找到位于neo4j目录下的conf文件,然后利用vim编辑器打开,设置如下:


[*]打开解释:

[*]https://i-blog.csdnimg.cn/direct/1573f3580b02442d937e29b4bfc4cf72.png
[*]https://i-blog.csdnimg.cn/direct/42ea7ab96c2440e58ce93097bcb962fb.png
[*]https://i-blog.csdnimg.cn/direct/40d6dda9f3ca4602ae86b044057bb5cd.png

[*]修改https://i-blog.csdnimg.cn/direct/64f71cc53efe4ed8bf59acc1249fc665.png
然后运行
sudo systemctl start neo4j 查看状态: 
sudo systemctl status neo4j https://i-blog.csdnimg.cn/direct/76e89b156dc54d779e09c9544d5b53f2.png
如许就运行成功了。然后就可以利用欣赏器,以如下格式
http://服务器公网ip:7474/ 访问到neo4j,ui界面。然后登录密码和账号初始都是: neo4j
然后就可以看到neo4j UI界面:https://i-blog.csdnimg.cn/direct/0c21e0f989a147548aa0fa508dd45f85.png
四. 导入数据集

实在数据集有多种导入情势,我这里就导入xlsx了,我利用的是golang
package main

import (
        "fmt"
        "github.com/tealeg/xlsx"
        //"github.com/neo4j/neo4j-go-driver/v4/neo4j"

        "github.com/neo4j/neo4j-go-driver/v5/neo4j"
        "log"
)

// 定义数据结构

type Clause1 struct {
        Subject   string   `json:"subject"`
        Predicate string   `json:"predicate"`
        Objects   []Object `json:"object"`
}

type Object struct {
        Name string `json:"name"`
}

type Clause struct {
        Node1      string
        Relationship string
        Node2      string
}

func main() {
        uri := "bolt://服务器公网ip:7687"
        username := "neo4j用户名"
        password := "neo4j密码"

        ReadExcel(uri, username, password, "D:\\Projects\\Go_Projects\\MKdomain\\规划行业知识图谱设计demov2.1.xlsx")
}

func ReadExcel(uri, username, password, filepath string) {
        // 连接Neo4j
        driver, err := neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, ""))
        if err != nil {
                log.Fatalf("Failed to create driver: %v", err)
        }
        defer driver.Close()

        // 验证连接
        session := driver.NewSession(neo4j.SessionConfig{AccessMode: neo4j.AccessModeWrite})
        defer session.Close()

        // 读取Excel文件
        filePath := filepath
        xlFile, err := xlsx.OpenFile(filePath)
        if err != nil {
                log.Fatalf("无法打开数据集: %v", err)
        }
        sheet := xlFile.Sheets
        //len(sheet.Rows);
        for i := 1; i < len(sheet.Rows); i++ {
                var emptyRow = true
                var clause Clause
                for idx, cell := range sheet.Rows.Cells {
                        text := cell.String()
                        if text != "" {
                                emptyRow = false
                        }
                        switch idx {
                        case 0:
                                clause.Node1 = text
                        case 1:
                                clause.Relationship = text
                        case 2:
                                clause.Node2 = text
                        }
                }

                if emptyRow {
                        log.Fatalf("the node1 is: %v, the node2 is: %v, the relationship is: %v, the cloumn is: %d.",
                                clause.Node1, clause.Node2, clause.Relationship, i)
                }

                cypher := `
                        MERGE (n1:Node {name: $node1})
                        MERGE (n2:Node {name: $node2})
                        MERGE (n1)-[:` + clause.Relationship + `]->(n2)
                `

                params := mapinterface{}{
                        "node1": clause.Node1,
                        "node2": clause.Node2,
                }

                // 写入数据库
                _, err := session.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) {
                        res, err := tx.Run(cypher, params)
                        if err != nil {
                                return nil, err
                        }
                        if res.Next() {
                                fmt.Println("数据插入完成")
                        }
                        return nil, res.Err()
                })

                if err != nil {
                        log.Fatalf("err: %v", err)
                }
        }
}
五. 一个小小的自我倾销

鉴于自己去导入然后生成知识图谱太过麻烦,于是博主开辟了一个知识图谱构建网站,只需上传三元组格式的数据集就可以生成想要的知识图谱了(固然灵动性会缺失一些,但是确实方便了许多):
 http://kd.mysuper.pro/ 
或者直接通过ip访问
http://139.9.74.218/
感谢支持。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 如何将neo4j,4.x版本摆设到服务器上