前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图计算Dgraph系列2-Schema操作

图计算Dgraph系列2-Schema操作

原创
作者头像
平常心
修改2020-11-16 10:00:21
1.1K0
修改2020-11-16 10:00:21
举报
文章被收录于专栏:个人总结系列个人总结系列

一. 环境准备

1. dgraph4j

代码语言:txt
复制
       <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>io.dgraph</groupId>
            <artifactId>dgraph4j</artifactId>
            <version>20.03.0</version>
        </dependency>

2.docker运行Dgraph

二. Schema的增删改

代码语言:txt
复制
public static DgraphClient getDgraphClient() {
    ManagedChannel channel =
            ManagedChannelBuilder.forAddress("localhost",9080).usePlaintext().build();

    DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);

    DgraphClient dgraphClient = new DgraphClient(stub);
    return  dgraphClient;
}

1.只创建Predicates

代码语言:txt
复制
String schema = "name: string @index(exact, term) .\n" +
                "boss_of: [uid] .\n" +
                "works_for: [uid] .\n" +
                "industry: string @index(term) .\n" +
                "work_here: [uid] ."
            ;
        Operation operation = Operation.newBuilder().setSchema(schema).build();
        dgraphClient.alter(operation);

2.修改Predicates

代码语言:txt
复制
//先删除,再添加,不然报 io.grpc.StatusRuntimeException: UNKNOWN: Type can't be changed from list to scalar for attr: [boss_of] without dropping it first.
dgraphClient.alter(Operation.newBuilder().setDropAttr("boss_of").build());
String schema =
        "boss_of: string .\n"
    ;
Operation operation = Operation.newBuilder().setSchema(schema).build();
dgraphClient.alter(operation);

3.只创建Types

代码语言:txt
复制
String schema =
        "type Person {\n" +
            "name\n" +
            "age\n" +
        "}"
    ;
Operation operation = Operation.newBuilder().setSchema(schema).build();
dgraphClient.alter(operation);

// 报异常,因为age没有在Predicates定义

String schema =
                "type Person {\n" +
                    "name\n" +
                    "boss_of\n" +
                "}"
            ;
        Operation operation = Operation.newBuilder().setSchema(schema).build();
        dgraphClient.alter(operation);

//如果Predicates 的boos_of删除,Person还是字段显示为2,但是在Change Type的时候,只有name被勾选。

在java API操作没有找到对应Types的操作,

4.创建Predicates和Types

代码语言:txt
复制
final DgraphClient dgraphClient = getDgraphClient();
//删除schema + 数据
dgraphClient.alter(Operation.newBuilder().setDropAll(true).build());

String schema =
        "industry: string @index(term) .\n" +
        "boss_of: [uid] .\n" +
        "name: string @index(exact, term) .\n" +
        "works_for: [uid] .\n" +
        "work_here: [uid] .\n" +

        "type Person {\n" +
        "    name\n" +
        "    boss_of\n" +
        "    works_for\n" +
        "}\n" +
        "type Company {\n" +
        "    name\n" +
        "    industry\n" +
        "    work_here \n" +
        "}"
    ;
Operation operation = Operation.newBuilder().setSchema(schema).build();
dgraphClient.alter(operation);

5.控制台查询

代码语言:txt
复制
schema(sch:[name, work_here, age, Person]) {
  type
  index
}
  • 查询不显示不存在的predicate, 如age;
  • Types查询不显示,如Person.

三.  go操作

1.创建scheam

代码语言:txt
复制
var (
	dgraph = flag.String("d", "localhost:9080", "Dgraph Alpha address")
)

func main() {
	client := getClient()
	createSchema(client)
}

func createSchema(client *dgo.Dgraph) error {
	schema := "name: string @index(term) . \n" +
		"age: int .\n" +
		"address: string .\n" +
		"type Person {\n" +
		"\tname\n" +
		"\tage\n" +
		"\taddress\n" +
		"}"

	ctx := context.Background()
	err :=  client.Alter(ctx, &api.Operation{Schema: schema})

	return err
}

2.修改schema

代码语言:txt
复制
func alterSchema(client *dgo.Dgraph) error {
	schema := "address: string @index(fulltext) . \n"
	ctx := context.Background()

	err :=  client.Alter(ctx, &api.Operation{Schema: schema})
	return err
}

3.删除schema

代码语言:txt
复制
func dropSchema(client *dgo.Dgraph) error {
	//err :=  client.Alter(context.Background(), &api.Operation{DropOp: api.Operation_DATA})
	//这种type操作没有成功,怎么删除Person
	//err :=  client.Alter(context.Background(), &api.Operation{DropOp: api.Operation_TYPE})
	//删除数据和schema
	err :=  client.Alter(context.Background(), &api.Operation{DropAll: true})
	return err
}

   // 怎么删除Type

   // 怎么删除Predicate

四. 总结

     对于Schema的操作,通过UI界面提供的功能操作比较方便,dgraph4j提供的java api操作不是很友好。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. 环境准备
    • 1. dgraph4j
      • 2.docker运行Dgraph
      • 二. Schema的增删改
        • 1.只创建Predicates
          • 2.修改Predicates
            • 3.只创建Types
              • 4.创建Predicates和Types
                • 5.控制台查询
                • 三.  go操作
                  • 1.创建scheam
                    • 2.修改schema
                      • 3.删除schema
                      • 四. 总结
                      相关产品与服务
                      容器服务
                      腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档