为什么
bin/gremlin.sh
的V().count()
不显示非零计数? docker run -d -p 8182:8182 janusgraph/janusgraph
g.addV()
和g.V().count().next()
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()
$ 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 服务器为您做,您需要使用
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()