这是本节的多页打印视图。
点击此处打印 .
返回本页常规视图 .
API
1 - HugeGraph RESTful API HugeGraph-Server通过HugeGraph-API基于HTTP协议为Client提供操作图的接口,主要包括元数据和
图数据的增删改查,遍历算法,变量,图操作及其他操作。
1.1 - Schema API 1.1 Schema HugeGraph 提供单一接口获取某个图的全部 Schema 信息,包括:PropertyKey、VertexLabel、EdgeLabel 和 IndexLabel。
Method & Url GET http://localhost:8080/graphs/hugegraph/schemaResponse Status Response Body {
"propertykeys" : [
{
"id" : 7 ,
"name" : "price" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.741"
}
},
{
"id" : 6 ,
"name" : "date" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.729"
}
},
{
"id" : 3 ,
"name" : "city" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.691"
}
},
{
"id" : 2 ,
"name" : "age" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.678"
}
},
{
"id" : 5 ,
"name" : "lang" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.718"
}
},
{
"id" : 4 ,
"name" : "weight" ,
"data_type" : "DOUBLE" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.707"
}
},
{
"id" : 1 ,
"name" : "name" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.609"
}
}
],
"vertexlabels" : [
{
"id" : 1 ,
"name" : "person" ,
"id_strategy" : "PRIMARY_KEY" ,
"primary_keys" : [
"name"
],
"nullable_keys" : [
"age"
],
"index_labels" : [
"personByCity" ,
"personByAgeAndCity"
],
"properties" : [
"name" ,
"age" ,
"city"
],
"status" : "CREATED" ,
"ttl" : 0 ,
"enable_label_index" : true ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.783"
}
},
{
"id" : 2 ,
"name" : "software" ,
"id_strategy" : "PRIMARY_KEY" ,
"primary_keys" : [
"name"
],
"nullable_keys" : [
"price"
],
"index_labels" : [
"softwareByPrice"
],
"properties" : [
"name" ,
"lang" ,
"price"
],
"status" : "CREATED" ,
"ttl" : 0 ,
"enable_label_index" : true ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.840"
}
}
],
"edgelabels" : [
{
"id" : 1 ,
"name" : "knows" ,
"source_label" : "person" ,
"target_label" : "person" ,
"frequency" : "MULTIPLE" ,
"sort_keys" : [
"date"
],
"nullable_keys" : [
"weight"
],
"index_labels" : [
"knowsByWeight"
],
"properties" : [
"weight" ,
"date"
],
"status" : "CREATED" ,
"ttl" : 0 ,
"enable_label_index" : true ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:41.840"
}
},
{
"id" : 2 ,
"name" : "created" ,
"source_label" : "person" ,
"target_label" : "software" ,
"frequency" : "SINGLE" ,
"sort_keys" : [
],
"nullable_keys" : [
"weight"
],
"index_labels" : [
"createdByDate" ,
"createdByWeight"
],
"properties" : [
"weight" ,
"date"
],
"status" : "CREATED" ,
"ttl" : 0 ,
"enable_label_index" : true ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:41.868"
}
}
],
"indexlabels" : [
{
"id" : 1 ,
"name" : "personByCity" ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"index_type" : "SECONDARY" ,
"fields" : [
"city"
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:40.886"
}
},
{
"id" : 4 ,
"name" : "createdByDate" ,
"base_type" : "EDGE_LABEL" ,
"base_value" : "created" ,
"index_type" : "SECONDARY" ,
"fields" : [
"date"
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:41.878"
}
},
{
"id" : 5 ,
"name" : "createdByWeight" ,
"base_type" : "EDGE_LABEL" ,
"base_value" : "created" ,
"index_type" : "RANGE_DOUBLE" ,
"fields" : [
"weight"
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:42.117"
}
},
{
"id" : 2 ,
"name" : "personByAgeAndCity" ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"index_type" : "SECONDARY" ,
"fields" : [
"age" ,
"city"
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:41.351"
}
},
{
"id" : 3 ,
"name" : "softwareByPrice" ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "software" ,
"index_type" : "RANGE_INT" ,
"fields" : [
"price"
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:41.587"
}
},
{
"id" : 6 ,
"name" : "knowsByWeight" ,
"base_type" : "EDGE_LABEL" ,
"base_value" : "knows" ,
"index_type" : "RANGE_DOUBLE" ,
"fields" : [
"weight"
],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2021-09-03 15:13:42.376"
}
}
]
} 1.2 - PropertyKey API 1.2 PropertyKey Params说明:
name:属性类型名称,必填 data_type:属性类型数据类型,包括:bool、byte、int、long、float、double、string、date、uuid、blob,默认string类型 cardinality:属性类型基数,包括:single、list、set,默认single 请求体字段说明:
id:属性类型id值 properties:属性的属性,对于属性而言,此项为空 user_data:设置属性类型的通用信息,比如可设置age属性的取值范围,最小为0,最大为100;目前此项不做任何校验,只为后期拓展提供预留入口 1.2.1 创建一个 PropertyKey Method & Url POST http://localhost:8080/graphs/hugegraph/schema/propertykeysRequest Body {
"name" : "age" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE"
} Response Status Response Body {
"property_key" : {
"id" : 1 ,
"name" : "age" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [],
"status" : "CREATED" ,
"user_data" : {
"~create_time" : "2022-05-13 13:47:23.745"
}
},
"task_id" : 0
} 1.2.2 为已存在的 PropertyKey 添加或移除 userdata Params action: 表示当前行为是添加还是移除,取值为append(添加)和eliminate(移除) Method & Url PUT http://localhost:8080/graphs/hugegraph/schema/propertykeys/age?action=appendRequest Body {
"name" : "age" ,
"user_data" : {
"min" : 0 ,
"max" : 100
}
} Response Status Response Body {
"property_key" : {
"id" : 1 ,
"name" : "age" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [],
"status" : "CREATED" ,
"user_data" : {
"min" : 0 ,
"max" : 100 ,
"~create_time" : "2022-05-13 13:47:23.745"
}
},
"task_id" : 0
} 1.2.3 获取所有的 PropertyKey Method & Url GET http://localhost:8080/graphs/hugegraph/schema/propertykeysResponse Status Response Body {
"propertykeys" : [
{
"id" : 3 ,
"name" : "city" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
},
{
"id" : 2 ,
"name" : "age" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
},
{
"id" : 5 ,
"name" : "lang" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
},
{
"id" : 4 ,
"name" : "weight" ,
"data_type" : "DOUBLE" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
},
{
"id" : 6 ,
"name" : "date" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
},
{
"id" : 1 ,
"name" : "name" ,
"data_type" : "TEXT" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
},
{
"id" : 7 ,
"name" : "price" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"properties" : [],
"user_data" : {}
}
]
} 1.2.4 根据name获取PropertyKey Method & Url GET http://localhost:8080/graphs/hugegraph/schema/propertykeys/age其中,age为要获取的PropertyKey的名字
Response Status Response Body {
"id" : 1 ,
"name" : "age" ,
"data_type" : "INT" ,
"cardinality" : "SINGLE" ,
"aggregate_type" : "NONE" ,
"write_type" : "OLTP" ,
"properties" : [],
"status" : "CREATED" ,
"user_data" : {
"min" : 0 ,
"max" : 100 ,
"~create_time" : "2022-05-13 13:47:23.745"
}
} 1.2.5 根据name删除PropertyKey Method & Url DELETE http://localhost:8080/graphs/hugegraph/schema/propertykeys/age其中,age为要获取的PropertyKey的名字
Response Status Response Body 1.3 - VertexLabel API 1.3 VertexLabel 假设已经创建好了1.1.3中列出来的 PropertyKeys
Params说明
id:顶点类型id值 name:顶点类型名称,必填 id_strategy: 顶点类型的ID策略,主键ID、自动生成、自定义字符串、自定义数字、自定义UUID,默认主键ID properties: 顶点类型关联的属性类型 primary_keys: 主键属性,当ID策略为PRIMARY_KEY时必须有值,其他ID策略时必须为空; enable_label_index: 是否开启类型索引,默认关闭 index_names:顶点类型创建的索引,详情见3.4 nullable_keys:可为空的属性 user_data:设置顶点类型的通用信息,作用同属性类型 1.3.1 创建一个VertexLabel Method & Url POST http://localhost:8080/graphs/hugegraph/schema/vertexlabelsRequest Body {
"name" : "person" ,
"id_strategy" : "DEFAULT" ,
"properties" : [
"name" ,
"age"
],
"primary_keys" : [
"name"
],
"nullable_keys" : [],
"enable_label_index" : true
} Response Status Response Body {
"id" : 1 ,
"primary_keys" : [
"name"
],
"id_strategy" : "PRIMARY_KEY" ,
"name" : "person2" ,
"index_names" : [
],
"properties" : [
"name" ,
"age"
],
"nullable_keys" : [
],
"enable_label_index" : true ,
"user_data" : {}
} 从 hugegraph-server v0.11.2 版本开始支持顶点的 TTL 功能。顶点的 TTL 是通过 VertexLabel 来设置的。比如希望 person 类型的顶点存活时间为一天,需要在创建 person VertexLabel 的时候将 TTL 字段设置为 86400000,即单位为毫秒。
{
"name" : "person" ,
"id_strategy" : "DEFAULT" ,
"properties" : [
"name" ,
"age"
],
"primary_keys" : [
"name"
],
"nullable_keys" : [],
"ttl" : 86400000 ,
"enable_label_index" : true
} 另外,当顶点中带有"创建时间"的属性且希望以"创建时间"属性作为计算顶点存活时间的起点时,可以设置 VertexLabel 中的 ttl_start_time 字段。比如 person VertexLabel 有 createdTime 属性,且 createdTime 是 Date 类型的参数,希望 person 类型的顶点从创建开始存活一天的时间,那么创建 person VertexLabel 的 Request Body 如下:
{
"name" : "person" ,
"id_strategy" : "DEFAULT" ,
"properties" : [
"name" ,
"age" ,
"createdTime"
],
"primary_keys" : [
"name"
],
"nullable_keys" : [],
"ttl" : 86400000 ,
"ttl_start_time" : "createdTime" ,
"enable_label_index" : true
} 1.3.2 为已存在的VertexLabel添加properties或userdata,或者移除userdata(目前不支持移除properties) Params action: 表示当前行为是添加还是移除,取值为append(添加)和eliminate(移除) Method & Url PUT http://localhost:8080/graphs/hugegraph/schema/vertexlabels/person?action=appendRequest Body {
"name" : "person" ,
"properties" : [
"city"
],
"nullable_keys" : [ "city" ],
"user_data" : {
"super" : "animal"
}
} Response Status Response Body {
"id" : 1 ,
"primary_keys" : [
"name"
],
"id_strategy" : "PRIMARY_KEY" ,
"name" : "person" ,
"index_names" : [
],
"properties" : [
"city" ,
"name" ,
"age"
],
"nullable_keys" : [
"city"
],
"enable_label_index" : true ,
"user_data" : {
"super" : "animal"
}
} 1.3.3 获取所有的VertexLabel Method & Url GET http://localhost:8080/graphs/hugegraph/schema/vertexlabelsResponse Status Response Body {
"vertexlabels" : [
{
"id" : 1 ,
"primary_keys" : [
"name"
],
"id_strategy" : "PRIMARY_KEY" ,
"name" : "person" ,
"index_names" : [
],
"properties" : [
"city" ,
"name" ,
"age"
],
"nullable_keys" : [
"city"
],
"enable_label_index" : true ,
"user_data" : {
"super" : "animal"
}
},
{
"id" : 2 ,
"primary_keys" : [
"name"
],
"id_strategy" : "PRIMARY_KEY" ,
"name" : "software" ,
"index_names" : [
],
"properties" : [
"price" ,
"name" ,
"lang"
],
"nullable_keys" : [
"price"
],
"enable_label_index" : false ,
"user_data" : {}
}
]
} 1.3.4 根据name获取VertexLabel Method & Url GET http://localhost:8080/graphs/hugegraph/schema/vertexlabels/personResponse Status Response Body {
"id" : 1 ,
"primary_keys" : [
"name"
],
"id_strategy" : "PRIMARY_KEY" ,
"name" : "person" ,
"index_names" : [
],
"properties" : [
"city" ,
"name" ,
"age"
],
"nullable_keys" : [
"city"
],
"enable_label_index" : true ,
"user_data" : {
"super" : "animal"
}
} 1.3.5 根据name删除VertexLabel 删除 VertexLabel 会导致删除对应的顶点以及相关的索引数据,会产生一个异步任务
Method & Url DELETE http://localhost:8080/graphs/hugegraph/schema/vertexlabels/personResponse Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/1(其中"1"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.4 - EdgeLabel API 1.4 EdgeLabel 假设已经创建好了1.2.3中的 PropertyKeys 和 1.3.3中的 VertexLabels
Params说明
name:顶点类型名称,必填 source_label: 源顶点类型的名称,必填 target_label: 目标顶点类型的名称,必填 frequency:两个点之间是否可以有多条边,可以取值SINGLE和MULTIPLE,非必填,默认值SINGLE properties: 边类型关联的属性类型,选填 sort_keys: 当允许关联多次时,指定区分键属性列表 nullable_keys:可为空的属性,选填,默认可为空 enable_label_index: 是否开启类型索引,默认关闭 1.4.1 创建一个EdgeLabel Method & Url POST http://localhost:8080/graphs/hugegraph/schema/edgelabelsRequest Body {
"name" : "created" ,
"source_label" : "person" ,
"target_label" : "software" ,
"frequency" : "SINGLE" ,
"properties" : [
"date"
],
"sort_keys" : [],
"nullable_keys" : [],
"enable_label_index" : true
} Response Status Response Body {
"id" : 1 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "created" ,
"index_names" : [
],
"properties" : [
"date"
],
"target_label" : "software" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
],
"enable_label_index" : true ,
"user_data" : {}
} 从 hugegraph-server v0.11.2 版本开始支持边的 TTL 功能。边的 TTL 是通过 EdgeLabel 来设置的。比如希望 knows 类型的边存活时间为一天,需要在创建 knows EdgeLabel 的时候将 TTL 字段设置为 86400000,即单位为毫秒。
{
"id" : 1 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "knows" ,
"index_names" : [
],
"properties" : [
"date" ,
"createdTime"
],
"target_label" : "person" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
],
"enable_label_index" : true ,
"ttl" : 86400000 ,
"user_data" : {}
} 另外,当边中带有"创建时间"的属性且希望以"创建时间"属性作为计算边存活时间的起点时,可以设置 EdgeLabel 中的 ttl_start_time 字段。比如 knows EdgeLabel 有 createdTime 属性,且 createdTime 是 Date 类型的参数,希望 knows 类型的边从创建开始存活一天的时间,那么创建 knows EdgeLabel 的 Request Body 如下:
{
"id" : 1 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "knows" ,
"index_names" : [
],
"properties" : [
"date" ,
"createdTime"
],
"target_label" : "person" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
],
"enable_label_index" : true ,
"ttl" : 86400000 ,
"ttl_start_time" : "createdTime" ,
"user_data" : {}
} 1.4.2 为已存在的EdgeLabel添加properties或userdata,或者移除userdata(目前不支持移除properties) Params action: 表示当前行为是添加还是移除,取值为append(添加)和eliminate(移除) Method & Url PUT http://localhost:8080/graphs/hugegraph/schema/edgelabels/created?action=appendRequest Body {
"name" : "created" ,
"properties" : [
"weight"
],
"nullable_keys" : [
"weight"
]
} Response Status Response Body {
"id" : 2 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "created" ,
"index_names" : [
],
"properties" : [
"date" ,
"weight"
],
"target_label" : "software" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
"weight"
],
"enable_label_index" : true ,
"user_data" : {}
} 1.4.3 获取所有的EdgeLabel Method & Url GET http://localhost:8080/graphs/hugegraph/schema/edgelabelsResponse Status Response Body {
"edgelabels" : [
{
"id" : 1 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "created" ,
"index_names" : [
],
"properties" : [
"date" ,
"weight"
],
"target_label" : "software" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
"weight"
],
"enable_label_index" : true ,
"user_data" : {}
},
{
"id" : 2 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "knows" ,
"index_names" : [
],
"properties" : [
"date" ,
"weight"
],
"target_label" : "person" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
],
"enable_label_index" : false ,
"user_data" : {}
}
]
} 1.4.4 根据name获取EdgeLabel Method & Url GET http://localhost:8080/graphs/hugegraph/schema/edgelabels/createdResponse Status Response Body {
"id" : 1 ,
"sort_keys" : [
],
"source_label" : "person" ,
"name" : "created" ,
"index_names" : [
],
"properties" : [
"date" ,
"city" ,
"weight"
],
"target_label" : "software" ,
"frequency" : "SINGLE" ,
"nullable_keys" : [
"city" ,
"weight"
],
"enable_label_index" : true ,
"user_data" : {}
} 1.4.5 根据name删除EdgeLabel 删除 EdgeLabel 会导致删除对应的边以及相关的索引数据,会产生一个异步任务
Method & Url DELETE http://localhost:8080/graphs/hugegraph/schema/edgelabels/createdResponse Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/1(其中"1"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.5 - IndexLabel API 1.5 IndexLabel 假设已经创建好了1.1.3中的 PropertyKeys 、1.2.3中的 VertexLabels 以及 1.3.3中的 EdgeLabels
1.5.1 创建一个IndexLabel Method & Url POST http://localhost:8080/graphs/hugegraph/schema/indexlabelsRequest Body {
"name" : "personByCity" ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"index_type" : "SECONDARY" ,
"fields" : [
"city"
]
} Response Status Response Body {
"index_label" : {
"id" : 1 ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"name" : "personByCity" ,
"fields" : [
"city"
],
"index_type" : "SECONDARY"
},
"task_id" : 2
} 1.5.2 获取所有的IndexLabel Method & Url GET http://localhost:8080/graphs/hugegraph/schema/indexlabelsResponse Status Response Body {
"indexlabels" : [
{
"id" : 3 ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "software" ,
"name" : "softwareByPrice" ,
"fields" : [
"price"
],
"index_type" : "RANGE"
},
{
"id" : 4 ,
"base_type" : "EDGE_LABEL" ,
"base_value" : "created" ,
"name" : "createdByDate" ,
"fields" : [
"date"
],
"index_type" : "SECONDARY"
},
{
"id" : 1 ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"name" : "personByCity" ,
"fields" : [
"city"
],
"index_type" : "SECONDARY"
},
{
"id" : 3 ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"name" : "personByAgeAndCity" ,
"fields" : [
"age" ,
"city"
],
"index_type" : "SECONDARY"
}
]
} 1.5.3 根据name获取IndexLabel Method & Url GET http://localhost:8080/graphs/hugegraph/schema/indexlabels/personByCityResponse Status Response Body {
"id" : 1 ,
"base_type" : "VERTEX_LABEL" ,
"base_value" : "person" ,
"name" : "personByCity" ,
"fields" : [
"city"
],
"index_type" : "SECONDARY"
} 1.5.4 根据name删除IndexLabel 删除 IndexLabel 会导致删除相关的索引数据,会产生一个异步任务
Method & Url DELETE http://localhost:8080/graphs/hugegraph/schema/indexlabels/personByCityResponse Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/1(其中"1"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.6 - Rebuild API 1.6 Rebuild 1.6.1 重建IndexLabel Method & Url PUT http://localhost:8080/graphs/hugegraph/jobs/rebuild/indexlabels/personByCityResponse Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/1(其中"1"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.6.2 VertexLabel对应的全部索引重建 Method & Url PUT http://localhost:8080/graphs/hugegraph/jobs/rebuild/vertexlabels/personResponse Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/2(其中"2"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.6.3 EdgeLabel对应的全部索引重建 Method & Url PUT http://localhost:8080/graphs/hugegraph/jobs/rebuild/edgelabels/createdResponse Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/3(其中"3"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.7 - Vertex API 2.1 Vertex 顶点类型中的 Id 策略决定了顶点的 Id 类型,其对应关系如下:
顶点的 GET/PUT/DELETE API 中 url 的 id 部分传入的应是带有类型信息的 id 值,这个类型信息用 json 串是否带引号表示,也就是说:
当 id 类型为 number 时,url 中的 id 不带引号,形如 xxx/vertices/123456 当 id 类型为 string 时,url 中的 id 带引号,形如 xxx/vertices/“123456” 接下来的示例均假设已经创建好了前述的各种 schema 信息
2.1.1 创建一个顶点 Method & Url POST http://localhost:8080/graphs/hugegraph/graph/verticesRequest Body {
"label" : "person" ,
"properties" : {
"name" : "marko" ,
"age" : 29
}
} Response Status Response Body {
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : [
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" : [
{
"id" : "1:marko>age" ,
"value" : 29
}
]
}
} 2.1.2 创建多个顶点 Method & Url POST http://localhost:8080/graphs/hugegraph/graph/vertices/batchRequest Body [
{
"label" : "person" ,
"properties" : {
"name" : "marko" ,
"age" : 29
}
},
{
"label" : "software" ,
"properties" : {
"name" : "ripple" ,
"lang" : "java" ,
"price" : 199
}
}
] Response Status Response Body [
"1:marko" ,
"2:ripple"
] 2.1.3 更新顶点属性 Method & Url PUT http://127.0.0.1:8080/graphs/hugegraph/graph/vertices/"1:marko"?action=appendRequest Body {
"label" : "person" ,
"properties" : {
"age" : 30 ,
"city" : "Beijing"
}
} 注意:属性的取值是有三种类别的,分别是single、set和list。如果是single,表示增加或更新属性值;如果是set或list,则表示追加属性值。
Response Status Response Body {
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [
{
"id" : "1:marko>city" ,
"value" : "Beijing"
}
],
"name" : [
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" : [
{
"id" : "1:marko>age" ,
"value" : 30
}
]
}
} 2.1.4 批量更新顶点属性 功能说明 批量更新顶点的属性,并支持多种更新策略,包括
SUM: 数值累加 BIGGER: 两个数字/日期取更大的 SMALLER: 两个数字/日期取更小的 UNION: Set属性取并集 INTERSECTION: Set属性取交集 APPEND: List属性追加元素 ELIMINATE: List/Set属性删除元素 OVERRIDE: 覆盖已有属性,如果新属性为null,则仍然使用旧属性 假设原顶点及属性为:
{
"vertices" :[
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"name" : "lop" ,
"lang" : "java" ,
"price" : 328
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing" ,
"weight" : 0.1 ,
"hobby" :[
"reading" ,
"football"
]
}
}
]
} Method & Url PUT http://127.0.0.1:8080/graphs/hugegraph/graph/vertices/batchRequest Body {
"vertices" :[
{
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"name" : "lop" ,
"lang" : "c++" ,
"price" : 299
}
},
{
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"name" : "josh" ,
"city" : "Shanghai" ,
"weight" : 0.2 ,
"hobby" :[
"swiming"
]
}
}
],
"update_strategies" :{
"price" : "BIGGER" ,
"age" : "OVERRIDE" ,
"city" : "OVERRIDE" ,
"weight" : "SUM" ,
"hobby" : "UNION"
},
"create_if_not_exist" : true
} Response Status Response Body {
"vertices" : [
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "lop" ,
"lang" : "c++" ,
"price" : 328
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Shanghai" ,
"weight" : 0.3 ,
"hobby" : [
"swiming" ,
"reading" ,
"football"
]
}
}
]
} 结果分析:
lang 属性未指定更新策略,直接用新值覆盖旧值,无论新值是否为null; price 属性指定 BIGGER 的更新策略,旧属性值为328,新属性值为299,所以仍然保留了旧属性值328; age 属性指定 OVERRIDE 更新策略,而新属性值中未传入age,相当于age为null,所以仍然保留了原属性值32; city 属性也指定了 OVERRIDE 更新策略,且新属性值不为null,所以覆盖了旧值; weight 属性指定了 SUM 更新策略,旧属性值为0.1,新属性值为0.2,最后的值为0.3; hobby 属性(基数为Set)指定了 UNION 更新策略,所以新值与旧值取了并集; 其他的更新策略使用方式可以类推,不再赘述。
2.1.5 删除顶点属性 Method & Url PUT http://127.0.0.1:8080/graphs/hugegraph/graph/vertices/"1:marko"?action=eliminateRequest Body {
"label" : "person" ,
"properties" : {
"city" : "Beijing"
}
} 注意:这里会直接删除属性(删除key和所有value),无论其属性的取值是single、set或list。
Response Status Response Body {
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : [
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" : [
{
"id" : "1:marko>age" ,
"value" : 30
}
]
}
} 2.1.6 获取符合条件的顶点 Params label: 顶点类型 properties: 属性键值对(根据属性查询的前提是预先建立了索引) limit: 查询最大数目 page: 页号 以上参数都是可选的,如果提供page参数,必须提供limit参数,不允许带其他参数。label, properties和limit可以任意组合。
属性键值对由JSON格式的属性名称和属性值组成,允许多个属性键值对作为查询条件,属性值支持精确匹配和范围匹配,精确匹配时形如properties={"age":29},范围匹配时形如properties={"age":"P.gt(29)"},范围匹配支持的表达式如下:
查询所有 age 为 20 且 label 为 person 的顶点
Method & Url GET http://localhost:8080/graphs/hugegraph/graph/vertices?label=person&properties={"age":29}&limit=1Response Status Response Body {
"vertices" : [
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [
{
"id" : "1:marko>city" ,
"value" : "Beijing"
}
],
"name" : [
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" : [
{
"id" : "1:marko>age" ,
"value" : 29
}
]
}
}
]
} 分页查询所有顶点,获取第一页(page不带参数值),限定3条
Method & Url GET http://localhost:8080/graphs/hugegraph/graph/vertices?page&limit=3Response Status Response Body {
"vertices" : [{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"price" : [{
"id" : "2:ripple>price" ,
"value" : 199
}],
"name" : [{
"id" : "2:ripple>name" ,
"value" : "ripple"
}],
"lang" : [{
"id" : "2:ripple>lang" ,
"value" : "java"
}]
}
},
{
"id" : "1:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [{
"id" : "1:vadas>city" ,
"value" : "Hongkong"
}],
"name" : [{
"id" : "1:vadas>name" ,
"value" : "vadas"
}],
"age" : [{
"id" : "1:vadas>age" ,
"value" : 27
}]
}
},
{
"id" : "1:peter" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [{
"id" : "1:peter>city" ,
"value" : "Shanghai"
}],
"name" : [{
"id" : "1:peter>name" ,
"value" : "peter"
}],
"age" : [{
"id" : "1:peter>age" ,
"value" : 35
}]
}
}
],
"page" : "001000100853313a706574657200f07ffffffc00e797c6349be736fffc8699e8a502efe10004"
} 返回的body里面是带有下一页的页号信息的,"page": "001000100853313a706574657200f07ffffffc00e797c6349be736fffc8699e8a502efe10004",
在查询下一页的时候将该值赋给page参数。
分页查询所有顶点,获取下一页(page带上上一页返回的page值),限定3条
Method & Url GET http://localhost:8080/graphs/hugegraph/graph/vertices?page=001000100853313a706574657200f07ffffffc00e797c6349be736fffc8699e8a502efe10004&limit=3Response Status Response Body {
"vertices" : [{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [{
"id" : "1:josh>city" ,
"value" : "Beijing"
}],
"name" : [{
"id" : "1:josh>name" ,
"value" : "josh"
}],
"age" : [{
"id" : "1:josh>age" ,
"value" : 32
}]
}
},
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [{
"id" : "1:marko>city" ,
"value" : "Beijing"
}],
"name" : [{
"id" : "1:marko>name" ,
"value" : "marko"
}],
"age" : [{
"id" : "1:marko>age" ,
"value" : 29
}]
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"price" : [{
"id" : "2:lop>price" ,
"value" : 328
}],
"name" : [{
"id" : "2:lop>name" ,
"value" : "lop"
}],
"lang" : [{
"id" : "2:lop>lang" ,
"value" : "java"
}]
}
}
],
"page" : null
} 此时"page": null表示已经没有下一页了 (注: 后端为 Cassandra 时,为了性能考虑,返回页恰好为最后一页时,返回 page 值可能非空,通过该 page 再请求下一页数据时则返回 空数据 及 page = null,其他情况类似)
2.1.7 根据Id获取顶点 Method & Url GET http://localhost:8080/graphs/hugegraph/graph/vertices/"1:marko"Response Status Response Body {
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : [
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" : [
{
"id" : "1:marko>age" ,
"value" : 29
}
]
}
} 2.1.8 根据Id删除顶点 Params 仅根据Id删除顶点
Method & Url DELETE http://localhost:8080/graphs/hugegraph/graph/vertices/"1:marko"Response Status 根据Label+Id删除顶点
通过指定Label参数和Id来删除顶点时,一般来说其性能比仅根据Id删除会更好。
Method & Url DELETE http://localhost:8080/graphs/hugegraph/graph/vertices/"1:marko"?label=personResponse Status 1.8 - Edge API 2.2 Edge 顶点 id 格式的修改也影响到了边的 Id 以及源顶点和目标顶点 id 的格式。
EdgeId是由 src-vertex-id + direction + label + sort-values + tgt-vertex-id 拼接而成,
但是这里的顶点id类型不是通过引号区分的,而是根据前缀区分:
当 id 类型为 number 时,EdgeId 的顶点 id 前有一个前缀L ,形如 “L123456>1»L987654” 当 id 类型为 string 时,EdgeId 的顶点 id 前有一个前缀S ,形如 “S1:peter>1»S2:lop” 接下来的示例均假设已经创建好了前述的各种schema和vertex信息
2.2.1 创建一条边 Params说明
label:边类型名称,必填 outV:源顶点id,必填 inV:目标顶点id,必填 outVLabel:源顶点类型。必填 inVLabel:目标顶点类型。必填 properties: 边关联的属性,对象内部结构为:name:属性名称 value:属性值 Method & Url POST http://localhost:8080/graphs/hugegraph/graph/edgesRequest Body {
"label" : "created" ,
"outV" : "1:peter" ,
"inV" : "2:lop" ,
"outVLabel" : "person" ,
"inVLabel" : "software" ,
"properties" : {
"date" : "2017-5-18" ,
"weight" : 0.2
}
} Response Status Response Body {
"id" : "S1:peter>1>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:peter" ,
"properties" : {
"date" : "2017-5-18" ,
"weight" : 0.2
}
} 2.2.2 创建多条边 Params check_vertex: 是否检查顶点存在(true | false),当设置为 true 而待插入边的源顶点或目标顶点不存在时会报错。 Method & Url POST http://localhost:8080/graphs/hugegraph/graph/edges/batchRequest Body [
{
"label" : "created" ,
"outV" : "1:peter" ,
"inV" : "2:lop" ,
"outVLabel" : "person" ,
"inVLabel" : "software" ,
"properties" : {
"date" : "2017-5-18" ,
"weight" : 0.2
}
},
{
"label" : "knows" ,
"outV" : "1:marko" ,
"inV" : "1:vadas" ,
"outVLabel" : "person" ,
"inVLabel" : "person" ,
"properties" : {
"date" : "2016-01-10" ,
"weight" : 0.5
}
}
] Response Status Response Body [
"S1:peter>1>>S2:lop" ,
"S1:marko>2>>S1:vadas"
] 2.2.3 更新边属性 Method & Url PUT http://localhost:8080/graphs/hugegraph/graph/edges/S1:peter>1>>S2:lop?action=appendRequest Body {
"properties" : {
"weight" : 1.0
}
} 注意:属性的取值是有三种类别的,分别是single、set和list。如果是single,表示增加或更新属性值;如果是set或list,则表示追加属性值。
Response Status Response Body {
"id" : "S1:peter>1>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:peter" ,
"properties" : {
"date" : "2017-5-18" ,
"weight" : 1
}
} 2.2.4 批量更新边属性 功能说明 与批量更新顶点属性类似
假设原边及属性为:
{
"edges" :[
{
"id" : "S1:josh>2>>S2:ripple" ,
"label" : "created" ,
"type" : "edge" ,
"outV" : "1:josh" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"inVLabel" : "software" ,
"properties" :{
"weight" : 1 ,
"date" : 1512835200000
}
},
{
"id" : "S1:marko>1>7JooBil0>S1:josh" ,
"label" : "knows" ,
"type" : "edge" ,
"outV" : "1:marko" ,
"outVLabel" : "person" ,
"inV" : "1:josh" ,
"inVLabel" : "person" ,
"properties" :{
"weight" : 1 ,
"date" : 1361289600000
}
}
]
} Method & Url PUT http://127.0.0.1:8080/graphs/hugegraph/graph/edges/batchRequest Body {
"edges" :[
{
"id" : "S1:josh>2>>S2:ripple" ,
"label" : "created" ,
"outV" : "1:josh" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"inVLabel" : "software" ,
"properties" :{
"weight" : 0.1 ,
"date" : 1522835200000
}
},
{
"id" : "S1:marko>1>7JooBil0>S1:josh" ,
"label" : "knows" ,
"outV" : "1:marko" ,
"outVLabel" : "person" ,
"inV" : "1:josh" ,
"inVLabel" : "person" ,
"properties" :{
"weight" : 0.2 ,
"date" : 1301289600000
}
}
],
"update_strategies" :{
"weight" : "SUM" ,
"date" : "BIGGER"
},
"check_vertex" : false ,
"create_if_not_exist" : true
} Response Status Response Body {
"edges" :[
{
"id" : "S1:josh>2>>S2:ripple" ,
"label" : "created" ,
"type" : "edge" ,
"outV" : "1:josh" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"inVLabel" : "software" ,
"properties" :{
"weight" : 1.1 ,
"date" : 1522835200000
}
},
{
"id" : "S1:marko>1>7JooBil0>S1:josh" ,
"label" : "knows" ,
"type" : "edge" ,
"outV" : "1:marko" ,
"outVLabel" : "person" ,
"inV" : "1:josh" ,
"inVLabel" : "person" ,
"properties" :{
"weight" : 1.2 ,
"date" : 1301289600000
}
}
]
} 2.2.5 删除边属性 Method & Url PUT http://localhost:8080/graphs/hugegraph/graph/edges/S1:peter>1>>S2:lop?action=eliminateRequest Body {
"properties" : {
"weight" : 1.0
}
} 注意:这里会直接删除属性(删除key和所有value),无论其属性的取值是single、set或list。
Response Status Response Body {
"id" : "S1:peter>1>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:peter" ,
"properties" : {
"date" : "20170324"
}
} 2.2.6 获取符合条件的边 Params vertex_id: 顶点id direction: 边的方向(OUT | IN | BOTH) label: 边的标签 properties: 属性键值对(根据属性查询的前提是预先建立了索引) offset:偏移,默认为0 limit: 查询数目,默认为100 page: 页号 支持的查询有以下几种:
提供vertex_id参数时,不可以使用参数page,direction、label、properties可选,offset和limit可以
限制结果范围 不提供vertex_id参数时,label和properties可选如果使用page参数,则:offset参数不可用(不填或者为0),direction不可用,properties最多只能有一个 如果不使用page参数,则:offset和limit可以用来限制结果范围,direction参数忽略 属性键值对由JSON格式的属性名称和属性值组成,允许多个属性键值对作为查询条件,属性值支持精确匹配和范围匹配,精确匹配时形如properties={"weight":0.8},范围匹配时形如properties={"age":"P.gt(0.8)"},范围匹配支持的表达式如下:
查询与顶点 person:josh(vertex_id=“1:josh”) 相连且 label 为 created 的边
Method & Url GET http://127.0.0.1:8080/graphs/hugegraph/graph/edges?vertex_id="1:josh"&direction=BOTH&label=created&properties={}Response Status Response Body {
"edges" : [
{
"id" : "S1:josh>1>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:josh" ,
"properties" : {
"date" : "20091111" ,
"weight" : 0.4
}
},
{
"id" : "S1:josh>1>>S2:ripple" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"outV" : "1:josh" ,
"properties" : {
"date" : "20171210" ,
"weight" : 1
}
}
]
} 分页查询所有边,获取第一页(page不带参数值),限定3条
Method & Url GET http://127.0.0.1:8080/graphs/hugegraph/graph/edges?page&limit=3Response Status Response Body {
"edges" : [{
"id" : "S1:peter>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:peter" ,
"properties" : {
"weight" : 0.2 ,
"date" : "20170324"
}
},
{
"id" : "S1:josh>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:josh" ,
"properties" : {
"weight" : 0.4 ,
"date" : "20091111"
}
},
{
"id" : "S1:josh>2>>S2:ripple" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"outV" : "1:josh" ,
"properties" : {
"weight" : 1 ,
"date" : "20171210"
}
}
],
"page" : "002500100753313a6a6f73681210010004000000020953323a726970706c65f07ffffffcf07ffffffd8460d63f4b398dd2721ed4fdb7716b420004"
} 返回的body里面是带有下一页的页号信息的,"page": "002500100753313a6a6f73681210010004000000020953323a726970706c65f07ffffffcf07ffffffd8460d63f4b398dd2721ed4fdb7716b420004",
在查询下一页的时候将该值赋给page参数。
分页查询所有边,获取下一页(page带上上一页返回的page值),限定3条
Method & Url GET http://127.0.0.1:8080/graphs/hugegraph/graph/edges?page=002500100753313a6a6f73681210010004000000020953323a726970706c65f07ffffffcf07ffffffd8460d63f4b398dd2721ed4fdb7716b420004&limit=3Response Status Response Body {
"edges" : [{
"id" : "S1:marko>1>20130220>S1:josh" ,
"label" : "knows" ,
"type" : "edge" ,
"inVLabel" : "person" ,
"outVLabel" : "person" ,
"inV" : "1:josh" ,
"outV" : "1:marko" ,
"properties" : {
"weight" : 1 ,
"date" : "20130220"
}
},
{
"id" : "S1:marko>1>20160110>S1:vadas" ,
"label" : "knows" ,
"type" : "edge" ,
"inVLabel" : "person" ,
"outVLabel" : "person" ,
"inV" : "1:vadas" ,
"outV" : "1:marko" ,
"properties" : {
"weight" : 0.5 ,
"date" : "20160110"
}
},
{
"id" : "S1:marko>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:marko" ,
"properties" : {
"weight" : 0.4 ,
"date" : "20171210"
}
}
],
"page" : null
} 此时"page": null表示已经没有下一页了 (注: 后端为 Cassandra 时,为了性能考虑,返回页恰好为最后一页时,返回 page 值可能非空,通过该 page 再请求下一页数据时则返回 空数据 及 page = null,其他情况类似)
2.2.7 根据Id获取边 Method & Url GET http://localhost:8080/graphs/hugegraph/graph/edges/S1:peter>1>>S2:lopResponse Status Response Body {
"id" : "S1:peter>1>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:peter" ,
"properties" : {
"date" : "2017-5-18" ,
"weight" : 0.2
}
} 2.2.8 根据Id删除边 Params 仅根据Id删除边
Method & Url DELETE http://localhost:8080/graphs/hugegraph/graph/edges/S1:peter>1>>S2:lopResponse Status 根据Label+Id删除边
通过指定Label参数和Id来删除边时,一般来说其性能比仅根据Id删除会更好。
Method & Url DELETE http://localhost:8080/graphs/hugegraph/graph/edges/S1:peter>1>>S2:lop?label=personResponse Status 1.9 - Traverser API 3.1 traverser API概述 HugeGraphServer为HugeGraph图数据库提供了RESTful API接口。除了顶点和边的CRUD基本操作以外,还提供了一些遍历(traverser)方法,我们称为traverser API。这些遍历方法实现了一些复杂的图算法,方便用户对图进行分析和挖掘。
HugeGraph支持的Traverser API包括:
K-out API,根据起始顶点,查找恰好N步可达的邻居,分为基础版和高级版:基础版使用GET方法,根据起始顶点,查找恰好N步可达的邻居 高级版使用POST方法,根据起始顶点,查找恰好N步可达的邻居,与基础版的不同在于:支持只统计邻居数量 支持边属性过滤 支持返回到达邻居的最短路径 K-neighbor API,根据起始顶点,查找N步以内可达的所有邻居,分为基础版和高级版:基础版使用GET方法,根据起始顶点,查找N步以内可达的所有邻居 高级版使用POST方法,根据起始顶点,查找N步以内可达的所有邻居,与基础版的不同在于:支持只统计邻居数量 支持边属性过滤 支持返回到达邻居的最短路径 Same Neighbors, 查询两个顶点的共同邻居 Jaccard Similarity API,计算jaccard相似度,包括两种:一种是使用GET方法,计算两个顶点的邻居的相似度(交并比) 一种是使用POST方法,在全图中查找与起点的jaccard similarity最高的N个点 Shortest Path API,查找两个顶点之间的最短路径 All Shortest Paths,查找两个顶点间的全部最短路径 Weighted Shortest Path,查找起点到目标点的带权最短路径 Single Source Shortest Path,查找一个点到其他各个点的加权最短路径 Multi Node Shortest Path,查找指定顶点集之间两两最短路径 Paths API,查找两个顶点间的全部路径,分为基础版和高级版:基础版使用GET方法,根据起点和终点,查找两个顶点间的全部路径 高级版使用POST方法,根据一组起点和一组终点,查找两个集合间符合条件的全部路径 Customized Paths API,从一批顶点出发,按(一种)模式遍历经过的全部路径 Template Path API,指定起点和终点以及起点和终点间路径信息,查找符合的路径 Crosspoints API,查找两个顶点的交点(共同祖先或者共同子孙) Customized Crosspoints API,从一批顶点出发,按多种模式遍历,最后一步到达的顶点的交点 Rings API,从起始顶点出发,可到达的环路路径 Rays API,从起始顶点出发,可到达边界的路径(即无环路径) Fusiform Similarity API,查找一个顶点的梭形相似点 Vertices API按ID批量查询顶点; 获取顶点的分区; 按分区查询顶点; Edges API 3.2. traverser API详解 使用方法中的例子,都是基于TinkerPop官网给出的图:
数据导入程序如下:
public class Loader {
public static void main ( String [] args ) {
HugeClient client = new HugeClient ( "http://127.0.0.1:8080" , "hugegraph" );
SchemaManager schema = client . schema ();
schema . propertyKey ( "name" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "age" ). asInt (). ifNotExist (). create ();
schema . propertyKey ( "city" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "weight" ). asDouble (). ifNotExist (). create ();
schema . propertyKey ( "lang" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "date" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "price" ). asInt (). ifNotExist (). create ();
schema . vertexLabel ( "person" )
. properties ( "name" , "age" , "city" )
. primaryKeys ( "name" )
. nullableKeys ( "age" )
. ifNotExist ()
. create ();
schema . vertexLabel ( "software" )
. properties ( "name" , "lang" , "price" )
. primaryKeys ( "name" )
. nullableKeys ( "price" )
. ifNotExist ()
. create ();
schema . indexLabel ( "personByCity" )
. onV ( "person" )
. by ( "city" )
. secondary ()
. ifNotExist ()
. create ();
schema . indexLabel ( "personByAgeAndCity" )
. onV ( "person" )
. by ( "age" , "city" )
. secondary ()
. ifNotExist ()
. create ();
schema . indexLabel ( "softwareByPrice" )
. onV ( "software" )
. by ( "price" )
. range ()
. ifNotExist ()
. create ();
schema . edgeLabel ( "knows" )
. multiTimes ()
. sourceLabel ( "person" )
. targetLabel ( "person" )
. properties ( "date" , "weight" )
. sortKeys ( "date" )
. nullableKeys ( "weight" )
. ifNotExist ()
. create ();
schema . edgeLabel ( "created" )
. sourceLabel ( "person" ). targetLabel ( "software" )
. properties ( "date" , "weight" )
. nullableKeys ( "weight" )
. ifNotExist ()
. create ();
schema . indexLabel ( "createdByDate" )
. onE ( "created" )
. by ( "date" )
. secondary ()
. ifNotExist ()
. create ();
schema . indexLabel ( "createdByWeight" )
. onE ( "created" )
. by ( "weight" )
. range ()
. ifNotExist ()
. create ();
schema . indexLabel ( "knowsByWeight" )
. onE ( "knows" )
. by ( "weight" )
. range ()
. ifNotExist ()
. create ();
GraphManager graph = client . graph ();
Vertex marko = graph . addVertex ( T . label , "person" , "name" , "marko" ,
"age" , 29 , "city" , "Beijing" );
Vertex vadas = graph . addVertex ( T . label , "person" , "name" , "vadas" ,
"age" , 27 , "city" , "Hongkong" );
Vertex lop = graph . addVertex ( T . label , "software" , "name" , "lop" ,
"lang" , "java" , "price" , 328 );
Vertex josh = graph . addVertex ( T . label , "person" , "name" , "josh" ,
"age" , 32 , "city" , "Beijing" );
Vertex ripple = graph . addVertex ( T . label , "software" , "name" , "ripple" ,
"lang" , "java" , "price" , 199 );
Vertex peter = graph . addVertex ( T . label , "person" , "name" , "peter" ,
"age" , 35 , "city" , "Shanghai" );
marko . addEdge ( "knows" , vadas , "date" , "20160110" , "weight" , 0 . 5 );
marko . addEdge ( "knows" , josh , "date" , "20130220" , "weight" , 1 . 0 );
marko . addEdge ( "created" , lop , "date" , "20171210" , "weight" , 0 . 4 );
josh . addEdge ( "created" , lop , "date" , "20091111" , "weight" , 0 . 4 );
josh . addEdge ( "created" , ripple , "date" , "20171210" , "weight" , 1 . 0 );
peter . addEdge ( "created" , lop , "date" , "20170324" , "weight" , 0 . 2 );
}
} 顶点ID为:
"2:ripple",
"1:vadas",
"1:peter",
"1:josh",
"1:marko",
"2:lop"边ID为:
"S1:peter>2>>S2:lop",
"S1:josh>2>>S2:lop",
"S1:josh>2>>S2:ripple",
"S1:marko>1>20130220>S1:josh",
"S1:marko>1>20160110>S1:vadas",
"S1:marko>2>>S2:lop"3.2.1 K-out API(GET,基础版) 3.2.1.1 功能介绍 根据起始顶点、方向、边的类型(可选)和深度depth,查找从起始顶点出发恰好depth步可达的顶点
Params source:起始顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH max_depth:步数,必填项 label:边的类型,选填项,默认代表所有edge label nearest:nearest为true时,代表起始顶点到达结果顶点的最短路径长度为depth,不存在更短的路径;nearest为false时,代表起始顶点到结果顶点有一条长度为depth的路径(未必最短且可以有环),选填项,默认为true max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的顶点的最大数目,选填项,默认为10000000 3.2.1.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/kout?source="1:marko"&max_depth=2Response Status Response Body {
"vertices" :[
"2:ripple" ,
"1:peter"
]
} 3.2.1.3 适用场景 查找恰好N步关系可达的顶点。两个例子:
家族关系中,查找一个人的所有孙子,person A通过连续的两条“儿子”边到达的顶点集合。 社交关系中发现潜在好友,例如:与目标用户相隔两层朋友关系的用户,可以通过连续两条“朋友”边到达的顶点。 3.2.2 K-out API(POST,高级版) 3.2.2.1 功能介绍 根据起始顶点、步骤(包括方向、边类型和过滤属性)和深度depth,查找从起始顶点出发恰好depth步可达的顶点。
与K-out基础版的不同在于:
支持只统计邻居数量 支持边属性过滤 支持返回到达邻居的最短路径 Params source:起始顶点id,必填项 从起始点出发的Step,必填项,结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) max_depth:步数,必填项 nearest:nearest为true时,代表起始顶点到达结果顶点的最短路径长度为depth,不存在更短的路径;nearest为false时,代表起始顶点到结果顶点有一条长度为depth的路径(未必最短且可以有环),选填项,默认为true count_only:Boolean值,true表示只统计结果的数目,不返回具体结果;false表示返回具体的结果,默认为false with_path:true表示返回起始点到每个邻居的最短路径,false表示不返回起始点到每个邻居的最短路径,选填项,默认为false with_vertex,选填项,默认为false:true表示返回结果包含完整的顶点信息(路径中的全部顶点)with_path为true时,返回所有路径中的顶点的完整信息 with_path为false时,返回所有邻居的完整信息 false时表示只返回顶点id capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的顶点的最大数目,选填项,默认为10000000 3.2.2.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/koutRequest Body {
"source" : "1:marko" ,
"step" : {
"direction" : "BOTH" ,
"labels" : [ "knows" , "created" ],
"properties" : {
"weight" : "P.gt(0.1)"
},
"max_degree" : 10000 ,
"skip_degree" : 100000
},
"max_depth" : 1 ,
"nearest" : true ,
"limit" : 10000 ,
"with_vertex" : true ,
"with_path" : true
} Response Status Response Body {
"size" : 3 ,
"kout" : [
"1:josh" ,
"1:vadas" ,
"2:lop"
],
"paths" : [
{
"objects" : [
"1:marko" ,
"1:josh"
]
},
{
"objects" : [
"1:marko" ,
"1:vadas"
]
},
{
"objects" : [
"1:marko" ,
"2:lop"
]
}
],
"vertices" : [
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "marko" ,
"age" : 29 ,
"city" : "Beijing"
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing"
}
},
{
"id" : "1:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "vadas" ,
"age" : 27 ,
"city" : "Hongkong"
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "lop" ,
"lang" : "java" ,
"price" : 328
}
}
]
} 3.2.2.3 适用场景 参见3.2.1.3
3.2.3 K-neighbor(GET,基础版) 3.2.3.1 功能介绍 根据起始顶点、方向、边的类型(可选)和深度depth,查找包括起始顶点在内、depth步之内可达的所有顶点
相当于:起始顶点、K-out(1)、K-out(2)、… 、K-out(max_depth)的并集
Params source: 起始顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH max_depth:步数,必填项 label:边的类型,选填项,默认代表所有edge label max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 limit:返回的顶点的最大数目,也即遍历过程中最大的访问的顶点数目,选填项,默认为10000000 3.2.3.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/kneighbor?source=“1:marko”&max_depth=2Response Status Response Body {
"vertices" :[
"2:ripple" ,
"1:marko" ,
"1:josh" ,
"1:vadas" ,
"1:peter" ,
"2:lop"
]
} 3.2.3.3 适用场景 查找N步以内可达的所有顶点,例如:
家族关系中,查找一个人五服以内所有子孙,person A通过连续的5条“亲子”边到达的顶点集合。 社交关系中发现好友圈子,例如目标用户通过1条、2条、3条“朋友”边可到达的用户可以组成目标用户的朋友圈子 3.2.4 K-neighbor API(POST,高级版) 3.2.4.1 功能介绍 根据起始顶点、步骤(包括方向、边类型和过滤属性)和深度depth,查找从起始顶点出发depth步内可达的所有顶点。
与K-neighbor基础版的不同在于:
支持只统计邻居数量 支持边属性过滤 支持返回到达邻居的最短路径 Params source:起始顶点id,必填项 从起始点出发的Step,必填项,结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) max_depth:步数,必填项 count_only:Boolean值,true表示只统计结果的数目,不返回具体结果;false表示返回具体的结果,默认为false with_path:true表示返回起始点到每个邻居的最短路径,false表示不返回起始点到每个邻居的最短路径,选填项,默认为false with_vertex,选填项,默认为false:true表示返回结果包含完整的顶点信息(路径中的全部顶点)with_path为true时,返回所有路径中的顶点的完整信息 with_path为false时,返回所有邻居的完整信息 false时表示只返回顶点id limit:返回的顶点的最大数目,选填项,默认为10000000 3.2.4.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/kneighborRequest Body {
"source" : "1:marko" ,
"step" : {
"direction" : "BOTH" ,
"labels" : [ "knows" , "created" ],
"properties" : {
"weight" : "P.gt(0.1)"
},
"max_degree" : 10000 ,
"skip_degree" : 100000
},
"max_depth" : 3 ,
"limit" : 10000 ,
"with_vertex" : true ,
"with_path" : true
} Response Status Response Body {
"size" : 6 ,
"kneighbor" : [
"2:ripple" ,
"1:marko" ,
"1:josh" ,
"1:vadas" ,
"1:peter" ,
"2:lop"
],
"paths" : [
{
"objects" : [
"1:marko" ,
"1:josh" ,
"2:ripple"
]
},
{
"objects" : [
"1:marko"
]
},
{
"objects" : [
"1:marko" ,
"1:josh"
]
},
{
"objects" : [
"1:marko" ,
"1:vadas"
]
},
{
"objects" : [
"1:marko" ,
"2:lop" ,
"1:peter"
]
},
{
"objects" : [
"1:marko" ,
"2:lop"
]
}
],
"vertices" : [
{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "ripple" ,
"lang" : "java" ,
"price" : 199
}
},
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "marko" ,
"age" : 29 ,
"city" : "Beijing"
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing"
}
},
{
"id" : "1:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "vadas" ,
"age" : 27 ,
"city" : "Hongkong"
}
},
{
"id" : "1:peter" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "peter" ,
"age" : 35 ,
"city" : "Shanghai"
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "lop" ,
"lang" : "java" ,
"price" : 328
}
}
]
} 3.2.4.3 适用场景 参见3.2.3.3
3.2.5 Same Neighbors 3.2.5.1 功能介绍 查询两个点的共同邻居
Params vertex:一个顶点id,必填项 other:另一个顶点id,必填项 direction:顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 limit:返回的共同邻居的最大数目,选填项,默认为10000000 3.2.5.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/sameneighbors?vertex=“1:marko”&other="1:josh"Response Status Response Body {
"same_neighbors" :[
"2:lop"
]
} 3.2.5.3 适用场景 查找两个顶点的共同邻居:
3.2.6 Jaccard Similarity(GET) 3.2.6.1 功能介绍 计算两个顶点的jaccard similarity(两个顶点邻居的交集比上两个顶点邻居的并集)
Params vertex:一个顶点id,必填项 other:另一个顶点id,必填项 direction:顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 3.2.6.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/jaccardsimilarity?vertex="1:marko"&other="1:josh"Response Status Response Body {
"jaccard_similarity" : 0.2
} 3.2.6.3 适用场景 用于评估两个点的相似性或者紧密度
3.2.7 Jaccard Similarity(POST) 3.2.7.1 功能介绍 计算与指定顶点的jaccard similarity最大的N个点
jaccard similarity的计算方式为:两个顶点邻居的交集比上两个顶点邻居的并集
Params vertex:一个顶点id,必填项 从起始点出发的Step,必填项,结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) top:返回一个起点的jaccard similarity中最大的top个,选填项,默认为100 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 3.2.7.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/jaccardsimilarityRequest Body {
"vertex" : "1:marko" ,
"step" : {
"direction" : "BOTH" ,
"labels" : [],
"max_degree" : 10000 ,
"skip_degree" : 100000
},
"top" : 3
} Response Status Response Body {
"2:ripple" : 0.3333333333333333 ,
"1:peter" : 0.3333333333333333 ,
"1:josh" : 0.2
} 3.2.7.3 适用场景 用于在图中找出与指定顶点相似性最高的顶点
3.2.8 Shortest Path 3.2.8.1 功能介绍 根据起始顶点、目的顶点、方向、边的类型(可选)和最大深度,查找一条最短路径
Params source:起始顶点id,必填项 target:目的顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH max_depth:最大步数,必填项 label:边的类型,选填项,默认代表所有edge label max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 3.2.8.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/shortestpath?source="1:marko"&target="2:ripple"&max_depth=3Response Status Response Body {
"path" :[
"1:marko" ,
"1:josh" ,
"2:ripple"
]
} 3.2.8.3 适用场景 查找两个顶点间的最短路径,例如:
社交关系网中,查找两个用户有关系的最短路径,即最近的朋友关系链 设备关联网络中,查找两个设备最短的关联关系 3.2.9 All Shortest Paths 3.2.9.1 功能介绍 根据起始顶点、目的顶点、方向、边的类型(可选)和最大深度,查找两点间所有的最短路径
Params source:起始顶点id,必填项 target:目的顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH max_depth:最大步数,必填项 label:边的类型,选填项,默认代表所有edge label max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 3.2.9.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/allshortestpaths?source="A"&target="Z"&max_depth=10Response Status Response Body {
"paths" :[
{
"objects" : [
"A" ,
"B" ,
"C" ,
"Z"
]
},
{
"objects" : [
"A" ,
"M" ,
"N" ,
"Z"
]
}
]
} 3.2.9.3 适用场景 查找两个顶点间的所有最短路径,例如:
社交关系网中,查找两个用户有关系的全部最短路径,即最近的朋友关系链 设备关联网络中,查找两个设备全部的最短关联关系 3.2.10 Weighted Shortest Path 3.2.10.1 功能介绍 根据起始顶点、目的顶点、方向、边的类型(可选)和最大深度,查找一条带权最短路径
Params source:起始顶点id,必填项 target:目的顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label weight:边的权重属性,必填项,必须是数字类型的属性 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 with_vertex:true表示返回结果包含完整的顶点信息(路径中的全部顶点),false时表示只返回顶点id,选填项,默认为false 3.2.10.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/weightedshortestpath?source="1:marko"&target="2:ripple"&weight="weight"&with_vertex=trueResponse Status Response Body {
"path" : {
"weight" : 2.0 ,
"vertices" : [
"1:marko" ,
"1:josh" ,
"2:ripple"
]
},
"vertices" : [
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "marko" ,
"age" : 29 ,
"city" : "Beijing"
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing"
}
},
{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "ripple" ,
"lang" : "java" ,
"price" : 199
}
}
]
} 3.2.10.3 适用场景 查找两个顶点间的带权最短路径,例如:
3.2.11 Single Source Shortest Path 3.2.11.1 功能介绍 从一个顶点出发,查找该点到图中其他顶点的最短路径(可选是否带权重)
Params source:起始顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label weight:边的权重属性,选填项,必须是数字类型的属性,如果不填或者虽然填了但是边没有该属性,则权重为1.0 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:查询到的目标顶点个数,也是返回的最短路径的条数,选填项,默认为10 with_vertex:true表示返回结果包含完整的顶点信息(路径中的全部顶点),false时表示只返回顶点id,选填项,默认为false 3.2.11.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/singlesourceshortestpath?source="1:marko"&with_vertex=trueResponse Status Response Body {
"paths" : {
"2:ripple" : {
"weight" : 2.0 ,
"vertices" : [
"1:marko" ,
"1:josh" ,
"2:ripple"
]
},
"1:josh" : {
"weight" : 1.0 ,
"vertices" : [
"1:marko" ,
"1:josh"
]
},
"1:vadas" : {
"weight" : 1.0 ,
"vertices" : [
"1:marko" ,
"1:vadas"
]
},
"1:peter" : {
"weight" : 2.0 ,
"vertices" : [
"1:marko" ,
"2:lop" ,
"1:peter"
]
},
"2:lop" : {
"weight" : 1.0 ,
"vertices" : [
"1:marko" ,
"2:lop"
]
}
},
"vertices" : [
{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "ripple" ,
"lang" : "java" ,
"price" : 199
}
},
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "marko" ,
"age" : 29 ,
"city" : "Beijing"
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing"
}
},
{
"id" : "1:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "vadas" ,
"age" : 27 ,
"city" : "Hongkong"
}
},
{
"id" : "1:peter" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "peter" ,
"age" : 35 ,
"city" : "Shanghai"
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "lop" ,
"lang" : "java" ,
"price" : 328
}
}
]
} 3.2.11.3 适用场景 查找从一个点出发到其他顶点的带权最短路径,比如:
查找从北京出发到全国其他所有城市的耗时最短的乘车方案 3.2.12 Multi Node Shortest Path 3.2.12.1 功能介绍 查找指定顶点集两两之间的最短路径
Params vertices:定义起始顶点,必填项,指定方式包括:ids:通过顶点id列表提供起始顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询起始顶点label:顶点的类型 properties:通过属性的值查询起始顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
step:表示从起始顶点到终止顶点走过的路径,必填项,Step的结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) max_depth:步数,必填项 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 with_vertex:true表示返回结果包含完整的顶点信息(路径中的全部顶点),false时表示只返回顶点id,选填项,默认为false 3.2.12.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/multinodeshortestpathRequest Body {
"vertices" : {
"ids" : [ "382:marko" , "382:josh" , "382:vadas" , "382:peter" , "383:lop" , "383:ripple" ]
},
"step" : {
"direction" : "BOTH" ,
"properties" : {
}
},
"max_depth" : 10 ,
"capacity" : 100000000 ,
"with_vertex" : true
} Response Status Response Body {
"paths" : [
{
"objects" : [
"382:peter" ,
"383:lop"
]
},
{
"objects" : [
"382:peter" ,
"383:lop" ,
"382:marko"
]
},
{
"objects" : [
"382:peter" ,
"383:lop" ,
"382:josh"
]
},
{
"objects" : [
"382:peter" ,
"383:lop" ,
"382:marko" ,
"382:vadas"
]
},
{
"objects" : [
"383:lop" ,
"382:marko"
]
},
{
"objects" : [
"383:lop" ,
"382:josh"
]
},
{
"objects" : [
"383:lop" ,
"382:marko" ,
"382:vadas"
]
},
{
"objects" : [
"382:peter" ,
"383:lop" ,
"382:josh" ,
"383:ripple"
]
},
{
"objects" : [
"382:marko" ,
"382:josh"
]
},
{
"objects" : [
"383:lop" ,
"382:josh" ,
"383:ripple"
]
},
{
"objects" : [
"382:marko" ,
"382:vadas"
]
},
{
"objects" : [
"382:marko" ,
"382:josh" ,
"383:ripple"
]
},
{
"objects" : [
"382:josh" ,
"383:ripple"
]
},
{
"objects" : [
"382:josh" ,
"382:marko" ,
"382:vadas"
]
},
{
"objects" : [
"382:vadas" ,
"382:marko" ,
"382:josh" ,
"383:ripple"
]
}
],
"vertices" : [
{
"id" : "382:peter" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "peter" ,
"age" : 29 ,
"city" : "Shanghai"
}
},
{
"id" : "383:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "lop" ,
"lang" : "java" ,
"price" : 328
}
},
{
"id" : "382:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "marko" ,
"age" : 29 ,
"city" : "Beijing"
}
},
{
"id" : "382:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing"
}
},
{
"id" : "382:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "vadas" ,
"age" : 27 ,
"city" : "Hongkong"
}
},
{
"id" : "383:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "ripple" ,
"lang" : "java" ,
"price" : 199
}
}
]
} 3.2.12.3 适用场景 查找多个点之间的最短路径,比如:
3.2.13 Paths (GET,基础版) 3.2.13.1 功能介绍 根据起始顶点、目的顶点、方向、边的类型(可选)和最大深度等条件查找所有路径
Params source:起始顶点id,必填项 target:目的顶点id,必填项 direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label max_depth:步数,必填项 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的路径的最大数目,选填项,默认为10 3.2.13.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/paths?source="1:marko"&target="1:josh"&max_depth=5Response Status Response Body {
"paths" :[
{
"objects" :[
"1:marko" ,
"1:josh"
]
},
{
"objects" :[
"1:marko" ,
"2:lop" ,
"1:josh"
]
}
]
} 3.2.13.3 适用场景 查找两个顶点间的所有路径,例如:
社交网络中,查找两个用户所有可能的关系路径 设备关联网络中,查找两个设备之间所有的关联路径 3.2.14 Paths (POST,高级版) 3.2.14.1 功能介绍 根据起始顶点、目的顶点、步骤(step)和最大深度等条件查找所有路径
Params sources:定义起始顶点,必填项,指定方式包括:ids:通过顶点id列表提供起始顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询起始顶点label:顶点的类型 properties:通过属性的值查询起始顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
targets:定义终止顶点,必填项,指定方式包括:ids:通过顶点id列表提供终止顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询终止顶点label:顶点的类型 properties:通过属性的值查询终止顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
step:表示从起始顶点到终止顶点走过的路径,必填项,Step的结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) max_depth:步数,必填项 nearest:nearest为true时,代表起始顶点到达结果顶点的最短路径长度为depth,不存在更短的路径;nearest为false时,代表起始顶点到结果顶点有一条长度为depth的路径(未必最短且可以有环),选填项,默认为true capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的路径的最大数目,选填项,默认为10 with_vertex:true表示返回结果包含完整的顶点信息(路径中的全部顶点),false时表示只返回顶点id,选填项,默认为false 3.2.14.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/pathsRequest Body {
"sources" : {
"ids" : [ "1:marko" ]
},
"targets" : {
"ids" : [ "1:peter" ]
},
"step" : {
"direction" : "BOTH" ,
"properties" : {
"weight" : "P.gt(0.01)"
}
},
"max_depth" : 10 ,
"capacity" : 100000000 ,
"limit" : 10000000 ,
"with_vertex" : false
} Response Status Response Body {
"paths" : [
{
"objects" : [
"1:marko" ,
"1:josh" ,
"2:lop" ,
"1:peter"
]
},
{
"objects" : [
"1:marko" ,
"2:lop" ,
"1:peter"
]
}
]
} 3.2.14.3 适用场景 查找两个顶点间的所有路径,例如:
社交网络中,查找两个用户所有可能的关系路径 设备关联网络中,查找两个设备之间所有的关联路径 3.2.15 Customized Paths 3.2.15.1 功能介绍 根据一批起始顶点、边规则(包括方向、边的类型和属性过滤)和最大深度等条件查找符合条件的所有的路径
Params sources:定义起始顶点,必填项,指定方式包括:ids:通过顶点id列表提供起始顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询起始顶点label:顶点的类型 properties:通过属性的值查询起始顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
steps:表示从起始顶点走过的路径规则,是一组Step的列表。必填项。每个Step的结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 weight_by:根据指定的属性计算边的权重,sort_by不为NONE时有效,与default_weight互斥 default_weight:当边没有属性作为权重计算值时,采取的默认权重,sort_by不为NONE时有效,与weight_by互斥 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) sample:当需要对某个step的符合条件的边进行采样时设置,-1表示不采样,默认为采样100 sort_by:根据路径的权重排序,选填项,默认为NONE:NONE表示不排序,默认值 INCR表示按照路径权重的升序排序 DECR表示按照路径权重的降序排序 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的路径的最大数目,选填项,默认为10 with_vertex:true表示返回结果包含完整的顶点信息(路径中的全部顶点),false时表示只返回顶点id,选填项,默认为false 3.2.15.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/customizedpathsRequest Body {
"sources" :{
"ids" :[
],
"label" : "person" ,
"properties" :{
"name" : "marko"
}
},
"steps" :[
{
"direction" : "OUT" ,
"labels" :[
"knows"
],
"weight_by" : "weight" ,
"max_degree" : -1
},
{
"direction" : "OUT" ,
"labels" :[
"created"
],
"default_weight" : 8 ,
"max_degree" : -1 ,
"sample" : 1
}
],
"sort_by" : "INCR" ,
"with_vertex" : true ,
"capacity" : -1 ,
"limit" : -1
} Response Status Response Body {
"paths" :[
{
"objects" :[
"1:marko" ,
"1:josh" ,
"2:lop"
],
"weights" :[
1 ,
8
]
}
],
"vertices" :[
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:marko>city" ,
"value" : "Beijing"
}
],
"name" :[
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" :[
{
"id" : "1:marko>age" ,
"value" : 29
}
]
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:josh>city" ,
"value" : "Beijing"
}
],
"name" :[
{
"id" : "1:josh>name" ,
"value" : "josh"
}
],
"age" :[
{
"id" : "1:josh>age" ,
"value" : 32
}
]
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"price" :[
{
"id" : "2:lop>price" ,
"value" : 328
}
],
"name" :[
{
"id" : "2:lop>name" ,
"value" : "lop"
}
],
"lang" :[
{
"id" : "2:lop>lang" ,
"value" : "java"
}
]
}
}
]
} 3.2.15.3 适用场景 适合查找各种复杂的路径集合,例如:
社交网络中,查找看过张艺谋所导演的电影的用户关注的大V的路径(张艺谋—>电影—->用户—>大V) 风控网络中,查找多个高风险用户的直系亲属的朋友的路径(高风险用户—>直系亲属—>朋友) 3.2.16 Template Paths 3.2.16.1 功能介绍 根据一批起始顶点、边规则(包括方向、边的类型和属性过滤)和最大深度等条件查找符合条件的所有的路径
Params sources:定义起始顶点,必填项,指定方式包括:ids:通过顶点id列表提供起始顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询起始顶点label:顶点的类型 properties:通过属性的值查询起始顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
targets:定义终止顶点,必填项,指定方式包括:ids:通过顶点id列表提供终止顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询终止顶点label:顶点的类型 properties:通过属性的值查询终止顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
steps:表示从起始顶点走过的路径规则,是一组Step的列表。必填项。每个Step的结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_times:当前step可以重复的次数,当为N时,表示从起始顶点可以经过当前step 1-N 次 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) with_ring:Boolean值,true表示包含环路;false表示不包含环路,默认为false capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的路径的最大数目,选填项,默认为10 with_vertex:true表示返回结果包含完整的顶点信息(路径中的全部顶点),false时表示只返回顶点id,选填项,默认为false 3.2.16.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/templatepathsRequest Body {
"sources" : {
"ids" : [],
"label" : "person" ,
"properties" : {
"name" : "vadas"
}
},
"targets" : {
"ids" : [],
"label" : "software" ,
"properties" : {
"name" : "ripple"
}
},
"steps" : [
{
"direction" : "IN" ,
"labels" : [ "knows" ],
"properties" : {
},
"max_degree" : 10000 ,
"skip_degree" : 100000
},
{
"direction" : "OUT" ,
"labels" : [ "created" ],
"properties" : {
},
"max_degree" : 10000 ,
"skip_degree" : 100000
},
{
"direction" : "IN" ,
"labels" : [ "created" ],
"properties" : {
},
"max_degree" : 10000 ,
"skip_degree" : 100000
},
{
"direction" : "OUT" ,
"labels" : [ "created" ],
"properties" : {
},
"max_degree" : 10000 ,
"skip_degree" : 100000
}
],
"capacity" : 10000 ,
"limit" : 10 ,
"with_vertex" : true
} Response Status Response Body {
"paths" : [
{
"objects" : [
"1:vadas" ,
"1:marko" ,
"2:lop" ,
"1:josh" ,
"2:ripple"
]
}
],
"vertices" : [
{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "ripple" ,
"lang" : "java" ,
"price" : 199
}
},
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "marko" ,
"age" : 29 ,
"city" : "Beijing"
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "josh" ,
"age" : 32 ,
"city" : "Beijing"
}
},
{
"id" : "1:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "vadas" ,
"age" : 27 ,
"city" : "Hongkong"
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" : {
"name" : "lop" ,
"lang" : "java" ,
"price" : 328
}
}
]
} 3.2.16.3 适用场景 适合查找各种复杂的模板路径,比如personA -(朋友)-> personB -(同学)-> personC,其中"朋友"和"同学"边可以分别是最多3层和4层的情况
3.2.17 Crosspoints 3.2.17.1 功能介绍 根据起始顶点、目的顶点、方向、边的类型(可选)和最大深度等条件查找相交点
Params source:起始顶点id,必填项 target:目的顶点id,必填项 direction:起始顶点到目的顶点的方向, 目的点到起始点是反方向,BOTH时不考虑方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label max_depth:步数,必填项 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的交点的最大数目,选填项,默认为10 3.2.17.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/crosspoints?source="2:lop"&target="2:ripple"&max_depth=5&direction=INResponse Status Response Body {
"crosspoints" :[
{
"crosspoint" : "1:josh" ,
"objects" :[
"2:lop" ,
"1:josh" ,
"2:ripple"
]
}
]
} 3.2.17.3 适用场景 查找两个顶点的交点及其路径,例如:
社交网络中,查找两个用户共同关注的话题或者大V 家族关系中,查找共同的祖先 3.2.18 Customized Crosspoints 3.2.18.1 功能介绍 根据一批起始顶点、多种边规则(包括方向、边的类型和属性过滤)和最大深度等条件查找符合条件的所有的路径终点的交集
Params sources:定义起始顶点,必填项,指定方式包括:
ids:通过顶点id列表提供起始顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询起始顶点label:顶点的类型 properties:通过属性的值查询起始顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
path_patterns:表示从起始顶点走过的路径规则,是一组规则的列表。必填项。每个规则是一个PathPattern
每个PathPattern是一组Step列表,每个Step结构如下:direction:表示边的方向(OUT,IN,BOTH),默认是BOTH labels:边的类型列表 properties:通过属性的值过滤边 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) skip_degree:用于设置查询过程中舍弃超级顶点的最小边数,即当某个顶点的邻接边数目大于 skip_degree 时,完全舍弃该顶点。选填项,如果开启时,需满足 skip_degree >= max_degree 约束,默认为0 (不启用),表示不跳过任何点 (注意: 开启此配置后,遍历时会尝试访问一个顶点的 skip_degree 条边,而不仅仅是 max_degree 条边,这样有额外的遍历开销,对查询性能影响可能有较大影响,请确认理解后再开启) capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000
limit:返回的路径的最大数目,选填项,默认为10
with_path:true表示返回交点所在的路径,false表示不返回交点所在的路径,选填项,默认为false
with_vertex,选填项,默认为false:
true表示返回结果包含完整的顶点信息(路径中的全部顶点)with_path为true时,返回所有路径中的顶点的完整信息 with_path为false时,返回所有交点的完整信息 false时表示只返回顶点id 3.2.18.2 使用方法 Method & Url POST http://localhost:8080/graphs/{graph}/traversers/customizedcrosspointsRequest Body {
"sources" :{
"ids" :[
"2:lop" ,
"2:ripple"
]
},
"path_patterns" :[
{
"steps" :[
{
"direction" : "IN" ,
"labels" :[
"created"
],
"max_degree" : -1
}
]
}
],
"with_path" : true ,
"with_vertex" : true ,
"capacity" : -1 ,
"limit" : -1
} Response Status Response Body {
"crosspoints" :[
"1:josh"
],
"paths" :[
{
"objects" :[
"2:ripple" ,
"1:josh"
]
},
{
"objects" :[
"2:lop" ,
"1:josh"
]
}
],
"vertices" :[
{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"price" :[
{
"id" : "2:ripple>price" ,
"value" : 199
}
],
"name" :[
{
"id" : "2:ripple>name" ,
"value" : "ripple"
}
],
"lang" :[
{
"id" : "2:ripple>lang" ,
"value" : "java"
}
]
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:josh>city" ,
"value" : "Beijing"
}
],
"name" :[
{
"id" : "1:josh>name" ,
"value" : "josh"
}
],
"age" :[
{
"id" : "1:josh>age" ,
"value" : 32
}
]
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"price" :[
{
"id" : "2:lop>price" ,
"value" : 328
}
],
"name" :[
{
"id" : "2:lop>name" ,
"value" : "lop"
}
],
"lang" :[
{
"id" : "2:lop>lang" ,
"value" : "java"
}
]
}
}
]
} 3.2.18.3 适用场景 查询一组顶点通过多种路径在终点有交集的情况。例如:
在商品图谱中,多款手机、学习机、游戏机通过不同的低级别的类目路径,最终都属于一级类目的电子设备 3.2.19 Rings 3.2.19.1 功能介绍 根据起始顶点、方向、边的类型(可选)和最大深度等条件查找可达的环路
例如:1 -> 25 -> 775 -> 14690 -> 25, 其中环路为 25 -> 775 -> 14690 -> 25
Params source:起始顶点id,必填项 direction:起始顶点发出的边的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label max_depth:步数,必填项 source_in_ring:环路是否包含起点,选填项,默认为true max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的可达环路的最大数目,选填项,默认为10 3.2.19.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/rings?source="1:marko"&max_depth=2Response Status Response Body {
"rings" :[
{
"objects" :[
"1:marko" ,
"1:josh" ,
"1:marko"
]
},
{
"objects" :[
"1:marko" ,
"1:vadas" ,
"1:marko"
]
},
{
"objects" :[
"1:marko" ,
"2:lop" ,
"1:marko"
]
}
]
} 3.2.19.3 适用场景 查询起始顶点可达的环路,例如:
风控项目中,查询一个用户可达的循环担保的人或者设备 设备关联网络中,发现一个设备周围的循环引用的设备 3.2.20 Rays 3.2.20.1 功能介绍 根据起始顶点、方向、边的类型(可选)和最大深度等条件查找发散到边界顶点的路径
例如:1 -> 25 -> 775 -> 14690 -> 2289 -> 18379, 其中 18379 为边界顶点,即没有从 18379 发出的边
Params source:起始顶点id,必填项 direction:起始顶点发出的边的方向(OUT,IN,BOTH),选填项,默认是BOTH label:边的类型,选填项,默认代表所有edge label max_depth:步数,必填项 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000 capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000 limit:返回的非环路的最大数目,选填项,默认为10 3.2.20.2 使用方法 Method & Url GET http://localhost:8080/graphs/{graph}/traversers/rays?source="1:marko"&max_depth=2&direction=OUTResponse Status Response Body {
"rays" :[
{
"objects" :[
"1:marko" ,
"1:vadas"
]
},
{
"objects" :[
"1:marko" ,
"2:lop"
]
},
{
"objects" :[
"1:marko" ,
"1:josh" ,
"2:ripple"
]
},
{
"objects" :[
"1:marko" ,
"1:josh" ,
"2:lop"
]
}
]
} 3.2.20.3 适用场景 查找起始顶点到某种关系的边界顶点的路径,例如:
家族关系中,查找一个人到所有还没有孩子的子孙的路径 设备关联网络中,找到某个设备到终端设备的路径 3.2.21.1 功能介绍 按照条件查询一批顶点对应的"梭形相似点"。当两个顶点跟很多共同的顶点之间有某种关系的时候,我们认为这两个点为"梭形相似点"。举个例子说明"梭形相似点":“读者A"读了100本书,可以定义读过这100本书中的80本以上的读者,是"读者A"的"梭形相似点”
Params sources:定义起始顶点,必填项,指定方式包括:
ids:通过顶点id列表提供起始顶点 label和properties:如果没有指定ids,则使用label和properties的联合条件查询起始顶点label:顶点的类型 properties:通过属性的值查询起始顶点 注意:properties中的属性值可以是列表,表示只要key对应的value在列表中就可以
label:边的类型,选填项,默认代表所有edge label
direction:起始顶点向外发散的方向(OUT,IN,BOTH),选填项,默认是BOTH
min_neighbors:最少邻居数目,邻居数目少于这个阈值时,认为起点不具备"梭形相似点"。比如想要找一个"读者A"读过的书的"梭形相似点",那么min_neighbors为100时,表示"读者A"至少要读过100本书才可以有"梭形相似点",必填项
alpha:相似度,代表:起点与"梭形相似点"的共同邻居数目占起点的全部邻居数目的比例,必填项
min_similars:“梭形相似点"的最少个数,只有当起点的"梭形相似点"数目大于或等于该值时,才会返回起点及其"梭形相似点”,选填项,默认值为1
top:返回一个起点的"梭形相似点"中相似度最高的top个,必填项,0表示全部
group_property:与min_groups一起使用,当起点跟其所有的"梭形相似点"某个属性的值有至少min_groups个不同值时,才会返回该起点及其"梭形相似点"。比如为"读者A"推荐"异地"书友时,需要设置group_property为读者的"城市"属性,min_group至少为2,选填项,不填代表不需要根据属性过滤
min_groups:与group_property一起使用,只有group_property设置时才有意义
max_degree:查询过程中,单个顶点遍历的最大邻接边数目,选填项,默认为10000
capacity:遍历过程中最大的访问的顶点数目,选填项,默认为10000000
limit:返回的结果数目上限(一个起点及其"梭形相似点"算一个结果),选填项,默认为10
with_intermediary:是否返回起点及其"梭形相似点"共同关联的中间点,默认为false
with_vertex,选填项,默认为false:
true表示返回结果包含完整的顶点信息 false时表示只返回顶点id 3.2.21.2 使用方法 Method & Url POST http://localhost:8080/graphs/hugegraph/traversers/fusiformsimilarityRequest Body {
"sources" :{
"ids" :[],
"label" : "person" ,
"properties" : {
"name" : "p1"
}
},
"label" : "read" ,
"direction" : "OUT" ,
"min_neighbors" : 8 ,
"alpha" : 0.75 ,
"min_similars" : 1 ,
"top" : 0 ,
"group_property" : "city" ,
"min_group" : 2 ,
"max_degree" : 10000 ,
"capacity" : -1 ,
"limit" : -1 ,
"with_intermediary" : false ,
"with_vertex" : true
} Response Status Response Body {
"similars" : {
"3:p1" : [
{
"id" : "3:p2" ,
"score" : 0.8888888888888888 ,
"intermediaries" : [
]
},
{
"id" : "3:p3" ,
"score" : 0.7777777777777778 ,
"intermediaries" : [
]
}
]
},
"vertices" : [
{
"id" : "3:p1" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "p1" ,
"city" : "Beijing"
}
},
{
"id" : "3:p2" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "p2" ,
"city" : "Shanghai"
}
},
{
"id" : "3:p3" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"name" : "p3" ,
"city" : "Beijing"
}
}
]
} 3.2.21.3 适用场景 查询一组顶点相似度很高的顶点。例如:
跟一个读者有类似书单的读者 跟一个玩家玩类似游戏的玩家 3.2.22 Vertices 3.2.22.1 根据顶点的id列表,批量查询顶点 Params Method & Url GET http://localhost:8080/graphs/hugegraph/traversers/vertices?ids="1:marko"&ids="2:lop"Response Status Response Body {
"vertices" :[
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:marko>city" ,
"value" : "Beijing"
}
],
"name" :[
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" :[
{
"id" : "1:marko>age" ,
"value" : 29
}
]
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"price" :[
{
"id" : "2:lop>price" ,
"value" : 328
}
],
"name" :[
{
"id" : "2:lop>name" ,
"value" : "lop"
}
],
"lang" :[
{
"id" : "2:lop>lang" ,
"value" : "java"
}
]
}
}
]
} 3.2.22.2 获取顶点 Shard 信息 通过指定的分片大小split_size,获取顶点分片信息(可以与 3.2.21.3 中的 Scan 配合使用来获取顶点)。
Params Method & Url GET http://localhost:8080/graphs/hugegraph/traversers/vertices/shards?split_size=67108864Response Status Response Body {
"shards" :[
{
"start" : "0" ,
"end" : "2165893" ,
"length" : 0
},
{
"start" : "2165893" ,
"end" : "4331786" ,
"length" : 0
},
{
"start" : "4331786" ,
"end" : "6497679" ,
"length" : 0
},
{
"start" : "6497679" ,
"end" : "8663572" ,
"length" : 0
},
......
]
} 3.2.22.3 根据Shard信息批量获取顶点 通过指定的分片信息批量查询顶点(Shard信息的获取参见 3.2.21.2 Shard)。
Params start:分片起始位置,必填项 end:分片结束位置,必填项 page:分页位置,选填项,默认为null,不分页;当page为“”时表示分页的第一页,从start指示的位置开始 page_limit:分页获取顶点时,一页中顶点数目的上限,选填项,默认为100000 Method & Url GET http://localhost:8080/graphs/hugegraph/traversers/vertices/scan?start=0&end=4294967295Response Status Response Body {
"vertices" :[
{
"id" : "2:ripple" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"price" :[
{
"id" : "2:ripple>price" ,
"value" : 199
}
],
"name" :[
{
"id" : "2:ripple>name" ,
"value" : "ripple"
}
],
"lang" :[
{
"id" : "2:ripple>lang" ,
"value" : "java"
}
]
}
},
{
"id" : "1:vadas" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:vadas>city" ,
"value" : "Hongkong"
}
],
"name" :[
{
"id" : "1:vadas>name" ,
"value" : "vadas"
}
],
"age" :[
{
"id" : "1:vadas>age" ,
"value" : 27
}
]
}
},
{
"id" : "1:peter" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:peter>city" ,
"value" : "Shanghai"
}
],
"name" :[
{
"id" : "1:peter>name" ,
"value" : "peter"
}
],
"age" :[
{
"id" : "1:peter>age" ,
"value" : 35
}
]
}
},
{
"id" : "1:josh" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:josh>city" ,
"value" : "Beijing"
}
],
"name" :[
{
"id" : "1:josh>name" ,
"value" : "josh"
}
],
"age" :[
{
"id" : "1:josh>age" ,
"value" : 32
}
]
}
},
{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" :{
"city" :[
{
"id" : "1:marko>city" ,
"value" : "Beijing"
}
],
"name" :[
{
"id" : "1:marko>name" ,
"value" : "marko"
}
],
"age" :[
{
"id" : "1:marko>age" ,
"value" : 29
}
]
}
},
{
"id" : "2:lop" ,
"label" : "software" ,
"type" : "vertex" ,
"properties" :{
"price" :[
{
"id" : "2:lop>price" ,
"value" : 328
}
],
"name" :[
{
"id" : "2:lop>name" ,
"value" : "lop"
}
],
"lang" :[
{
"id" : "2:lop>lang" ,
"value" : "java"
}
]
}
}
]
} 3.2.22.4 适用场景 按id列表查询顶点,可用于批量查询顶点,比如在path查询到多条路径之后,可以进一步查询某条路径的所有顶点属性。 获取分片和按分片查询顶点,可以用来遍历全部顶点 3.2.23 Edges 3.2.23.1 根据边的id列表,批量查询边 Params Method & Url GET http://localhost:8080/graphs/hugegraph/traversers/edges?ids="S1:josh>1>>S2:lop"&ids="S1:josh>1>>S2:ripple"Response Status Response Body {
"edges" : [
{
"id" : "S1:josh>1>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:josh" ,
"properties" : {
"date" : "20091111" ,
"weight" : 0.4
}
},
{
"id" : "S1:josh>1>>S2:ripple" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"outV" : "1:josh" ,
"properties" : {
"date" : "20171210" ,
"weight" : 1
}
}
]
} 3.2.23.2 获取边 Shard 信息 通过指定的分片大小split_size,获取边分片信息(可以与 3.2.22.3 中的 Scan 配合使用来获取边)。
Params Method & Url GET http://localhost:8080/graphs/hugegraph/traversers/edges/shards?split_size=4294967295Response Status Response Body {
"shards" :[
{
"start" : "0" ,
"end" : "1073741823" ,
"length" : 0
},
{
"start" : "1073741823" ,
"end" : "2147483646" ,
"length" : 0
},
{
"start" : "2147483646" ,
"end" : "3221225469" ,
"length" : 0
},
{
"start" : "3221225469" ,
"end" : "4294967292" ,
"length" : 0
},
{
"start" : "4294967292" ,
"end" : "4294967295" ,
"length" : 0
}
]
} 3.2.23.3 根据 Shard 信息批量获取边 通过指定的分片信息批量查询边(Shard信息的获取参见 3.2.22.2)。
Params start:分片起始位置,必填项 end:分片结束位置,必填项 page:分页位置,选填项,默认为null,不分页;当page为“”时表示分页的第一页,从start指示的位置开始 page_limit:分页获取边时,一页中边数目的上限,选填项,默认为100000 Method & Url GET http://localhost:8080/graphs/hugegraph/traversers/edges/scan?start=0&end=3221225469Response Status Response Body {
"edges" :[
{
"id" : "S1:peter>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:peter" ,
"properties" :{
"weight" : 0.2 ,
"date" : "20170324"
}
},
{
"id" : "S1:josh>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:josh" ,
"properties" :{
"weight" : 0.4 ,
"date" : "20091111"
}
},
{
"id" : "S1:josh>2>>S2:ripple" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:ripple" ,
"outV" : "1:josh" ,
"properties" :{
"weight" : 1 ,
"date" : "20171210"
}
},
{
"id" : "S1:marko>1>20130220>S1:josh" ,
"label" : "knows" ,
"type" : "edge" ,
"inVLabel" : "person" ,
"outVLabel" : "person" ,
"inV" : "1:josh" ,
"outV" : "1:marko" ,
"properties" :{
"weight" : 1 ,
"date" : "20130220"
}
},
{
"id" : "S1:marko>1>20160110>S1:vadas" ,
"label" : "knows" ,
"type" : "edge" ,
"inVLabel" : "person" ,
"outVLabel" : "person" ,
"inV" : "1:vadas" ,
"outV" : "1:marko" ,
"properties" :{
"weight" : 0.5 ,
"date" : "20160110"
}
},
{
"id" : "S1:marko>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:marko" ,
"properties" :{
"weight" : 0.4 ,
"date" : "20171210"
}
}
]
} 3.2.23.4 适用场景 按id列表查询边,可用于批量查询边 获取分片和按分片查询边,可以用来遍历全部边 1.10 - Rank API 4.1 rank API 概述 HugeGraphServer 除了上一节提到的遍历(traverser)方法,还提供了一类专门做推荐的方法,我们称为rank API,
可在图中为一个点推荐与其关系密切的其它点。
4.2 rank API 详解 4.2.1 Personal Rank API Personal Rank 算法典型场景是用于推荐应用中, 根据某个点现有的出边, 推荐具有相近 / 相同关系的其他点,
比如根据某个人的阅读记录 / 习惯, 向它推荐其他可能感兴趣的书, 或潜在的书友, 举例如下:
假设给定 1个 Person 点 是 tom, 它喜欢 a,b,c,d,e 5本书, 我们的想给 tom 推荐一些书友, 以及一些书, 最容易的想法就是看看还有哪些人喜欢过这些书 (共同兴趣) 那么此时, 需要有其它的 Person 点比如 neo, 他喜欢 b,d,f 3本书, 以及 jay, 它喜欢 c,d,e,g 4本书, lee 它喜欢 a,d,e,f 4本书 由于 tom 已经看过的书不需要重复推荐, 所以返回结果里应该期望推荐有共同喜好的其他书友看过, 但 tom 没看过的书, 比如推荐 “f” 和 “g” 书, 且优先级 f > g 此时再计算 tom 的个性化 rank 值, 就会返回排序后 TopN 推荐的 书友 + 书 的结果了 (如果只需要推荐的书, 选择 OTHER_LABEL 即可) 4.2.1.0 数据准备 上面是一个简单的例子, 这里再提供一个公开的 1MB 测试数据集 MovieLens 为例,
用户需下载该数据集,然后使用 HugeGraph-Loader 导入到 HugeGraph 中,简单起见,数据中顶点 user
和 movie 的属性都忽略,仅使用 id 字段即可,边 rating 的具体评分值也忽略。loader 使用的元数据
文件和输入源映射文件内容如下:
////////////////////////////////////////////////////////////
// UserID::Gender::Age::Occupation::Zip-code
// MovieID::Title::Genres
// UserID::MovieID::Rating::Timestamp
////////////////////////////////////////////////////////////
// Define schema
schema . propertyKey ( "id" ). asInt (). ifNotExist (). create ();
schema . propertyKey ( "rate" ). asInt (). ifNotExist (). create ();
schema . vertexLabel ( "user" )
. properties ( "id" )
. primaryKeys ( "id" )
. ifNotExist ()
. create ();
schema . vertexLabel ( "movie" )
. properties ( "id" )
. primaryKeys ( "id" )
. ifNotExist ()
. create ();
schema . edgeLabel ( "rating" )
. sourceLabel ( "user" )
. targetLabel ( "movie" )
. properties ( "rate" )
. ifNotExist ()
. create (); {
"vertices" : [
{
"label" : "user" ,
"input" : {
"type" : "file" ,
"path" : "users.dat" ,
"format" : "TEXT" ,
"delimiter" : "::" ,
"header" : [ "UserID" , "Gender" , "Age" , "Occupation" , "Zip-code" ]
},
"ignored" : [ "Gender" , "Age" , "Occupation" , "Zip-code" ],
"mapping" : {
"UserID" : "id"
}
},
{
"label" : "movie" ,
"input" : {
"type" : "file" ,
"path" : "movies.dat" ,
"format" : "TEXT" ,
"delimiter" : "::" ,
"header" : [ "MovieID" , "Title" , "Genres" ]
},
"ignored" : [ "Title" , "Genres" ],
"mapping" : {
"MovieID" : "id"
}
}
],
"edges" : [
{
"label" : "rating" ,
"source" : [ "UserID" ],
"target" : [ "MovieID" ],
"input" : {
"type" : "file" ,
"path" : "ratings.dat" ,
"format" : "TEXT" ,
"delimiter" : "::" ,
"header" : [ "UserID" , "MovieID" , "Rating" , "Timestamp" ]
},
"ignored" : [ "Timestamp" ],
"mapping" : {
"UserID" : "id" ,
"MovieID" : "id" ,
"Rating" : "rate"
}
}
]
} 注意将映射文件中input.path的值修改为自己本地的路径。
4.2.1.1 功能介绍 适用于二分图,给出所有源顶点相关的其他顶点及其相关性组成的列表。
二分图:也称二部图,是图论里的一种特殊模型,也是一种特殊的网络流。其最大的特点在于,可以将图里的顶点分为两个集合,两个集合之间的点有边相连,但集合内的点之间没有直接关联。
假设有一个用户和物品的二分图,基于随机游走的 PersonalRank 算法步骤如下:
选定一个起点用户 u,其初始权重为 1.0,从 Vu 开始游走(有 alpha 的概率走到邻居点,1 - alpha 的概率停留); 如果决定向外游走, 那么会选取某一个类型的出边, 例如 rating 来查找共同的打分人:那就从当前节点的邻居节点中按照均匀分布随机选择一个,并且按照均匀分布划分权重值; 给源顶点补偿权重 1 - alpha; 重复步骤2; 达到一定步数或达到精度后收敛,得到推荐列表。 Params 必填项 :
source: 源顶点 id label: 源点出发的某类边 label,须连接两类不同顶点 选填项 :
alpha:每轮迭代时从某个点往外走的概率,与 PageRank 算法中的 alpha 类似,取值区间为 (0, 1], 默认值 0.85 max_degree: 查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 max_depth: 迭代次数,取值区间为 [2, 50], 默认值 5 with_label:筛选结果中保留哪些结果,可选以下三类, 默认为 BOTH_LABELSAME_LABEL:仅保留与源顶点相同类别的顶点 OTHER_LABEL:仅保留与源顶点不同类别(二分图的另一端)的顶点 BOTH_LABEL:同时保留与源顶点相同和相反类别的顶点 limit: 返回的顶点的最大数目,默认为 100 max_diff: 提前收敛的精度差, 默认为 0.0001 (后续实现 ) sorted:返回的结果是否根据 rank 排序,为 true 时降序排列,反之不排序,默认为 true 4.2.1.2 使用方法 Method & Url POST http://localhost:8080/graphs/hugegraph/traversers/personalrankRequest Body {
"source" : "1:1" ,
"label" : "rating" ,
"alpha" : 0.6 ,
"max_depth" : 15 ,
"with_label" : "OTHER_LABEL" ,
"sorted" : true ,
"limit" : 10
} Response Status Response Body {
"2:2858" : 0.0005014026017816927 ,
"2:1196" : 0.0004336708357653617 ,
"2:1210" : 0.0004128083140214213 ,
"2:593" : 0.00038117341069881513 ,
"2:480" : 0.00037005373269728036 ,
"2:1198" : 0.000366641614652057 ,
"2:2396" : 0.0003622362410538888 ,
"2:2571" : 0.0003593312457300953 ,
"2:589" : 0.00035922123055598566 ,
"2:110" : 0.0003466135844390885
} 4.2.1.3 适用场景 两类不同顶点连接形成的二分图中,给某个点推荐相关性最高的其他顶点,例如:
阅读推荐: 找出优先给某人推荐的其他书籍 , 也可以同时推荐共同喜好最高的书友 (例: 微信 “你的好友也在看 xx 文章” 功能) 社交推荐: 找出拥有相同关注话题的其他博主 , 也可以推荐可能感兴趣的新闻/消息 (例: Weibo 中的 “热点推荐” 功能) 商品推荐: 通过某人现在的购物习惯, 找出应优先推给它的商品列表 , 也可以给它推荐带货 播主 (例: TaoBao 的 “猜你喜欢” 功能) 4.2.2 Neighbor Rank API 4.2.2.0 数据准备 public class Loader {
public static void main ( String [] args ) {
HugeClient client = new HugeClient ( "http://127.0.0.1:8080" , "hugegraph" );
SchemaManager schema = client . schema ();
schema . propertyKey ( "name" ). asText (). ifNotExist (). create ();
schema . vertexLabel ( "person" )
. properties ( "name" )
. useCustomizeStringId ()
. ifNotExist ()
. create ();
schema . vertexLabel ( "movie" )
. properties ( "name" )
. useCustomizeStringId ()
. ifNotExist ()
. create ();
schema . edgeLabel ( "follow" )
. sourceLabel ( "person" )
. targetLabel ( "person" )
. ifNotExist ()
. create ();
schema . edgeLabel ( "like" )
. sourceLabel ( "person" )
. targetLabel ( "movie" )
. ifNotExist ()
. create ();
schema . edgeLabel ( "directedBy" )
. sourceLabel ( "movie" )
. targetLabel ( "person" )
. ifNotExist ()
. create ();
GraphManager graph = client . graph ();
Vertex O = graph . addVertex ( T . label , "person" , T . id , "O" , "name" , "O" );
Vertex A = graph . addVertex ( T . label , "person" , T . id , "A" , "name" , "A" );
Vertex B = graph . addVertex ( T . label , "person" , T . id , "B" , "name" , "B" );
Vertex C = graph . addVertex ( T . label , "person" , T . id , "C" , "name" , "C" );
Vertex D = graph . addVertex ( T . label , "person" , T . id , "D" , "name" , "D" );
Vertex E = graph . addVertex ( T . label , "movie" , T . id , "E" , "name" , "E" );
Vertex F = graph . addVertex ( T . label , "movie" , T . id , "F" , "name" , "F" );
Vertex G = graph . addVertex ( T . label , "movie" , T . id , "G" , "name" , "G" );
Vertex H = graph . addVertex ( T . label , "movie" , T . id , "H" , "name" , "H" );
Vertex I = graph . addVertex ( T . label , "movie" , T . id , "I" , "name" , "I" );
Vertex J = graph . addVertex ( T . label , "movie" , T . id , "J" , "name" , "J" );
Vertex K = graph . addVertex ( T . label , "person" , T . id , "K" , "name" , "K" );
Vertex L = graph . addVertex ( T . label , "person" , T . id , "L" , "name" , "L" );
Vertex M = graph . addVertex ( T . label , "person" , T . id , "M" , "name" , "M" );
O . addEdge ( "follow" , A );
O . addEdge ( "follow" , B );
O . addEdge ( "follow" , C );
D . addEdge ( "follow" , O );
A . addEdge ( "follow" , B );
A . addEdge ( "like" , E );
A . addEdge ( "like" , F );
B . addEdge ( "like" , G );
B . addEdge ( "like" , H );
C . addEdge ( "like" , I );
C . addEdge ( "like" , J );
E . addEdge ( "directedBy" , K );
F . addEdge ( "directedBy" , B );
F . addEdge ( "directedBy" , L );
G . addEdge ( "directedBy" , M );
}
} 4.2.2.1 功能介绍 在一般图结构中,找出每一层与给定起点相关性最高的前 N 个顶点及其相关度,用图的语义理解就是:从起点往外走,
走到各层各个顶点的概率。
Params source: 源顶点 id,必填项 alpha:每轮迭代时从某个点往外走的概率,与 PageRank 算法中的 alpha 类似,必填项,取值区间为 (0, 1] steps: 表示从起始顶点走过的路径规则,是一组 Step 的列表,每个 Step 对应结果中的一层,必填项。每个 Step 的结构如下:direction:表示边的方向(OUT, IN, BOTH),默认是 BOTH labels:边的类型列表,多个边类型取并集 max_degree:查询过程中,单个顶点遍历的最大邻接边数目,默认为 10000 (注: 0.12版之前 step 内仅支持 degree 作为参数名, 0.12开始统一使用 max_degree, 并向下兼容 degree 写法) top:在结果中每一层只保留权重最高的前 N 个结果,默认为 100,最大值为 1000 capacity: 遍历过程中最大的访问的顶点数目,选填项,默认为10000000 4.2.2.2 使用方法 Method & Url POST http://localhost:8080/graphs/hugegraph/traversers/neighborrankRequest Body {
"source" : "O" ,
"steps" :[
{
"direction" : "OUT" ,
"labels" :[
"follow"
],
"max_degree" : -1 ,
"top" : 100
},
{
"direction" : "OUT" ,
"labels" :[
"follow" ,
"like"
],
"max_degree" : -1 ,
"top" : 100
},
{
"direction" : "OUT" ,
"labels" :[
"directedBy"
],
"max_degree" : -1 ,
"top" : 100
}
],
"alpha" : 0.9 ,
"capacity" : -1
} Response Status Response Body {
"ranks" : [
{
"O" : 1
},
{
"B" : 0.4305 ,
"A" : 0.3 ,
"C" : 0.3
},
{
"G" : 0.17550000000000002 ,
"H" : 0.17550000000000002 ,
"I" : 0.135 ,
"J" : 0.135 ,
"E" : 0.09000000000000001 ,
"F" : 0.09000000000000001
},
{
"M" : 0.15795 ,
"K" : 0.08100000000000002 ,
"L" : 0.04050000000000001
}
]
} 4.2.2.3 适用场景 为给定的起点在不同的层中找到最应该推荐的顶点。
比如:在观众、朋友、电影、导演的四层图结构中,根据某个观众的朋友们喜欢的电影,为这个观众推荐电影;或者根据这些电影是谁拍的,为其推荐导演。 1.11 - Variable API 5.1 Variables Variables可以用来存储有关整个图的数据,数据按照键值对的方式存取
5.1.1 创建或者更新某个键值对 Method & Url PUT http://localhost:8080/graphs/hugegraph/variables/nameRequest Body Response Status Response Body 5.1.2 列出全部键值对 Method & Url GET http://localhost:8080/graphs/hugegraph/variablesResponse Status Response Body 5.1.3 列出某个键值对 Method & Url GET http://localhost:8080/graphs/hugegraph/variables/nameResponse Status Response Body 5.1.4 删除某个键值对 Method & Url DELETE http://localhost:8080/graphs/hugegraph/variables/nameResponse Status 1.12 - Graphs API 6.1 Graphs 6.1.1 列出数据库中全部的图 Method & Url GET http://localhost:8080/graphsResponse Status Response Body {
"graphs" : [
"hugegraph" ,
"hugegraph1"
]
} 6.1.2 查看某个图的信息 Method & Url GET http://localhost:8080/graphs/hugegraphResponse Status Response Body {
"name" : "hugegraph" ,
"backend" : "cassandra"
} 6.1.3 清空某个图的全部数据,包括schema、vertex、edge和index等,该操作需要管理员权限 Params 由于清空图是一个比较危险的操作,为避免用户误调用,我们给API添加了用于确认的参数:
confirm_message: 默认为I'm sure to delete all data Method & Url DELETE http://localhost:8080/graphs/hugegraph/clear?confirm_message=I%27m+sure+to+delete+all+dataResponse Status 6.1.4 克隆一个图,该操作需要管理员权限 Params clone_graph_name: 已有图的名称;从已有的图来克隆,用户可选择传递配置文件,传递时将替换已有图中的配置; Method & Url POST http://localhost:8080/graphs/hugegraph_clone?clone_graph_name=hugegraphRequest Body 【可选】 gremlin.graph=com.baidu.hugegraph.auth.HugeFactoryAuthProxy
backend=rocksdb
serializer=binary
store=hugegraph_clone
rocksdb.data_path=./hg2
rocksdb.wal_path=./hg2Response Status Response Body {
"name" : "hugegraph_clone" ,
"backend" : "rocksdb"
} 6.1.5 创建一个图,该操作需要管理员权限 Method & Url POST http://localhost:8080/graphs/hugegraph2Request Body gremlin.graph=com.baidu.hugegraph.auth.HugeFactoryAuthProxy
backend=rocksdb
serializer=binary
store=hugegraph2
rocksdb.data_path=./hg2
rocksdb.wal_path=./hg2Response Status Response Body {
"name" : "hugegraph2" ,
"backend" : "rocksdb"
} 6.1.6 删除某个图及其全部数据 Params 由于删除图是一个比较危险的操作,为避免用户误调用,我们给API添加了用于确认的参数:
confirm_message: 默认为I'm sure to drop the graph Method & Url DELETE http://localhost:8080/graphs/hugegraph_clone?confirm_message=I%27m%20sure%20to%20drop%20the%20graphResponse Status 6.2 Conf 6.2.1 查看某个图的配置,该操作需要管理员权限 Method & Url GET http://localhost:8080/graphs/hugegraph/confResponse Status Response Body # gremlin entrence to create graph
gremlin.graph = com.baidu.hugegraph.HugeFactory
# cache config
#schema.cache_capacity=1048576
#graph.cache_capacity=10485760
#graph.cache_expire=600
# schema illegal name template
#schema.illegal_name_regex=\s+|~.*
#vertex.default_label=vertex
backend = cassandra
serializer = cassandra
store = hugegraph
... 6.3 Mode 合法的图模式包括:NONE,RESTORING,MERGING,LOADING
None 模式(默认),元数据和图数据的写入属于正常状态。特别的:元数据(schema)创建时不允许指定 ID 图数据(vertex)在 id strategy 为 Automatic 时,不允许指定 ID LOADING:批量导入数据时自动启用,特别的: Restore 时存在两种不同的模式: Restoring 和 Merging
Restoring 模式,恢复到一个新图中,特别的:元数据(schema)创建时允许指定 ID 图数据(vertex)在 id strategy 为 Automatic 时,允许指定 ID Merging 模式,合并到一个已存在元数据和图数据的图中,特别的:元数据(schema)创建时不允许指定 ID 图数据(vertex)在 id strategy 为 Automatic 时,允许指定 ID 正常情况下,图模式为 None,当需要 Restore 图时,需要根据需要临时修改图模式为 Restoring 模式或者 Merging 模式,并在完成 Restore 时,恢复图模式为 None。
6.3.1 查看某个图的模式. Method & Url GET http://localhost:8080/graphs/hugegraph/modeResponse Status Response Body 合法的图模式包括:NONE,RESTORING,MERGING
6.3.2 设置某个图的模式. 该操作需要管理员权限 Method & Url PUT http://localhost:8080/graphs/hugegraph/modeRequest Body 合法的图模式包括:NONE,RESTORING,MERGING
Response Status Response Body 6.3.3 查看某个图的读模式. Params Method & Url GET http://localhost:8080/graphs/hugegraph/graph_read_modeResponse Status Response Body {
"graph_read_mode" : "ALL"
} 6.3.4 设置某个图的读模式. 该操作需要管理员权限 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/graph_read_modeRequest Body 合法的图模式包括:ALL,OLTP_ONLY,OLAP_ONLY
Response Status Response Body {
"graph_read_mode" : "OLTP_ONLY"
} 6.4 Snapshot 6.4.1 创建快照 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/snapshot_createResponse Status Response Body {
"hugegraph" : "snapshot_created"
} 6.4.2 快照恢复 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/snapshot_resumeResponse Status Response Body {
"hugegraph" : "snapshot_resumed"
} 6.5 Compact 6.5.1 手动压缩图,该操作需要管理员权限 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/compactResponse Status Response Body {
"nodes" : 1 ,
"cluster_id" : "local" ,
"servers" : {
"local" : "OK"
}
} 1.13 - Task API 7.1 Task 7.1.1 列出某个图中全部的异步任务 Params status: 异步任务的状态 limit:返回异步任务数目上限 Method & Url GET http://localhost:8080/graphs/hugegraph/tasks?status=successResponse Status Response Body {
"tasks" : [{
"task_name" : "hugegraph.traversal().V()" ,
"task_progress" : 0 ,
"task_create" : 1532943976585 ,
"task_status" : "success" ,
"task_update" : 1532943976736 ,
"task_result" : "0" ,
"task_retries" : 0 ,
"id" : 2 ,
"task_type" : "gremlin" ,
"task_callable" : "com.baidu.hugegraph.api.job.GremlinAPI$GremlinJob" ,
"task_input" : "{\"gremlin\":\"hugegraph.traversal().V()\",\"bindings\":{},\"language\":\"gremlin-groovy\",\"aliases\":{\"hugegraph\":\"graph\"}}"
}]
} 7.1.2 查看某个异步任务的信息 Method & Url GET http://localhost:8080/graphs/hugegraph/tasks/2Response Status Response Body {
"task_name" : "hugegraph.traversal().V()" ,
"task_progress" : 0 ,
"task_create" : 1532943976585 ,
"task_status" : "success" ,
"task_update" : 1532943976736 ,
"task_result" : "0" ,
"task_retries" : 0 ,
"id" : 2 ,
"task_type" : "gremlin" ,
"task_callable" : "com.baidu.hugegraph.api.job.GremlinAPI$GremlinJob" ,
"task_input" : "{\"gremlin\":\"hugegraph.traversal().V()\",\"bindings\":{},\"language\":\"gremlin-groovy\",\"aliases\":{\"hugegraph\":\"graph\"}}"
} 7.1.3 删除某个异步任务信息,不删除异步任务本身 Method & Url DELETE http://localhost:8080/graphs/hugegraph/tasks/2Response Status 7.1.4 取消某个异步任务,该异步任务必须具有处理中断的能力 假设已经通过Gremlin API 创建了一个异步任务如下:
"for (int i = 0; i < 10; i++) {" +
"hugegraph.addVertex(T.label, 'man');" +
"hugegraph.tx().commit();" +
"try {" +
"sleep(1000);" +
"} catch (InterruptedException e) {" +
"break;" +
"}" +
"}" Method & Url PUT http://localhost:8080/graphs/hugegraph/tasks/2?action=cancel请保证在10秒内发送该请求,如果超过10秒发送,任务可能已经执行完成,无法取消。
Response Status Response Body 此时查询 label 为 man 的顶点数目,一定是小于 10 的。
1.14 - Gremlin API 8.1 Gremlin 8.1.1 向HugeGraphServer发送gremlin语句(GET),同步执行 Params gremlin: 要发送给HugeGraphServer执行的gremlin语句 bindings: 用来绑定参数,key是字符串,value是绑定的值(只能是字符串或者数字),功能类似于MySQL的 Prepared Statement,用于加速语句执行 language: 发送语句的语言类型,默认为gremlin-groovy aliases: 为存在于图空间的已有变量添加别名 查询顶点
Method & Url GET http://127.0.0.1:8080/gremlin?gremlin=hugegraph.traversal().V('1:marko')Response Status Response Body {
"requestId" : "c6ef47a8-b634-4b07-9d38-6b3b69a3a556" ,
"status" : {
"message" : "" ,
"code" : 200 ,
"attributes" : {}
},
"result" : {
"data" : [{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [{
"id" : "1:marko>city" ,
"value" : "Beijing"
}],
"name" : [{
"id" : "1:marko>name" ,
"value" : "marko"
}],
"age" : [{
"id" : "1:marko>age" ,
"value" : 29
}]
}
}],
"meta" : {}
}
} 8.1.2 向HugeGraphServer发送gremlin语句(POST),同步执行 Method & Url POST http://localhost:8080/gremlin查询顶点
Request Body {
"gremlin" : "hugegraph.traversal().V('1:marko')" ,
"bindings" : {},
"language" : "gremlin-groovy" ,
"aliases" : {}
} Response Status Response Body {
"requestId" : "c6ef47a8-b634-4b07-9d38-6b3b69a3a556" ,
"status" : {
"message" : "" ,
"code" : 200 ,
"attributes" : {}
},
"result" : {
"data" : [{
"id" : "1:marko" ,
"label" : "person" ,
"type" : "vertex" ,
"properties" : {
"city" : [{
"id" : "1:marko>city" ,
"value" : "Beijing"
}],
"name" : [{
"id" : "1:marko>name" ,
"value" : "marko"
}],
"age" : [{
"id" : "1:marko>age" ,
"value" : 29
}]
}
}],
"meta" : {}
}
} 注意:
这里是直接使用图对象(hugegraph),先获取其遍历器(traversal()),再获取顶点。
不能直接写成graph.traversal().V()或g.V(),可以通过"aliases": {"graph": "hugegraph", "g": "__g_hugegraph"}
为图和遍历器添加别名后使用别名操作。其中,hugegraph是原生存在的变量,__g_hugegraph是HugeGraphServer额外添加的变量,
每个图都会存在一个对应的这样格式(_g ${graph})的遍历器对象。
响应体的结构与其他 Vertex 或 Edge 的 RESTful API的结构有区别,用户可能需要自行解析。
查询边
Request Body {
"gremlin" : "g.E('S1:marko>2>>S2:lop')" ,
"bindings" : {},
"language" : "gremlin-groovy" ,
"aliases" : {
"graph" : "hugegraph" ,
"g" : "__g_hugegraph"
}
} Response Status Response Body {
"requestId" : "3f117cd4-eedc-4e08-a106-ee01d7bb8249" ,
"status" : {
"message" : "" ,
"code" : 200 ,
"attributes" : {}
},
"result" : {
"data" : [{
"id" : "S1:marko>2>>S2:lop" ,
"label" : "created" ,
"type" : "edge" ,
"inVLabel" : "software" ,
"outVLabel" : "person" ,
"inV" : "2:lop" ,
"outV" : "1:marko" ,
"properties" : {
"weight" : 0.4 ,
"date" : "20171210"
}
}],
"meta" : {}
}
} 8.1.3 向HugeGraphServer发送gremlin语句(POST),异步执行 Method & Url POST http://localhost:8080/graphs/hugegraph/jobs/gremlin查询顶点
Request Body {
"gremlin" : "g.V('1:marko')" ,
"bindings" : {},
"language" : "gremlin-groovy" ,
"aliases" : {}
} 注意:
异步执行Gremlin语句暂不支持aliases,可以使用 graph 代表要操作的图,也可以直接使用图的名字, 例如 hugegraph;
另外g代表 traversal,等价于 graph.traversal() 或者 hugegraph.traversal()
Response Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/1(其中"1"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
查询边
Request Body {
"gremlin" : "g.E('S1:marko>2>>S2:lop')" ,
"bindings" : {},
"language" : "gremlin-groovy" ,
"aliases" : {}
} Response Status Response Body 注:
可以通过GET http://localhost:8080/graphs/hugegraph/tasks/2(其中"2"是task_id)来查询异步任务的执行状态,更多异步任务RESTful API
1.15 - Authentication API 9.1 用户认证与权限控制 开启权限及相关配置请先参考 权限配置 文档
用户认证与权限控制概述: HugeGraph支持多用户认证、以及细粒度的权限访问控制,采用基于“用户-用户组-操作-资源”的4层设计,灵活控制用户角色与权限。
资源描述了图数据库中的数据,比如符合某一类条件的顶点,每一个资源包括type、label、properties三个要素,共有18种type、
任意label、任意properties的组合形成的资源,一个资源的内部条件是且关系,多个资源之间的条件是或关系。用户可以属于一个或多个用户组,
每个用户组可以拥有对任意个资源的操作权限,操作类型包括:读、写、删除、执行等种类。 HugeGraph支持动态创建用户、用户组、资源,
支持动态分配或取消权限。初始化数据库时超级管理员用户被创建,后续可通过超级管理员创建各类角色用户,新创建的用户如果被分配足够权限后,可以由其创建或管理更多的用户。
举例说明: user(name=boss) -belong-> group(name=all) -access(read)-> target(graph=graph1, resource={label: person,
city: Beijing}) 描述:用户’boss’拥有对’graph1’图中北京人的读权限。
接口说明: 用户认证与权限控制接口包括5类:UserAPI、GroupAPI、TargetAPI、BelongAPI、AccessAPI。
9.2 用户(User)API 用户接口包括:创建用户,删除用户,修改用户,和查询用户相关信息接口。
9.2.1 创建用户 Params user_name: 用户名称 user_password: 用户密码 user_phone: 用户手机号 user_email: 用户邮箱 其中 user_name 和 user_password 为必填。
Request Body {
"user_name" : "boss" ,
"user_password" : "******" ,
"user_phone" : "182****9088" ,
"user_email" : "123@xx.com"
} Method & Url POST http://localhost:8080/graphs/hugegraph/auth/usersResponse Status Response Body 返回报文中,密码为加密后的密文
{
"user_password" : "******" ,
"user_email" : "123@xx.com" ,
"user_update" : "2020-11-17 14:31:07.833" ,
"user_name" : "boss" ,
"user_creator" : "admin" ,
"user_phone" : "182****9088" ,
"id" : "-63:boss" ,
"user_create" : "2020-11-17 14:31:07.833"
} 9.2.2 删除用户 Params Method & Url DELETE http://localhost:8080/graphs/hugegraph/auth/users/-63:testResponse Status Response Body 9.2.3 修改用户 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/auth/users/-63:testRequest Body 修改user_name、user_password和user_phone
{
"user_name" : "test" ,
"user_password" : "******" ,
"user_phone" : "183****9266"
} Response Status Response Body 返回结果是包含修改过的内容在内的整个用户组对象
{
"user_password" : "******" ,
"user_update" : "2020-11-12 10:29:30.455" ,
"user_name" : "test" ,
"user_creator" : "admin" ,
"user_phone" : "183****9266" ,
"id" : "-63:test" ,
"user_create" : "2020-11-12 10:27:13.601"
} 9.2.4 查询用户列表 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/usersResponse Status Response Body {
"users" : [
{
"user_password" : "******" ,
"user_update" : "2020-11-11 11:41:12.254" ,
"user_name" : "admin" ,
"user_creator" : "system" ,
"id" : "-63:admin" ,
"user_create" : "2020-11-11 11:41:12.254"
}
]
} 9.2.5 查询某个用户 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/users/-63:adminResponse Status Response Body {
"users" : [
{
"user_password" : "******" ,
"user_update" : "2020-11-11 11:41:12.254" ,
"user_name" : "admin" ,
"user_creator" : "system" ,
"id" : "-63:admin" ,
"user_create" : "2020-11-11 11:41:12.254"
}
]
} 9.2.6 查询某个用户的角色 Method & Url GET http://localhost:8080/graphs/hugegraph/auth/users/-63:boss/roleResponse Status Response Body {
"roles" : {
"hugegraph" : {
"READ" : [
{
"type" : "ALL" ,
"label" : "*" ,
"properties" : null
}
]
}
}
} 9.3 用户组(Group)API 用户组会赋予相应的资源权限,用户会被分配不同的用户组,即可拥有不同的资源权限。 用户组接口包括:创建用户组,删除用户组,修改用户组,和查询用户组相关信息接口。
9.3.1 创建用户组 Params group_name: 用户组名称 group_description: 用户组描述 Request Body {
"group_name" : "all" ,
"group_description" : "group can do anything"
} Method & Url POST http://localhost:8080/graphs/hugegraph/auth/groupsResponse Status Response Body {
"group_creator" : "admin" ,
"group_name" : "all" ,
"group_create" : "2020-11-11 15:46:08.791" ,
"group_update" : "2020-11-11 15:46:08.791" ,
"id" : "-69:all" ,
"group_description" : "group can do anything"
} 9.3.2 删除用户组 Params Method & Url DELETE http://localhost:8080/graphs/hugegraph/auth/groups/-69:grantResponse Status Response Body 9.3.3 修改用户组 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/auth/groups/-69:grantRequest Body 修改group_description
{
"group_name" : "grant" ,
"group_description" : "grant"
} Response Status Response Body 返回结果是包含修改过的内容在内的整个用户组对象
{
"group_creator" : "admin" ,
"group_name" : "grant" ,
"group_create" : "2020-11-12 09:50:58.458" ,
"group_update" : "2020-11-12 09:57:58.155" ,
"id" : "-69:grant" ,
"group_description" : "grant"
} 9.3.4 查询用户组列表 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/groupsResponse Status Response Body {
"groups" : [
{
"group_creator" : "admin" ,
"group_name" : "all" ,
"group_create" : "2020-11-11 15:46:08.791" ,
"group_update" : "2020-11-11 15:46:08.791" ,
"id" : "-69:all" ,
"group_description" : "group can do anything"
}
]
} 9.3.5 查询某个用户组 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/groups/-69:allResponse Status Response Body {
"group_creator" : "admin" ,
"group_name" : "all" ,
"group_create" : "2020-11-11 15:46:08.791" ,
"group_update" : "2020-11-11 15:46:08.791" ,
"id" : "-69:all" ,
"group_description" : "group can do anything"
} 9.4 资源(Target)API 资源描述了图数据库中的数据,比如符合某一类条件的顶点,每一个资源包括type、label、properties三个要素,共有18种type、
任意label、任意properties的组合形成的资源,一个资源的内部条件是且关系,多个资源之间的条件是或关系。 资源接口包括:资源的创建、删除、修改和查询。
9.4.1 创建资源 Params target_name: 资源名称 target_graph: 资源图 target_url: 资源地址 target_resources: 资源定义(列表) target_resources可以包括多个target_resource,以列表的形式存储。 每个target_resource包含:
type:可选值 VERTEX, EDGE等, 可填ALL,则表示可以是顶点或边; label:可选值,⼀个顶点或边类型的名称,可填*,则表示任意类型; properties:map类型,可包含多个属性的键值对,必须匹配所有属性值,属性值⽀持填条件范围(age:
P.gte(18)),properties如果为null表示任意属性均可,如果属性名和属性值均为‘*ʼ也表示任意属性均可。 如精细资源:“target_resources”: [{“type”:“VERTEX”,“label”:“person”,“properties”:{“city”:“Beijing”,“age”:“P.gte(20)”}}]** 资源定义含义:类型是’person’的顶点,且城市属性是’Beijing’,年龄属性大于等于20。
Request Body {
"target_name" : "all" ,
"target_graph" : "hugegraph" ,
"target_url" : "127.0.0.1:8080" ,
"target_resources" : [
{
"type" : "ALL"
}
]
} Method & Url POST http://localhost:8080/graphs/hugegraph/auth/targetsResponse Status Response Body {
"target_creator" : "admin" ,
"target_name" : "all" ,
"target_url" : "127.0.0.1:8080" ,
"target_graph" : "hugegraph" ,
"target_create" : "2020-11-11 15:32:01.192" ,
"target_resources" : [
{
"type" : "ALL" ,
"label" : "*" ,
"properties" : null
}
],
"id" : "-77:all" ,
"target_update" : "2020-11-11 15:32:01.192"
} 9.4.2 删除资源 Params Method & Url DELETE http://localhost:8080/graphs/hugegraph/auth/targets/-77:gremlinResponse Status Response Body 9.4.3 修改资源 Params Method & Url PUT http://localhost:8080/graphs/hugegraph/auth/targets/-77:gremlinRequest Body 修改资源定义中的type
{
"target_name" : "gremlin" ,
"target_graph" : "hugegraph" ,
"target_url" : "127.0.0.1:8080" ,
"target_resources" : [
{
"type" : "NONE"
}
]
} Response Status Response Body 返回结果是包含修改过的内容在内的整个用户组对象
{
"target_creator" : "admin" ,
"target_name" : "gremlin" ,
"target_url" : "127.0.0.1:8080" ,
"target_graph" : "hugegraph" ,
"target_create" : "2020-11-12 09:34:13.848" ,
"target_resources" : [
{
"type" : "NONE" ,
"label" : "*" ,
"properties" : null
}
],
"id" : "-77:gremlin" ,
"target_update" : "2020-11-12 09:37:12.780"
} 9.4.4 查询资源列表 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/targetsResponse Status Response Body {
"targets" : [
{
"target_creator" : "admin" ,
"target_name" : "all" ,
"target_url" : "127.0.0.1:8080" ,
"target_graph" : "hugegraph" ,
"target_create" : "2020-11-11 15:32:01.192" ,
"target_resources" : [
{
"type" : "ALL" ,
"label" : "*" ,
"properties" : null
}
],
"id" : "-77:all" ,
"target_update" : "2020-11-11 15:32:01.192"
},
{
"target_creator" : "admin" ,
"target_name" : "grant" ,
"target_url" : "127.0.0.1:8080" ,
"target_graph" : "hugegraph" ,
"target_create" : "2020-11-11 15:43:24.841" ,
"target_resources" : [
{
"type" : "GRANT" ,
"label" : "*" ,
"properties" : null
}
],
"id" : "-77:grant" ,
"target_update" : "2020-11-11 15:43:24.841"
}
]
} 9.4.5 查询某个资源 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/targets/-77:grantResponse Status Response Body {
"target_creator" : "admin" ,
"target_name" : "grant" ,
"target_url" : "127.0.0.1:8080" ,
"target_graph" : "hugegraph" ,
"target_create" : "2020-11-11 15:43:24.841" ,
"target_resources" : [
{
"type" : "GRANT" ,
"label" : "*" ,
"properties" : null
}
],
"id" : "-77:grant" ,
"target_update" : "2020-11-11 15:43:24.841"
} 9.5 关联角色(Belong)API 关联用户和用户组的关系,一个用户可以关联一个或者多个用户组。用户组拥有相关资源的权限,不同用户组的资源权限可以理解为不同的角色。即给用户关联角色。 关联角色接口包括:用户关联角色的创建、删除、修改和查询。
9.5.1 创建用户的关联角色 Params user: 用户 Id group: 用户组 Id belong_description: 描述 Request Body {
"user" : "-63:boss" ,
"group" : "-69:all"
} Method & Url POST http://localhost:8080/graphs/hugegraph/auth/belongsResponse Status Response Body {
"belong_create" : "2020-11-11 16:19:35.422" ,
"belong_creator" : "admin" ,
"belong_update" : "2020-11-11 16:19:35.422" ,
"id" : "S-63:boss>-82>>S-69:all" ,
"user" : "-63:boss" ,
"group" : "-69:all"
} 9.5.2 删除关联角色 Params Method & Url DELETE http://localhost:8080/graphs/hugegraph/auth/belongs/S-63:boss>-82>>S-69:grantResponse Status Response Body 9.5.3 修改关联角色 关联角色只能修改描述,不能修改 user 和 group 属性,如果需要修改关联角色,需要删除原来关联关系,新增关联角色。
Params Method & Url PUT http://localhost:8080/graphs/hugegraph/auth/belongs/S-63:boss>-82>>S-69:grantRequest Body 修改belong_description
{
"belong_description" : "update test"
} Response Status Response Body 返回结果是包含修改过的内容在内的整个用户组对象
{
"belong_description" : "update test" ,
"belong_create" : "2020-11-12 10:40:21.720" ,
"belong_creator" : "admin" ,
"belong_update" : "2020-11-12 10:42:47.265" ,
"id" : "S-63:boss>-82>>S-69:grant" ,
"user" : "-63:boss" ,
"group" : "-69:grant"
} 9.5.4 查询关联角色列表 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/belongsResponse Status Response Body {
"belongs" : [
{
"belong_create" : "2020-11-11 16:19:35.422" ,
"belong_creator" : "admin" ,
"belong_update" : "2020-11-11 16:19:35.422" ,
"id" : "S-63:boss>-82>>S-69:all" ,
"user" : "-63:boss" ,
"group" : "-69:all"
}
]
} 9.5.5 查看某个关联角色 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/belongs/S-63:boss>-82>>S-69:allResponse Status Response Body {
"belong_create" : "2020-11-11 16:19:35.422" ,
"belong_creator" : "admin" ,
"belong_update" : "2020-11-11 16:19:35.422" ,
"id" : "S-63:boss>-82>>S-69:all" ,
"user" : "-63:boss" ,
"group" : "-69:all"
} 9.6 赋权(Access)API 给用户组赋予资源的权限,主要包含:读操作(READ)、写操作(WRITE)、删除操作(DELETE)、执行操作(EXECUTE)等。 赋权接口包括:赋权的创建、删除、修改和查询。
9.6.1 创建赋权(用户组赋予资源的权限) Params group: 用户组 Id target: 资源 Id access_permission: 权限许可 access_description: 赋权描述 access_permission:
READ:读操作,所有的查询,包括查询Schema、查顶点/边,查询顶点和边的数量VERTEX_AGGR/EDGE_AGGR,也包括读图的状态STATUS、变量VAR、任务TASK等; WRITE:写操作,所有的创建、更新操作,包括给Schema增加property key,给顶点增加或更新属性等; DELETE:删除操作,包括删除元数据、删除顶点/边; EXECUTE:执⾏操作,包括执⾏Gremlin语句、执⾏Task、执⾏metadata函数; Request Body {
"group" : "-69:all" ,
"target" : "-77:all" ,
"access_permission" : "READ"
} Method & Url POST http://localhost:8080/graphs/hugegraph/auth/accessesResponse Status Response Body {
"access_permission" : "READ" ,
"access_create" : "2020-11-11 15:54:54.008" ,
"id" : "S-69:all>-88>11>S-77:all" ,
"access_update" : "2020-11-11 15:54:54.008" ,
"access_creator" : "admin" ,
"group" : "-69:all" ,
"target" : "-77:all"
} 9.6.2 删除赋权 Params Method & Url DELETE http://localhost:8080/graphs/hugegraph/auth/accesses/S-69:all>-88>12>S-77:allResponse Status Response Body 9.6.3 修改赋权 赋权只能修改描述,不能修改用户组、资源和权限许可,如果需要修改赋权的关系,可以删除原来的赋权关系,新增赋权。
Params Method & Url PUT http://localhost:8080/graphs/hugegraph/auth/accesses/S-69:all>-88>12>S-77:allRequest Body 修改access_description
{
"access_description" : "test"
} Response Status Response Body 返回结果是包含修改过的内容在内的整个用户组对象
{
"access_description" : "test" ,
"access_permission" : "WRITE" ,
"access_create" : "2020-11-12 10:12:03.074" ,
"id" : "S-69:all>-88>12>S-77:all" ,
"access_update" : "2020-11-12 10:16:18.637" ,
"access_creator" : "admin" ,
"group" : "-69:all" ,
"target" : "-77:all"
} 9.6.4 查询赋权列表 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/accessesResponse Status Response Body {
"accesses" : [
{
"access_permission" : "READ" ,
"access_create" : "2020-11-11 15:54:54.008" ,
"id" : "S-69:all>-88>11>S-77:all" ,
"access_update" : "2020-11-11 15:54:54.008" ,
"access_creator" : "admin" ,
"group" : "-69:all" ,
"target" : "-77:all"
}
]
} 9.6.5 查询某个赋权 Params Method & Url GET http://localhost:8080/graphs/hugegraph/auth/accesses/S-69:all>-88>11>S-77:allResponse Status Response Body {
"access_permission" : "READ" ,
"access_create" : "2020-11-11 15:54:54.008" ,
"id" : "S-69:all>-88>11>S-77:all" ,
"access_update" : "2020-11-11 15:54:54.008" ,
"access_creator" : "admin" ,
"group" : "-69:all" ,
"target" : "-77:all"
} 1.16 - Other API 10.1 Other 10.1.1 查看HugeGraph的版本信息 Method & Url GET http://localhost:8080/versionsResponse Status Response Body {
"versions" : {
"version" : "v1" ,
"core" : "0.4.5.1" ,
"gremlin" : "3.2.5" ,
"api" : "0.13.2.0"
}
} 2 - HugeGraph Java Client 本文的代码都是java语言写的,但其风格与gremlin(groovy)是非常类似的。用户只需要把代码中的变量声明替换成def或直接去掉,
就能将java代码转变为groovy;另外就是每一行语句最后可以不加分号,groovy认为一行就是一条语句。
用户在HugeGraph-Studio中编写的gremlin(groovy)可以参考本文的java代码,下面会举出几个例子。
1 HugeGraph-Client HugeGraph-Client 是操作 graph 的总入口,用户必须先创建出 HugeGraph-Client 对象,与 HugeGraph-Server 建立连接(伪连接)后,才能获取到 schema、graph 以及 gremlin 的操作入口对象。
目前 HugeGraph-Client 只允许连接服务端已存在的图,无法自定义图进行创建。其创建方法如下:
// HugeGraphServer地址:"http://localhost:8080"
// 图的名称:"hugegraph"
HugeClient hugeClient = HugeClient . builder ( "http://localhost:8080" , "hugegraph" )
. configTimeout ( 20 ) // 默认 20s 超时
. configUser ( "**" , "**" ) // 默认未开启用户权限
. build (); 上述创建 HugeClient 的过程如果失败会抛出异常,用户需要try-catch。如果成功则继续获取 schema、graph 以及 gremlin 的 manager。
在HugeGraph - Hubble / Studio中通过gremlin来操作时,不需要使用HugeClient,可以忽略。
2 元数据 2.1 SchemaManager SchemaManager 用于管理 HugeGraph 中的四种元数据,分别是PropertyKey(属性类型)、VertexLabel(顶点类型)、EdgeLabel(边类型)和 IndexLabel(索引标签)。在定义元数据信息之前必须先创建 SchemaManager 对象。
用户可使用如下方法获得SchemaManager对象:
SchemaManager schema = hugeClient . schema () 在HugeGraph-Studio中通过gremlin创建schema对象:
下面分别对三种元数据的定义过程进行介绍。
2.2 PropertyKey 2.2.1 接口及参数介绍 PropertyKey 用来规范顶点和边的属性的约束,暂不支持定义属性的属性。
PropertyKey 允许定义的约束信息包括:name、datatype、cardinality、userdata,下面逐一介绍。
name: 属性的名字,用来区分不同的 PropertyKey,不允许有同名的属性; datatype:属性值类型,必须从下表中选择符合具体业务场景的一项显式设置; cardinality:属性值是单值还是多值,多值的情况下又分为允许有重复值和不允许有重复值,该项默认为 single,如有必要可从下表中选择一项设置; userdata:用户可以自己添加一些约束或额外信息,然后自行检查传入的属性是否满足约束,或者必要的时候提取出额外信息 2.2.2 创建 PropertyKey schema . propertyKey ( "name" ). asText (). valueSet (). ifNotExist (). create () 在HugeGraph-Studio中通过gremlin创建上述PropertyKey对象的语法完全一致,如果用户没有定义出schema变量,应该这样写:
graph . schema (). propertyKey ( "name" ). asText (). valueSet (). ifNotExist (). create () 以下的示例中,gremlin与java的语法完全一致,不再赘述。
ifNotExist():为 create 添加判断机制,若当前 PropertyKey 已经存在则不再创建,否则创建该属性。若不添加判断,在 properkey 已存在的情况下会抛出异常信息,下同,不再赘述。 2.2.3 删除 PropertyKey schema . propertyKey ( "name" ). remove () 2.2.4 查询 PropertyKey // 获取PropertyKey对象
schema . getPropertyKey ( "name" )
// 获取PropertyKey属性
schema . getPropertyKey ( "name" ). cardinality ()
schema . getPropertyKey ( "name" ). dataType ()
schema . getPropertyKey ( "name" ). name ()
schema . getPropertyKey ( "name" ). userdata () 2.3 VertexLabel 2.3.1 接口及参数介绍 VertexLabel 用来定义顶点类型,描述顶点的约束信息:
VertexLabel 允许定义的约束信息包括:name、idStrategy、properties、primaryKeys和 nullableKeys,下面逐一介绍。
name: 属性的名字,用来区分不同的 VertexLabel,不允许有同名的属性; idStrategy: 每一个 VertexLabel 都可以选择自己的 Id 策略,目前有三种策略供选择,即 Automatic(自动生成)、Customize(用户传入)和 PrimaryKey(主属性键)。其中 Automatic 使用 Snowflake 算法生成 Id,Customize 需要用户自行传入字符串或数字类型的 Id,PrimaryKey 则允许用户从 VertexLabel 的属性中选择若干主属性作为区分的依据,HugeGraph 内部会根据主属性的值拼接生成 Id。idStrategy 默认使用 Automatic的,但如果用户没有显式设置 idStrategy 又调用了 primaryKeys(…) 方法设置了主属性,则 idStrategy 将自动使用 PrimaryKey; properties: 定义顶点的属性,传入的参数是 PropertyKey 的 name primaryKeys: 当用户选择了 PrimaryKey 的 Id 策略时,需要从 VertexLabel 的属性中选择若干主属性作为区分的依据; 需要注意的是,Id 策略的选择与 primaryKeys 的设置有一些相互约束,不能随意调用,约束关系见下表:
nullableKeys: 对于通过 properties(…) 方法设置过的属性,默认全都是不可为空的,也就是在创建顶点时该属性必须赋值,这样可能对用户数据提出了太过严格的完整性要求。为避免这样的强约束,用户可以通过
本方法设置若干属性为可空的,这样添加顶点时该属性可以不赋值。 注意:primaryKeys 和 nullableKeys 不能有交集,因为一个属性不能既作为主属性,又是可空的。
enableLabelIndex:用户可以指定是否需要为label创建索引。不创建则无法全局搜索指定label的顶点和边,创建则可以全局搜索,做类似于g.V().hasLabel('person'), g.E().has('label', 'person')这样的查询,
但是插入数据时性能上会更加慢,并且需要占用更多的存储空间。此项默认为 true。 userdata:用户可以自己添加一些约束或额外信息,然后自行检查传入的属性是否满足约束,或者必要的时候提取出额外信息 2.3.2 创建 VertexLabel // 使用 Automatic 的 Id 策略
schema . vertexLabel ( "person" ). properties ( "name" , "age" ). ifNotExist (). create ();
schema . vertexLabel ( "person" ). useAutomaticId (). properties ( "name" , "age" ). ifNotExist (). create ();
// 使用 Customize_String 的 Id 策略
schema . vertexLabel ( "person" ). useCustomizeStringId (). properties ( "name" , "age" ). ifNotExist (). create ();
// 使用 Customize_Number 的 Id 策略
schema . vertexLabel ( "person" ). useCustomizeNumberId (). properties ( "name" , "age" ). ifNotExist (). create ();
// 使用 PrimaryKey 的 Id 策略
schema . vertexLabel ( "person" ). properties ( "name" , "age" ). primaryKeys ( "name" ). ifNotExist (). create ();
schema . vertexLabel ( "person" ). usePrimaryKeyId (). properties ( "name" , "age" ). primaryKeys ( "name" ). ifNotExist (). create (); 2.3.3 追加 VertexLabel VertexLabel 是可以追加约束的,不过仅限 properties 和 nullableKeys,而且追加的属性也必须添加到 nullableKeys 集合中。
schema . vertexLabel ( "person" ). properties ( "price" ). nullableKeys ( "price" ). append (); 2.3.4 删除 VertexLabel schema . vertexLabel ( "person" ). remove (); 2.3.5 查询 VertexLabel // 获取VertexLabel对象
schema . getVertexLabel ( "name" )
// 获取property key属性
schema . getVertexLabel ( "person" ). idStrategy ()
schema . getVertexLabel ( "person" ). primaryKeys ()
schema . getVertexLabel ( "person" ). name ()
schema . getVertexLabel ( "person" ). properties ()
schema . getVertexLabel ( "person" ). nullableKeys ()
schema . getVertexLabel ( "person" ). userdata () 2.4 EdgeLabel 2.4.1 接口及参数介绍 EdgeLabel 用来定义边类型,描述边的约束信息。
EdgeLabel 允许定义的约束信息包括:name、sourceLabel、targetLabel、frequency、properties、sortKeys 和 nullableKeys,下面逐一介绍。
name: 属性的名字,用来区分不同的 EdgeLabel,不允许有同名的属性; frequency: 字面意思是频率,表示在两个具体的顶点间某个关系出现的次数,可以是单次(single)或多次(frequency),默认为single; sortKeys: 当 EdgeLabel 的 frequency 为 multiple 时,需要某些属性来区分这多次的关系,故引入了 sortKeys(排序键); nullableKeys: 与顶点中的 nullableKeys 概念一致,不再赘述 注意:sortKeys 和 nullableKeys也不能有交集。
2.4.2 创建 EdgeLabel schema . edgeLabel ( "knows" ). link ( "person" , "person" ). properties ( "date" ). ifNotExist (). create ();
schema . edgeLabel ( "created" ). multiTimes (). link ( "person" , "software" ). properties ( "date" ). sortKeys ( "date" ). ifNotExist (). create (); 2.4.3 追加 EdgeLabel schema . edgeLabel ( "knows" ). properties ( "price" ). nullableKeys ( "price" ). append (); 2.4.4 删除 EdgeLabel schema . edgeLabel ( "knows" ). remove (); 2.4.5 查询 EdgeLabel // 获取EdgeLabel对象
schema . getEdgeLabel ( "knows" )
// 获取property key属性
schema . getEdgeLabel ( "knows" ). frequency ()
schema . getEdgeLabel ( "knows" ). sourceLabel ()
schema . getEdgeLabel ( "knows" ). targetLabel ()
schema . getEdgeLabel ( "knows" ). sortKeys ()
schema . getEdgeLabel ( "knows" ). name ()
schema . getEdgeLabel ( "knows" ). properties ()
schema . getEdgeLabel ( "knows" ). nullableKeys ()
schema . getEdgeLabel ( "knows" ). userdata () 2.5 IndexLabel 2.5.1 接口及参数介绍 IndexLabel 用来定义索引类型,描述索引的约束信息,主要是为了方便查询。
IndexLabel 允许定义的约束信息包括:name、baseType、baseValue、indexFields、indexType,下面逐一介绍。
name: 属性的名字,用来区分不同的 IndexLabel,不允许有同名的属性; baseType: 表示要为 VertexLabel 还是 EdgeLabel 建立索引, 与下面的 baseValue 配合使用;
baseValue: 指定要建立索引的 VertexLabel 或 EdgeLabel 的名称;
indexFields: 要在哪些属性上建立索引,可以是为多列建立联合索引; indexType: 建立的索引类型,目前支持五种,即 Secondary、Range、Search、Shard 和 Unique。Secondary 支持精确匹配的二级索引,允许建立联合索引,联合索引支持索引前缀搜索单个属性,支持相等查询,比如:person顶点的city属性的二级索引,可以用g.V().has("city", "北京") 查询"city属性值是北京"的全部顶点 联合索引,支持前缀查询和相等查询,比如:person顶点的city和street属性的联合索引,可以用g.V().has ("city", "北京").has('street', '中关村街道') 查询"city属性值是北京且street属性值是中关村"的全部顶点,或者g.V() .has("city", "北京")查询"city属性值是北京"的全部顶点 secondary index的查询都是基于"是"或者"相等"的查询条件,不支持"部分匹配"
Range 支持数值类型的范围查询必须是单个数字或者日期属性,比如:person顶点的age属性的范围索引,可以用g.V().has("age", P.gt(18)) 查询"age属性值大于18"的顶点。除了P.gt()以外,还支持P.gte(), P.lte(), P.lt(),
P.eq(), P.between(), P.inside()和P.outside()等 Search 支持全文检索的索引必须是单个文本属性,比如:person顶点的address属性的全文索引,可以用g.V().has("address", Text .contains('大厦')查询"address属性中包含大厦"的全部顶点 search index的查询是基于"是"或者"包含"的查询条件
Shard 支持前缀匹配 + 数字范围查询的索引N个属性的分片索引,支持前缀相等情况下的范围查询,比如:person顶点的city和age属性的分片索引,可以用g.V().has ("city", "北京").has("age", P.between(18, 30)) 查询"city属性是北京且年龄大于等于18小于30"的全部顶点 shard index N个属性全是文本属性时,等价于secondary index shard index只有单个数字或者日期属性时,等价于range index shard index可以有任意数字或者日期属性,但是查询时最多只能提供一个范围查找条件,且该范围查找条件的属性的前缀属性都是相等查询条件
Unique 支持属性值唯一性约束,即可以限定属性的值不重复,允许联合索引,但不支持查询单个或者多个属性的唯一性索引,不可用来查询,只可对属性的值进行限定,当出现重复值时将报错 2.5.2 创建 IndexLabel schema . indexLabel ( "personByAge" ). onV ( "person" ). by ( "age" ). range (). ifNotExist (). create ();
schema . indexLabel ( "createdByDate" ). onE ( "created" ). by ( "date" ). secondary (). ifNotExist (). create ();
schema . indexLabel ( "personByLived" ). onE ( "person" ). by ( "lived" ). search (). ifNotExist (). create ();
schema . indexLabel ( "personByCityAndAge" ). onV ( "person" ). by ( "city" , "age" ). shard (). ifNotExist (). create ();
schema . indexLabel ( "personById" ). onV ( "person" ). by ( "id" ). unique (). ifNotExist (). create (); 2.5.3 删除 IndexLabel schema . indexLabel ( "personByAge" ). remove () 2.5.4 查询 IndexLabel // 获取IndexLabel对象
schema . getIndexLabel ( "personByAge" )
// 获取property key属性
schema . getIndexLabel ( "personByAge" ). baseType ()
schema . getIndexLabel ( "personByAge" ). baseValue ()
schema . getIndexLabel ( "personByAge" ). indexFields ()
schema . getIndexLabel ( "personByAge" ). indexType ()
schema . getIndexLabel ( "personByAge" ). name () 3 图数据 3.1 Vertex 顶点是构成图的最基本元素,一个图中可以有非常多的顶点。下面给出一个添加顶点的例子:
Vertex marko = graph . addVertex ( T . label , "person" , "name" , "marko" , "age" , 29 );
Vertex lop = graph . addVertex ( T . label , "software" , "name" , "lop" , "lang" , "java" , "price" , 328 ); 添加顶点的关键是顶点属性,添加顶点函数的参数个数必须为偶数,且满足key1 -> val1, key2 -> val2 ···的顺序排列,键值对之间的顺序是自由的。 参数中必须包含一对特殊的键值对,就是T.label -> "val",用来定义该顶点的类别,以便于程序从缓存或后端获取到该VertexLabel的schema定义,然后做后续的约束检查。例子中的label定义为person。 如果顶点类型的 Id 策略为 AUTOMATIC,则不允许用户传入 id 键值对。 如果顶点类型的 Id 策略为 CUSTOMIZE_STRING,则用户需要自己传入 String 类型 id 的值,键值对形如:"T.id", "123456"。 如果顶点类型的 Id 策略为 CUSTOMIZE_NUMBER,则用户需要自己传入 Number 类型 id 的值,键值对形如:"T.id", 123456。 如果顶点类型的 Id 策略为 PRIMARY_KEY,参数还必须全部包含该primaryKeys对应属性的名和值,如果不设置会抛出异常。比如之前person的primaryKeys是name,例子中就设置了name的值为marko。 对于非 nullableKeys 的属性,必须要赋值。 剩下的参数就是顶点其他属性的设置,但并非必须。 调用addVertex方法后,顶点会立刻被插入到后端存储系统中。 3.2 Edge 有了点,还需要边才能构成完整的图。下面给出一个添加边的例子:
Edge knows1 = marko . addEdge ( "knows" , vadas , "city" , "Beijing" ); 由(源)顶点来调用添加边的函数,函数第一个参数为边的label,第二个参数是目标顶点,这两个参数的位置和顺序是固定的。后续的参数就是key1 -> val1, key2 -> val2 ···的顺序排列,设置边的属性,键值对顺序自由。 源顶点和目标顶点必须符合 EdgeLabel 中 source-label 和 target-label 的定义,不能随意添加。 对于非 nullableKeys 的属性,必须要赋值。 注意:当frequency为multiple时必须要设置sortKeys对应属性类型的值。
4 简单示例 简单示例见HugeGraph-Client
3 - Gremlin-Console Gremlin-Console是由Tinkerpop自己开发的一个交互式客户端,用户可以使用该客户端对Graph做各种操作,主要有两种使用模式:
单机离线调用模式; Client/Server请求模式; 1 单机离线调用模式 由于lib目录下已经包含了HugeCore的jar包,且HugeGraph已经作为插件注册到Console中,用户可以直接写groovy脚本调用HugeGraph-Core的代码,然后交由Gremlin-Console内的解析引擎执行,就能在不启动Server的情况下操作图。
这种模式便于用户快速上手体验,但是不适合大量数据插入和查询的场景。下面给一个示例:
在script目录下有一个示例脚本:example.groovy
import com.baidu.hugegraph.HugeFactory
import com.baidu.hugegraph.dist.RegisterUtil
import org.apache.tinkerpop.gremlin.structure.T
RegisterUtil . registerCassandra ();
RegisterUtil . registerScyllaDB ();
conf = "conf/hugegraph.properties"
graph = HugeFactory . open ( conf );
schema = graph . schema ();
schema . propertyKey ( "name" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "age" ). asInt (). ifNotExist (). create ();
schema . propertyKey ( "city" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "weight" ). asDouble (). ifNotExist (). create ();
schema . propertyKey ( "lang" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "date" ). asText (). ifNotExist (). create ();
schema . propertyKey ( "price" ). asInt (). ifNotExist (). create ();
schema . vertexLabel ( "person" ). properties ( "name" , "age" , "city" ). primaryKeys ( "name" ). ifNotExist (). create ();
schema . vertexLabel ( "software" ). properties ( "name" , "lang" , "price" ). primaryKeys ( "name" ). ifNotExist (). create ();
schema . indexLabel ( "personByName" ). onV ( "person" ). by ( "name" ). secondary (). ifNotExist (). create ();
schema . indexLabel ( "personByCity" ). onV ( "person" ). by ( "city" ). secondary (). ifNotExist (). create ();
schema . indexLabel ( "personByAgeAndCity" ). onV ( "person" ). by ( "age" , "city" ). secondary (). ifNotExist (). create ();
schema . indexLabel ( "softwareByPrice" ). onV ( "software" ). by ( "price" ). range (). ifNotExist (). create ();
schema . edgeLabel ( "knows" ). sourceLabel ( "person" ). targetLabel ( "person" ). properties ( "date" , "weight" ). ifNotExist (). create ();
schema . edgeLabel ( "created" ). sourceLabel ( "person" ). targetLabel ( "software" ). properties ( "date" , "weight" ). ifNotExist (). create ();
schema . indexLabel ( "createdByDate" ). onE ( "created" ). by ( "date" ). secondary (). ifNotExist (). create ();
schema . indexLabel ( "createdByWeight" ). onE ( "created" ). by ( "weight" ). range (). ifNotExist (). create ();
schema . indexLabel ( "knowsByWeight" ). onE ( "knows" ). by ( "weight" ). range (). ifNotExist (). create ();
marko = graph . addVertex ( T . label , "person" , "name" , "marko" , "age" , 29 , "city" , "Beijing" );
vadas = graph . addVertex ( T . label , "person" , "name" , "vadas" , "age" , 27 , "city" , "Hongkong" );
lop = graph . addVertex ( T . label , "software" , "name" , "lop" , "lang" , "java" , "price" , 328 );
josh = graph . addVertex ( T . label , "person" , "name" , "josh" , "age" , 32 , "city" , "Beijing" );
ripple = graph . addVertex ( T . label , "software" , "name" , "ripple" , "lang" , "java" , "price" , 199 );
peter = graph . addVertex ( T . label , "person" , "name" , "peter" , "age" , 35 , "city" , "Shanghai" );
marko . addEdge ( "knows" , vadas , "date" , "20160110" , "weight" , 0.5 );
marko . addEdge ( "knows" , josh , "date" , "20130220" , "weight" , 1.0 );
marko . addEdge ( "created" , lop , "date" , "20171210" , "weight" , 0.4 );
josh . addEdge ( "created" , lop , "date" , "20091111" , "weight" , 0.4 );
josh . addEdge ( "created" , ripple , "date" , "20171210" , "weight" , 1.0 );
peter . addEdge ( "created" , lop , "date" , "20170324" , "weight" , 0.2 );
graph . tx (). commit ();
g = graph . traversal ();
System . out . println ( ">>>> query all vertices: size=" + g . V (). toList (). size ());
System . out . println ( ">>>> query all edges: size=" + g . E (). toList (). size ()); 其实这一段groovy脚本几乎就是Java代码,不同之处仅在于变量的定义可以不写类型声明,以及每一行末尾的分号可以去掉。
g.V() 是获取所有的顶点,g.E() 是获取所有的边,toList() 是把结果存到一个 List 中,参考TinkerPop Terminal Steps 。
下面进入gremlin-console,并传入该脚本令其执行:
bin/gremlin-console.sh scripts/example.groovy
objc[ 5038] : Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java ( 0x10137a4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib ( 0x102bbb4e0) . One of the two will be used. Which one is undefined.
\, ,,/
( o o)
-----oOOo-( 3) -oOOo-----
plugin activated: com.baidu.hugegraph
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph
2018-01-15 14:36:19 7516 [ main] [ WARN ] com.baidu.hugegraph.config.HugeConfig [] - The config option 'rocksdb.data_path' is redundant, please ensure it has been registered
2018-01-15 14:36:19 7523 [ main] [ WARN ] com.baidu.hugegraph.config.HugeConfig [] - The config option 'rocksdb.wal_path' is redundant, please ensure it has been registered
2018-01-15 14:36:19 7604 [ main] [ INFO ] com.baidu.hugegraph.HugeGraph [] - Opening backend store 'cassandra' for graph 'hugegraph'
>>>> query all vertices: size = 6
>>>> query all edges: size = 6 可以看到,插入了6个顶点、6条边,并查询出来了。进入console之后,还可继续输入groovy语句对图做操作:
gremlin > g . V ()
==> v [ 2 : ripple ]
==> v [ 1 : vadas ]
==> v [ 1 : peter ]
==> v [ 1 : josh ]
==> v [ 1 : marko ]
==> v [ 2 : lop ]
gremlin > g . E ()
==> e [ S1: josh > 2 >> S2: ripple ][ 1 : josh - created -> 2 : ripple ]
==> e [ S1: marko > 1 > 20160110 > S1: vadas ][ 1 : marko - knows -> 1 : vadas ]
==> e [ S1: peter > 2 >> S2: lop ][ 1 : peter - created -> 2 : lop ]
==> e [ S1: josh > 2 >> S2: lop ][ 1 : josh - created -> 2 : lop ]
==> e [ S1: marko > 1 > 20130220 > S1: josh ][ 1 : marko - knows -> 1 : josh ]
==> e [ S1: marko > 2 >> S2: lop ][ 1 : marko - created -> 2 : lop ] 更多的Gremlin语句请参考Tinkerpop官网
2 Client/Server请求模式 因为Gremlin-Console只能通过WebSocket连接HugeGraph-Server,默认HugeGraph-Server是对外提供HTTP连接的,所以先修改gremlin-server的配置。
注意:将连接方式修改为WebSocket后,HugeGraph-Client、HugeGraph-Loader、HugeGraph-Studio等配套工具都不能使用了。
# vim conf/gremlin-server.yaml
host : 127.0.0.1
port : 8182
scriptEvaluationTimeout : 30000
# If you want to start gremlin-server for gremlin-console(web-socket),
# please change `HttpChannelizer` to `WebSocketChannelizer` or comment this line.
channelizer : org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
graphs : {
hugegraph : conf/hugegraph.properties,
hugegraph1 : conf/hugegraph1.properties
}
plugins :
- com.baidu.hugegraph
scriptEngines : {
gremlin-groovy : {
imports : [ java.lang.Math],
staticImports : [ java.lang.Math.PI],
scripts : [ scripts/empty-sample.groovy]
}
}
serializers :
- { className : org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0,
config : {
serializeResultToString : false ,
ioRegistries : [ com.baidu.hugegraph.io.HugeGraphIoRegistry]
}
}
- { className : org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0,
config : {
serializeResultToString : true ,
ioRegistries : [ com.baidu.hugegraph.io.HugeGraphIoRegistry]
}
}
- { className : org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0,
config : {
serializeResultToString : false ,
ioRegistries : [ com.baidu.hugegraph.io.HugeGraphIoRegistry]
}
}
- { className : org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0,
config : {
serializeResultToString : false ,
ioRegistries : [ com.baidu.hugegraph.io.HugeGraphIoRegistry]
}
}
- { className : org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0,
config : {
serializeResultToString : false ,
ioRegistries : [ com.baidu.hugegraph.io.HugeGraphIoRegistry]
}
}
metrics : {
consoleReporter : {enabled : false, interval : 180000 },
csvReporter : {enabled : true, interval : 180000, fileName : /tmp/gremlin-server-metrics.csv},
jmxReporter : {enabled : false },
slf4jReporter : {enabled : false, interval : 180000 },
gangliaReporter : {enabled : false, interval : 180000, addressingMode : MULTICAST},
graphiteReporter : {enabled : false, interval : 180000 }
}
maxInitialLineLength : 4096
maxHeaderSize : 8192
maxChunkSize : 8192
maxContentLength : 65536
maxAccumulationBufferComponents : 1024
resultIterationBatchSize : 64
writeBufferLowWaterMark : 32768
writeBufferHighWaterMark : 65536
ssl : {
enabled : false
} 将channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer修改成channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer或直接注释,然后按照步骤启动Server。
然后进入gremlin-console
bin/gremlin-console.sh
objc[ 5761] : Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java ( 0x10ec584c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib ( 0x10ecdc4e0) . One of the two will be used. Which one is undefined.
\, ,,/
( o o)
-----oOOo-( 3) -oOOo-----
plugin activated: com.baidu.hugegraph
plugin activated: tinkerpop.server
plugin activated: tinkerpop.utilities
plugin activated: tinkerpop.tinkergraph 连接server,需在配置文件中指定连接参数,在conf目录下有一个默认的remote.yaml
# cat conf/remote.yaml
hosts : [ localhost]
port : 8182
serializer : {
className : org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0,
config : {
serializeResultToString : true ,
ioRegistries : [ com.baidu.hugegraph.io.HugeGraphIoRegistry]
}
} gremlin > : remote connect tinkerpop . server conf /remote.yaml
2018-01-15 15:30:31 11528 [main] [INFO ] org.apache.tinkerpop.gremlin.driver.Connection [] - Created new connection for ws://localhost:8182/ gremlin
2018 - 01 - 15 15 : 30 : 31 11538 [ main ] [ INFO ] org . apache . tinkerpop . gremlin . driver . Connection [] - Created new connection for ws: //localhost:8182/gremlin
2018 - 01 - 15 15 : 30 : 31 11538 [ main ] [ INFO ] org . apache . tinkerpop . gremlin . driver . ConnectionPool [] - Opening connection pool on Host { address = localhost /127.0.0.1:8182, hostUri=ws://localhost:8182/ gremlin } with core size of 2
==> Configured localhost / 127.0 . 0.1 : 8182 连接成功之后,在console的上下文中能使用的变量只有hugegraph和hugegraph1两个图对象(在gremlin-server.yaml中配置),如果想拥有更多的变量,可以在scripts/empty-sample.groovy中添加,如:
import org.apache.tinkerpop.gremlin.server.util.LifeCycleHook
// an init script that returns a Map allows explicit setting of global bindings.
def globals = [:]
// defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
// note that the name of the key in the "global" map is unimportant.
globals << [ hook: [
onStartUp : { ctx ->
ctx . logger . info ( "Executed once at startup of Gremlin Server." )
},
onShutDown: { ctx ->
ctx . logger . info ( "Executed once at shutdown of Gremlin Server." )
}
] as LifeCycleHook ]
// define schema manger for hugegraph
schema = hugegraph . schema ()
// define the default TraversalSource to bind queries to - this one will be named "g".
g = hugegraph . traversal () 这样在console中便可以直接使用schema和g这两个对象,做元数据的管理和图的查询了。
不定义了也没关系,因为所有的对象都可以通过graph获得,例如:
gremlin > :> hugegraph . traversal (). V ()
==> v [ 2 : ripple ]
==> v [ 1 : vadas ]
==> v [ 1 : peter ]
==> v [ 1 : josh ]
==> v [ 1 : marko ]
==> v [ 2 : lop ] 在Client/Server模式下,所有跟Server有关的操作都要加上:> ,如果不加,表示在console本地操作。
还可以把多条语句放在一个字符串变量中,然后一次性发给server:
gremlin > script = """
graph = hugegraph;
marko = graph.addVertex(T.label, "person", "name", "marko", "age", 29, "city", "Beijing");
vadas = graph.addVertex(T.label, "person", "name", "vadas", "age", 27, "city", "Hongkong");
lop = graph.addVertex(T.label, "software", "name", "lop", "lang", "java", "price", 328);
josh = graph.addVertex(T.label, "person", "name", "josh", "age", 32, "city", "Beijing");
ripple = graph.addVertex(T.label, "software", "name", "ripple", "lang", "java", "price", 199);
peter = graph.addVertex(T.label, "person", "name", "peter", "age", 35, "city", "Shanghai");
marko.addEdge("knows", vadas, "date", "20160110", "weight", 0.5);
marko.addEdge("knows", josh, "date", "20130220", "weight", 1.0);
marko.addEdge("created", lop, "date", "20171210", "weight", 0.4);
josh.addEdge("created", lop, "date", "20091111", "weight", 0.4);
josh.addEdge("created", ripple, "date", "20171210", "weight", 1.0);
peter.addEdge("created", lop, "date", "20170324", "weight", 0.2);
graph.tx().commit();
g = graph.traversal();
g.V().toList().size();
"""
gremlin > :> @script
==> 6 更多关于gremlin-console的使用,请参考Tinkerpop官网