资讯专栏INFORMATION COLUMN

Neo4j: 使用 GraphAware UUID 给新节点和关系自动添加UUID

genefy / 1398人阅读

摘要:更新的自动生成方式在结合使用的情况下当通过创建节点的时候无法返回节点的因为其是作为事务回调异步中生成的无法同步返回该方式已经被的生成策略替代我们知道的内部是会重用的因此无法使用其作为业务对象的唯一标识因此我们在这里给对象自动添加属性作为

更新: 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包, 下载地址:

下载页面
https://graphaware.com/produc...

框架库
http://products.graphaware.co...
UUID库
http://products.graphaware.co...

保证 graphaware-server-community-all-3.3.2.51.jargraphaware-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
配置 修改配置文件

复制插件到plugins目录

嵌入模式 添加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);
    }
}
参考资料

https://github.com/graphaware...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/17683.html

相关文章

  • 在选择数据库的路上,我们遇到过哪些坑?(2)

    摘要:有关进行调用的进一步危害,请观看这段有关安全漏洞的讨论。索引索引基本上会复制数据库中的信息片段,这样有利于它迅速找到节点。不管怎样,它都能事务性地依次通过数据库中的所有节点。 【编者按】你会怎么选择数据库,是关系数据库、XML 数据库、资源描述框架(RDF),还是图形数据库?本文的第1部分深入而生动地探讨了各种选择。在第2部分,将深入介绍使用 Neo4j 的注意点。文章系国内 ITOM...

    lavnFan 评论0 收藏0
  • Neo4j: 迁移MySQL的数据到Neo4j

    摘要:目的用于社交关系的管理和维护社交关系是一种网状的关系图难于维护这样的数据安装和配置需要满足下面几个条件安装插件安装驱动安装插件设置一下环境变量安装插件和驱动把复制到安装目录中的子目录下重启如何使用使用存储过程加载驱动执行查询 目的: 用于社交关系的管理和维护. 社交关系是一种网状的关系图, RDBMS难于维护这样的数据. 安装和配置 需要满足下面几个条件 安装 neo4j-apoc-...

    Chaz 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<