如何在java程序中引入neo4j数据库

如何在java程序中引入neo4j数据库,第1张

neo4j采纳java语言开发,如果我们要在java程序中以内嵌方式应用neo4j,只需导入neo4j的对应包即可。

首先,我们来创建一个maven项目并改动pom.xml添加对neo4j的依附。

<?xml version="1.0" encoding="UTF-8"?><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 "><modelVersion>4.0.0</modelVersion><groupId>neo4j-learn</groupId><artifactId>neo4j-learn</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.neo4j</groupId><artifactId>neo4j</artifactId><version>1.9.4</version></dependency></dependencies></project>

然后,我们在项目中创建一个neo4j.properties(数据库的配置文件)文件和一个java类(调用数据库)。

neo4j.properties

# Default values for the low-level graph engine #neostore.nodestore.db.mapped_memory=25M #neostore.relationshipstore.db.mapped_memory=50M #neostore.propertystore.db.mapped_memory=90M #neostore.propertystore.db.strings.mapped_memory=130M #neostore.propertystore.db.arrays.mapped_memory=130M # Autoindexing # Enable auto-indexing for nodes, default is false #node_auto_indexing=true # The node property keys to be auto-indexed, if enabled #node_keys_indexable=name,age # Enable auto-indexing for relationships, default is false #relationship_auto_indexing=true # The relationship property keys to be auto-indexed, if enabled #relationship_keys_indexable=name,age # Keep logical logs, needed for online backups to work keep_logical_logs=true # Enable online backups to be taken from this database. online_backup_enabled=true # Uncomment and specify these lines for running Neo4j in High Availability mode. # ha.server_id is a unique integer for each instance of the Neo4j database in the cluster. # (as opposed to the coordinator instance IDs) # example: ha.server_id=1 #ha.server_id= # ha.coordinators is a comma-separated list (without spaces) of the host:port of where to # find one or more of the Neo4j coordinator servers. # Avoid localhost due to IP resolution issues on some systems. # example: ha.coordinators=localhost:2181,1.2.3.4:4321 #ha.coordinators=localhost:2181 # You can also, optionally, configure the ha.cluster_name. This is the name of the cluster this # instance is supposed to join. Accepted characters are alphabetical, numerical, dot and dash. # This configuration is useful if you have multiple Neo4j HA clusters managed by the same # Coordinator cluster. # Example: ha.cluster_name = my.neo4j.ha.cluster #ha.cluster_name = # IP and port for this instance to bind to to communicate data with the # other neo4j instances in the cluster. This is broadcasted to the other # cluster members, so different members can have different communication ports. # Optional if the members are on different machines so the IP is different for every member. #ha.server = localhost:6001 # The interval at which slaves will pull updates from the master. Comment out # the option to disable periodic pulling of updates. Unit is seconds. ha.pull_interval = 10 # The session timeout for the zookeeper client. Lower values make new master # election happen closer to the master loosing connection but also more sensitive # to zookeeper quorum hiccups. If experiencing master switches without reason # consider increasing this value. Unit is seconds #ha.zk_session_timeout = 5 # Amount of slaves the master will try to push a transaction to upon commit (default is 1). # The master will optimistically continue and not fail the transaction even if it fails to # reach the push factor. Setting this to 0 will increase write performance when writing # through master but could potentially lead to branched data (or loss of transaction) # if the master goes down. #ha.tx_push_factor=1 # Strategy the master will use when pushing data to slaves (if the push factor is greater than 0). # There are two options available "fixed" (default) or "round_robin". Fixed will start by # pushing to slaves ordered by server id (highest first) improving performance since the # slaves only have to cache up one transaction at a time. #ha.tx_push_strategy=fixed # Enable this to be able to upgrade a store from 1.4 ->1.5 or 1.4 ->1.6 #allow_store_upgrade=true # Enable this to specify a parser other than the default one. 1.5, 1.6, 1.7 are available #cypher_parser_version=1.6

java文件(neo4j示例文件改动而来)

Neo4J的安装使用非常简单。下载一个稳定版本,解压,运行Neo4j.bat(windows版本) 即可。下载地址http://www.neo4j.org/download , 目前比较好用的稳定版本是1.9.1。

然后可以访问WEB管理界面,地址:http://localhost:7474/webadmin

Neo4J的使用方式包括:

1)可以写程序,添加,更新,用JAVA,Python,PHP, .NET等语言都可以实现。

2)可以用命令行,添加,更新,查看,Neo4J提供基于WEB的执行界面,提供类SQL语言执行,这些语言包括Cypher,Gremlin等。

如下图:

3)可以基于Neo4J提供的WEB UI界面添加,更新节点和关系,如下:

4) 可以使用附加工具(ETL)导入数据。也可以从关系数据库中导入数据。

APOC是Neo4j 3.3版本推出时正式推荐的一个Java用户扩展过程包,里面包含丰富的函数和过程,作为对Cypher所不能提供的复杂图算法和数据 *** 作功能的补充,APOC还具有使用灵活、高性能等优势。

APOC的安装:

启动neo4j,运行如下cypher,判断类型

return apoc.meta.type('hello') 返回 STRING ,注意返回值都是大写。

return apoc.meta.type(["hello", "world"]) 返回 LIST

create(n:Fruit{name:'apple', color:['red', 'green']})

match(n:Fruit) return apoc.meta.type(n.color) 返回 STRING[]

return apoc.meta.type(1) 返回 INTEGER

Tip:

如果出现上面的错误,是因为安装的时候没有修改配置文件

应用:

对Neo4j中的数据进行修改,将字符串数组压平为字符串,但是该属性中既有字符串,又有字符串数组,需要判断该属性是哪种数据类型,进行相应的 *** 作。Cypher自带的 size 函数,对于字符串返回的是字符串的长度,对于集合类型返回的是其中的元素个数。例如:

在前边 create(n:Fruit{name:'apple', color:['red', 'green']}) 的基础上 create(:Fruit{name:'banana', color:'yellow'})

查询 match(n:Fruit) return n.name, size(n.color)

使用apoc中的函数: apoc.meta.type()

查询 match(n:Fruit) return n.name, apoc.meta.type(n.color)

查找所有color属性为字符串数组类型的节点:

match(n:Fruit) where apoc.meta.type(n.color) = 'STRING[]' return n.name, n.color

此外 apoc.meta.typeName() 函数和 apoc.meta.type() 相同

压平:

对数据类型为字符串数组的属性值进行压平,中间用逗号隔开,逗号后边跟一个空格,末尾不带有括号。

create(n:Fruit{name:'grape', color:['purple', 'green', 'white']})

match(n:Fruit) where apoc.meta.type(n.color) = 'STRING[]' return substring(reduce(s='', x IN n.color | s + ', ' + x), 2)

这里使用到了Cypher自带的 reduce 函数。

若将color属性为字符串数组的,设置为字符串数组中的第一个元素:

match(n:Fruit) where apoc.meta.type(n.color) = 'STRING[]' set n.color = n.color[0]

连接:

参考:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/bake/11803403.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存