implement http api for python
This commit is contained in:
parent
dd13072d4a
commit
568419839c
|
@ -0,0 +1,124 @@
|
|||
"""
|
||||
# Filename: Benchmark.py
|
||||
# Author: yangchaofan
|
||||
# Last Modified: 2018-7-18 15:13
|
||||
# Description: a simple example of multi-thread query
|
||||
"""
|
||||
|
||||
import threading
|
||||
import sys
|
||||
sys.path.append('../src')
|
||||
import GstoreConnector
|
||||
|
||||
# variables definition
|
||||
tnum = 3000
|
||||
correctness = True
|
||||
|
||||
threads = []
|
||||
result = [15, 0, 828, 27, 27, 5916]
|
||||
sparql = []
|
||||
sparql0 = "select ?x where\
|
||||
{\
|
||||
?x <ub:name> <FullProfessor0> .\
|
||||
}"
|
||||
sparql1 = "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.\
|
||||
}"
|
||||
sparql2 = "select distinct ?x where\
|
||||
{\
|
||||
?x <rdf:type> <ub:Course>.\
|
||||
?x <ub:name> ?y.\
|
||||
}"
|
||||
sparql3 = "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>.\
|
||||
}"
|
||||
sparql4 = "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>.\
|
||||
}"
|
||||
sparql5 = "select distinct ?x where\
|
||||
{\
|
||||
?x <rdf:type> <ub:UndergraduateStudent>.\
|
||||
}"
|
||||
|
||||
# thread function
|
||||
def Mythread(rnum, sparql, filename):
|
||||
global correctness
|
||||
|
||||
# query
|
||||
gc = GstoreConnector.GstoreConnector("172.31.222.78", 3305)
|
||||
#gc.build("test", "data/lubm/lubm.nt", "root", "123456")
|
||||
#gc.load("test", "root", "123456")
|
||||
gc.fquery("root", "123456", "test", sparql, filename)
|
||||
#res = gc.query("root", "123456", "test", sparql)
|
||||
|
||||
# read the file to a str
|
||||
with open(filename, "r") as f:
|
||||
res = f.read()
|
||||
|
||||
# count the nums
|
||||
m = 0
|
||||
for i in range(len(sparql)):
|
||||
if (sparql[i] == "?"):
|
||||
m = m + 1
|
||||
if (sparql[i] == "{"):
|
||||
break
|
||||
n = 0
|
||||
for i in range(len(res)):
|
||||
if (res[i] == "{"):
|
||||
n = n + 1
|
||||
Num = (n-3)/(m+1)
|
||||
|
||||
# compare the result
|
||||
if (rnum != Num):
|
||||
correctness = False
|
||||
print("sparql: "+sparql)
|
||||
print("Num: "+str(Num))
|
||||
|
||||
# create sparql
|
||||
sparql.append(sparql0)
|
||||
sparql.append(sparql1)
|
||||
sparql.append(sparql2)
|
||||
sparql.append(sparql3)
|
||||
sparql.append(sparql4)
|
||||
sparql.append(sparql5)
|
||||
|
||||
#create the threads
|
||||
for i in range(tnum):
|
||||
filename = "result/res" + str(i) + ".txt"
|
||||
t = threading.Thread(target=Mythread, args=(result[i%6],sparql[i%6],filename,))
|
||||
threads.append(t)
|
||||
|
||||
# start threads
|
||||
for i in threads:
|
||||
i.start()
|
||||
|
||||
# wait for the threads
|
||||
for i in threads:
|
||||
i.join()
|
||||
|
||||
if (correctness == True):
|
||||
print("The answers are correct!")
|
||||
else:
|
||||
print("The answers exist errors!")
|
||||
|
||||
print("Main thread exit")
|
|
@ -0,0 +1,44 @@
|
|||
"""
|
||||
# Filename: PyAPIExample.py
|
||||
# Author: yangchaofan
|
||||
# Last Modified: 2018-7-18 15:10
|
||||
# Description: a simple example of python API
|
||||
"""
|
||||
import sys
|
||||
sys.path.append('../src')
|
||||
import GstoreConnector
|
||||
|
||||
# before you run this example, make sure that you have started up gStore server (use ./ghttp ~ ~)
|
||||
username = "root"
|
||||
password = "123456"
|
||||
sparql = "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>. \
|
||||
}"
|
||||
filename = "res.txt"
|
||||
|
||||
# start a gc with given IP and Port
|
||||
gc = GstoreConnector.GstoreConnector("172.31.222.78", 3305)
|
||||
|
||||
# unload the database
|
||||
#ret = gc.unload("test", username, password)
|
||||
|
||||
# build database with a RDF graph
|
||||
ret = gc.build("test", "data/lubm/lubm.nt", username, password)
|
||||
|
||||
# load the database
|
||||
ret = gc.load("test", username, password)
|
||||
|
||||
# query
|
||||
print (gc.query(username, password, "test", sparql))
|
||||
|
||||
# query and save the result in a file
|
||||
gc.fquery(username, password, "test", sparql, filename)
|
||||
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
"""
|
||||
# Filename: GStoreConnector.py
|
||||
# Author: yangchaofan
|
||||
# Last Modified: 2018-7-18 14:44
|
||||
# Description: http api for python
|
||||
"""
|
||||
|
||||
import requests
|
||||
|
||||
defaultServerIP = "127.0.0.1"
|
||||
defaultServerPort = "3305"
|
||||
|
||||
class GstoreConnector:
|
||||
def __init__(self, ip, port):
|
||||
if (ip == "localhost"):
|
||||
self.serverIP = defaultServerIP
|
||||
else:
|
||||
self.serverIP = ip
|
||||
self.serverPort = port
|
||||
self.Url = "http://" + self.serverIP + ":" + str(self.serverPort)
|
||||
|
||||
def UrlEncode(self, s):
|
||||
ret = ""
|
||||
for i in range(len(s)):
|
||||
c = s[i]
|
||||
if ((ord(c)==42) or (ord(c)==45) or (ord(c)==46) or (ord(c)==47) or (ord(c)==58) or (ord(c)==95)):
|
||||
ret += c
|
||||
elif ((ord(c)>=48) and (ord(c)<=57)):
|
||||
ret += c
|
||||
elif ((ord(c)>=65) and (ord(c)<=90)):
|
||||
ret += c
|
||||
elif ((ord(c)>=97) and (ord(c)<=122)):
|
||||
ret += c
|
||||
else:
|
||||
ret += "{}{:X}".format("%", ord(c))
|
||||
return ret
|
||||
|
||||
def Get(self, strUrl):
|
||||
r = requests.get(self.UrlEncode(strUrl))
|
||||
return r.text
|
||||
|
||||
def fGet(self, strUrl, filename):
|
||||
r = requests.get(self.UrlEncode(strUrl), stream=True)
|
||||
with open(filename, 'wb') as fd:
|
||||
for chunk in r.iter_content(4096):
|
||||
fd.write(chunk)
|
||||
return
|
||||
|
||||
def load(self, db_name, username, password):
|
||||
cmd = self.Url + "/?operation=load&db_name=" + db_name + "&username=" + username + "&password=" + password
|
||||
res = self.Get(cmd)
|
||||
print res
|
||||
if res == "load database done.":
|
||||
return True
|
||||
return False
|
||||
|
||||
def unload(self, db_name, username, password):
|
||||
cmd = self.Url + "/?operation=unload&db_name=" + db_name + "&username=" + username + "&password=" + password
|
||||
res = self.Get(cmd)
|
||||
print(res)
|
||||
if res == "unload database done.":
|
||||
return True
|
||||
return False
|
||||
|
||||
def build(self, db_name, rdf_file_path, username, password):
|
||||
cmd = self.Url + "/?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + username + "&password=" + password
|
||||
res = self.Get(cmd)
|
||||
print(res)
|
||||
if res == "import RDF file to database done.":
|
||||
return True
|
||||
return False
|
||||
|
||||
def query(self, username, password, db_name, sparql):
|
||||
cmd = self.Url + "/?operation=query&username=" + username + "&password=" + password + "&db_name=" + db_name + "&format=json&sparql=" + sparql
|
||||
return self.Get(cmd)
|
||||
|
||||
def fquery(self, username, password, db_name, sparql, filename):
|
||||
cmd = self.Url + "/?operation=query&username=" + username + "&password=" + password + "&db_name=" + db_name + "&format=json&sparql=" + sparql
|
||||
self.fGet(cmd, filename)
|
||||
return
|
||||
|
||||
def show(self):
|
||||
cmd = self.Url + "/?operation=show"
|
||||
return Get(cmd)
|
||||
|
||||
def user(self, type, username1, password1, username2, addtion):
|
||||
cmd = self.Url + "/?operation=user&type=" + type + "&username1=" + username1+ "&password1=" + password1 + "&username2=" + username2 + "&addtion=" +addition
|
||||
return self.Get(cmd)
|
||||
|
||||
def showUser(self):
|
||||
cmd = self.Url + "/?operation=showUser"
|
||||
return self.Get(cmd)
|
||||
|
||||
def monitor(self, db_name):
|
||||
cmd = self.Url + "/?operation=monitor&db_name=" + db_name;
|
||||
return self.Get(cmd)
|
||||
|
||||
def checkpoint(self, db_name):
|
||||
cmd = self.Url + "/?operation=checkpoint&db_name=" + db_name
|
||||
return self.Get(cmd)
|
Loading…
Reference in New Issue