protege和neo4j联系_Neo4j: 使用 GraphAware UUID 给新节点和关系自动添加UUID

更新: 2018-04-21

GraphAware 的UUID自动生成方式在结合Spring Data Neo4j使用的情况下, 当通过Neo4jRepository.save()创建节点的时候, 无法返回节点的UUID. 因为其UUID是作为事务回调(异步)中生成的, 无法同步返回.

该方式已经被Spring Data Neo4j 5.0的ID生成策略(UuidStrategy)替代.

import lombok.Getter;

import lombok.NonNull;

import lombok.Setter;

import org.neo4j.ogm.annotation.GeneratedValue;

import org.neo4j.ogm.annotation.Id;

import org.neo4j.ogm.annotation.NodeEntity;

import org.neo4j.ogm.annotation.typeconversion.Convert;

import org.neo4j.ogm.id.UuidStrategy;

import org.neo4j.ogm.typeconversion.UuidStringConverter;

import java.util.UUID;

@NodeEntity

@Getter

@Setter

public class User {

@Id

@GeneratedValue(strategy = UuidStrategy.class)

@Convert(UuidStringConverter.class)

@NonNull

private UUID id;

}

我们知道Neo4j的内部ID是会重用的, 因此无法使用其作为业务对象的唯一标识. 因此我们在这里给对象自动添加"uuid"属性作为唯一标识.

独立服务器

需要 GraphAware Neo4j Framework 和 GraphAware Neo4j UUID 两个jar包, 下载地址:

保证 graphaware-server-community-all-3.3.2.51.jar 和 graphaware-uuid-3.3.2.51.14.jar 两个库存在于 Neo4j 安装目录的 plugins 子目录之下. 并重启 Neo4j 服务器.

在conf/neo4j.conf配置文件中添加如下设置:

com.graphaware.runtime.enabled=true

#UIDM becomes the module ID:

com.graphaware.module.UIDM.1=com.graphaware.module.uuid.UuidBootstrapper

#optional, default is uuid:

com.graphaware.module.UIDM.uuidProperty=uuid

#optional, default is false:

com.graphaware.module.UIDM.stripHyphens=false

#optional, default is all nodes:

#com.graphaware.module.UIDM.node=hasLabel('Label1') || hasLabel('Label2')

#optional, default is no relationships:

#com.graphaware.module.UIDM.relationship=isType('Type1')

#com.graphaware.module.UIDM.relationship=com.graphaware.runtime.policy.all.IncludeAllBusinessRelationships

#optional, default is uuidIndex

com.graphaware.module.UIDM.uuidIndex=uuidIndex

#optional, default is uuidRelIndex

com.graphaware.module.UIDM.uuidRelationshipIndex=uuidRelIndex

配置

修改配置文件

1e479cd936973c07937e24d97c58a0df.png

复制插件到plugins目录

f5096ce4406e4a7159be75964479de2f.png

嵌入模式

添加Maven依赖

com.graphaware.neo4j

runtime

LATEST

com.graphaware.neo4j

uuid

3.3.2.51.14

然后在程序中如下使用

创建配置文件

dbms.logs.debug.level=DEBUG

com.graphaware.runtime.enabled=true

com.graphaware.module.UIDM.1=com.graphaware.module.uuid.UuidBootstrapper

com.graphaware.module.UIDM.uuidProperty=uuid

com.graphaware.module.UIDM.stripHyphens=false

com.graphaware.module.UIDM.uuidIndex=uuidIndex

com.graphaware.module.UIDM.uuidRelationshipIndex=uuidRelIndex

public void init() throws Exception {}

graphDb = new GraphDatabaseFactory()

.newEmbeddedDatabaseBuilder(DATABASE_DIRECTORY)

.setConfig(GraphDatabaseSettings.pagecache_memory, "512M")

.setConfig(GraphDatabaseSettings.string_block_size, "60")

.setConfig(GraphDatabaseSettings.array_block_size, "300")

.setConfig(GraphDatabaseSettings.store_internal_log_level, "DEBUG")

.loadPropertiesFromFile(path.toAbsolutePath().toString())

.setConfig(bolt.enabled, "true")

.setConfig(bolt.type, "BOLT")

.setConfig(bolt.listen_address, "0.0.0.0:7687")

.setConfig(bolt.encryption_level, BoltConnector.EncryptionLevel.OPTIONAL.toString())

.setConfig(ShellSettings.remote_shell_enabled, Settings.TRUE)

.newGraphDatabase();

registerFunction(graphDb, ga.uuid.NodeUuidFunctions.class);

registerFunction(graphDb, ga.uuid.RelationshipUuidFunctions.class);

Util.registerShutdownHook(graphDb);

GraphAwareRuntime runtime = GraphAwareRuntimeFactory.createRuntime(database); // database 是一个 GraphDatabaseService 实例

UuidModule module = new UuidModule("UUIDM", UuidConfiguration.defaultConfiguration());

runtime.registerModule(module);

runtime.start();

}

/**

* 注册用户定义函数

*

* @param db

* @param procedures

* @throws KernelException

*/

public static void registerFunction(GraphDatabaseService db, Class>... procedures) throws KernelException {

Procedures proceduresService = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Procedures.class);

for (Class> procedure : procedures) {

log.info("=== register function: {}", procedure.toString());

proceduresService.registerFunction(procedure);

}

}

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值