<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>
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;
}
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);
//先删除,再添加,不然报 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);
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的操作,
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);
schema(sch:[name, work_here, age, Person]) {
type
index
}
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
}
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
}
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 删除。