add a new query function
This commit is contained in:
parent
470712a4e2
commit
dd13072d4a
|
@ -0,0 +1,141 @@
|
|||
/*=============================================================================
|
||||
# Filename: Benchmark.java
|
||||
# Author: Bookug Lobert
|
||||
# Mail: zengli-bookug@pku.edu.cn
|
||||
# Last Modified: 2017-12-04 11:24
|
||||
# Description:
|
||||
=============================================================================*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import jgsc.GstoreConnector;
|
||||
|
||||
class MyThread extends Thread {
|
||||
public static boolean correctness = true;
|
||||
private int num = 0;
|
||||
private GstoreConnector gc;
|
||||
private String sparql;
|
||||
private int rnum = 0;
|
||||
public MyThread(int _num, GstoreConnector _gc, String _sparql, int _rnum) {
|
||||
num = _num;
|
||||
gc = _gc;
|
||||
sparql = _sparql;
|
||||
rnum = _rnum;
|
||||
}
|
||||
public void run() {
|
||||
String filename = "result/" + num + ".txt";
|
||||
String answer = "";
|
||||
// String answer = gc.query("root", "123456", "test", sparql);
|
||||
gc.query("root", "123456", "test", sparql, filename);
|
||||
|
||||
FileInputStream in = null;
|
||||
try {
|
||||
in = new FileInputStream(filename);
|
||||
int size = in.available();
|
||||
byte[] buffer = new byte[size];
|
||||
in.read(buffer);
|
||||
in.close();
|
||||
answer = new String(buffer);
|
||||
} catch (IOException e) {
|
||||
System.out.println("error occurs: fail to open " + filename);
|
||||
}
|
||||
|
||||
// get result count
|
||||
int m = 0;
|
||||
for(int i = 0; i<sparql.length(); ++i)
|
||||
{
|
||||
if(sparql.charAt(i) == '?')
|
||||
++m;
|
||||
if(sparql.charAt(i) == '{')
|
||||
break;
|
||||
}
|
||||
int n = 0;
|
||||
for(int i = 0; i<answer.length(); ++i)
|
||||
{
|
||||
if(answer.charAt(i) == '{')
|
||||
++n;
|
||||
}
|
||||
int Num = (n-3)/(m+1);
|
||||
|
||||
// compare the result
|
||||
if (rnum != Num)
|
||||
correctness = false;
|
||||
}
|
||||
}
|
||||
|
||||
public class Benchmark1
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
GstoreConnector gc = new GstoreConnector("172.31.222.78", 3305);
|
||||
gc.load("test", "root", "123456");
|
||||
String[] spq = new String[6];
|
||||
spq[0] = "select ?x where"
|
||||
+"{"
|
||||
+"?x <ub:name> <FullProfessor0> ."
|
||||
+"}";
|
||||
spq[1] = "select distinct ?x where"
|
||||
+"{"
|
||||
+"?x <rdf:type> <ub:GraduateStudent>."
|
||||
+"?y <rdf:type> <ub:GraduateStudent>."
|
||||
+"?z <rdf:type> <ub:GraduateStudent>."
|
||||
+"?x <ub:memberOf> ?z."
|
||||
+"?z <ub:subOrganizationOf> ?y."
|
||||
+"?x <ub:undergaduateDegreeFrom> ?y."
|
||||
+"}";
|
||||
spq[2] = "select distinct ?x where"
|
||||
+"{"
|
||||
+"?x <rdf:type> <ub:Course>."
|
||||
+"?x <ub:name> ?y."
|
||||
+"}";
|
||||
spq[3] = "select ?x where"
|
||||
+"{"
|
||||
+"?x <rdf:type> <ub:UndergraduateStudent>."
|
||||
+"?y <ub:name> <Course1>."
|
||||
+"?x <ub:takesCourse> ?y."
|
||||
+"?z <ub:teacherOf> ?y."
|
||||
+"?z <ub:name> <FullProfessor1>."
|
||||
+"?z <ub:worksFor> ?w."
|
||||
+"?w <ub:name> <Department0>."
|
||||
+"}";
|
||||
spq[4] = "select distinct ?x where"
|
||||
+"{"
|
||||
+"?x <rdf:type> <ub:UndergraduateStudent>."
|
||||
+"?y <ub:name> <Course1>."
|
||||
+"?x <ub:takesCourse> ?y."
|
||||
+"?z <ub:teacherOf> ?y."
|
||||
+"?z <ub:name> <FullProfessor1>."
|
||||
+"?z <ub:worksFor> ?w."
|
||||
+"?w <ub:name> <Department0>."
|
||||
+"}";
|
||||
spq[5] = "select distinct ?x where"
|
||||
+"{"
|
||||
+"?x <rdf:type> <ub:UndergraduateStudent>."
|
||||
+"}";
|
||||
|
||||
|
||||
int[] result = {15, 0, 828, 27, 27, 5916};
|
||||
int tnum = 6;
|
||||
tnum = 3000;
|
||||
MyThread[] qt = new MyThread[tnum];
|
||||
for(int i = 0; i < tnum; ++i)
|
||||
{
|
||||
qt[i] = new MyThread(i, gc, spq[i%6], result[i%6]);
|
||||
qt[i].start();
|
||||
}
|
||||
for(int i = 0; i < tnum; ++i)
|
||||
{
|
||||
try {
|
||||
qt[i].join();
|
||||
}
|
||||
catch(InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (MyThread.correctness == true)
|
||||
System.out.println("The results are correct!");
|
||||
else
|
||||
System.out.println("The results exist errors!");
|
||||
}
|
||||
}
|
|
@ -13,7 +13,7 @@ public class JavaAPIExample
|
|||
public static void main(String[] args)
|
||||
{
|
||||
// initialize the GStore server's IP address and port.
|
||||
GstoreConnector gc = new GstoreConnector("172.31.222.93", 9016);
|
||||
GstoreConnector gc = new GstoreConnector("172.31.222.78", 3305);
|
||||
|
||||
//below are for parallel test
|
||||
//GstoreConnector gc = new GstoreConnector("172.31.222.94", 9000);
|
||||
|
@ -86,7 +86,7 @@ public class JavaAPIExample
|
|||
|
||||
// build a new database by a RDF file.
|
||||
// note that the relative path is related to gserver.
|
||||
gc.build("LUBM10", "data/lubm/LUBM_10.n3", "root", "123456");
|
||||
gc.build("LUBM10", "data/lubm/lubm.nt", "root", "123456");
|
||||
gc.load("LUBM10", "root", "123456");
|
||||
|
||||
// then you can execute SPARQL query on this database.
|
||||
|
@ -119,7 +119,10 @@ public class JavaAPIExample
|
|||
//The related code is in api/http/java/src/jgsc/GstoreConnector.java
|
||||
answer = gc.query("root", "123456", "LUBM10", sparql);
|
||||
System.out.println(answer);
|
||||
|
||||
|
||||
// make a SPARQL query and save the result in ans.txt
|
||||
gc.query("root", "123456", "LUBM10", sparql, "ans.txt");
|
||||
|
||||
//monitor a database
|
||||
answer = gc.monitor("LUBM10");
|
||||
System.out.println(answer);
|
||||
|
|
|
@ -69,12 +69,12 @@ public class GstoreConnector {
|
|||
// 获取所有响应头字段
|
||||
Map<String, List<String>> map = connection.getHeaderFields();
|
||||
// 遍历所有的响应头字段
|
||||
for (String key : map.keySet()) {
|
||||
System.out.println(key + "--->" + map.get(key));
|
||||
}
|
||||
//for (String key : map.keySet()) {
|
||||
// System.out.println(key + "--->" + map.get(key));
|
||||
//}
|
||||
|
||||
long t1 = System.currentTimeMillis(); //ms
|
||||
System.out.println("Time to get header: "+(t1 - t0)+" ms");
|
||||
//System.out.println("Time to get header: "+(t1 - t0)+" ms");
|
||||
//System.out.println("============================================");
|
||||
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
|
@ -83,13 +83,13 @@ public class GstoreConnector {
|
|||
while ((line = in.readLine()) != null) {
|
||||
//PERFORMANCE: this can be very costly if result is very large, because many temporary Strings are produced
|
||||
//In this case, just print the line directly will be much faster
|
||||
result.append(line);
|
||||
result.append(line+"\n");
|
||||
//System.out.println("get data size: " + line.length());
|
||||
//System.out.println(line);
|
||||
}
|
||||
|
||||
long t2 = System.currentTimeMillis(); //ms
|
||||
System.out.println("Time to get data: "+(t2 - t1)+" ms");
|
||||
//System.out.println("Time to get data: "+(t2 - t1)+" ms");
|
||||
} catch (Exception e) {
|
||||
System.out.println("error in get request: " + e);
|
||||
e.printStackTrace();
|
||||
|
@ -107,6 +107,81 @@ public class GstoreConnector {
|
|||
return result.toString();
|
||||
}
|
||||
|
||||
public void sendGet(String param, String filename) {
|
||||
String url = "http://" + this.serverIP + ":" + this.serverPort;
|
||||
BufferedReader in = null;
|
||||
System.out.println("parameter: "+param);
|
||||
|
||||
FileWriter fw = null;
|
||||
try {
|
||||
fw = new FileWriter(filename);
|
||||
} catch (IOException e) {
|
||||
System.out.println("can not open " + filename + "!");
|
||||
}
|
||||
|
||||
try {
|
||||
param = URLEncoder.encode(param, "UTF-8");
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
throw new RuntimeException("Broken VM does not support UTF-8");
|
||||
}
|
||||
|
||||
try {
|
||||
String urlNameString = url + "/" + param;
|
||||
System.out.println("request: "+urlNameString);
|
||||
URL realUrl = new URL(urlNameString);
|
||||
// 打开和URL之间的连接
|
||||
URLConnection connection = realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
connection.setRequestProperty("accept", "*/*");
|
||||
connection.setRequestProperty("connection", "Keep-Alive");
|
||||
//set agent to avoid: speed limited by server if server think the client not a browser
|
||||
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
// 建立实际的连接
|
||||
connection.connect();
|
||||
|
||||
long t0 = System.currentTimeMillis(); //ms
|
||||
|
||||
// 获取所有响应头字段
|
||||
Map<String, List<String>> map = connection.getHeaderFields();
|
||||
// 遍历所有的响应头字段
|
||||
//for (String key : map.keySet()) {
|
||||
// System.out.println(key + "--->" + map.get(key));
|
||||
//}
|
||||
|
||||
long t1 = System.currentTimeMillis(); // ms
|
||||
//System.out.println("Time to get header: "+(t1 - t0)+" ms");
|
||||
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
if (fw != null)
|
||||
fw.write(line+"\n");
|
||||
}
|
||||
|
||||
long t2 = System.currentTimeMillis(); //ms
|
||||
//System.out.println("Time to get data: "+(t2 - t1)+" ms");
|
||||
} catch (Exception e) {
|
||||
//System.out.println("error in get request: " + e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 使用finally块来关闭输入流
|
||||
finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
if (fw != null) {
|
||||
fw.close();
|
||||
}
|
||||
} catch (Exception e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
//NOTICE: no need to connect now, HTTP connection is kept by default
|
||||
public boolean load(String _db_name, String _username, String _password) {
|
||||
boolean connect_return = this.connect();
|
||||
|
@ -139,7 +214,6 @@ public class GstoreConnector {
|
|||
return false;
|
||||
}
|
||||
|
||||
//String cmd = "unload/" + _db_name;
|
||||
String cmd = "?operation=unload&db_name=" + _db_name + "&username=" + _username + "&password=" + _password;
|
||||
String msg = this.sendGet(cmd);
|
||||
|
||||
|
@ -215,6 +289,21 @@ public class GstoreConnector {
|
|||
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void query(String _username, String _password, String _db_name, String _sparql, String _filename) {
|
||||
boolean connect_return = this.connect();
|
||||
if (!connect_return) {
|
||||
System.err.println("connect to server error. @GstoreConnector.query");
|
||||
}
|
||||
|
||||
String cmd = "?operation=query&username=" + _username + "&password=" + _password + "&db_name=" + _db_name + "&format=json&sparql=" + _sparql;
|
||||
this.sendGet(cmd, _filename);
|
||||
|
||||
this.disconnect();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// public String show() {
|
||||
// return this.show(false);
|
||||
|
|
Loading…
Reference in New Issue