import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
public class Neo4jUtils {
public static String getResponseString(String payload,String connStr,String user,String pwd) {
String rs = "";
String txUri = connStr;
String user_name=user;
String password=pwd;
Client client = Client.create();
final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(
user_name, password);
client.addFilter(authFilter);
WebResource resource = client.resource(txUri);
ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON).entity(payload)
.post(ClientResponse.class);
rs = response.getEntity(String.class);
response.close();
return rs;
}
public static String genQueryString(String cypherString,Map<String,String> paramsMap){
String result= "";
JSONObject paramJSON = new JSONObject();
if(paramsMap!=null){
for(Iterator<String> it=paramsMap.keySet().iterator();it.hasNext();){
String key = it.next();
String value = paramsMap.get(key);
paramJSON.put(key, value);
}
}
JSONObject statementJSON = new JSONObject();
statementJSON.put("statement", cypherString);
statementJSON.put("parameters", paramsMap);
JSONArray statementsJA = new JSONArray();
statementsJA.add(statementJSON);
JSONObject statementsJSON = new JSONObject();
statementsJSON.put("statements", statementsJA);
result = statementsJSON.toString();
return result;
}
public static String genQueryString(String cypherString,Map<String,String> paramsMap , List<String> resultDataContents){
String result= "";
JSONObject paramJSON = new JSONObject();
if(paramsMap!=null){
for(Iterator<String> it=paramsMap.keySet().iterator();it.hasNext();){
String key = it.next();
String value = paramsMap.get(key);
paramJSON.put(key, value);
}
}
JSONObject statementJSON = new JSONObject();
statementJSON.put("statement", cypherString);
statementJSON.put("parameters", paramsMap);
statementJSON.put("resultDataContents", resultDataContents) ;
JSONArray statementsJA = new JSONArray();
statementsJA.add(statementJSON);
JSONObject statementsJSON = new JSONObject();
statementsJSON.put("statements", statementsJA);
result = statementsJSON.toString();
return result;
}
public static String genQueryString(String cypherString){
return genQueryString(cypherString,null);
}
public static Long getRows(String cypherString,Map<String,String> paramsMap,String connStr,String user,String pwd){
Long rows = 0l;
String res = getResponseString(genQueryString(cypherString,paramsMap), connStr, user, pwd);
JSONObject jObj = JSONObject.fromObject(res);
rows = jObj.getJSONArray("results").getJSONObject(0).getJSONArray("data").getJSONObject(0).getJSONArray("row").getLong(0);
return rows;
}
public static Long getRows(String cypherString,String connStr,String user,String pwd){
return getRows(cypherString,null, connStr, user, pwd);
}
}
java访问neo4j
最新推荐文章于 2024-08-16 08:38:57 发布