Janusgraph学习记录(5)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

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还是更便捷一点。


总结

欢迎评论区交流一起进步。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值