【Neo4j与知识图谱】Neo4j的常用语法与一个简单知识图谱构建示例

2023-11-10

在这里插入图片描述

一、Cypher基本语法

Neo4j是一种基于图形结构的NoSQL数据库,它采用了Cypher查询语言来查询和操作图形数据。下面是Neo4j中语法知识的详细总结和示例:

1.创建节点和关系

在Neo4j中,可以使用CREATE语句来创建节点和关系。下面是创建一个节点的示例:

CREATE (n:Person {name: 'John', age: 30})

这将创建一个标签为Person、属性为name和age的节点。

下面是创建一个关系的示例:

MATCH (a:Person {name: 'John'}), (b:Person {name: 'Jane'})
CREATE (a)-[:FRIENDS]->(b)

这将创建一个FROM节点a到TO节点b的FRIENDS关系。

2.查询节点和关系

在Neo4j中,可以使用MATCH语句来查询节点和关系。下面是查询所有节点的示例:

MATCH (n)
RETURN n

这将返回所有节点。

下面是查询两个节点之间关系的示例:

MATCH (a:Person {name: 'John'})-[r:FRIENDS]->(b:Person {name: 'Jane'})
RETURN r

这将返回FROM节点a到TO节点b的FRIENDS关系r。

3.更新节点和关系

在Neo4j中,可以使用SET语句来更新节点和关系的属性。下面是更新节点属性的示例:

MATCH (n:Person {name: 'John'})
SET n.age = 35

这将将节点n的age属性更新为35。

下面是更新关系属性的示例:

MATCH (a:Person {name: 'John'})-[r:FRIENDS]->(b:Person {name: 'Jane'})
SET r.since = '2022-01-01'

这将将FRIENDS关系r的since属性更新为2022年1月1日。

4.删除节点和关系

在Neo4j中,可以使用DELETE语句来删除节点和关系。下面是删除节点的示例:

MATCH (n:Person {name: 'John'})
DELETE n

这将删除所有名为John的Person节点。

下面是删除关系的示例:

MATCH (a:Person {name: 'John'})-[r:FRIENDS]->(b:Person {name: 'Jane'})
DELETE r

这将删除FROM节点a到TO节点b的所有FRIENDS关系。

二、小示例

使用Neo4j创建一个包含10个用户和20个商品以及它们之间的购买关系的知识图谱,并进行查询、修改等操作。

2.1 准备数据和创建实体和关系

我们需要准备好数据。我将用以下的CSV文件作为示例数据:

注意下面所有的文件都要放在数据库对应位置,如下图
在这里插入图片描述

users.csv

userID,name
user1,John Doe
user2,Jane Doe
user3,Bob Smith
user4,Emily Jones
user5,David Lee
user6,Samantha Brown
user7,Alice Johnson
user8,Michael Davis
user9,Kate Johnson
user10,Ryan Wilson

products.csv

productID,name,category,price
product1,iPhone,Electronics,999
product2,iPad,Electronics,599
product3,MacBook,Electronics,1299
product4,Kindle,E-Books,89
product5,Harry Potter Book,Books,10
product6,Game of Thrones Book,Books,12
product7,MacBook Pro,Electronics,1499
product8,Samsung TV,Electronics,799
product9,Sony Camera,Electronics,599
product10,Canon Printer,Electronics,199
product11,Lenovo Laptop,Electronics,799
product12,Microsoft Surface,Electronics,999
product13,Fitbit Watch,Wearable,149
product14,Apple Watch,Wearable,399
product15,Sony Headphones,Electronics,299
product16,Samsung Monitor,Electronics,199
product17,Nintendo Switch,Electronics,299
product18,Playstation 5,Electronics,499
product19,Xbox Series X,Electronics,499
product20,Bose Speakers,Electronics,349

其中,每个用户具有userIDname两个属性,每个商品具有productIDnamecategoryprice四个属性。

现在,我们将使用Neo4j的Cypher语言来创建这个知识图谱。首先,我们需要使用LOAD CSV命令将数据加载到Neo4j中。

LOAD CSV WITH HEADERS FROM "file:///users.csv" AS row
CREATE (:User {userID: row.userID, name: row.name})
phpCopy codeLOAD CSV WITH HEADERS FROM "file:///products.csv" AS row
CREATE (:Product {productID: row.productID, name: row.name, category: row.category, price: toFloat(row.price)})

接下来,我们将创建40条购买关系。我们可以使用MATCHCREATE命令来实现这一点。例如,以下命令创建了用户1购买了商品1的关系:

MATCH (u:User {userID: "user1"}), (p:Product {productID: "product1"})
CREATE (u)-[:PURCHASED]->(p)

我们可以重复执行上述命令,为每个用户和商品创建购买关系。例如,以下命令创建了用户2购买了商品3的关系:

MATCH (u:User {userID: "user2"}), (p:Product {productID: "product3"})
CREATE (u)-[:PURCHASED]->(p)

也可以使用CSV文件直接导入:

