为什么 V().count() 使用 gremlin.sh 显示 0 但使用代码不显示为零?

问题描述 投票:0回答:1

问题

为什么

bin/gremlin.sh
V().count()
不显示非零计数?
一般来说,我是 Gremlin [查询语言]Graph-DB(图形数据库) 的新手。 在 Java-Project 和 Gremlin-Console 中运行有什么区别?

繁殖

步骤

  1. 安装Docker
  2. 启动JanusGraph [Docker容器]
    docker run -d -p 8182:8182 janusgraph/janusgraph
  3. Java 项目中的程序运行
    g.addV()
    g.V().count().next()
  4. Command-Execute
    V().count().next()
    in
    gremlin.sh
    on the JanusGraph [Docker-container]
    docker exec -it [janusgraph.container.name] sh
    bin/gremlin.sh
    :remote connect tinkerpop.server conf/remote.yaml
    V.count()
    V.count().next()
    
  5. 结果
    1. Expect:
      V().count().next()
      returns 会返回相同的值
    2. 实际:
      V().count().next()
      gremlin.sh
      -返回返回
      count=0
$ ls
bin  conf  data  ext  lib  LICENSE.txt  logs  NOTICE.txt  scripts
$ ls bin
gremlin.bat  gremlin-server.bat  gremlin.sh  janusgraph-server.sh
$ bin/gremlin.sh

         \,,,/
         (o o)
-----oOOo-(3)-oOOo-----
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/janusgraph/lib/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/janusgraph/lib/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
plugin activated: tinkerpop.server
plugin activated: tinkerpop.tinkergraph
15:26:57 WARN  org.apache.hadoop.util.NativeCodeLoader - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
plugin activated: tinkerpop.hadoop
plugin activated: tinkerpop.spark
plugin activated: tinkerpop.utilities
plugin activated: janusgraph.imports
gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Configured localhost/127.0.0.1:8182
gremlin> V().count()
==>0
gremlin> V().count().next()
==>0
gremlin> :exit
$ 

代码

│   pom.xml
│
├───src
│   ├───main
│   │   ├───java
│   │   │       Main.java
│   │   │
│   │   └───resources
│   │       │   log4j2.xml
│   │       │
│   │       ├───conf
│   │       │       remote-graph.properties
│   │       │       remote-objects.yaml

Main.java

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Transaction;
import org.apache.tinkerpop.gremlin.structure.Vertex;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;

public class Main {
    private static final Logger logger = LogManager.getLogger(Main.class);
    public static void main(String[] args) throws Exception {
        GraphTraversalSource g = traversal().withRemote("conf/remote-graph.properties");
        Transaction tx = g.tx();
        tx.open();
        g.V().drop();
//        tx.commit();
//        tx = g.tx();
        Vertex v1 = g.addV("person").property("name","marko").next();
        Vertex v2 = g.addV("person").property("name","stephen").next();
        g.V(v1).addE("knows").to(v2).property("weight",0.75).iterate();
        tx.commit();
        logger.info("g.V().count().next():\t" + g.V().count().next());
        g.close();
    }
}

remote-graph.properties

gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection
gremlin.remote.driver.clusterFile=src/main/resources/conf/remote-objects.yaml
gremlin.remote.driver.sourceName=g

pom.xml

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>2.20.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tinkerpop</groupId>
            <artifactId>gremlin-core</artifactId>
            <version>3.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.janusgraph</groupId>
            <artifactId>janusgraph-core</artifactId>
            <version>0.6.4-20230429-020227.dc3f877</version>
        </dependency>
    </dependencies>
gremlin tinkerpop janusgraph tinkerpop3 gremlin-server
1个回答
0
投票

如果您明确地打开一个事务,而不是让 Gremlin 服务器为您做,您需要使用

tx.begin()
在该事务的上下文中将数据添加到图中。例如(这是 Python,但概念在 Java 中是相同的)


tx = g.tx()
gtx = tx.begin()

try:
    id1 = gtx.addV('tx-a1').next()
    print('Logic between transaction members')
    id2 = gtx.addV('tx-a2').next()
    tx.commit()
except Exception as e:
    print(e)
    tx.rollback()
© www.soinside.com 2019 - 2024. All rights reserved.