Schema 定义

更新时间:
复制为 MD 格式

Lindorm 图引擎在使用前必须先定义 Schema。本文介绍如何通过 HTTP API 完成 Schema 的初始化、属性扩展、新增顶点/边类型、查询以及子图数据清理。

说明

Lindorm 图引擎当前处于灰度阶段,请提交工单申请加白后再购买使用。

前提条件

  • 已开通 Lindorm 图引擎,并配置白名单。

  • 使用向量能力需要开通向量引擎。

首次初始化图 Schema

接口路径:/schema/mgmt/apply?db=${DB_NAME}。该接口用于首次定义子图的完整 Schema,包括顶点类型、边类型及其连接关系。系统默认创建一个名为 default 的子图,未使用多图功能时所有操作均作用在该子图上。

  • 请求格式

    curl -X POST "http://${SERVER_HOST}:${GREMLIN_PORT}/schema/mgmt/apply?db=${DB_NAME}" \
      -u ${SUB_USER}:${SUB_PASSWORD} \
      -H "Content-Type: application/json" \
      -d '{
        "vertexLabels": [...],
        "edgeLabels": [...],
        "connections": [...]
      }'
  • 参数说明

    参数

    说明

    SERVER_HOST

    服务器主机名(例如:localhost)。

    GREMLIN_PORT

    Gremlin 服务器端口(例如:16032)。

    DB_NAME

    数据库(子图)名称,默认 default

    SUB_USER

    认证用户名。

    SUB_PASSWORD

    认证密码。

  • Schema 结构定义

    • 不带向量检索能力

      以下示例定义了一个社交网络图 Schema,包含 personsoftware 两种顶点类型,以及 knowscreated 两种边类型:

      {
        "vertexLabels": [
          {
            "label": "person",
            "properties": [
              {"name": "name", "dataType": "STRING"},
              {"name": "age",  "dataType": "INT"},
              {"name": "city", "dataType": "STRING", "cardinality": "single"}
            ]
          },
          {
            "label": "software",
            "properties": [
              {"name": "name",  "dataType": "STRING"},
              {"name": "lang",  "dataType": "STRING"},
              {"name": "price", "dataType": "INT"}
            ]
          }
        ],
        "edgeLabels": [
          {
            "label": "knows",
            "properties": [
              {"name": "date",   "dataType": "STRING"},
              {"name": "weight", "dataType": "DOUBLE"}
            ]
          },
          {
            "label": "created",
            "properties": [
              {"name": "date",   "dataType": "STRING"},
              {"name": "weight", "dataType": "DOUBLE"}
            ]
          }
        ],
        "connections": [
          {"edgeLabel": "knows",   "outVertex": "person", "inVertex": "person"},
          {"edgeLabel": "created", "outVertex": "person", "inVertex": "software"}
        ]
      }
    • 带向量检索能力

      如果需要使用向量检索能力,可在顶点属性中加入 VECTOR_FLOAT 类型字段:

      {
        "vertexLabels": [
          {
            "label": "person",
            "properties": [
              {"name": "name", "dataType": "STRING"},
              {"name": "age",  "dataType": "INT"},
              {"name": "city", "dataType": "STRING", "cardinality": "single"},
              {
                "name": "embedding",
                "dataType": "VECTOR_FLOAT",
                "vectorMeta": {
                  "dimension": 128,
                  "distanceMethod": "EUCLIDEAN",
                  "indexType": "HNSW",
                  "indexParams": {
                    "M": "24",
                    "EF_CONSTRUCT": "200"
                  }
                }
              }
            ]
          },
          {
            "label": "software",
            "properties": [
              {"name": "name",  "dataType": "STRING"},
              {"name": "lang",  "dataType": "STRING"},
              {"name": "price", "dataType": "INT"}
            ]
          }
        ],
        "edgeLabels": [
          {
            "label": "knows",
            "properties": [
              {"name": "date",   "dataType": "STRING"},
              {"name": "weight", "dataType": "DOUBLE"}
            ]
          },
          {
            "label": "created",
            "properties": [
              {"name": "date",   "dataType": "STRING"},
              {"name": "weight", "dataType": "DOUBLE"}
            ]
          }
        ],
        "connections": [
          {"edgeLabel": "knows",   "outVertex": "person", "inVertex": "person"},
          {"edgeLabel": "created", "outVertex": "person", "inVertex": "software"}
        ]
      }
  • Schema 元素说明

    元素

    说明

    vertexLabels

    顶点类型定义数组,每个元素描述一种顶点。

    edgeLabels

    边类型定义数组,每个元素描述一种边。

    connections

    边连接关系数组。指定边的起点和终点类型

    label

    标签数组。

    properties

    属性定义数组。

    name

    属性字段名。

    dataType

    属性的数据类型。

    • STRING:字符串。

    • INT:32 位整数。

    • LONG:64 位整数。

    • DOUBLE:64 位浮点数。

    • FLOAT:32 位浮点数。

    • BOOLEAN布尔值。

    • VECTOR_FLOAT:浮点向量,需配合 vectorMeta 使用。取值参见下文vectorMeta参数说明。

      重要

      仅支持顶点,使用向量能力需要开通向量引擎,且联系研发进行后台配置

    cardinality

    属性值。

    • single(默认):单值属性,每个顶点该字段只存储一个值。

    • set:多值属性,每个顶点该字段可存储多个不重复的值,set 属性仅支持顶点;边的属性不支持。

    outVertex

    边的起点顶点标签。

    inVertex

    边的终点顶点标签。

  • 向量参数vectorMeta说明

    参数

    必填

    说明

    dimension

    向量维度。

    distanceMethod

    距离度量方式。默认 EUCLIDEAN(即 L2),可选 COSINE

    indexType

    索引类型,默认 HNSW

    indexParams

    索引构建参数(如 MEF_CONSTRUCT)。

