当前位置: 首页 > 工具软件 > 3DGraph > 使用案例 >

dgraph学习(一)

鱼渝
2023-12-01

schema

dgraph的节点分为两类
1、node节点
2、value字面量
例如:a b是两个人,S P O(三元组)

a friend b , a age 12 那么a b是两个节点 12是字面量 那么friend跟age都是谓语 friend指向b ,b是一个node ,age指向12 ,12是一个字面量 ,那么谓语所处的边为什么有时候指向一个node, 有时候指向一个字面量呢, 这就是由模式所定义的 或者说模式就是边的定义或者说模式就是谓语的定义。

添加模式

当项添加数据到已有的模式中可以直接添加 ,但是需要添加新数据到还没有存在模式中的时候 可以有两种方式

1、直接添加数据 让Dgraph自己找出其中的模式
2、先添加一个新的模式 然后添加数据
在模式中我们需要定义索引,因为dgraph的函数跟filter过滤器都是只可以应用于添加了索引的谓语 ,在模式中添加索引的话 那么各种查询进行不了

http方式

Alpha server 端口

1、/alter 用于修改表结构 包括删除数据库
2、/query 用户查询
3、/mutate 添加或修改数据
4、/commit 提交事务

curl -X POST http://127.0.0.1:8080/alter -d 'name: string @index(term) .'
# 返回 {"code":"Success","message":"Done"}
# 删除全库字段
curl -X POST http://127.0.0.1:8080/alter -d 'name: string @index(term) .'
# 删除全库 学习时候 先执行一下
curl -X POST http://127.0.0.1:8080/alter -d '{"drop_all": true}'

官方数据准备

# 添加数据库结构 用于后面搜索 
curl -X POST http://127.0.0.1:8080/alter -d $'
name: string @index(term) @lang .
age: int @index(int) .
friend: uid @count .
'
# -H "X-Dgraph-CommitNow: true" 代表事务自动提交
# 此节重点介绍查询,具体事务后面介绍。大体上 就是 mutate 后 用commit 提交
curl -X POST http://127.0.0.1:8080/mutate -H "X-Dgraph-CommitNow: true" -d $'
{
  set {
    _:michael <name> "Michael" .
    _:michael <age> "39" .
    _:michael <friend> _:amit .
    _:michael <friend> _:sarah .
    _:michael <friend> _:sang .
    _:michael <friend> _:catalina .
    _:michael <friend> _:artyom .
    _:michael <owns_pet> _:rammy .

    _:amit <name> "अमित"@hi .
    _:amit <name> "অমিত"@bn .
    _:amit <name> "Amit"@en .
    _:amit <age> "35" .
    _:amit <friend> _:michael .
    _:amit <friend> _:sang .
    _:amit <friend> _:artyom .

    _:luke <name> "Luke"@en .
    _:luke <name> "Łukasz"@pl .
    _:luke <age> "77" .

    _:artyom <name> "Артём"@ru .
    _:artyom <name> "Artyom"@en .
    _:artyom <age> "35" .

    _:sarah <name> "Sarah" .
    _:sarah <age> "55" .

    _:sang <name> "상현"@ko .
    _:sang <name> "Sang Hyun"@en .
    _:sang <age> "24" .
    _:sang <friend> _:amit .
    _:sang <friend> _:catalina .
    _:sang <friend> _:hyung .
    _:sang <owns_pet> _:goldie .

    _:hyung <name> "형신"@ko .
    _:hyung <name> "Hyung Sin"@en .
    _:hyung <friend> _:sang .

    _:catalina <name> "Catalina" .
    _:catalina <age> "19" .
    _:catalina <friend> _:sang .
    _:catalina <owns_pet> _:perro .

    _:rammy <name> "Rammy the sheep" .

    _:goldie <name> "Goldie" .

    _:perro <name> "Perro" .
  }
}
'

相当于json

