0

我必须创建模式,以便标签“City”有属性“id”。我必须在我创建的标签类型为“City”的每个节点上创建“id”的自动增量值。我正在使用 neo4j 的 PHP 库:

我已经在 Neo4j 中看到了这个任务 Auto Increment,但它没有办法通过 php 或特定策略来完成。

4

2 回答 2

0

这在技术上不是您要求的(因为它不是自动增量或数字),因为 neo4j 没有自动增量......但这就是我通过生成随机字符串来解决相同问题的方法身份证

$length如果您想要不同的默认长度,请更改,$tries仅用于防止无限循环...(此函数尝试创建 5 长度的唯一 ID 3 次,然后尝试创建 6 长 ID 等...

function makeNewID($client, $length = 5, $trys = 0)
   {
      //chars to pick from
      $charlist = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
      $strlen = strlen($charlist)-1;

      //alpha numeric generated in this for statement
      for ($newID = '', $i = 0; $i < $length; $newID .= $charlist[mt_rand(0, $strlen)], ++$i);
      //query the database to see if the 'new id exists'
      $query = new Everyman\Neo4j\Cypher\Query($client, 'MATCH (n{id:"'.$newID.'"}) RETURN n.id LIMIT 1');
      $result = $query->getResultSet();
      if(!empty($result[0]['x']->getProperty('id')))
      {
        $trys++;
        if($trys >= 3){$length++; $trys = 0;}
        $newID = makeNewID($Neo, $length, $trys);
      }
      return $newID;
   }

PS。我删除了代码中的抽象层,并用实际的 neo4jphp lib 代码替换了它,但我还没有测试它。

于 2014-05-19T00:50:06.430 回答
0

You could write an unmanaged extension in Java (or other JVM language) that uses a TransactionEventHandler to manage counters and deploy it to your Neo4j server.

于 2014-04-20T15:10:20.253 回答