关于在JAVA使用调用rest api请求反应的疑问,请求高手解答,大恩不言谢。
发布于 8 年前 作者 chenjf0221 3716 次浏览 来自 问答

具体场景如下 Timestamp benfore = new Timestamp(System.currentTimeMillis()); System.out.println(“开始调用rest api”+benfore); String strs = sendTransactionalCypherQuery(“match (n:Info{id:‘150’}) return n”); System.out.println(strs); Timestamp end = new Timestamp(System.currentTimeMillis()); System.out.println(“结束 http 调用 rest api”+end); System.out.println(“用时”+(end.getTime()-benfore.getTime())+“毫秒”);

   private static String sendTransactionalCypherQuery(String query) {
	// START SNIPPET: queryAllNodes
	final String txUri = SERVER_ROOT_URI + "transaction/commit";
	WebResource resource = Client.create().resource(txUri);

	String payload = "{\"statements\" : [ {\"statement\" : \"" + query + "\"} ]}";
	ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
			.entity(payload).post(ClientResponse.class);
	// final URI location = response.getLocation();
	String str = response.getEntity(String.class);
	System.out.println(String.format(
			"POST [%s] to [%s], status code [%d], returned data: " + System.getProperty("line.separator") + "%s",
			payload, txUri, response.getStatus(), response.getEntity(String.class)));
	// System.out.println(response.getEntity(String.class));
	// URI location = response.getLocation();
	response.close();
	// END SNIPPET: queryAllNodes
	return str;
}
 使用 jersey Client请求uri上列代码,请求一个单一的节点查询大概需要花100ms左右
 开始调用rest api2017-04-27 10:44:45.438