假设我们有一个名为 user_product.csv 的 CSV 文件,其中包含了用户和商品之间的购买关系。文件格式如下:

UserID,ProductID
user1,product1
user1,product2
user1,product3
user2,product1
user2,product2
user2,product4
user3,product2
user3,product3
user3,product4
user4,product3
user4,product4
user4,product5
user5,product1
user5,product3
user6,product2
user6,product3
user6,product4
user7,product1
user7,product3
user8,product2
user8,product3
user8,product5
user9,product1
user9,product3
user9,product4
user10,product2
user10,product5
user1,product11
user1,product12
user1,product13
user2,product11
user2,product12
user2,product14
user3,product12
user3,product13
user3,product14
user4,product13
user4,product14
user4,product15
user5,product11
user5,product13
user6,product20
user6,product13
user6,product14
user7,product11
user7,product13
user8,product20
user8,product13
user8,product15
user9,product10
user9,product13
user9,product14
user10,product12
user10,product15
user1,product9
user1,product7
user1,product3
user2,product19
user2,product17
user2,product18
user3,product16
user3,product15
user3,product13
user4,product17
user4,product20
user4,product10
user5,product7
user5,product9
user6,product8
user6,product10
user6,product16
user7,product17
user7,product18
user8,product20
user8,product3
user8,product5
user9,product10
user9,product9
user9,product7
user10,product8
user10,product6

现在我们要将这些关系导入到 Neo4j 中,我们可以使用 LOAD CSV 命令来实现:

LOAD CSV WITH HEADERS FROM "file:///user_product.csv" AS row
MATCH (u:User {userID:row.userID})
MATCH (p:Product {productID:row.productID})
CREATE (u)-[:PURCHASED]->(p);

上述命令首先使用 LOAD CSV 加载 user_product.csv 文件,然后使用 MATCH 命令将用户和商品节点匹配,并最终使用 CREATE 命令创建购买关系。

2.2 进行查询修改等操作

  1. 查询商品

为了查询我们的商品,我们可以使用以下Cypher语句:

MATCH (p:Product)
RETURN p

这将返回所有的商品节点。
在这里插入图片描述

  1. 查询用户

为了查询我们的用户,我们可以使用以下Cypher语句:

MATCH (u:User)
RETURN u

这将返回我们所有的用户节点。

  1. 查询购买关系

为了查询我们的购买关系,我们可以使用以下Cypher语句:

MATCH p=()-[r:PURCHASED]->() RETURN p

这将返回所有的购买关系。
在这里插入图片描述

接下来,让我们看一下如何修改我们的知识图谱中的数据。

  1. 修改商品名称

为了修改商品的名称,我们可以使用以下Cypher语句:

MATCH (p:Product)
WHERE p.productID = 'product1'
SET p.name = 'New_product1'
RETURN p

这将修改ID为“product1”的商品的名称,并返回修改后的节点。
在这里插入图片描述

删除购买关系

为了删除购买关系,我们可以使用以下Cypher语句:

MATCH (u:User)-[p:PURCHASED]->(pr:Product)
WHERE u.userID = 'user1' AND pr.productID = 'product2'
DELETE p

这将删除用户ID为“user1”和商品ID为“product2”的购买关系。

image-20230318112032419

