提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
Janusgraph学习记录(5)
前言
看到第六章,学习下书中的java代码。
一、使用步骤
1.连接数据库并做基础操作
创建cluster实例,连接到数据库,代码如下:
public static Cluster connectToDatabase() {
Cluster.Builder builder = Cluster.build();
builder.addContactPoint("192.168.171.77");
builder.port(8182);
return builder.create();
}
建立graphtraversalsource:
public static GraphTraversalSource getGraphTraversalSource(Cluster cluster) {
return traversal().
withRemote(
DriverRemoteConnection.using(
cluster
));
}
删除元素:
public static String deletePerson(GraphTraversalSource g) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name for the person to delete:");
String name = keyboard.nextLine();
//This returns a count of the vertices dropped
Long vertexCount = g.V().has("person", "first_name", name).
sideEffect(__.drop()).
count().
next();
return vertexCount.toString();
}
sideEffect(traversal):将被提供的遍历作为额外的过程进行处理,不影响传递给下一个操作的结果。
查找特定关系的元素:
public static String getFriends(GraphTraversalSource g) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the name for the person to find the friends of:");
String name = keyboard.nextLine();
//Returns a list of Objects representing the friend person vertex properties
List<Object> friends = g.V().has("person", "first_name", name).
out("friends").dedup().by("first_name").
values("first_name").
toList();
return StringUtils.join(friends, System.lineSeparator());
}
dedup():去除结果集中相同的元素,可通过by(property)来指定根据什么维度进行去重。
个人目前感觉用python还是更便捷一点。
总结
欢迎评论区交流一起进步。。