数据库读取和写入操作的结果必须先进行适当处理,然后才能用于应用程序。
返回Response
有些方法(如gql()
和uql()
)返回一个Response
对象。为在应用中使用其中包含的数据,首先要从Response
对象提取DataItem
,再将DataItem
转换成一个合适的驱动数据结构。
一个Response
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
aliases |
List<Alias > |
结果别名列表;每个Alias 包括属性name 和type |
items |
Map<String, DataItem > |
一个映射,键是别名名称,值是别名代表的数据 |
explainPlan |
ExplainPlan |
执行计划 |
status |
Status |
执行状态,包括属性code 和message |
statistics |
Statistics |
执行的统计信息,包括属性nodeAffected 、edgeAffected 、totalCost 和engineCost |
提取DataItem
使用get()
或alias()
方法从一个Response
对象提取DataItem
。
一个DataItem
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
alias |
String | 别名名称 |
type |
Ultipa.ResultType |
结果类型 |
entities |
List | 结果数据列表 |
get()
通过别名索引获取数据。
参数
index: int
:别名索引。
返回值
DataItem
:返回的数据。
Response response = driver.gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3");
System.out.println(response.get(0).getAlias());
System.out.println(response.get(0).getType());
System.out.println(response.get(0).getEntities());
GQL语句返回n
和e
两个别名;get()
方法获取与索引为0的别名n
对应的DataItem
。
n
RESULT_TYPE_NODE
[Node(uuid=72059793061183504, id=ULTIPA800000000000003B, schema=account, values={industry=Publishing, name=Velox, gender=female, year=1976}), Node(uuid=648520545364606993, id=ULTIPA800000000000003E, schema=account, values={industry=Food&Beverage, name=Claire, gender=female, year=1989}), Node(uuid=720578139402534937, id=ULTIPA8000000000000050, schema=account, values={industry=Education, name=Holly, gender=male, year=2000})]
alias()
通过别名名称获取数据。
参数
name: String
:别名名称。
返回值
DataItem
:返回的数据。
Response response = driver.gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3");
System.out.println(response.alias("e").getAlias());
System.out.println(response.alias("e").getType());
System.out.println(response.alias("e").getEntities());
GQL语句返回n
和e
两个别名;alias()
方法获取别名e
对应的DataItem
。
e
RESULT_TYPE_EDGE
[Edge(uuid=139, fromUuid=6269012880322985990, toUuid=-7998395137233256453, from=ULTIPA800000000000000E, to=ULTIPA800000000000000D, schema=agree, values={targetPost=905, timestamp=1572662452, datetime=2018-10-14T06:27:42}), Edge(uuid=378, fromUuid=72059793061183493, toUuid=8214567919347040275, from=ULTIPA800000000000003B, to=ULTIPA800000000000000F, schema=follow, values={}), Edge(uuid=531, fromUuid=72059793061183493, toUuid=4827864298099310661, from=ULTIPA800000000000003B, to=ULTIPA80000000000003F4, schema=wishlist, values={toUuid=1012, uuid=1368, fromUuid=59, timestamp=Sat Mar 23 17:09:12 CST 2019, datetime=2019-03-23T17:09:12})]
转换DataItem
使用一个as<DataStructure>()
方法将DataItem.entities
转换成对应的驱动数据结构。
例如,以下请求中的GQL语句从图中获取三个点,asNodes()
方法将与别名n
对应的DataItem
转换成一个Node
对象列表:
Response response = driver.gql("MATCH (n) RETURN n LIMIT 3");
List<Node> nodeList = response.alias("n").asNodes();
for (Node node : nodeList) {
System.out.println(node.getID());
}
以下是DataItem
所有的转换方法。请留意每种方法适用的DataItem.type
和DataItem.alias
。
方法 |
DataItem.type |
DataItem.alias |
返回 |
描述 |
---|---|---|---|---|
asNodes() |
NODE | 任何 | List<Node > |
转换成一个Node 对象列表 |
asFirstNode() |
NODE | 任何 | Node |
将返回的第一个结果数据转换成一个Node 对象 |
asEdges() |
EDGE | 任何 | List<Edge > |
转换成一个Edge 对象列表 |
asFirstEdge() |
EDGE | 任何 | Edge |
将返回的第一个结果数据转换成一个Edge 对象 |
asGraph() |
GRAPH | 任何 | Graph |
转换成一个Graph 对象 |
asGraphSets() |
TABLE | _graph |
List<GraphSet > |
转换成一个GraphSet 对象列表 |
asSchemas() |
TABLE | _nodeSchema , _edgeSchema |
List<Schema > |
转换成一个Schema 对象列表 |
asProperties() |
TABLE | _nodeProperty , _edgeProperty |
List<Property > |
转换成一个Property 对象列表 |
asAttr() |
ATTR | 任何 | Attr |
转换成一个Attr 对象 |
asTable() |
TABLE | 任何 | Table |
转换成一个Table 对象 |
asHDCGraphs() |
TABLE | _hdcGraphList |
List<HDCGraph > |
转换成一个HDCGraph 对象列表 |
asAlgos() |
TABLE | _algoList |
List<Algo > |
转换成一个Algo 对象列表 |
asProjections() |
TABLE | _projectionList |
List<Projection > |
转换成一个Projection 对象列表 |
asIndexes() |
TABLE | _nodeIndex , _edgeIndex , _nodeFulltext , _edgeFulltext |
List<Index > |
转换成一个Index 对象列表 |
asPrivilieges() |
TABLE | _privilege |
List<Priviliege > |
转换成一个Priviliege 对象列表 |
asPolicies() |
TABLE | _policy |
List<Policy > |
转换成一个Policy 对象列表 |
asUsers() |
TABLE | _user |
List<User > |
转换成一个User 对象列表 |
asProcesses() |
TABLE | _top |
List<Process > |
转换成一个Process 对象列表 |
asJobs() |
TABLE | _job |
List<Job > |
转换成Job 对象列表 |
返回ResponseWithExistCheck
有些方法(如createGraphIfNotExist()
)返回一个ResponseWithExistCheck
对象。
一个返回ResponseWithExistCheck
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
exist |
Boolean | 要创建的对象是否已经存在 |
response |
Response |
主要请求结果 |
返回InsertResponse
有些数据插入方法(如insertNodesBatchBySchema()
和insertNodesBatchAuto()
)返回一个InsertResponse
对象。
一个InsertResponse
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
ids |
List<String> | 插入的点_id 列表;请留意,InsertRequestConfig.silent 为True 时列表为空 |
uuids |
List<Long> | 插入的边_uuid 列表;请留意,InsertRequestConfig.silent 为True 时列表为空 |
errorItems |
Map<Integer, InsertErrorCode > |
错误信息映射,键是发生错误的点或边的顺序索引,值是错误代码 |
status |
Status |
执行状态,包括属性code 和message |
statistics |
Statistics |
执行的统计信息,包括属性nodeAffected 、edgeAffected 、totalCost 和engineCost |
返回JobResponse
有些方法(如createFulltext()
)返回一个JobResponse
对象。
一个JobResponse
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
jobId |
String | 执行的作业ID |
status |
Status |
执行状态,包括属性code 和message |
statistics |
Statistics |
执行的统计信息,包括属性nodeAffected 、edgeAffected 、totalCost 和engineCost |
返回驱动数据结构
一些方法返回驱动数据结构的一个实例或一个列表,可直接使用。例如:
getGraph()
方法返回一个GraphSet
对象。showGraph()
方法返回List<GraphSet>
。