为已有类型扩展属性

接口路径:POST /schema/mgmt/addProperty?db=${dbName}/schema/mgmt/apply 仅用于首次初始化,需要为已有顶点或边类型新增属性时请使用本接口。下文按属性类型给出四种典型用法:普通属性、set 多值属性、向量属性、为已有边扩展属性。

  • 添加普通属性

    • 请求格式:

      curl -X POST 'http://${host}:${port}/schema/mgmt/addProperty?db=default' \
        -H 'Content-Type: application/json' \
        -u '${user}:${password}' \
        -d '{
          "label": "person",
          "labelType": "vertex",
          "properties": [
            {"name": "email", "dataType": "STRING"},
            {"name": "score", "dataType": "DOUBLE"}
          ]
        }'
    • 响应示例:

      {
        "status": "200 OK",
        "payload": {
          "label": "person",
          "labelType": "vertex",
          "propertiesAdded": ["email", "score"]
        }
      }
  • 添加 set 属性(多值属性)

    set 属性支持一个字段存储多个值(如标签列表)。set 属性仅支持顶点,labelType 须为 vertex

    curl -X POST 'http://${host}:${port}/schema/mgmt/addProperty?db=default' \
      -H 'Content-Type: application/json' \
      -u '${user}:${password}' \
      -d '{
        "label": "person",
        "labelType": "vertex",
        "properties": [
          {"name": "tags", "dataType": "STRING", "cardinality": "set"}
        ]
      }'
  • 添加向量属性

    向量属性需要额外提供 vectorMeta 字段,系统会自动创建对应的向量索引表。向量属性仅支持顶点,labelType 须为 vertex

    curl -X POST 'http://${host}:${port}/schema/mgmt/addProperty?db=default' \
      -H 'Content-Type: application/json' \
      -u '${user}:${password}' \
      -d '{
        "label": "person",
        "labelType": "vertex",
        "properties": [
          {
            "name": "embedding",
            "dataType": "VECTOR_FLOAT",
            "vectorMeta": {
              "dimension": 128,
              "distanceMethod": "EUCLIDEAN",
              "indexType": "HNSW",
              "indexParams": {
                "M": "16",
                "EF_CONSTRUCT": "200"
              }
            }
          }
        ]
      }'
  • 为已有边类型扩展属性

    labelType 设为 edge 即可对边类型进行属性扩展。边不支持 set 属性和向量属性。

    • 请求格式:

      curl -X POST 'http://${host}:${port}/schema/mgmt/addProperty?db=default' \
        -H 'Content-Type: application/json' \
        -u '${user}:${password}' \
        -d '{
          "label": "knows",
          "labelType": "edge",
          "properties": [
            {"name": "since", "dataType": "LONG"}
          ]
        }'
    • 响应示例:

      {
        "status": "200 OK",
        "payload": {
          "label": "knows",
          "labelType": "edge",
          "propertiesAdded": ["since"]
        }
      }

