1
更新: 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

配置

修改配置文件

clipboard.png

复制插件到plugins目录

clipboard.png

嵌入模式

添加Maven依赖

<dependency>
  <groupId>com.graphaware.neo4j</groupId>
  <artifactId>runtime</artifactId>
  <version>LATEST</version>
</dependency>
<dependency>
  <groupId>com.graphaware.neo4j</groupId>
  <artifactId>uuid</artifactId>
  <version>3.3.2.51.14</version>
</dependency>

然后在程序中如下使用

创建配置文件

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);
    }
}

参考资料


developerworks
1.7k 声望266 粉丝