图计算Dgraph学习系列2-Schema操作

浏览: 48

一. 环境准备

1. dgraph4j

       <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的增删改

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

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);

image.png

2. 修改Predicates

//先删除,再添加,不然报 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);

image.png

3.只创建Types

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);

image.png

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

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


5.创建Predicates和Types

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);

image.png

image.png

控制台查询:

schema(sch:[name, work_here, age, Person]) {
type
index
}

image.png

1. 查询不显示不存在的predicate, 如age;

2. Types查询不显示,如Person.


三.  go操作

1. 创建scheam

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
}

image.png

image.png

2. 修改schema

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
}

image.png

3. 删除schema

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操作不是很友好。

推荐 0
本文由 平常心 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册