{
    "set": [{
            "uid": "_:michael",
            "name": "Michael",
            "age": 39,
            "owns_pet": {
                "uid": "_:rammy",
                "name": "Rammy the sheep"
            },
            "friend": [{
                    "uid": "_:amit"
                },
                {
                    "uid": "_:sarah"
                },
                {
                    "uid": "_:sang"
                },
                {
                    "uid": "_:catalina"
                },
                {
                    "uid": "_:artyom"
                }
            ]
        },
        {
            "uid": "_:amit",
            "name@hi": "अमित",
            "name@bn": "অমিত",
            "name@en": "Amit",
            "age": 35,
            "friend": [{
                    "uid": "_:michael"
                },
                {
                    "uid": "_:sang"
                },
                {
                    "uid": "_:artyom"
                }
            ]
        },
        {
            "uid": "_:luke",
            "name@pl": "Łukasz",
            "name@en": "Luke",
            "age": 77
        },
        {
            "uid": "_:artyom",
            "name@ru": "Артём",
            "name@en": "Artyom",
            "age": 35
        },
        {
            "uid": "_:sarah",
            "name": "Sarah",
            "age": 55
        },
        {
            "uid": "_:sang",
            "name@ko": "상현",
            "name@en": "Sang Hyun",
            "age": 24,
            "owns_pet": {
                "uid": "_:goldie",
                "name": "Goldie"
            },
            "friend": [{
                    "uid": "_:amit"
                },
                {
                    "uid": "_:catalina"
                },
                {
                    "uid": "_:hyung"
                }
            ]
        },
        {
            "uid": "_:hyung",
            "name@en": "Hyung Sin",
            "name@ko": "형신",
            "friend": {
                "uid": "_:sang"
            }
        },
        {
            "uid": "_:catalina",
            "name": "Catalina",
            "age": 19,
            "owns_pet": {
                "uid": "_:perro",
                "name": "Perro"
            },
            "friend": {
                "uid": "_:sang"
            }
        }
    ]
}

查询schema

schema(pred: [name, age, friend, owns_pet]) {
  type
  index
}

输出 很显然下面的输出都是按照谓语一个一个输出这个谓语指向的那个的类型(是node还是字面量) 以及是否具有索引 如果为false就不输出

{
  "data": {
    "schema": [
      {
        "predicate": "age",
        "type": "int",
        "index": true
      },
      {
        "predicate": "friend",
        "type": "uid"
      },
      {
        "predicate": "name",
        "type": "string",
        "index": true
      },
      {
        "predicate": "owns_pet",
        "type": "uid"
      }
    ]
  },
  "extensions": {
    "server_latency": {},
    "txn": {
      "start_ts": 10457
    },
    "metrics": {
      "num_uids": {
        "_total": 0
      }
    }
  }
}

增加数据

增加schema

# Define Types

type Person {
    name
    boss_of
    works_for
}

type Company {
    name
    industry
    work_here #this is an alias
}

# Define Directives and index

industry: string @index(term) .
boss_of: [uid] .
name: string @index(exact, term) .
works_for: [uid] .
work_here: [uid] .

增加数据

{
  set {
    _:company1 <name> "CompanyABC" .
    _:company1 <dgraph.type> "Company" .
    _:company2 <name> "The other company" .
    _:company2 <dgraph.type> "Company" .

    _:company1 <industry> "Machinery" .

    _:company2 <industry> "High Tech" .

    _:jack <works_for> _:company1 .
    _:jack <dgraph.type> "Person" .

    _:ivy <works_for> _:company1 .
    _:ivy <dgraph.type> "Person" .

    _:zoe <works_for> _:company1 .
    _:zoe <dgraph.type> "Person" .

    _:jack <name> "Jack" .
    _:ivy <name> "Ivy" .
    _:zoe <name> "Zoe" .
    _:jose <name> "Jose" .
    _:alexei <name> "Alexei" .

    _:jose <works_for> _:company2 .
    _:jose <dgraph.type> "Person" .
    _:alexei <works_for> _:company2 .
    _:alexei <dgraph.type> "Person" .

    _:ivy <boss_of> _:jack .

    _:alexei <boss_of> _:jose .
  }
}

数据整合

需要查找到要整合的唯一标识uid,如果使用name等字段的话可能会出现,建立新的uid,将两次整合的数据建立联系 (/mutate)

{
  set {
    # <uid_of_michael> <boss_of> <uid_of_sarah> .
    # <uid_of_sarah> <works_for> <uid_of_company> .
  }
}

 类似资料: