一、Hugegragh安装和基本使用

Hugegragh安装和基本使用

版本:

hugeGraph server v 0.12.0

hugeGragh-hubble v 1.6.0

下述如有疑问可以进群探讨:QQ群:719751534

安装

下载

下载地址 Download · GitBook (hugegraph.github.io)

下载 hugeGraph server 和hugeGragh-hubble

image-20220426225847462

上传至服务器

解压后如图

image-20220426225945221

修改配置文件

# 默认使用rocksdb数据库
rocksdb.data_path=/opt/hugegragh/disk # 数据存放路径
rocksdb.wal_path=/opt/hugegragh/disk # 日志存放路径

image-20220426230037681

如果是服务器上安装的 请修改为服务ip,方便hubble连接操作

image-20220426230207738

启动

# 初始化
执行 init-store.sh
# 启动
执行 start-hugegraph.sh
# 关闭
执行 stop-hugegreaph.sh

image-20220426230305719

启动图形管理界面

修改访问地址和端口

image-20220426230602589

启动与关闭

# 启动
执行 start-hubble.sh
# 关闭
执行 stop-hubble.sh

image-20220426230635136

访问 ip:8088

基本使用

创建图

创建失败:

检查 rest-server.properties 文件中的地址是否正确

关闭 stop-hugegreaph.sh

重新执行 init-store.sh

启动 start-hugegreaph.sh

重新创建

image-20220427092942807

创建属性

image-20220427093439715

创建顶点

image-20220427095959816

创建边

image-20220427094056747

添加顶点

# 添加一个顶点
graph.addVertex(T.label,"star",T.id,1,"starId","1",'starName',"小明") 
graph.addVertex(T.label,"star",T.id,2,"starId","2",'starName',"小张") 
# 查询所有顶点
g.V()
# 查询所有边
g.E()
# 根据id删除顶点
g.V('1').drop()

相关文档 Gremlin 常用语法总结 - 大唐札记 (ittang.com)

image-20220427100128211

创建关系

from = g.V(1).next()
to = g.V(2).next()
from.addEdge("relation",to,"startRelation","朋友") # startRelation 跟视频中的starRelation 不同。小瑕疵不想重新建了 -.-

image-20220427100602399

代码测试

初试client

下拉client

https://github.com/hugegraph/hugegraph-client.git

修改 com.baidu.hugegraph.BaseClientTest 类中的连接信息

public class MyTest {

    @Before
    public void before() {
        BaseClientTest.open();
    }

    @Test
    public void one() {
        String gremlin = "g.V()";

        GremlinRequest gremlinRequest = new GremlinRequest(gremlin);

        ResultSet resultSet = BaseClientTest.gremlin().execute(gremlinRequest);

        resultSet.iterator().forEachRemaining(item -> {
            System.out.println(item.getString());
        });
        System.out.println("结束");
    }
}

image-20220427101612997

实战

json关系下载

ComplexNetwork/relation.json at master · xjtushilei/ComplexNetwork (github.com)

添加依赖

视频里面用的是fastjson,up用可以,我用就会出现classDefException等问题

所以改成了gson

    <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
编写代码

将relation.json放入resource目录下

 /**
     * 实战
     */
    @Test
    public void two() throws IOException {
        String dataJson = FileUtils.readFileToString(new File("E:\\gitee\\hugegraph-client\\src\\test\\resources\\relation.json"), StandardCharsets.UTF_8);
        Type type = new TypeToken<ArrayList<RelationVo>>() {
        }.getType();
        Gson gson = new Gson();
        ArrayList<RelationVo> arrayList = gson.fromJson(dataJson, type);
        String V = "graph.addVertex(T.label,\"star\",T.id,%s,\"starId\",\"%s\",'starName',\"%s\") ";
        String R = "from = g.V(%s).next()\n" +
                "to = g.V(%s).next()\n" +
                "from.addEdge(\"relation\",to,\"startRelation\",\"%s\")";
        arrayList.forEach(item -> {
            int id1 = item.getId1();
            int id2 = item.getId2();
            String name1 = item.getName1();
            String name2 = item.getName1();
            String relationship = item.getRelationship();

            String ev1 = String.format(V, id1, id1, name1);
            String ev2 = String.format(V, id2, id2, name2);
            String er = String.format(R, id1, id2, relationship);
            // 创建顶点
            executeGremlin(ev1);
            executeGremlin(ev2);
            // 添加关系
            executeGremlin(er);
        });

    }
   private void executeGremlin(String gremlin) {
//        System.out.println(gremlin);
        GremlinRequest gremlinRequest = new GremlinRequest(gremlin);
        BaseClientTest.gremlin().execute(gremlinRequest);
    }

执行后

image-20220428193758804

部分查询语句

# 赵薇所有人际关系
g.V(139401).repeat(out()).until(out().count().is(0)).path()
# 查询周杰伦的关系
g.V(129156).out().path()
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值