POST [{"statements" : [ {"statement" : "match (n:Info{id:'150'}) return n"} ]}] to [http://192.168.1.201:7474/db/data/transaction/commit], status code [200], returned data: 

{"results":[{"columns":["n"],"data":[{"row":[{"id":"150","title":"江西省交通运输厅行政处罚汇总表"}],"meta":[{"id":528,"type":"node","deleted":false}]}]}],"errors":[]}
结束 http 调用 rest api2017-04-27 10:44:45.536
用时98毫秒

而使用cql直接在数据库中查询大概只需要 Started streaming 1 record after 1 ms and completed after 1 ms. 几毫秒 这种数据库交互时间的差距非常奇怪,但确实无从下手,望有高人指点一二。不胜感激。

1 回复

刚测完这个bug,应中文社区群主要求来此处回复一下。 其实这个问题很简单,其原因主要是用了jersey Client调用rest api,结果就出现了如上了效率慢的情况。 然后,我解决方式就是用最简单的httpClient去调用rest api执行增删改操作。 不过值得一提的是使用httpClient查询数据的话返回的数据是json格式,如下: JSONObject cquery = new JSONObject(); JSONObject cparam = new JSONObject(); cquery.put(“query”, “match (n:Info{id:12})-[r*…2]-(m) return n,m”); cquery.put(“params”, cparam); JSONObject json = HttpRequestUtils.httpPost(ConstantsNeo4j.CURL,cquery); System.out.println(json); 返回数据如下: {“data”:[ [{ “labels”:“http://localhost:7474/db/data/node/291/labels",“outgoing_relationships”:"http://localhost:7474/db/data/node/291/relationships/out”, “data”:{“id”:12,“title”:“江西公共服务机构名单”}, “traverse”:“http://localhost:7474/db/data/node/291/traverse/{returnType}",“all_typed_relationships”:"http://localhost:7474/db/data/node/291/relationships/all/{-list|&|types}”, “self”:“http://localhost:7474/db/data/node/291",“property”:"http://localhost:7474/db/data/node/291/properties/{key}”, “outgoing_typed_relationships”:“http://localhost:7474/db/data/node/291/relationships/out/{-list|&|types}”, “properties”:“http://localhost:7474/db/data/node/291/properties”, “incoming_relationships”:“http://localhost:7474/db/data/node/291/relationships/in”, “extensions”:{},“create_relationship”:“http://localhost:7474/db/data/node/291/relationships”, “paged_traverse”:“http://localhost:7474/db/data/node/291/paged/traverse/{returnType}{?pageSize,leaseTime}”, “all_relationships”:“http://localhost:7474/db/data/node/291/relationships/all”, “incoming_typed_relationships”:“http://localhost:7474/db/data/node/291/relationships/in/{-list|&|types}”, “metadata”:{“id”:291,“labels”:[“Info”]}},{“labels”:“http://localhost:7474/db/data/node/292/labels”, “outgoing_relationships”:“http://localhost:7474/db/data/node/292/relationships/out",“data”:{“id”:157,“name”:"haluo”}, “traverse”:“http://localhost:7474/db/data/node/292/traverse/{returnType}”, “all_typed_relationships”:“http://localhost:7474/db/data/node/292/relationships/all/{-list|&|types}”, “self”:“http://localhost:7474/db/data/node/292",“property”:"http://localhost:7474/db/data/node/292/properties/{key}”, “outgoing_typed_relationships”:“http://localhost:7474/db/data/node/292/relationships/out/{-list|&|types}”, “properties”:“http://localhost:7474/db/data/node/292/properties”, “incoming_relationships”:“http://localhost:7474/db/data/node/292/relationships/in”, “extensions”:{},“create_relationship”:“http://localhost:7474/db/data/node/292/relationships”, “paged_traverse”:“http://localhost:7474/db/data/node/292/paged/traverse/{returnType}{?pageSize,leaseTime}”, “all_relationships”:“http://localhost:7474/db/data/node/292/relationships/all”, “incoming_typed_relationships”:“http://localhost:7474/db/data/node/292/relationships/in/{-list|&|types}”, “metadata”:{“id”:292,“labels”:[“Tag”]}}],[{“labels”:“http://localhost:7474/db/data/node/291/labels”, “outgoing_relationships”:“http://localhost:7474/db/data/node/291/relationships/out”, “data”:{“id”:12,“title”:“江西公共服务机构名单”},“traverse”:“http://localhost:7474/db/data/node/291/traverse/{returnType}”, “all_typed_relationships”:“http://localhost:7474/db/data/node/291/relationships/all/{-list|&|types}”, “self”:“http://localhost:7474/db/data/node/291",“property”:"http://localhost:7474/db/data/node/291/properties/{key}”, “outgoing_typed_relationships”:“http://localhost:7474/db/data/node/291/relationships/out/{-list|&|types}”, “properties”:“http://localhost:7474/db/data/node/291/properties”, “incoming_relationships”:“http://localhost:7474/db/data/node/291/relationships/in","extensions”:{}, “create_relationship”:“http://localhost:7474/db/data/node/291/relationships”, “paged_traverse”:“http://localhost:7474/db/data/node/291/paged/traverse/{returnType}{?pageSize,leaseTime}”, “all_relationships”:“http://localhost:7474/db/data/node/291/relationships/all”, “incoming_typed_relationships”:“http://localhost:7474/db/data/node/291/relationships/in/{-list|&|types}”, “metadata”:{“id”:291,“labels”:[“Info”]}},{“labels”:“http://localhost:7474/db/data/node/293/labels”, “outgoing_relationships”:“http://localhost:7474/db/data/node/293/relationships/out",“data”:{“id”:158,“name”:"haha”}, “traverse”:“http://localhost:7474/db/data/node/293/traverse/{returnType}”, “all_typed_relationships”:“http://localhost:7474/db/data/node/293/relationships/all/{-list|&|types}”, “self”:“http://localhost:7474/db/data/node/293",“property”:"http://localhost:7474/db/data/node/293/properties/{key}”, “outgoing_typed_relationships”:“http://localhost:7474/db/data/node/293/relationships/out/{-list|&|types}”, “properties”:“http://localhost:7474/db/data/node/293/properties",“incoming_relationships”:"http://localhost:7474/db/data/node/293/relationships/in”, “extensions”:{},“create_relationship”:“http://localhost:7474/db/data/node/293/relationships”, “paged_traverse”:“http://localhost:7474/db/data/node/293/paged/traverse/{returnType}{?pageSize,leaseTime}”, “all_relationships”:“http://localhost:7474/db/data/node/293/relationships/all”, “incoming_typed_relationships”:“http://localhost:7474/db/data/node/293/relationships/in/{-list|&|types}”, “metadata”:{“id”:293,“labels”:[“Tag”]}}]],“columns”:[“n”,“m”]} 感觉并不太好去解析这个数据,但是使用jersey Client执行同样的语句返回的数据会比较容易解析如下: private static String sendTransactionalCypherQuery(String query) { // START SNIPPET: queryAllNodes final String txUri = SERVER_ROOT_URI + “transaction/commit”; WebResource resource = Client.create().resource(txUri);

	String payload = "{\"statements\" : [ {\"statement\" : \"" + query + "\"} ]}";
	ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
			.entity(payload).post(ClientResponse.class);
	// final URI location = response.getLocation();
	String str = response.getEntity(String.class);
	System.out.println(String.format(
			"POST [%s] to [%s], status code [%d], returned data: " + System.getProperty("line.separator") + "%s",
			payload, txUri, response.getStatus(), response.getEntity(String.class)));
	// System.out.println(response.getEntity(String.class));
	// URI location = response.getLocation();
	response.close();
	// END SNIPPET: queryAllNodes
	return str;
}

返回数据: {“results”:[{“columns”:[“n”,“m”],“data”:[{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“name”:“交通”,“id”:“130”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:497,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“id”:“4”,“title”:“2015年12月驾驶证逾期未换证公告”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:496,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“id”:“5”,“title”:“2016年08月机动车临界强制报废公告”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:498,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“id”:“6”,“title”:“2016年08月驾驶证停止使用公告”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:499,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“id”:“7”,“title”:“2016年09月驾驶证停止使用公告”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:500,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“id”:“59”,“title”:“江西省交通运输厅行政处罚汇总表”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:510,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“id”:“188”,“title”:“test22”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:539,“type”:“node”,“deleted”:false}]},{ “row”:[{“id”:“150”,“title”:“江西省交通运输厅行政处罚汇总表”},{“name”:“天气”,“id”:“155”}], “meta”:[{“id”:528,“type”:“node”,“deleted”:false},{“id”:530,“type”:“node”,“deleted”:false}]}]}],“errors”:[]}

回到顶部