上面就是一个知识图谱创建的知识和示例了,期待后面更多内容。
最后欢迎关注公众号【智能建造小硕】(分享计算机编程、人工智能、智能建造、日常学习、科研和写作经验等,欢迎大家关注交流。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Neo4j与知识图谱】Neo4j的常用语法与一个简单知识图谱构建示例 的相关文章

随机推荐

  • Spring6 框架学习

    Spring6 框架学习 1 Spring介绍 1 1 简介 2002年 Rod Jahnson 首次推出了 Spring 框架雏形interface21 框架 2004年3月24日 Spring 框架以 interface21 框架为基础
  • QObject: Cannot create children for a parent that is in a different thread

    一篇又臭又长的流水账 要看结论可以直接拉到最后 在一个项目中 需要使用串口接收外部的对射管状态 然后调用传感器 由于在之前的项目中 自制了一个带有UI的串口管理类 继承QDialog 最早在主线程中生成这个串口管理类 但是发现程序变得越来越
  • error Command failed with exit code 1.

    从传统JQuery转变到Node js最烦的就是一接手新项目就得npm install 浪费大量时间不说还总会报莫明其妙的错误 这次又遇到了一个error Command failed with exit code 1 因为它的相关处报的问
  • 100+国产大模型排行榜!部分超越ChatGPT-4

    国产大模型的发展速度惊人 至少说明在国内的显卡数量是足够多的 如果能集中资源 或许能快速跟进ChatGPT 不过 其中不少厂家号称已经超越ChatGPT 4 让人感到欣慰 觉得哪个好 评论区见
  • 【金融】新成立基金建仓时点、行业分布与市场行情关系探究

    需要进一步交流 获取数据和代码的同学欢迎私信奥 基于新成立基金建仓带入市场的巨量资金会推动市场行情这一逻辑 开展了一系列研究 首先提出了通过基金净值识别建仓行为 累计绝对值涨跌幅法 和通过基金 值识别建仓行为 法 的两种方法 在通过回顾历史
  • 一键修改分辨率bat_设置分辨率的批处理

    if computername name1 SETRES h800 v600 b32 f85 if computername name2 SETRES h1024 v768 b32 f85 if computername name3 SET
  • libevent服务端,单线程应用

    libevent版本 libevent 2 1 12 stable include
  • 数据库MySQL与SQLite

    常用数据库及Qt中的用法 一 常用数据库 数据库管理系统 DBMS 是旨在使用 检索和定义规则以验证和操作数据库中的数据的软件 有四种DBMS类型 关系型 面向对象型 分层型和网络型 有很多开源数据库 包括MySQL SQLite等 SQL
  • Android13 Windows11-VMware-Ubuntu 源码下载和全编译

    Windows11 VMware Ubuntu Android13 源码下载和全编译 官方教程文档 想用 Mac 编译源码的朋友基本可以放弃想法了 我试过了各种兼容错误 不得已用了 Windows 装 VMware Ubuntu 一 硬件配
  • 知道这20个正则表达式,能让你少写1,000行代码

    正则表达式 一个十分古老而又强大的文本处理工具 仅仅用一段非常简短的表达式语句 便能够快速实现一个非常复杂的业务逻辑 熟练地掌握正则表达式的话 能够使你的开发效率得到极大的提升 正则表达式经常被用于字段或任意字符串的校验 如下面这段校验基本
  • python3 [爬虫入门实战]scrapy爬取盘多多五百万数据并存mongoDB

    总结 虽然是第二次爬取 但是多多少少还是遇到一些坑 总的结果还是好的 scrapy比多线程多进程强多了啊 中途没有一次被中断过 此版本是盘多多爬取数据的scrapy版本 涉及数据量较大 到现在已经是近500万的数据了 1 抓取的内容 主要爬
  • tensorrt的安装和使用

    安装 提前安装好 CUDA 和 CUDNN 登录 NVIDIA 官方网站下载和主机 CUDA 版本适配的 TensorRT 压缩包即可 以 CUDA 版本是 10 2 为例 选择适配 CUDA 10 2 的 tar 包 然后执行类似如下的命
  • rsync 远程同步

    Rsync 简介 rsync Remote Sync 远程同步 是一个开源的快速备份工具 可以在不同主机之间镜像同步整个目录树 支持增量备份 并保持链接和权限 且采用优化的同步算法 传输前执行压缩 因此非常适用于异地备份 镜像服务器等应用
  • auto 和 auto &是不一样的

    前言 什么时候用auto什么时候用auto 呢 先看代码testauto cpp include
  • unity 实现文本选中_Unity中如何读取TXT文本内容

    在游戏开发过程中 我们是离不开需求的 而需求的事情是由策划来做的 那么我们和策划也是需要沟通交流的 那么是怎么交流的呢 策划不需要写代码 只需要思考一些游戏的内容 数据 玩法的实现 然后让我们按照策划的东西来弄 所以和策划的沟通就是非常频繁
  • pgsql 自定义排序

    需求简述 用户要求查询数据表 使得输出结果指定中文字段chn name按照自定义的顺序 电 水 风 火等顺序排序 表内容 自定义排序sql 排序结果 工作中遇到的sql查询案例 如果有更简便的查询sql 欢迎多多交流
  • PlutoSDR学习指南【1】环境搭建+资料分享

    1 软件无线电及Pluto简介 软件无线电平台最通俗的语言来说 即通信系统中的功能采用软件实现 且可反复使用 比如简单的发射和接收信号 可以通过软件设置 信号的编码解码 可以通过软件设置 甚至你可以自己定义一种协议 用你自己的协议来实现一套
  • Python 已知 RSA 模数和指数,生成公钥进行加密

    在学习 js 加密的过程中 关于 RSA 加密知识有所接触 因此记录一下实际过程中遇到的问题 在这里我们主要讲解当没有公钥 已知公钥模数和指数的情况下 实现 RSA 加密 一 cryptography 包获取 RSA 公钥 首先需要安装 c
  • 【福利】Google Cloud Next ’23 精彩待发,Cloud Ace 作为联合赞助商提前发福利~

    Cloud Ace 是 Google Cloud 全球战略合作伙伴 在亚太地区 欧洲 南北美洲和非洲拥有二十多个办公室 Cloud Ace 在谷歌专业领域认证及专业知识目前排名全球第一位 并连续多次获得 Google Cloud 各类奖项
  • 【Neo4j与知识图谱】Neo4j的常用语法与一个简单知识图谱构建示例

    文章目录 一 Cypher基本语法 1 创建节点和关系 2 查询节点和关系 3 更新节点和关系 4 删除节点和关系 二 小示例 2 1 准备数据和创建实体和关系 2 2 进行查询修改等操作 一 Cypher基本语法 Neo4j是一种基于图形