添加新顶点类型

接口路径:POST /schema/mgmt/addVertexLabel?db=${dbName}。该接口在已有子图中创建一个新的顶点类型并定义其属性,系统会自动创建对应的底层物理表。

  • 请求格式:

    curl -X POST 'http://${host}:${port}/schema/mgmt/addVertexLabel?db=default' \
      -H 'Content-Type: application/json' \
      -u '${user}:${password}' \
      -d '{
        "label": "product",
        "properties": [
          {"name": "productName", "dataType": "STRING"},
          {"name": "price",       "dataType": "DOUBLE"},
          {"name": "category",    "dataType": "STRING"}
        ]
      }'
  • 响应示例:

    {
      "status": "200 OK",
      "payload": {
        "vertexLabelCreated": "product",
        "propertiesCount": 3
      }
    }

添加新边类型

接口路径:POST /schema/mgmt/addEdgeLabel?db=${dbName}。该接口在已有子图中创建一个新的边类型,并通过 connection 字段指定其起点和终点的顶点类型。引用的顶点类型必须已经存在,否则接口会返回错误。

  • 请求格式:

    curl -X POST 'http://${host}:${port}/schema/mgmt/addEdgeLabel?db=default' \
      -H 'Content-Type: application/json' \
      -u '${user}:${password}' \
      -d '{
        "label": "purchased",
        "properties": [
          {"name": "purchaseDate", "dataType": "STRING"},
          {"name": "quantity",     "dataType": "INT"}
        ],
        "connection": {
          "outVertex": "person",
          "inVertex": "product"
        }
      }'
  • 响应示例:

    {
      "status": "200 OK",
      "payload": {
        "edgeLabelCreated": "purchased",
        "propertiesCount": 2,
        "connectionRegistered": "person-purchased->product"
      }
    }

    响应中的 connectionRegistered 字段格式为 ${outVertex}-${edgeLabel}->${inVertex},表示已成功注册的边方向。

查询当前 Schema

接口路径:GET /schema/mgmt/list?db=${dbName}。该接口用于获取指定子图的完整 Schema 定义,包括所有顶点类型、边类型及其连接关系。所需权限,READDATA

  • 请求格式:

    curl -X GET 'http://${host}:${port}/schema/mgmt/list?db=default' \
      -u '${user}:${password}'
  • 响应示例:

    {
      "payload": {
        "edgeConnections": {
          "created": [
            "person",
            "software"
          ],
          "knows": [
            "person",
            "person"
          ]
        },
        "edgeLabels": {
          "created": [
            {
              "dataType": "STRING",
              "name": "date"
            },
            {
              "dataType": "DOUBLE",
              "name": "weight"
            }
          ],
          "knows": [
            {
              "dataType": "STRING",
              "name": "date"
            },
            {
              "dataType": "DOUBLE",
              "name": "weight"
            }
          ]
        },
        "schemaVersion": 0,
        "vertexLabels": {
          "software": [
            {
              "dataType": "STRING",
              "name": "name"
            },
            {
              "dataType": "STRING",
              "name": "lang"
            },
            {
              "dataType": "INT",
              "name": "price"
            }
          ],
          "person": [
            {
              "dataType": "STRING",
              "name": "name"
            },
            {
              "dataType": "INT",
              "name": "age"
            },
            {
              "dataType": "STRING",
              "name": "city"
            }
          ]
        }
      },
      "status": "200 OK"
    }

清空子图数据

接口路径:/db/del 。该接口用于快速清理子图,调用时需使用 root 账号。当传入的是系统默认子图 default 时,仅清空数据并删除其中的 Schema 定义,但 default 子图本身会保留;当传入的是其他子图时,整个子图会被一并删除。

curl -X GET 'http://${host}:${port}/db/del?db=default' \
  -u '${root_user}:${root_password}'