update docs in http api

This commit is contained in:
mrmrfan 2018-07-28 20:21:58 +08:00
parent ca57beae3e
commit 152f355faa
8 changed files with 295 additions and 7 deletions

View File

@ -9,7 +9,7 @@
import java.io.*;
import java.util.*;
import jgsc.GstoreConnector;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
class MyThread extends Thread {
public static boolean correctness = true;
//public static int cnum = 0;

View File

@ -1,11 +1,11 @@
/*=============================================================================
# Filename: Benchmark.java
# Author: Bookug Lobert
# Mail: zengli-bookug@pku.edu.cn
# Last Modified: 2017-12-04 11:24
# Filename: Benchmark1.java
# Author: yangchaofan
# Last Modified: 2018-7-28 15:46
# Description:
=============================================================================*/
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
import java.io.*;
import java.util.*;
import jgsc.GstoreConnector;

View File

@ -7,7 +7,7 @@
import jgsc.GstoreConnector;
// before run this example, you must start up the GStore server at first (use command ./gserver).
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
public class JavaAPIExample
{
public static void main(String[] args)

View File

@ -0,0 +1,138 @@
<?php
/*
# Filename: Benchmark.php
# Author: yangchaofan
# Last Modified: 2018-7-28 15:37
# Description: a multi-thread example for php API
*/
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
require '../src/GstoreConnector.php';
// variable definition
$tnum = 3000;
$correctness = true;
$threads = array();
$result = array(15, 0, 828, 27, 27, 5916);
$sparql = array();
$sparql0 = "select ?x where
{
?x <ub:name> <FullProfessor0> .
}";
$sparql1 = "select distinct ?x where
{
?x <rdf:type> <ub:GraduateStudent>.
?y <rdf:type> <ub:GraduateStudent>.
?y <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>.
}";
class Mythread extends Thread {
var $rnum, $sparql, $filename;
public function __construct($rnum, $sparql, $filename) {
$this->rnum = $rnum;
$this->sparql = $sparql;
$this->filename = $filename;
}
public function run () {
// query
$gc = new 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", $this->sparql, $this->filename);
//$res = $gc->query("root", "123456", "test", $sparql);
// read the file to a str
$f = fopen($this->filename, "r") or exit("fail to open " . $this->filename);
$res = "";
while(!feof($f)) {
$res .= (fgets($f). PHP_EOL);
}
fclose($f);
// count the num
$m = 0;
for ($i = 0; $i < strlen($this->sparql); $i++) {
if ($this->sparql[$i] == "?")
$m = $m + 1;
if ($this->sparql[$i] == "{")
break;
}
$n = 0;
for ($i = 0; $i < strlen($res); $i++) {
if ($res[$i] == "{")
$n = $n + 1;
}
$Num = ($n-3)/($m+1);
// compare the result
if ($this->rnum != $Num) {
$correctness = false;
echo "sparql: " . $this->sparql . PHP_EOL;
echo "Num: " . strval($Num) . PHP_EOL;
}
}
}
# create sparql
array_push($sparql, $sparql0, $sparql1, $sparql2);
array_push($sparql, $sparql3, $sparql4, $sparql5);
# create the threads
for ($i = 0; $i < $tnum; $i++) {
$filename = "result/" . strval($i) . ".txt";
$threads[] = new Mythread($result[$i%6], $sparql[$i%6], $filename);
}
foreach ($threads as $i)
$i->start();
foreach ($threads as $i)
$i->join();
if ($correctness == true)
echo "The answers are correct!" . PHP_EOL;
else
echo "The answers exist errors!" . PHP_EOL;
echo "Main thread exit" . PHP_EOL;
?>

View File

@ -0,0 +1,40 @@
<?php
/*
# Filename: phpAPIExample.php
# Author: yangchaofan
# Last Modified: 2018-7-27 21:50
# Description: a simple example of php API
*/
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
require "../src/GstoreConnector.php";
// example
$username = "root";
$password = "123456";
$filename = "res.txt";
$sparql = "select ?x where { ?x <ub:name> <FullProfessor0> .}";
// start a gc
$gc = new GstoreConnector("172.31.222.78", 3305);
// build database
$ret = $gc->build("test", "data/lubm/lubm.nt", $username, $password);
echo $ret . PHP_EOL;
// load rdf
$ret = $gc->load("test", $username, $password);
echo $ret . PHP_EOL;
// unload rdf
//$ret = $gc->unload("test", $username, $password);
//echo $ret . PHP_EOL;
// query
echo $gc->query($username, $password, "test", $sparql) . PHP_EOL;
// fquery--make a SPARQL query and save the result in the file
$gc->fquery($username, $password, "test", $sparql, $filename);
?>

View File

@ -0,0 +1,109 @@
<?php
/*
# Filename: GstoreConnector.php
# Author: yangchaofan
# Last Modified: 2018-7-18 14:50
# Description: http api for php
*/
class GstoreConnector {
var $serverIP;
var $serverPort;
var $Url;
function __construct($ip, $port) {
$this->serverIP = $ip;
$this->serverPort = $port;
$this->Url = "http://" . $ip . ":" . strval($port);
}
function Encode($url) {
$ret = "";
for ($i = 0; $i < strlen($url); $i++) {
$c = $url[$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;
else if (ord($c)>=48 and ord($c)<=57)
$ret .= $c;
else if (ord($c)>=65 and ord($c)<=90)
$ret .= $c;
else if (ord($c)>=97 and ord($c)<=122)
$ret .= $c;
else if (ord($c)==32)
$ret .= "+";
else
$ret .= sprintf("%%%X", ord($c));
}
return $ret;
}
function Get($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$this->Encode($url));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$res = curl_exec($curl);
if ($res == FALSE) {
echo "CURL ERROR: ".curl_error($curl).PHP_EOL;
}
curl_close($curl);
return $res;
}
function fGet($url, $filename){
$curl = curl_init();
curl_setopt($curl, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($curl, CURLOPT_URL, $this->Encode($url));
$fp = fopen($filename, "w");
if (!$fp) {
echo "open file failed".PHP_EOL;
return;
}
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_exec($curl);
curl_close($curl);
fclose($fp);
return;
}
function load($db_name, $username, $password) {
$cmd = $this->Url . "/?operation=load&db_name=" . $db_name . "&username=" . $username . "&password=" . $password;
$res = $this->Get($cmd);
echo $res.PHP_EOL;
if ($res == "load database done.")
return true;
return false;
}
function unload($db_name, $username, $password) {
$cmd = $this->Url . "/?operation=unload&db_name=" . $db_name . "&username=" . $username . "&password=" . $password;
$res = $this->Get($cmd);
echo $res.PHP_EOL;
if ($res == "unload database done.")
return true;
return false;
}
function build($db_name, $rdf_file_path, $username, $password) {
$cmd = $this->Url . "/?operation=build&db_name=" . $db_name . "&ds_path=" . $rdf_file_path . "&username=" . $username . "&password=" . $password;
$res = $this->Get($cmd);
echo $res.PHP_EOL;
if ($res == "import RDF file to database done.")
return true;
return false;
}
function query($username, $password, $db_name, $sparql) {
$cmd = $this->Url . "/?operation=query&username=" . $username . "&password=" . $password . "&db_name=" . $db_name . "&format=json&sparql=" . $sparql;
return $this->Get($cmd);
}
function fquery($username, $password, $db_name, $sparql, $filename) {
$cmd = $this->Url . "/?operation=query&username=" . $username . "&password=" . $password . "&db_name=" . $db_name . "&format=json&sparql=" . $sparql;
$this->fGet($cmd, $filename);
return;
}
}
?>

View File

@ -4,6 +4,7 @@
# Last Modified: 2018-7-18 15:13
# Description: a simple example of multi-thread query
"""
# before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
import threading
import sys

View File

@ -8,7 +8,7 @@ import sys
sys.path.append('../src')
import GstoreConnector
# before you run this example, make sure that you have started up gStore server (use ./ghttp ~ ~)
# before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
username = "root"
password = "123456"
sparql = "select ?x where \