🌍neo4j
高性能NoSql图形数据库
环境要求
基本定义
1.图像数据结构
Nodes(节点)
Relationships(关系)
🐋docker安装部署
拉取最新的neo4j镜像
docker pull neo4j
运行Neo4j 容器
docker run -it -d -p 7474:7474 -p 7687:7687 neo4j:latest
打开Neo4j 浏览器管理界面
http://localhost:7474
设置data和import
data用于数据存放
import用于导入数据(csv文件)
docker run -it -d -p 7474:7474 -p 7687:7687 neo4j -v /home/user/Desktop/import:/var/lib/neo4j/import -v /home/user/Desktop/data:/data
🖥服务器部署
- 首先是下载解压 community版本
- 更改 /conf/neo4j.conf文件
# With default configuration Neo4j only accepts local connections.
# To accept non-local connections, uncomment this line:
#特别注意:默认是注释的,不开启,外网不能访问web端口neo4j
dbms.default_listen_address=0.0.0.0
# You can also choose a specific network interface, and configure a non-default
# port for each connector, by setting their individual listen_address.
# The address at which this server can be reached by its clients. This may be the server's IP address or DNS name, or
# it may be the address of a reverse proxy which sits in front of the server. This setting may be overridden for
# individual connectors below.
#dbms.default_advertised_address=localhost
# You can also choose a specific advertised hostname or IP address, and
# configure an advertised port for each connector, by setting their
# individual advertised_address.
# By default, encryption is turned off.
# To turn on encryption, an ssl policy for the connector needs to be configured
# Read more in SSL policy section in this file for how to define a SSL policy.
# Bolt connector
dbms.connector.bolt.enabled=true
#dbms.connector.bolt.tls_level=DISABLED
dbms.connector.bolt.listen_address=0.0.0.0:7687
#dbms.connector.bolt.advertised_address=:7687
# HTTP Connector. There can be zero or one HTTP connectors.
dbms.connector.http.enabled=true
dbms.connector.http.listen_address=0.0.0.0:7474
#dbms.connector.http.advertised_address=:7474
# HTTPS Connector. There can be zero or one HTTPS connectors.
dbms.connector.https.enabled=false
dbms.connector.https.listen_address=0.0.0.0:7473
#dbms.connector.https.advertised_address=:7473
- 启动neo4j
./bin/neo4j
参考资料
💨QuickStart
单个操作的流程(纯手动连接)
//设置driver
Driver driver = GraphDatabase.driver("bolt://192.168.216.131:7687", AuthTokens.basic("neo4j", "123456" ));
//打开session
Session session = driver.session();
//执行语句
String cql = "create (n:Person{name:$name,title:$title})";
String insert_test_str = session.writeTransaction(new TransactionWork<String>() {
@Override
public String execute(Transaction tx) {
Result result = tx.run(cql,parameters("name","diomchen","title","The Code Hero"));
return "Create Node OK!";
}
});
//关闭session
session.close();
//关闭driver
driver.close();
单个操作的流程(整合Springboot)
#首先是数据库的配置
neo4j:
uri: bolt://<host>:7687
username: neo4j
password: 123456
mongo数据库对象
@Data
@Document(collection = "addresstxs")
public class Addresstxs {
@Id
private String id;
@Field("address")
private String address;
@Field("height")
private Integer height;
@Field("index")
private Integer index;
@Field("txid")
private String txid;
@Field("type")
private Integer type;
}
neo4j数据库对象
@Data
@Node(labels = {"Addresstxs"})
public class NAddresstxs {
// //自动生成id
// @Id @GeneratedValue
// private Long id;
//交易地址表Id
@Id
private String addresstxsId;
//交易地址
@Property(name ="address")
private String address;
@Property(name="height")
private Integer height;
@Property(name="index")
private Integer index;
@Property(name="txid")
private String txid;
//交易类型:1.输入交易 2.输出交易 3.同一交易
@Property(name = "type")
private Integer type;
}
Dao层
@Repository
public interface AddresstxsRepository extends Neo4jRepository<NAddresstxs,String> {
@Query("create (n:Addresstxs{ addresstxsId:$addresstxsId , address:$address , type:$type}) return n")
List<NAddresstxs> addAddresstxs(@Param("addresstxsId") String addresstxsId, @Param("address") String address, @Param("type") Integer type);
@Query("match (n) return n")
List<NAddresstxs> getAddresstxs();
/*
特别需要注意返回的类型,否则会报以下问题
*/
}
解决办法:看看返回类型是否和 Neo4j Node 节点一致
Service层
public interface AddresstxsService { //创建Addresstxs节点 int createAddresstxsNode(Addresstxs addresstxs); //getAddresstxs List<NAddresstxs> getAddr();}@Service@Transactionalpublic class AddresstxsServiceImpl implements AddresstxsService{ @Resource private AddresstxsRepository addresstxsRepository; ......}
neo4j 3.x 和 4.x 版本存在较大不同
//java中cql(3.x)String cql = "create (n:Person{name:{name},title:{title}})";//java中cql(4.x)String cql = "create (n:Person{name:$name,title:$title})";
关于数据处理成可导入csv相关规范
- 一个数据集必须包含 node 和 relationship
- 每个文件头行都必须描述数据
- 即使是大规模数据,也尽可能将数据集分成多个文件
- 可为node定义label,键入关系作为可选参数
对于relationships,需要遵循下列三条规则
- START_ID
- END_ID
- TYPE
参考资料:
bin/neo4j-admin import --id-type=STRING --nodes import/executive.csv --nodes import/stock.csv --nodes import/concept.csv --nodes import/industry.csv --relationships import/executive_stock.csv --relationships import/stock_industry.csv --relationships import/stock_concept.csv
导入工具比较
参考资料:
LOAD CSV使用
:auto using periodic commit 200 load csv from "file:///t.csv" as linecreate (n:TA{id:line[0],address:line[1],height:line[2],index:line[3],txid:line[4],type:line[5]})//with HEADERS 可以避免读入header//但是不能使用 :auto using periodic commit 200
注意事项:
- 解决办法:在COMMOND前面加上:auto (一般使用using periodic commit会出现)
- 解决办法:将需要导入的csv文件上传至**/neo4j-xx.x/import**目录下面
apoc加载json数据
call apoc.load.json("file:///txt.json") yield valuemerge (n:TA{txid:value.txid})on create set n.height = value.height
merge 会查找是否存在该属性节点,没有则创建
参考链接:
Cypher语言
#创建节点create (a:person{name:"",age:"",gender:"" ... like:"")#建立关系match (a:Person),(b:Person) where a.like="football" and b.like="tennis" create (a)-[r:LOVE]->(b)#
参考资料
Neo4j小细节
neo4j节点不显示问题
参考链接:
关于导入导出的问题
一般采用apoc的loadCsv函数或者自带的LOAD CSV,对于其他开源插件一般不考虑,因为基本没人维护
参考链接:
🎈开发感受
- 在springboot框架中运用有两种,一个是自写query语句,另一个就是使用Neo4jTemplate
- 在导入大量数据的时候,导入csv肯定是最简单和最快的,json也行。网上开源的neo4j插件基本无人维护,都是几年前的版本,不推荐使用。
- apoc是neo4j里面最主流的插件,灵活好用