以下是一个使用Gremlin查询来遍历整个图的示例代码:
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import org.apache.tinkerpop.gremlin.structure.Vertex;
public class GraphTraversalExample {
public static void main(String[] args) {
Cluster cluster = Cluster.build().addContactPoint("localhost").create();
Client client = cluster.connect();
try {
ResultSet resultSet = client.submit("g.V().hasLabel('person')");
while (resultSet.hasNext()) {
Vertex vertex = resultSet.next().getVertex();
System.out.println("Found person: " + vertex.property("name").value());
}
} finally {
client.close();
cluster.close();
}
}
}
上述代码示例假设你已经安装了Apache TinkerPop和Gremlin Java驱动程序。在此示例中,我们使用Gremlin查询“g.V().hasLabel('person')”来遍历整个图,并找到类型为“person”的所有顶点。然后,我们遍历结果集并输出每个人的名称。
请注意,上述代码示例假设你的图中有一个顶点属性“name”,用于存储人的名称。你可以根据你的图的结构和需求,调整Gremlin查询和输出的内容。