Update C++, java, python, php API and add POST request-type.

This commit is contained in:
suxunbin 2019-05-20 12:07:54 +08:00
parent 5747dcde1f
commit 7f1724fc68
35 changed files with 3059 additions and 2529 deletions

File diff suppressed because it is too large Load Diff

View File

@ -61,9 +61,9 @@ int main(int argc, char *argv[])
ofp.open("system.db/password" + port + ".txt", ios::in);
ofp >> system_password;
ofp.close();
CHttpClient hc;
GstoreConnector gc;
string res;
int ret;
ret = hc.Get("http://127.0.0.1:" + port + "/?operation=stop&username=" + SYSTEM_USERNAME + "&password=" + system_password, res);
ret = gc.Get("http://127.0.0.1:" + port + "/?operation=stop&username=" + SYSTEM_USERNAME + "&password=" + system_password, res);
return 0;
}

View File

@ -4,11 +4,28 @@
#include <cstring>
#include <iostream>
using namespace std;
CHttpClient::CHttpClient(void) :
#define defaultServerIP "127.0.0.1"
#define defaultServerPort 9000
GstoreConnector::GstoreConnector(void) :
m_bDebug(false)
{
}
CHttpClient::~CHttpClient(void)
GstoreConnector::GstoreConnector(std::string _ip, int _port, std::string _user, std::string _passwd) :
m_bDebug(false)
{
if (_ip == "localhost")
this->serverIP = defaultServerIP;
else
this->serverIP = _ip;
this->serverPort = _port;
this->Url = "http://" + this->serverIP + ":" + std::to_string(this->serverPort);
this->username = _user;
this->password = _passwd;
}
GstoreConnector::~GstoreConnector(void)
{
}
@ -42,7 +59,6 @@ static const std::string UrlEncode(const std::string& s)
return ret;
}
static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
if(itype == CURLINFO_TEXT)
@ -81,38 +97,9 @@ static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
return nmemb;
}
int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
int GstoreConnector::Get(const std::string & strUrl, std::string & strResponse)
{
strResponse.clear();
CURLcode res;
CURL* curl = curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
int CHttpClient::Get(const std::string &strUrl, const std::string &filename, bool SavedOnFile) {
if (!SavedOnFile)
return -1;
CURLcode res;
CURL* curl = curl_easy_init();
if (NULL == curl)
@ -124,42 +111,6 @@ int CHttpClient::Get(const std::string &strUrl, const std::string &filename, boo
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 4096);
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
FILE* fw = fopen(filename.c_str(), "wb");
if (!fw)
{
cout << "open file failed" << endl;
return -1;
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fw);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fw);
return res;
}
int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
{
strResponse.clear();
CURLcode res;
CURL* curl = curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
@ -176,7 +127,143 @@ int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
return res;
}
int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
int GstoreConnector::Get(const std::string &strUrl, const std::string &filename, bool SavedOnFile) {
if (!SavedOnFile)
return -1;
CURLcode res;
CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 4096);
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
FILE* fw = fopen(filename.c_str(), "wb");
if (!fw)
{
cout << "open file failed" << endl;
return -1;
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fw);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fw);
return res;
}
int GstoreConnector::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse)
{
strResponse.clear();
CURLcode res;
CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
//curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
int GstoreConnector::Post(const std::string &strUrl, const std::string & strPost, const std::string &filename, bool SavedOnFile)
{
if (!SavedOnFile)
return -1;
CURLcode res;
CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, 4096);
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
FILE* fw = fopen(filename.c_str(), "wb");
if (!fw)
{
cout << "open file failed" << endl;
return -1;
}
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fw);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fw);
return res;
}
int GstoreConnector::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
{
strResponse.clear();
CURLcode res;
CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if (NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
//curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
int GstoreConnector::Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath)
{
strResponse.clear();
CURLcode res;
@ -209,53 +296,208 @@ int CHttpClient::Posts(const std::string & strUrl, const std::string & strPost,
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
//curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
//curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
int CHttpClient::Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath)
std::string GstoreConnector::build(std::string db_name, std::string rdf_file_path, std::string request_type)
{
strResponse.clear();
CURLcode res;
CURL* curl = curl_easy_init();
if(NULL == curl)
std::string res;
if (request_type == "GET")
{
return CURLE_FAILED_INIT;
std::string strUrl = this->Url + "/?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
if(m_bDebug)
else if (request_type == "POST")
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
std::string strUrl = this->Url + "/build";
std::string strPost = "{\"db_name\": \"" + db_name + "\", \"ds_path\": \"" + rdf_file_path + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
curl_easy_setopt(curl, CURLOPT_URL, UrlEncode(strUrl).c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void CHttpClient::SetDebug(bool bDebug)
std::string GstoreConnector::load(std::string db_name, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=load&db_name=" + db_name + "&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/load";
std::string strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::unload(std::string db_name, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=unload&db_name=" + db_name + "&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/unload";
std::string strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::user(std::string type, std::string username2, std::string addition, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=user&type=" + type + "&username1=" + this->username + "&password1=" + this->password + "&username2=" + username2 + "&addition=" + addition;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/user";
std::string strPost = "{\"type\": \"" + type + "\", \"username1\": \"" + this->username + "\", \"password1\": \"" + this->password + "\", \"username2\": \"" + username2 + "\", \"addition\": \"" + addition + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::showUser(std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=showUser&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/showUser";
std::string strPost = "{\"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::query(std::string db_name, std::string format, std::string sparql, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=query&username=" + this->username + "&password=" + this->password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/query";
std::string strPost = "{\"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\", \"db_name\": \"" + db_name + "\", \"format\": \"" + format + "\", \"sparql\": \"" + sparql + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
void GstoreConnector::fquery(std::string db_name, std::string format, std::string sparql, std::string filename, std::string request_type)
{
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=query&username=" + this->username + "&password=" + this->password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql;
int ret = this->Get(strUrl, filename, true);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/query";
std::string strPost = "{\"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\", \"db_name\": \"" + db_name + "\", \"format\": \"" + format + "\", \"sparql\": \"" + sparql + "\"}";
int ret = this->Post(strUrl, strPost, filename, true);
}
return;
}
std::string GstoreConnector::drop(std::string db_name, bool is_backup, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl;
if (is_backup)
strUrl = this->Url + "/?operation=drop&db_name=" + db_name + "&username=" + this->username + "&password=" + this->password + "&is_backup=true";
else
strUrl = this->Url + "/?operation=drop&db_name=" + db_name + "&username=" + this->username + "&password=" + this->password + "&is_backup=false";
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/drop";
std::string strPost;
if (is_backup)
strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\", \"is_backup\": \"true\"}";
else
strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\", \"is_backup\": \"false\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::monitor(std::string db_name, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=monitor&db_name=" + db_name + "&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/monitor";
std::string strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::checkpoint(std::string db_name, std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=checkpoint&db_name=" + db_name + "&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/checkpoint";
std::string strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
std::string GstoreConnector::show(std::string request_type)
{
std::string res;
if (request_type == "GET")
{
std::string strUrl = this->Url + "/?operation=show&username=" + this->username + "&password=" + this->password;
int ret = this->Get(strUrl, res);
}
else if (request_type == "POST")
{
std::string strUrl = this->Url + "/show";
std::string strPost = "{\"username\": \"" + this->username + "\", \"password\": \"" + this->password + "\"}";
int ret = this->Post(strUrl, strPost, res);
}
return res;
}
void GstoreConnector::SetDebug(bool bDebug)
{
m_bDebug = bDebug;
}

View File

@ -1,3 +1,10 @@
/*
# Filename: client.h
# Author: suxunbin
# Last Modified: 2019-5-17 14:56
# Description: http api for C++
*/
#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__
@ -9,11 +16,17 @@
#include <string>
class CHttpClient
class GstoreConnector
{
public:
CHttpClient(void);
~CHttpClient(void);
std::string serverIP;
int serverPort;
std::string Url;
std::string username;
std::string password;
GstoreConnector(void);
GstoreConnector(std::string _ip, int _port, std::string _user, std::string _passwd);
~GstoreConnector(void);
public:
/**
@ -23,6 +36,9 @@ public:
* @param strResponse ,
* @return Post成功
*/
int Post(const std::string & strUrl, const std::string & strPost, const std::string & filename, bool SavedOnFile);
int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
/**
@ -56,6 +72,17 @@ public:
int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
public:
std::string build(std::string db_name, std::string rdf_file_path, std::string request_type = "GET");
std::string load(std::string db_name, std::string request_type = "GET");
std::string unload(std::string db_name, std::string request_type = "GET");
std::string user(std::string type, std::string username2, std::string addition, std::string request_type = "GET");
std::string showUser(std::string request_type = "GET");
std::string query(std::string db_name, std::string format, std::string sparql, std::string request_type = "GET");
void fquery(std::string db_name, std::string format, std::string sparql, std::string filename, std::string request_type = "GET");
std::string drop(std::string db_name, bool is_backup, std::string request_type = "GET");
std::string monitor(std::string db_name, std::string request_type = "GET");
std::string checkpoint(std::string db_name, std::string request_type = "GET");
std::string show(std::string request_type = "GET");
void SetDebug(bool bDebug);
private:

View File

@ -1,30 +0,0 @@
//NOTICE: you need to use libcurl-devel for C++ to use HTTP client, please read client.cpp and client.h seriously
#include <stdio.h>
#include <iostream>
#include <string>
#include "client.h"
using namespace std;
int main()
{
CHttpClient hc;
string res;
int ret;
//NOTICE: here no need to add admin.html
//ret = hc.Get("172.31.222.78:9000/build/lubm/data/LUBM_10.n3", res);
//cout<<res<<endl;
ret = hc.Get("172.31.222.78:9000/load/lubm10", res);
cout<<res<<endl;
//ret = hc.Get("127.0.0.1:8080/query/data/ex0.sql", res);
//cout<<res<<endl;
//ret = hc.Get("127.0.0.1:8080/monitor", res);
//cout<<res<<endl;
ret = hc.Get("172.31.222.78:9000/unload", res);
cout<<res<<endl;
return 0;
}

View File

@ -1,3 +1,10 @@
/*=============================================================================
# Filename: Benchmark.cpp
# Author: suxunbin
# Last Modified: 2019-5-17 21:02
# Description: a simple example of multi-thread query
=============================================================================*/
#include "client.h"
#include <pthread.h>
#include <iostream>
@ -6,113 +13,132 @@
#include <cstring>
#include <fstream>
using namespace std;
//#define tnum 12000
#define tnum 3000
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
// default db_name: lubm(must be built in advance)
#define IP "127.0.0.1"
#define Port 9000
#define username "root"
#define password "123456"
#define tnum 1000
#define RequestType "POST"
bool correctness = true;
bool lcorrectness = true;
bool ucorrectness = true;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
pthread_mutex_t mutex;
string int2string(int n)
{
string s;
stringstream ss;
ss<<n;
ss>>s;
return s;
}
struct MyThread_args{
int num;
string sparql;
int rnum;
string sparql;
string filename;
string request_type;
};
void* MyThread_run(void* thread_args)
{
struct MyThread_args *args;
args = (struct MyThread_args *)thread_args;
CHttpClient hc;
string res;
int ret;
ret = hc.Get("http://172.31.222.94:9000/?operation=query&username=root&password=123456&db_name=dbpedia&format=json&sparql="+args->sparql,res);
GstoreConnector gc(IP, Port, username, password);
// query
string res = gc.query("lubm", "json", args->sparql, args->request_type);
// fquery
//gc.fquery("lubm", "json", args->sparql, args->filename, args->request_type);
//ifstream f(args->filename);
//stringstream buffer;
//buffer << f.rdbuf();
//string res = buffer.str();
//f.close();
//count the num
int m = 0;
for(int i = 0; i<args->sparql.length(); ++i)
for (int i = 0; i<args->sparql.length(); ++i)
{
if(args->sparql[i]=='?')
if (args->sparql[i] == '?')
++m;
if(args->sparql[i]=='{')
if (args->sparql[i] == '{')
break;
}
int n = 0;
for(int i = 0; i<res.length(); ++i)
for (int i = 0; i<res.length(); ++i)
{
if(res[i]=='{')
if (res[i] == '{')
++n;
}
int Num = (n-3)/(m+1);
//if(Num<=10)
//{
// ofstream f("result/"+int2string(args->num)+".txt");
// f<<res<<endl;
// f.close();
//}
if(args->rnum != Num)
int Num = (n - 3) / (m + 1);
// compare the result
if (args->rnum != Num)
{
correctness = false;
cout << "sparql: " << args->sparql << endl;
cout << "Num: " << Num << endl;
}
pthread_exit(NULL);
}
int main()
{
int result[6] = {10, 14, 14, 199424, 33910, 1039};
int result[6] = {15, 0, 828, 27, 27, 5916};
string* sparql = new string[6];
sparql[0] = "select ?v0 where\
{\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfBotswana> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfNamibia> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> .\
}";
sparql[1] = "select ?v0 where\
{\
?v0 <http://dbpedia.org/ontology/associatedBand> <http://dbpedia.org/resource/LCD_Soundsystem> .\
}";
sparql[2] = "select ?v2 where\
{\
<http://dbpedia.org/resource/!!Destroy-Oh-Boy!!> <http://dbpedia.org/property/title> ?v2 .\
}";
sparql[3] = "select ?v0 ?v2 where\
{\
?v0 <http://dbpedia.org/ontology/activeYearsStartYear> ?v2 .\
}";
sparql[4] = "select ?v0 ?v1 ?v2 where\
{\
?v0 <http://dbpedia.org/property/dateOfBirth> ?v2 .\
?v1 <http://dbpedia.org/property/genre> ?v2 .\
}";
sparql[5] = "select ?v0 ?v1 ?v2 ?v3 where\
{\
?v0 <http://dbpedia.org/property/familycolor> ?v1 .\
?v0 <http://dbpedia.org/property/glotto> ?v2 .\
?v0 <http://dbpedia.org/property/lc> ?v3 .\
}";
sparql[0] = "select ?x where \
{ \
?x <ub:name> <FullProfessor0>. \
}";
sparql[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.\
}";
sparql[2] = "select distinct ?x where\
{ \
?x <rdf:type> <ub:Course>.\
?x <ub:name> ?y.\
}";
sparql[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>. \
}";
sparql[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>. \
}";
sparql[5] = "select distinct ?x where\
{ \
?x <rdf:type> <ub:UndergraduateStudent>.\
}";
pthread_t qt[tnum];
void *status;
struct MyThread_args args[tnum];
for(int i = 0;i<tnum;i++)
{
args[i].num=i;
args[i].sparql=sparql[i%6];
args[i].rnum=result[i%6];
string filename = "result/res" + to_string(i) + ".txt";
args[i].num = i;
args[i].rnum = result[i%6];
args[i].sparql = sparql[i%6];
args[i].filename = filename;
args[i].request_type = RequestType;
pthread_create(&qt[i],NULL,MyThread_run,(void *)&args[i]);
}
for(int i = 0;i<tnum;i++)
{
pthread_join(qt[i],&status);
}
if(correctness == true)
cout<< "The answers are correct!" <<endl;

View File

@ -1,253 +0,0 @@
#include "client.h"
#include <pthread.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
#define tnum 3000
#define bnum 10
#define lnum 10
#define unum 10
bool correctness = true;
bool lcorrectness = true;
bool ucorrectness = true;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
pthread_mutex_t mutex;
string int2string(int n)
{
string s;
stringstream ss;
ss<<n;
ss>>s;
return s;
}
struct MyThread_args{
int num;
string sparql;
int rnum;
};
struct mm_args{
string db_name;
};
void* MyThread_run(void* thread_args)
{
struct MyThread_args *args;
args = (struct MyThread_args *)thread_args;
CHttpClient hc;
string res;
int ret;
//ret = hc.Get("http://172.31.222.78:3305/?operation=query&db_name=test&format=json&sparql="+args->sparql,res);
ret = hc.Get("http://172.31.222.78:3305/?operation=query&db_name=test&format=json&sparql="+args->sparql,"result/"+int2string(args->num)+".txt",1);
//open result file
ifstream f("result/"+int2string(args->num)+".txt");
stringstream buffer;
buffer << f.rdbuf();
res = buffer.str();
f.close();
//get result count
int m = 0;
for(int i = 0; i<args->sparql.length(); ++i)
{
if(args->sparql[i]=='?')
++m;
if(args->sparql[i]=='{')
break;
}
int n = 0;
for(int i = 0; i<res.length(); ++i)
{
if(res[i]=='{')
++n;
}
int Num = (n-3)/(m+1);
if(args->rnum != Num)
{
correctness = false;
cout << "sparql: " << args->sparql << endl;
cout << "Num: " << Num << endl;
}
pthread_exit(NULL);
}
/*void* load_thread(void* thread_args)
{
struct mm_args *args;
args = (struct mm_args *)thread_args;
string db_name = args->db_name;
CHttpClient hc;
string res;
int ret;
ret = hc.Get("http://172.31.222.93:9016/?operation=load&db_name="+db_name+"&username=root&password=123456", res);
res ="load: " + res + "\n";
cout << res;
pthread_exit(NULL);
}
void* unload_thread(void* thread_args)
{
struct mm_args *args;
args = (struct mm_args *)thread_args;
string db_name = args->db_name;
CHttpClient hc;
string res;
int ret;
ret = hc.Get("http://172.31.222.93:9016/?operation=unload&db_name="+db_name+"&username=root&password=123456", res);
res = "unload: " + res + "\n";
cout << res;
pthread_exit(NULL);
}*/
int main()
{
/*string * db_name = new string[4];
db_name[0] = "lubm";
db_name[1] = "lubm1";
db_name[2] = "test";
db_name[3] = "dbpedia";
CHttpClient hc;
string res;
int ret;
for(int i = 0; i < 3; i++)
{
ret = hc.Get("http://172.31.222.93:9016/?operation=build&db_name="+db_name[i]+"&ds_path=data/lubm/LUBM_10.n3&username=root&password=123456", res);
cout << res << endl;
}*/
//int result[6] = {10, 14, 14, 199424, 33910, 1039};
int result[6] = {15, 0, 828, 27, 27, 5916};
string* sparql = new string[6];
/* sparql[0] = "select ?v0 where\
{\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfBotswana> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfNamibia> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> .\
}";
sparql[1] = "select ?v0 where\
{\
?v0 <http://dbpedia.org/ontology/associatedBand> <http://dbpedia.org/resource/LCD_Soundsystem> .\
}";
sparql[2] = "select ?v2 where\
{\
<http://dbpedia.org/resource/!!Destroy-Oh-Boy!!> <http://dbpedia.org/property/title> ?v2 .\
}";
sparql[3] = "select ?v0 ?v2 where\
{\
?v0 <http://dbpedia.org/ontology/activeYearsStartYear> ?v2 .\
}";
sparql[4] = "select ?v0 ?v1 ?v2 where\
{\
?v0 <http://dbpedia.org/property/dateOfBirth> ?v2 .\
?v1 <http://dbpedia.org/property/genre> ?v2 .\
}";
sparql[5] = "select ?v0 ?v1 ?v2 ?v3 where\
{\
?v0 <http://dbpedia.org/property/familycolor> ?v1 .\
?v0 <http://dbpedia.org/property/glotto> ?v2 .\
?v0 <http://dbpedia.org/property/lc> ?v3 .\
}";*/
sparql[0] = "select ?x where\
{\
?x <ub:name> <FullProfessor0> .\
}";
sparql[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.\
}";
sparql[2] = "select distinct ?x where\
{\
?x <rdf:type> <ub:Course>.\
?x <ub:name> ?y.\
}";
sparql[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>.\
}";
sparql[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>.\
}";
sparql[5] = "select distinct ?x where\
{\
?x <rdf:type> <ub:UndergraduateStudent>.\
}";
pthread_t qt[tnum];
//pthread_t lqt[lnum];
//pthread_t uqt[unum];
void *status;
struct MyThread_args args[tnum];
//struct mm_args aas[lnum]
for(int i = 0;i<tnum;i++)
{
args[i].num=i;
args[i].sparql=sparql[i%6];
args[i].rnum=result[i%6];
pthread_create(&qt[i],NULL,MyThread_run,(void *)&args[i]);
}
/*for(int i = 0; i < lnum; i++)
{
//pthread_create(build_thread, db_name[i%3]);
aas[i].db_name = db_name[i%4];
pthread_create(&lqt[i], NULL, load_thread, (void *)&aas[i]);
pthread_create(&uqt[i], NULL, unload_thread, (void *)&aas[i]);
}*/
for(int i = 0;i<tnum;i++)
{
pthread_join(qt[i],&status);
}
/*for(int i = 0; i < lnum; i++)
{
pthread_join(lqt[i], &status);
pthread_join(uqt[i], &status);
}*/
if(correctness == true)
cout<< "The answers are correct!" <<endl;
else
cout<< "The answers exist errors!" <<endl;
//if(lcorrectness == true)
// cout<< "load operations are correct!" <<endl;
//else
// cout<< "load operations exist errors!" <<endl;
//if(ucorrectness == true)
// cout<< "unload operations are correct!" <<endl;
//else
// cout<< "unload operations exist errors!" <<endl;
pthread_exit(NULL);
return 0;
}

View File

@ -1,85 +0,0 @@
/*=============================================================================
# Filename: CppAPIExample.cpp
# Author: Bookug Lobert
# Mail: 1181955272@qq.com
# Last Modified: 2016-02-21 21:32
# Description: originally written by hanshuo, modified by zengli
=============================================================================*/
#include "GstoreConnector.h"
#include <string>
#include <iostream>
using namespace std;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
int main(int argc, char * argv[])
{
string username = "root";
string password = "123456";
// initialize the GStore server's IP address and port.
cout << "in" <<endl;
GstoreConnector gc("172.31.222.94", 9000);
cout << "out"<<endl;
// build a new database by a RDF file.
// note that the relative path is related to gserver
gc.build("lubm", "data/lubm/lubm.nt", username, password);
//load the database that you've build.
gc.load("lubm", username, password);
// then you can execute SPARQL query on this database.
std::string 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>. \
}";
std::string answer = gc.query(username, password, "lubm", sparql);
std::cout << answer << std::endl;
// make a SPARQL query and save the result in ans.txt
gc.query(username, password, "lubm", sparql, "ans.txt");
// unload this database.
gc.unload("lubm", username, password);
// also, you can load some exist database directly and then query.
gc.load("lubm", username, password);
answer = gc.query(username, password, "lubm", sparql);
std::cout << answer << std::endl;
//you can monitor a database
answer = gc.monitor("lubm", username, password);
std::cout << answer << std::endl;
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", username, password, "Jack", "2");
std::cout << answer << std::endl;
//add privilege to user Jack(add_query, add_load, add_unload)
answer = gc.user("add_query", username, password, "Jack", "lubm");
std::cout << answer << std::endl;
//then Jack can query the database LUBM10
answer = gc.query("Jack", "2", "lubm", sparql);
std::cout << answer << std::endl;
//delete privilege of user Jack(delete_query, delete_load, delete_unload)
answer = gc.user("delete_query", username, password, "Jack", "lubm");
std::cout << answer << std::endl;
//delete user(with username: Jack, password: 2)
answer = gc.user("delete_user", username, password, "Jack", "2");
std::cout << answer << std::endl;
gc.unload("lubm", username, password);
return 0;
}

View File

@ -0,0 +1,85 @@
/*=============================================================================
# Filename: GET-example.cpp
# Author: suxunbin
# Last Modified: 2019-5-17 20:51
# Description: a simple GET-example of C++ API
=============================================================================*/
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include "client.h"
using namespace std;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
// "GET" is a default parameter that can be omitted
int main(int argc, char * argv[])
{
std::string IP = "127.0.0.1";
int Port = 9000;
std::string username = "root";
std::string password = "123456";
std::string 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>. \
}";
std::string filename = "res.txt";
// start a gc with given IP, Port, username and password
GstoreConnector gc(IP, Port, username, password);
// build a database with a RDF graph
std::string res = gc.build("lubm", "data/lubm/lubm.nt");
cout << res << endl;
// load the database
res = gc.load("lubm");
cout << res << endl;
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//res = gc.user("add_user", "user1", "111111");
//cout << res << endl;
// show all users
res = gc.showUser();
cout << res << endl;
// query
res = gc.query("lubm", "json", sparql);
cout << res << endl;
// query and save the result in a file
gc.fquery("lubm", "json", sparql, filename);
// save the database if you have changed the database
res = gc.checkpoint("lubm");
cout << res << endl;
// show information of the database
res = gc.monitor("lubm");
cout << res << endl;
// show all databases
res = gc.show();
cout << res << endl;
// unload the database
res = gc.unload("lubm");
cout << res << endl;
// drop the database
res = gc.drop("lubm", false); //delete the database directly
//res = gc.drop("lubm", true); //leave a backup
cout << res << endl;
return 0;
}

View File

@ -1,24 +1,23 @@
CC=g++ -std=c++11
#CC=ccache g++
all: Benchmark CppAPIExample
#all: example Benchmark CppAPIExample
all: POST-example GET-example Benchmark
Benchmark: Benchmark.o
$(CC) -o Benchmark.exe Benchmark.o -L../lib -lclient -lcurl -lpthread
$(CC) -o Benchmark Benchmark.o -L../lib -lclient -lcurl -lpthread
CppAPIExample: CppAPIExample.o
$(CC) -o CppAPIExample.exe CppAPIExample.o -I../ -L../lib -lgstoreconnector -lclient -lcurl
GET-example: GET-example.o
$(CC) -o GET-example GET-example.o -I../ -lcurl -L../lib -lclient
Benchmark.o: Benchmark.cpp
POST-example: POST-example.o
$(CC) -o POST-example POST-example.o -I../ -lcurl -L../lib -lclient
GET-example.o: GET-example.cpp ../client.cpp ../client.h
$(CC) -c -I../ GET-example.cpp -o GET-example.o
Benchmark.o: Benchmark.cpp ../client.cpp ../client.h
$(CC) -c -I../ Benchmark.cpp -o Benchmark.o
POST-example.o: POST-example.cpp ../client.cpp ../client.h
$(CC) -c -I../ POST-example.cpp -o POST-example.o
#example: example.o
#$(CC) -o example example.o -I../ -lcurl -L../lib -lclient
#example.o: example.cpp
#$(CC) -c -I../ example.cpp -o example.o
CppAPIExample.o: CppAPIExample.cpp
$(CC) -c -I../ -I../src CppAPIExample.cpp -o CppAPIExample.o
clean:
rm -rf *.o *.exe
rm -rf *.o POST-example GET-example Benchmark

View File

@ -0,0 +1,84 @@
/*=============================================================================
# Filename: POST-example.cpp
# Author: suxunbin
# Last Modified: 2019-5-17 20:21
# Description: a simple POST-example of C++ API
=============================================================================*/
#include <stdio.h>
#include <iostream>
#include <string>
#include <fstream>
#include "client.h"
using namespace std;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
int main(int argc, char * argv[])
{
std::string IP = "127.0.0.1";
int Port = 9000;
std::string username = "root";
std::string password = "123456";
std::string 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>. \
}";
std::string filename = "res.txt";
// start a gc with given IP, Port, username and password
GstoreConnector gc(IP, Port, username, password);
// build a database with a RDF graph
std::string res = gc.build("lubm", "data/lubm/lubm.nt", "POST");
cout << res << endl;
// load the database
res = gc.load("lubm", "POST");
cout << res << endl;
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//res = gc.user("add_user", "user1", "111111", "POST");
//cout << res << endl;
// show all users
res = gc.showUser("POST");
cout << res << endl;
// query
res = gc.query("lubm", "json", sparql, "POST");
cout << res << endl;
// query and save the result in a file
gc.fquery("lubm", "json", sparql, filename, "POST");
// save the database if you have changed the database
res = gc.checkpoint("lubm", "POST");
cout << res << endl;
// show information of the database
res = gc.monitor("lubm", "POST");
cout << res << endl;
// show all databases
res = gc.show("POST");
cout << res << endl;
// unload the database
res = gc.unload("lubm", "POST");
cout << res << endl;
// drop the database
res = gc.drop("lubm", false, "POST"); //delete the database directly
//res = gc.drop("lubm", true, "POST"); //leave a backup
cout << res << endl;
return 0;
}

View File

@ -1,95 +0,0 @@
//NOTICE: you need to use libcurl-devel for C++ to use HTTP client, please read client.cpp and client.h seriously
#include <stdio.h>
#include <iostream>
#include <string>
#include "client.h"
using namespace std;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
int main()
{
CHttpClient hc;
string res;
int ret;
//ret = hc.Get("http://172.31.222.94:9000/?operation=build&db_name=lubm&ds_path=data/LUBM_10.n3", res);
//cout<<res<<endl;
//ret = hc.Get("http://172.31.222.94:9000/?operation=load&db_name=lubm", res);
//cout<<res<<endl;
int result[6] = {10, 14, 14, 199424, 33910, 1039};
string* sparql = new string[6];
sparql[0] = "select ?v0 where\
{\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfBotswana> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfNamibia> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> .\
}";
sparql[1] = "select ?v0 where\
{\
?v0 <http://dbpedia.org/ontology/associatedBand> <http://dbpedia.org/resource/LCD_Soundsystem> .\
}";
sparql[2] = "select ?v2 where\
{\
<http://dbpedia.org/resource/!!Destroy-Oh-Boy!!> <http://dbpedia.org/property/title> ?v2 .\
}";
sparql[3] = "select ?v0 ?v2 where\
{\
?v0 <http://dbpedia.org/ontology/activeYearsStartYear> ?v2 .\
}";
sparql[4] = "select ?v0 ?v1 ?v2 where\
{\
?v0 <http://dbpedia.org/property/dateOfBirth> ?v2 .\
?v1 <http://dbpedia.org/property/genre> ?v2 .\
}";
sparql[5] = "select ?v0 ?v1 ?v2 ?v3 where\
{\
?v0 <http://dbpedia.org/property/familycolor> ?v1 .\
?v0 <http://dbpedia.org/property/glotto> ?v2 .\
?v0 <http://dbpedia.org/property/lc> ?v3 .\
}";
int tnum = 6;
tnum = 3000;
bool correctness = true;
for(int i = 0; i < tnum; ++i)
{
ret = hc.Get("http://172.31.222.94:9000/?operation=query&format=json&sparql="+sparql[i%6],res);
cout<< i <<endl;
int m = 0;
for(int ii = 0; ii<sparql[i%6].length(); ++ii)
{
if(sparql[i%6][ii] == '?')
++m;
if(sparql[i%6][ii] == '{')
break;
}
int n = 0;
for(int ii = 0; ii<res.length(); ++ii)
{
if(res[ii] == '{')
++n;
}
int Num = (n-3)/(m+1);
if(Num!=result[i%6])
correctness =false;
}
if (correctness == true)
cout<< "The answers are correct!" <<endl;
else
cout<< "The answers exist errors!" <<endl;
//ret = hc.Get("127.0.0.1:8080/query/data/ex0.sql", res);
//cout<<res<<endl;
//ret = hc.Get("http://172.31.222.94:9000/?operation=monitor", res);
//cout<<res<<endl;
//ret = hc.Get("http://172.31.222.94:9000/?operation=unload&db_name=lubm", res);
//cout<<res<<endl;
return 0;
}

View File

@ -1,202 +0,0 @@
#include "client.h"
#include <pthread.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
#define tnum 10
#define bnum 10
#define lnum 10
#define unum 10
bool correctness = true;
bool lcorrectness = true;
bool ucorrectness = true;
pthread_mutex_t mutex;
string ip = "http://172.31.222.94:9000";
string int2string(int n)
{
string s;
stringstream ss;
ss<<n;
ss>>s;
return s;
}
struct MyThread_args{
int num;
string sparql;
int rnum;
};
struct mm_args{
string db_name;
};
void* MyThread_run(void* thread_args)
{
struct MyThread_args *args;
args = (struct MyThread_args *)thread_args;
CHttpClient hc;
string res;
int ret;
ret = hc.Get(ip+"/?operation=query&db_name=dbpedia&format=json&sparql="+args->sparql,res);
int m = 0;
for(int i = 0; i<args->sparql.length(); ++i)
{
if(args->sparql[i]=='?')
++m;
if(args->sparql[i]=='{')
break;
}
int n = 0;
for(int i = 0; i<res.length(); ++i)
{
if(res[i]=='{')
++n;
}
int Num = (n-3)/(m+1);
//if(Num<=10)
//{
// ofstream f("result/"+int2string(args->num)+".txt");
// f<<res<<endl;
// f.close();
//}
if(args->rnum != Num)
{
correctness = false;
cout << "sparql: " << args->sparql << endl;
cout << "Num: " << Num << endl;
}
pthread_exit(NULL);
}
void* load_thread(void* thread_args)
{
struct mm_args *args;
args = (struct mm_args *)thread_args;
string db_name = args->db_name;
CHttpClient hc;
string res;
int ret;
ret = hc.Get(ip+"/?operation=load&db_name="+db_name+"&username=root&password=123456", res);
res ="load: " + res + "\n";
cout << res;
pthread_exit(NULL);
}
void* unload_thread(void* thread_args)
{
struct mm_args *args;
args = (struct mm_args *)thread_args;
string db_name = args->db_name;
CHttpClient hc;
string res;
int ret;
ret = hc.Get(ip+"/?operation=unload&db_name="+db_name+"&username=root&password=123456", res);
res = "unload: " + res + "\n";
cout << res;
pthread_exit(NULL);
}
int main()
{
string * db_name = new string[4];
db_name[0] = "lubm";
db_name[1] = "lubm1";
db_name[2] = "test";
db_name[3] = "dbpedia";
CHttpClient hc;
string res;
int ret;
for(int i = 0; i < 3; i++)
{
ret = hc.Get(ip+"/?operation=build&db_name="+db_name[i]+"&ds_path=data/lubm/lubm.nt&username=root&password=123456", res);
cout << res << endl;
}
int result[6] = {10, 14, 14, 199424, 33910, 1039};
string* sparql = new string[6];
sparql[0] = "select ?v0 where\
{\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfBotswana> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfNamibia> .\
?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> .\
}";
sparql[1] = "select ?v0 where\
{\
?v0 <http://dbpedia.org/ontology/associatedBand> <http://dbpedia.org/resource/LCD_Soundsystem> .\
}";
sparql[2] = "select ?v2 where\
{\
<http://dbpedia.org/resource/!!Destroy-Oh-Boy!!> <http://dbpedia.org/property/title> ?v2 .\
}";
sparql[3] = "select ?v0 ?v2 where\
{\
?v0 <http://dbpedia.org/ontology/activeYearsStartYear> ?v2 .\
}";
sparql[4] = "select ?v0 ?v1 ?v2 where\
{\
?v0 <http://dbpedia.org/property/dateOfBirth> ?v2 .\
?v1 <http://dbpedia.org/property/genre> ?v2 .\
}";
sparql[5] = "select ?v0 ?v1 ?v2 ?v3 where\
{\
?v0 <http://dbpedia.org/property/familycolor> ?v1 .\
?v0 <http://dbpedia.org/property/glotto> ?v2 .\
?v0 <http://dbpedia.org/property/lc> ?v3 .\
}";
pthread_t qt[tnum];
pthread_t lqt[lnum];
pthread_t uqt[unum];
void *status;
struct MyThread_args args[tnum];
struct mm_args aas[lnum];
for(int i = 0;i<tnum;i++)
{
args[i].num=i;
args[i].sparql=sparql[i%6];
args[i].rnum=result[i%6];
pthread_create(&qt[i],NULL,MyThread_run,(void *)&args[i]);
}
for(int i = 0; i < lnum; i++)
{
//pthread_create(build_thread, db_name[i%3]);
aas[i].db_name = db_name[i%4];
pthread_create(&lqt[i], NULL, load_thread, (void *)&aas[i]);
pthread_create(&uqt[i], NULL, unload_thread, (void *)&aas[i]);
}
for(int i = 0;i<tnum;i++)
{
pthread_join(qt[i],&status);
}
for(int i = 0; i < lnum; i++)
{
pthread_join(lqt[i], &status);
pthread_join(uqt[i], &status);
}
if(correctness == true)
cout<< "The answers are correct!" <<endl;
else
cout<< "The answers exist errors!" <<endl;
//if(lcorrectness == true)
// cout<< "load operations are correct!" <<endl;
//else
// cout<< "load operations exist errors!" <<endl;
//if(ucorrectness == true)
// cout<< "unload operations are correct!" <<endl;
//else
// cout<< "unload operations exist errors!" <<endl;
pthread_exit(NULL);
return 0;
}

View File

View File

@ -1,7 +0,0 @@
CC=g++ -std=c++11
test: test.o
$(CC) -o test test.o
test.o: test.cpp
$(CC) -c test.cpp -o test.o
clean:
rm -rf *.o test

View File

@ -1,69 +0,0 @@
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string encode(string& s)
{
string ret;
unsigned char *ptr = (unsigned char *)s.c_str();
ret.reserve(s.length());
for(int i=0;i<s.length();++i)
{
if((int(ptr[i])==58) || (int(ptr[i])==47) ||(int(ptr[i])==42) || (int(ptr[i])==45) || (int(ptr[i])==46) || (int(ptr[i])==95))
ret += ptr[i];
else if((int(ptr[i])>=48) && (int(ptr[i])<=57))
ret += ptr[i];
else if((int(ptr[i])>=65) && (int(ptr[i])<=90))
ret += ptr[i];
else if((int(ptr[i])>=97) && (int(ptr[i])<=122))
ret += ptr[i];
else if(int(ptr[i])==32)
ret += '+';
else
{
char buf[5];
memset(buf,0,5);
snprintf(buf,5,"%%%X",ptr[i]);
ret.append(buf);
}
}
return ret;
}
string decode(string& s)
{
string ret;
char ch;
int ii;
for(size_t i = 0; i < s.length(); ++i)
{
if(int(s[i]) == 37)
{
sscanf(s.substr(i+1,2).c_str(), "%x", &ii);
ch = static_cast<char>(ii);
ret += ch;
i = i + 2;
}
else if(s[i] == '+')
{
ret += ' ';
}
else
{
ret += s[i];
}
}
return (ret);
}
int main()
{
string a;
cin >> a;
string b=encode(a);
cout<<b<<endl;
string c=decode(b);
if(a==c)
cout<<"OK"<<endl;
return 0;
}

View File

@ -1,69 +1,79 @@
/*=============================================================================
# Filename: Benchmark.java
# Author: Bookug Lobert
# Mail: zengli-bookug@pku.edu.cn
# Last Modified: 2017-12-04 11:24
# Description:
# Author: suxunbin
# Last Modified: 2019-5-17 10:51
# Description: a simple example of multi-thread query
=============================================================================*/
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)
// default db_name: lubm(must be built in advance)
class MyThread extends Thread {
public static String IP = "127.0.0.1";
public static int Port = 9000;
public static String username = "root";
public static String password = "123456";
public static boolean correctness = true;
//public static int cnum = 0;
private int num = 0;
private GstoreConnector gc;
private String sparql;
private int rnum = 0;
public MyThread(int _num, GstoreConnector _gc, String _sparql, int _rnum) {
private String sparql;
private String filename;
private String RequestType;
public MyThread(int _num, int _rnum, String _sparql, String _filename, String _RequestType) {
num = _num;
gc = _gc;
sparql = _sparql;
rnum = _rnum;
sparql = _sparql;
filename = _filename;
RequestType = _RequestType;
}
public void run() {
Thread t = Thread.currentThread();
//try {
// File f = new File("result/"+num+".txt");
// DataOutputStream of = new DataOutputStream(new FileOutputStream(f));
// of.writeBytes(t.getName()+" "+t.getId()+"\n");
// of.writeBytes("Thread: "+num+"\n");
String answer = gc.query("root", "123456", "dbpedia", sparql);
// of.writeBytes(answer);
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);
// of.writeBytes("The number of the results: "+Num+"\n");
// of.writeBytes("Thread "+t.getName()+" ends!");
// of.close();
if (rnum != Num)
correctness = false;
// if (rnum == Num)
// {
// synchronized (this){
// cnum++;
// }
// }
//}
//catch(IOException e) {
// e.printStackTrace();
//}
// query
GstoreConnector gc = new GstoreConnector(IP, Port, username, password);
String res = gc.query("lubm", "json", sparql, RequestType);
// fquery
/*GstoreConnector gc = new GstoreConnector(IP, Port, username, password);
gc.fquery("lubm", "json", sparql, filename, RequestType);
String res = "";
FileInputStream in = null;
try {
in = new FileInputStream(filename);
int size = in.available();
byte[] buffer = new byte[size];
in.read(buffer);
in.close();
res = new String(buffer);
} catch (IOException e) {
System.out.println("error occurs: fail to open " + filename);
}*/
// count the num
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<res.length(); ++i)
{
if(res.charAt(i) == '{')
++n;
}
int Num = (n-3)/(m+1);
// compare the result
if (rnum != Num){
correctness = false;
System.out.println("sparql: " + sparql);
System.out.println("Num: " + Num);
}
}
}
@ -71,58 +81,60 @@ public class Benchmark
{
public static void main(String[] args)
{
GstoreConnector gc = new GstoreConnector("172.31.222.94", 9000);
//gc.load("dbpedia");
String[] spq = new String[6];
spq[0] = "select ?v0 where"
+"{"
+"?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfBotswana> ."
+"?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfNamibia> ."
+"?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> ."
+"}";
spq[1] = "select ?v0 where"
+"{"
+"?v0 <http://dbpedia.org/ontology/associatedBand> <http://dbpedia.org/resource/LCD_Soundsystem> ."
+"}";
spq[2] = "select ?v2 where"
+"{"
+"<http://dbpedia.org/resource/!!Destroy-Oh-Boy!!> <http://dbpedia.org/property/title> ?v2 ."
+"}";
spq[3] = "select ?v0 ?v2 where"
+"{"
+"?v0 <http://dbpedia.org/ontology/activeYearsStartYear> ?v2 ."
+"}";
spq[4] = "select ?v0 ?v1 ?v2 where"
+"{"
+"?v0 <http://dbpedia.org/property/dateOfBirth> ?v2 ."
+"?v1 <http://dbpedia.org/property/genre> ?v2 ."
+"}";
spq[5] = "select ?v0 ?v1 ?v2 ?v3 where"
+"{"
+"?v0 <http://dbpedia.org/property/familycolor> ?v1 ."
+"?v0 <http://dbpedia.org/property/glotto> ?v2 ."
+"?v0 <http://dbpedia.org/property/lc> ?v3 ."
+"}";
//spq[6] = "select ?v0 ?v1 ?v2 ?v3 ?v4 ?v5 ?v6 ?v7 ?v8 ?v9 where"
// +"{"
// +"?v0 <http://dbpedia.org/property/dateOfBirth> ?v1 ."
// +"?v0 <http://dbpedia.org/property/genre> ?v2 ."
// +"?v0 <http://dbpedia.org/property/instrument> ?v3 ."
// +"?v0 <http://dbpedia.org/property/label> ?v4 ."
// +"?v0 <http://dbpedia.org/property/placeOfBirth> ?v5 ."
// +"?v6 <http://dbpedia.org/property/name> ?v7 ."
// +"?v6 <http://dbpedia.org/property/occupation> ?v8 ."
// +"?v6 <http://dbpedia.org/property/placeOfBirth> ?v5 ."
// +"?v6 <http://dbpedia.org/property/instrument> ?v3 ."
// +"?v6 <http://dbpedia.org/property/notableInstruments> ?v9 ."
// +"}";
int[] result = {10, 14, 14, 199424, 33910, 1039};
int tnum = 6;
tnum = 100;
int tnum = 1000;
String RequestType = "POST";
int[] result = {15, 0, 828, 27, 27, 5916};
String[] sparql = new String[6];
sparql[0] = "select ?x where"
+"{"
+"?x <ub:name> <FullProfessor0> ."
+"}";
sparql[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."
+"}";
sparql[2] = "select distinct ?x where"
+"{"
+"?x <rdf:type> <ub:Course>."
+"?x <ub:name> ?y."
+"}";
sparql[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>."
+"}";
sparql[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>."
+"}";
sparql[5] = "select distinct ?x where"
+"{"
+"?x <rdf:type> <ub:UndergraduateStudent>."
+"}";
MyThread[] qt = new MyThread[tnum];
for(int i = 0; i < tnum; ++i)
{
qt[i] = new MyThread(i, gc, spq[i%6], result[i%6]);
String filename = "result/res" + i + ".txt";
qt[i] = new MyThread(i, result[i%6], sparql[i%6], filename, RequestType);
qt[i].start();
}
for(int i = 0; i < tnum; ++i)
@ -134,19 +146,10 @@ public class Benchmark
e.printStackTrace();
}
}
//gc.unload("dbpedia");
//double accuracy = MyThread.cnum / tnum;
try{
File f = new File("result/correctness.txt");
DataOutputStream of = new DataOutputStream(new FileOutputStream(f));
if (MyThread.correctness == true)
of.writeBytes("The results are correct!");
else
of.writeBytes("The results exist errors!");
of.close();
}
catch(IOException e) {
e.printStackTrace();
}
if (MyThread.correctness == true)
System.out.println("The answers are correct!");
else
System.out.println("The answers exist errors!");
}
}

View File

@ -1,141 +0,0 @@
/*=============================================================================
# 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;
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", "lubm", 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.94", 9000);
gc.load("lubm", "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!");
}
}

View File

@ -0,0 +1,79 @@
/*
# Filename: GETexample.java
# Author: suxunbin
# Last Modified: 2019-5-17 10:30
# Description: a simple GET-example of java API
*/
import jgsc.GstoreConnector;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
// "GET" is a default parameter that can be omitted
public class GETexample{
public static void main(String[] args)
{
String IP = "127.0.0.1";
int Port = 9000;
String username = "root";
String password = "123456";
String 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>."
+"}";
String filename = "res.txt";
// start a gc with given IP, Port, username and password
GstoreConnector gc = new GstoreConnector(IP, Port, username, password);
// build a database with a RDF graph
String res = gc.build("lubm", "data/lubm/lubm.nt");
System.out.println(res);
// load the database
res = gc.load("lubm");
System.out.println(res);
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//res = gc.user("add_user", "user1", "111111");
//System.out.println(res);
// show all users
res = gc.showUser();
System.out.println(res);
// query
res = gc.query("lubm", "json", sparql);
System.out.println(res);
// query and save the result in a file
gc.fquery("lubm", "json", sparql, filename);
// save the database if you have changed the database
res = gc.checkpoint("lubm");
System.out.println(res);
// show information of the database
res = gc.monitor("lubm");
System.out.println(res);
// show all databases
res = gc.show();
System.out.println(res);
// unload the database
res = gc.unload("lubm");
System.out.println(res);
// drop the database
res = gc.drop("lubm", false); //delete the database directly
//res = gc.drop("lubm", true); //leave a backup
System.out.println(res);
}
}

View File

@ -1,152 +0,0 @@
/*
* JavaAPIExample.java
*
* Created on: 2014-11-4
* Author: zengli
*/
import jgsc.GstoreConnector;
// 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)
{
String username = "root";
String password = "123456";
// initialize the GStore server's IP address and port.
GstoreConnector gc = new GstoreConnector("172.31.222.94", 9000);
//below are for parallel test
//GstoreConnector gc = new GstoreConnector("172.31.222.94", 9000);
//String[] sparql = new String[6];
//sparql[0] = "select ?v0 where"
//+"{"
//+"?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfBotswana> ."
//+"?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/class/yago/LanguagesOfNamibia> ."
//+"?v0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/Language> ."
//+"}";
//sparql[1] = "select ?v0 where"
//+"{"
//+"?v0 <http://dbpedia.org/ontology/associatedBand> <http://dbpedia.org/resource/LCD_Soundsystem> ."
//+"}";
//sparql[2] = "select ?v2 where"
//+"{"
//+"<http://dbpedia.org/resource/!!Destroy-Oh-Boy!!> <http://dbpedia.org/property/title> ?v2 ."
//+"}";
//sparql[3] = "select ?v0 ?v2 where"
//+"{"
//+"?v0 <http://dbpedia.org/ontology/activeYearsStartYear> ?v2 ."
//+"}";
//sparql[4] = "select ?v0 ?v1 ?v2 where"
//+"{"
//+"?v0 <http://dbpedia.org/property/dateOfBirth> ?v2 ."
//+"?v1 <http://dbpedia.org/property/genre> ?v2 ."
//+"}";
//sparql[5] = "select ?v0 ?v1 ?v2 ?v3 where"
//+"{"
//+"?v0 <http://dbpedia.org/property/familycolor> ?v1 ."
//+"?v0 <http://dbpedia.org/property/glotto> ?v2 ."
//+"?v0 <http://dbpedia.org/property/lc> ?v3 ."
//+"}";
//int[] result = {10, 14, 14, 199424, 33910, 1039};
//int tnum = 6;
////tnum = 3000;
//boolean correctness=true;
//for(int i = 0; i < tnum; ++i)
//{
//String answer = gc.query(sparql[i%6]);
//int m = 0;
//for(int ii = 0; ii<sparql[i%6].length(); ++ii)
//{
//if(sparql[i%6].charAt(ii) == '?')
//++m;
//if(sparql[i%6].charAt(ii) == '{')
//break;
//}
//int n = 0;
//for(int ii = 0; ii<answer.length(); ++ii)
//{
//if(answer.charAt(ii) == '{')
//++n;
//}
//int Num = (n-3)/(m+1);
//if(Num!=result[i%6])
//correctness =false;
//System.out.println(i+"\n");
//}
//if(correctness == true)
//System.out.println("correct!");
//for sparql endpoint, URL can also be used here, like freebase.gstore-pku.com:80
//GstoreConnector gc = new GstoreConnector("tourist.gstore-pku.com", 80);
//test download function
//String file = gc.test_download("big.txt");
//String file = gc.test_download("big.txt");
//System.out.println(file.substring(file.length()-100, file.length()));
// build a new database by a RDF file.
// note that the relative path is related to gserver.
gc.build("lubm", "data/lubm/lubm.nt", username, password);
gc.load("lubm", username, password);
// then you can execute SPARQL query on this database.
String 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>. "
+ "}";
String answer = gc.query(username, password, "lubm", sparql);
System.out.println(answer);
// unload this database.
gc.unload("lubm", username, password);
// also, you can load some exist database directly and then query.
gc.load("lubm", username, password);
//sparql = "delete where "
//+ "{"
//+ "<http://www.founder/121> ?predicate ?object ."
//+ "}";
//sparql="select ?s where { ?s ?p ?o . }";
//PERFORMANCE: if we use the query above(as comment), result will be very large and the time cost is large, too
//The method to improve it is to receive a line and output/save to file at once, instead of combining all lines into a String
//The related code is in api/http/java/src/jgsc/GstoreConnector.java
answer = gc.query(username, password, "lubm", sparql);
System.out.println(answer);
// make a SPARQL query and save the result in ans.txt
gc.query(username, password, "lubm", sparql, "ans.txt");
//monitor a database
answer = gc.monitor("lubm", username, password);
System.out.println(answer);
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", username, password, "Jack", "2");
System.out.println(answer);
//add privilege to user Jack(add_query, add_load, add_unload)
answer = gc.user("add_query", username, password, "Jack", "lubm");
System.out.println(answer);
//then Jack can query the database LUBM10
answer = gc.query("Jack", "2", "lubm", sparql);
System.out.println(answer);
//delete privilege of user Jack(delete_query, delete_load, delete_unload)
answer = gc.user("delete_query", username, password, "Jack", "lubm");
System.out.println(answer);
//delete user(with username: Jack, password: 2)
answer = gc.user("delete_user", username, password, "Jack", "2");
System.out.println(answer);
gc.unload("lubm", username, password);
}
}

View File

@ -1,25 +1,25 @@
all: JavaAPIExample.class Benchmark.class Benchmark1.class
JavaAPIExample.class: JavaAPIExample.java
javac -cp ../lib/GstoreJavaAPI.jar JavaAPIExample.java
all: POSTexample.class GETexample.class Benchmark.class
Benchmark.class: Benchmark.java
javac -cp ../lib/GstoreJavaAPI.jar Benchmark.java
javac -encoding gbk -cp ../lib/GstoreJavaAPI.jar Benchmark.java
Benchmark1.class: Benchmark1.java
javac -cp ../lib/GstoreJavaAPI.jar Benchmark1.java
POSTexample.class: POSTexample.java
javac -encoding gbk -cp ../lib/GstoreJavaAPI.jar POSTexample.java
.PHONY: clean run
GETexample.class: GETexample.java
javac -encoding gbk -cp ../lib/GstoreJavaAPI.jar GETexample.java
run: JavaAPIExample.class
java -cp ../lib/GstoreJavaAPI.jar:. JavaAPIExample
test: Benchmark.class
.PHONY: clean POST-example GET-example Benchmark
POST-example: POSTexample.class
java -cp ../lib/GstoreJavaAPI.jar:. POSTexample
GET-example: GETexample.class
java -cp ../lib/GstoreJavaAPI.jar:. GETexample
Benchmark: Benchmark.class
java -XX:-UseGCOverheadLimit -Xmx64G -cp ../lib/GstoreJavaAPI.jar:. Benchmark
test1: Benchmark1.class
java -XX:-UseGCOverheadLimit -Xmx64G -cp ../lib/GstoreJavaAPI.jar:. Benchmark1
#test: Benchmark.class
#java -XX:-UseGCOverheadLimit -Xmx64G -cp ../lib/GstoreJavaAPI.jar:. Benchmark >> time.txt
clean:
rm -f JavaAPIExample.class
rm -rf *.class

View File

@ -0,0 +1,78 @@
/*
# Filename: POSTexample.java
# Author: suxunbin
# Last Modified: 2019-5-16 21:30
# Description: a simple POST-example of java API
*/
import jgsc.GstoreConnector;
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
public class POSTexample{
public static void main(String[] args)
{
String IP = "127.0.0.1";
int Port = 9000;
String username = "root";
String password = "123456";
String 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>."
+"}";
String filename = "res.txt";
// start a gc with given IP, Port, username and password
GstoreConnector gc = new GstoreConnector(IP, Port, username, password);
// build a database with a RDF graph
String res = gc.build("lubm", "data/lubm/lubm.nt", "POST");
System.out.println(res);
// load the database
res = gc.load("lubm", "POST");
System.out.println(res);
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//res = gc.user("add_user", "user1", "111111", "POST");
//System.out.println(res);
// show all users
res = gc.showUser("POST");
System.out.println(res);
// query
res = gc.query("lubm", "json", sparql, "POST");
System.out.println(res);
// query and save the result in a file
gc.fquery("lubm", "json", sparql, filename, "POST");
// save the database if you have changed the database
res = gc.checkpoint("lubm", "POST");
System.out.println(res);
// show information of the database
res = gc.monitor("lubm", "POST");
System.out.println(res);
// show all databases
res = gc.show("POST");
System.out.println(res);
// unload the database
res = gc.unload("lubm", "POST");
System.out.println(res);
// drop the database
res = gc.drop("lubm", false, "POST"); //delete the database directly
//res = gc.drop("lubm", true, "POST"); //leave a backup
System.out.println(res);
}
}

View File

@ -1,3 +1,9 @@
/*
# Filename: GstoreConnector.java
# Author: suxunbin
# Last Modified: 2019-5-16 21:00
# Description: http api for java
*/
package jgsc;
import java.io.*;
@ -16,84 +22,66 @@ public class GstoreConnector {
private String serverIP;
private int serverPort;
//private Socket socket = null;
private String Url;
private String username;
private String password;
public GstoreConnector() {
this.serverIP = GstoreConnector.defaultServerIP;
this.serverPort = GstoreConnector.defaultServerPort;
}
public GstoreConnector(int _port) {
this.serverIP = GstoreConnector.defaultServerIP;
this.serverPort = _port;
}
public GstoreConnector(String _ip, int _port) {
this.serverIP = _ip;
public GstoreConnector(String _ip, int _port, String _user, String _passwd) {
if (_ip.equals("localhost")) {
this.serverIP = GstoreConnector.defaultServerIP;
}
else{
this.serverIP = _ip;
}
this.serverPort = _port;
this.Url = "http://" + this.serverIP + ":" + this.serverPort;
this.username = _user;
this.password = _passwd;
}
//PERFORMANCE: what if the query result is too large? receive and save to file directly at once
//In addition, set the -Xmx larger(maybe in scale of Gs) if the query result could be very large,
//this may help to reduce the GC cost
public String sendGet(String param) {
String url = "http://" + this.serverIP + ":" + this.serverPort;
public String sendGet(String strUrl) {
StringBuffer result = new StringBuffer();
BufferedReader in = null;
//System.out.println("parameter: "+param);
try {
param = URLEncoder.encode(param, "UTF-8");
strUrl = URLEncoder.encode(strUrl, "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);
strUrl = this.Url + "/" + strUrl;
URL realUrl = new URL(strUrl);
// open the connection with the URL
URLConnection connection = realUrl.openConnection();
// set request properties
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.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// create the real connection
connection.connect();
long t0 = System.currentTimeMillis(); //ms
// get all response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// traverse all response header fields
//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("============================================");
// define BufferedReader to read the response of the URL
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line;
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+"\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");
} catch (Exception e) {
System.out.println("error in get request: " + e);
e.printStackTrace();
}
// use finally to close the input stream
finally {
try {
@ -107,11 +95,68 @@ public class GstoreConnector {
return result.toString();
}
public void sendGet(String param, String filename) {
String url = "http://" + this.serverIP + ":" + this.serverPort;
public String sendPost(String strUrl, String strPost) {
PrintWriter out = null;
StringBuffer result = new StringBuffer();
BufferedReader in = null;
System.out.println("parameter: "+param);
try {
strUrl = URLEncoder.encode(strUrl, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Broken VM does not support UTF-8");
}
try {
strUrl = this.Url + "/" + strUrl;
URL realUrl = new URL(strUrl);
// open the connection with the URL
URLConnection connection = realUrl.openConnection();
// set request properties
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setDoOutput(true);
connection.setDoInput(true);
out = new PrintWriter(connection.getOutputStream());
out.print(strPost);
out.flush();
// get all response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// define BufferedReader to read the response of the URL
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result.append(line+"\n");
}
} catch (Exception e) {
System.out.println("error in post request: " + e);
e.printStackTrace();
}
// use finally to close the input stream
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result.toString();
}
public void sendGet(String strUrl, String filename) {
BufferedReader in = null;
if (filename == null)
return;
@ -123,36 +168,28 @@ public class GstoreConnector {
}
try {
param = URLEncoder.encode(param, "UTF-8");
strUrl = URLEncoder.encode(strUrl, "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);
strUrl = this.Url + "/" + strUrl;
URL realUrl = new URL(strUrl);
// open the connection with the URL
URLConnection connection = realUrl.openConnection();
// set request properties
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.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// create the real connection
connection.connect();
long t0 = System.currentTimeMillis(); //ms
connection.connect();
// get all response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// traverse all response header fields
//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");
// define BufferedReader to read the response of the URL
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
@ -163,13 +200,11 @@ public class GstoreConnector {
fw.write(chars);
chars = new char[2048];
}
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);
System.out.println("error in get request: " + e);
e.printStackTrace();
}
// use finally to close the input stream
finally {
try {
@ -186,223 +221,298 @@ public class GstoreConnector {
return;
}
public void sendPost(String strUrl, String strPost, String filename) {
PrintWriter out = null;
BufferedReader in = null;
//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();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.load");
return false;
}
if (filename == null)
return;
String cmd = "?operation=load&db_name=" + _db_name + "&username=" + _username + "&password=" + _password;
String msg = this.sendGet(cmd);
//if (!send_return) {
//System.err.println("send load command error. @GstoreConnector.load");
//return false;
//}
this.disconnect();
System.out.println(msg);
if (msg.equals("load database done.")) {
return true;
FileWriter fw = null;
try {
fw = new FileWriter(filename);
} catch (IOException e) {
System.out.println("can not open " + filename + "!");
}
return false;
}
public boolean unload(String _db_name,String _username, String _password) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.unload");
return false;
try {
strUrl = URLEncoder.encode(strUrl, "UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Broken VM does not support UTF-8");
}
String cmd = "?operation=unload&db_name=" + _db_name + "&username=" + _username + "&password=" + _password;
String msg = this.sendGet(cmd);
try {
strUrl = this.Url + "/" + strUrl;
URL realUrl = new URL(strUrl);
this.disconnect();
// open the connection with the URL
URLConnection connection = realUrl.openConnection();
System.out.println(msg);
if (msg.equals("unload database done.")) {
return true;
// set request properties
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setDoOutput(true);
connection.setDoInput(true);
out = new PrintWriter(connection.getOutputStream());
out.print(strPost);
out.flush();
// get all response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// define BufferedReader to read the response of the URL
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
char chars[] = new char[2048];
int b;
while ((b = in.read(chars, 0, 2048)) != -1) {
if (fw != null)
fw.write(chars);
chars = new char[2048];
}
} catch (Exception e) {
System.out.println("error in post request: " + e);
e.printStackTrace();
}
return false;
}
public boolean build(String _db_name, String _rdf_file_path, String _username, String _password) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.build");
return false;
// use finally to close the input stream
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
//TODO: also use encode to support spaces?
//Consider change format into ?name=DBname
String cmd = "?operation=build&db_name=" + _db_name + "&ds_path=" + _rdf_file_path + "&username=" + _username + "&password=" + _password;;
String msg = this.sendGet(cmd);
this.disconnect();
System.out.println(msg);
if (msg.equals("import RDF file to database done.")) {
return true;
}
return false;
}
//TODO: not implemented
public boolean drop(String _db_name) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.drop");
return false;
}
String cmd = "drop/" + _db_name;
String msg = this.sendGet(cmd);
this.disconnect();
System.out.println(msg);
return msg.equals("drop database done.");
}
public String query(String _username, String _password, String _db_name, String _sparql) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.query");
return "connect to server error.";
}
//URL encode should be used here
//try {
//_sparql = URLEncoder.encode("\""+_sparql+"\"", "UTF-8");
//}
//catch (UnsupportedEncodingException ex) {
//throw new RuntimeException("Broken VM does not support UTF-8");
//}
String cmd = "?operation=query&username=" + _username + "&password=" + _password + "&db_name=" + _db_name + "&format=json&sparql=" + _sparql;
//String cmd = "query/\"" + _sparql + "\"";
String msg = this.sendGet(cmd);
this.disconnect();
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);
// }
//show all databases
public String show(String _username, String _password) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.show");
return "connect to server error.";
public String build(String db_name, String rdf_file_path, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "build";
String strPost = "{\"db_name\": \"" + db_name + "\", \"ds_path\": \"" + rdf_file_path + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
String cmd = "?operation=show&username=" + _username + "&password=" + _password;
String msg = this.sendGet(cmd);
this.disconnect();
return msg;
}
public String user(String type, String username1, String password1, String username2, String addition) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.show");
return "connect to server error.";
}
String cmd = "?operation=user&type=" + type + "&username1=" + username1 + "&password1=" + password1 + "&username2=" + username2 + "&addition=" + addition;
String msg = this.sendGet(cmd);
this.disconnect();
return msg;
}
public String showUser() {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.show");
return "connect to server error.";
}
String cmd = "?operation=showUser";
String msg = this.sendGet(cmd);
this.disconnect();
return msg;
}
public String monitor(String db_name, String _username, String _password) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.show");
return "connect to server error.";
}
String cmd = "?operation=monitor&db_name=" + db_name + "&username=" + _username + "&password=" + _password;
String msg = this.sendGet(cmd);
this.disconnect();
return msg;
}
public String checkpoint(String db_name, String _username, String _password) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.show");
return "connect to server error.";
}
String cmd = "?operation=checkpoint&db_name=" + db_name + "&username=" + _username + "&password=" + _password;
String msg = this.sendGet(cmd);
this.disconnect();
return msg;
}
public String test_download(String filepath)
{
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.query");
return "connect to server error.";
}
//TEST: a small file, a large file
String cmd = "?operation=delete&download=true&filepath=" + filepath;
String msg = this.sendGet(cmd);
this.disconnect();
return msg;
}
private boolean connect() {
return true;
return res;
}
private boolean disconnect() {
return true;
public String build(String db_name, String rdf_file_path) {
String res = this.build(db_name, rdf_file_path, "GET");
return res;
}
public String load(String db_name, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=load&db_name=" + db_name + "&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "load";
String strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String load(String db_name) {
String res = this.load(db_name, "GET");
return res;
}
public String unload(String db_name, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=unload&db_name=" + db_name + "&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "unload";
String strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String unload(String db_name) {
String res = this.unload(db_name, "GET");
return res;
}
public String user(String type, String username2, String addition, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=user&type=" + type + "&username1=" + this.username + "&password1=" + this.password + "&username2=" + username2 + "&addition=" + addition;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "user";
String strPost = "{\"type\": \"" + type + "\", \"username1\": \"" + this.username + "\", \"password1\": \"" + this.password + "\", \"username2\": \"" + username2 + "\", \"addition\": \"" + addition + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String user(String type, String username2, String addition) {
String res = this.user(type, username2, addition, "GET");
return res;
}
public String showUser(String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=showUser&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "showUser";
String strPost = "{\"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String showUser() {
String res = this.showUser("GET");
return res;
}
public String query(String db_name, String format, String sparql, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=query&username=" + this.username + "&password=" + this.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "query";
String strPost = "{\"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\", \"db_name\": \"" + db_name + "\", \"format\": \"" + format + "\", \"sparql\": \"" + sparql + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String query(String db_name, String format, String sparql) {
String res = this.query(db_name, format, sparql, "GET");
return res;
}
public void fquery(String db_name, String format, String sparql, String filename, String request_type) {
if (request_type.equals("GET")) {
String strUrl = "?operation=query&username=" + this.username + "&password=" + this.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql;
this.sendGet(strUrl, filename);
}
else if (request_type.equals("POST")) {
String strUrl = "query";
String strPost = "{\"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\", \"db_name\": \"" + db_name + "\", \"format\": \"" + format + "\", \"sparql\": \"" + sparql + "\"}";
this.sendPost(strUrl, strPost, filename);
}
return;
}
public void fquery(String db_name, String format, String sparql, String filename) {
this.fquery(db_name, format, sparql, filename, "GET");
return;
}
public String drop(String db_name, boolean is_backup, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl;
if (is_backup) {
strUrl = "?operation=drop&db_name=" + db_name + "&username=" + this.username + "&password=" + this.password + "&is_backup=true";
}
else{
strUrl = "?operation=drop&db_name=" + db_name + "&username=" + this.username + "&password=" + this.password + "&is_backup=false";
}
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "drop";
String strPost;
if (is_backup) {
strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\", \"is_backup\": \"true\"}";
}
else{
strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\", \"is_backup\": \"false\"}";
}
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String drop(String db_name, boolean is_backup) {
String res = this.drop(db_name, is_backup, "GET");
return res;
}
public String monitor(String db_name, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=monitor&db_name=" + db_name + "&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "monitor";
String strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String monitor(String db_name) {
String res = this.monitor(db_name, "GET");
return res;
}
public String checkpoint(String db_name, String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=checkpoint&db_name=" + db_name + "&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "checkpoint";
String strPost = "{\"db_name\": \"" + db_name + "\", \"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String checkpoint(String db_name) {
String res = this.checkpoint(db_name, "GET");
return res;
}
public String show(String request_type) {
String res = "";
if (request_type.equals("GET")) {
String strUrl = "?operation=show&username=" + this.username + "&password=" + this.password;
res = this.sendGet(strUrl);
}
else if (request_type.equals("POST")) {
String strUrl = "show";
String strPost = "{\"username\": \"" + this.username + "\", \"password\": \"" + this.password + "\"}";
res = this.sendPost(strUrl, strPost);
}
return res;
}
public String show() {
String res = this.show("GET");
return res;
}
private static byte[] packageMsgData(String _msg) {
@ -455,39 +565,5 @@ public class GstoreConnector {
return ret;
}
public static void main(String[] args) {
// initialize the GStore server's IP address and port.
GstoreConnector gc = new GstoreConnector("172.31.222.93", 9016);
// build a new database by a RDF file.
// note that the relative path is related to gserver.
//gc.build("db_LUBM10", "example/rdf_triple/LUBM_10_GStore.n3");
String sparql = "select ?x where {"
+ "?x <rdf:type> <cdblp.cn/class/Paper>. "
+ "?x <cdblp.cn/schema/property/has_author> <cdblp.cn/author/wangshan>. "
+ "}";
boolean flag = gc.load("lubm", "root", "123456");
System.out.println(flag);
String answer = gc.query("root", "123456", "lubm", sparql);
System.out.println(answer);
answer = gc.query("root", "123456", "lubm", sparql);
System.out.println(answer);
sparql = "select ?x where {"
+ "?x <rdf:type> <cdblp.cn/class/Paper>. "
+ "?x <cdblp.cn/schema/property/has_author> <cdblp.cn/author/yuge>. "
+ "}";
answer = gc.query("root", "123456", "lubm", sparql);
System.out.println(answer);
//To count the time cost
//long startTime=System.nanoTime(); //ns
//long startTime=System.currentTimeMillis(); //ms
//doSomeThing(); //test code
//long endTime=System.currentTimeMillis(); //get end time
//System.out.println("run time "+(end-start)+"ms");
}
}

View File

@ -1,90 +1,53 @@
<?php
/*
# Filename: Benchmark.php
# Author: yangchaofan
# Last Modified: 2018-7-28 15:37
# Description: a multi-thread example for php API
# Author: suxunbin
# Last Modified: 2019-5-16 16:05
# 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)
// default db_name: lubm(must be built in advance)
require '../src/GstoreConnector.php';
// variable definition
$tnum = 3000;
$correctness = true;
$tnum = 1000;
$RequestType = "POST";
$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>.
}";
$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>.}";
// thread function
class Mythread extends Thread {
var $rnum, $sparql, $filename;
public function __construct($rnum, $sparql, $filename) {
var $rnum, $sparql, $filename, $RequestType;
public $correctness;
public function __construct($rnum, $sparql, $filename, $RequestType) {
$this->rnum = $rnum;
$this->sparql = $sparql;
$this->filename = $filename;
$this->RequestType = $RequestType;
}
public function run () {
// query
$gc = new GstoreConnector("172.31.222.94", 9000);
$gc = new GstoreConnector("127.0.0.1", 9000, "root", "123456");
$res = $gc->query("lubm", "json", $this->sparql, $this->RequestType);
//$gc->build("test", "data/lubm/lubm.nt", "root", "123456");
//$gc.load("test", "root", "123456");
$gc->fquery("root", "123456", "lubm", $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);
// fquery
//$gc = new GstoreConnector("127.0.0.1", 9000, "root", "123456");
//$gc->fquery("lubm", "json", $this->sparql, $this->filename, $this->RequestType);
//$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;
@ -103,36 +66,40 @@ class Mythread extends Thread {
// compare the result
if ($this->rnum != $Num) {
$correctness = false;
$this->correctness = false;
echo "sparql: " . $this->sparql . PHP_EOL;
echo "Num: " . strval($Num) . PHP_EOL;
}
else
$this->correctness = true;
}
}
# create sparql
// create sparql
array_push($sparql, $sparql0, $sparql1, $sparql2);
array_push($sparql, $sparql3, $sparql4, $sparql5);
# create the threads
// create the threads
for ($i = 0; $i < $tnum; $i++) {
$filename = "result/" . strval($i) . ".txt";
$threads[] = new Mythread($result[$i%6], $sparql[$i%6], $filename);
$filename = "result/res" . strval($i) . ".txt";
$threads[] = new Mythread($result[$i%6], $sparql[$i%6], $filename, $RequestType);
}
// start threads
foreach ($threads as $i)
$i->start();
$correctness = true;
// wait for the threads
foreach ($threads as $i)
{
$i->join();
$correctness = $correctness && $i->correctness;
}
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,71 @@
<?php
/*
# Filename: GET-example.php
# Author: suxunbin
# Last Modified: 2019-5-16 15:30
# Description: a simple GET-example of php API
*/
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
// "GET" is a default parameter that can be omitted
require "../src/GstoreConnector.php";
$IP = "127.0.0.1";
$Port = 9000;
$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, Port, username and password
$gc = new GstoreConnector($IP, $Port, $username, $password);
// build a database with a RDF graph
$res = $gc->build("lubm", "data/lubm/lubm.nt");
echo $res . PHP_EOL;
// load the database
$res = $gc->load("lubm");
echo $res . PHP_EOL;
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//$res = $gc->user("add_user", "user1", "111111");
//echo $res . PHP_EOL;
// show all users
$res = $gc->showUser();
echo $res. PHP_EOL;
// query
$res = $gc->query("lubm", "json", $sparql);
echo $res. PHP_EOL;
// query and save the result in a file
$gc->fquery("lubm", "json", $sparql, $filename);
// save the database if you have changed the database
$res = $gc->checkpoint("lubm");
echo $res. PHP_EOL;
// show information of the database
$res = $gc->monitor("lubm");
echo $res. PHP_EOL;
// show all databases
$res = $gc->show();
echo $res. PHP_EOL;
// unload the database
$res = $gc->unload("lubm");
echo $res. PHP_EOL;
// drop the database
$res = $gc->drop("lubm", false); #delete the database directly
//$res = $gc->drop("lubm", true); #leave a backup
echo $res. PHP_EOL;
?>

View File

@ -0,0 +1,70 @@
<?php
/*
# Filename: POST-example.php
# Author: suxunbin
# Last Modified: 2019-5-16 15:30
# Description: a simple POST-example of php API
*/
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
require "../src/GstoreConnector.php";
$IP = "127.0.0.1";
$Port = 9000;
$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, Port, username and password
$gc = new GstoreConnector($IP, $Port, $username, $password);
// build a database with a RDF graph
$res = $gc->build("lubm", "data/lubm/lubm.nt", "POST");
echo $res . PHP_EOL;
// load the database
$res = $gc->load("lubm", "POST");
echo $res . PHP_EOL;
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//$res = $gc->user("add_user", "user1", "111111", "POST");
//echo $res . PHP_EOL;
// show all users
$res = $gc->showUser("POST");
echo $res. PHP_EOL;
// query
$res = $gc->query("lubm", "json", $sparql, "POST");
echo $res. PHP_EOL;
// query and save the result in a file
$gc->fquery("lubm", "json", $sparql, $filename, "POST");
// save the database if you have changed the database
$res = $gc->checkpoint("lubm", "POST");
echo $res. PHP_EOL;
// show information of the database
$res = $gc->monitor("lubm", "POST");
echo $res. PHP_EOL;
// show all databases
$res = $gc->show("POST");
echo $res. PHP_EOL;
// unload the database
$res = $gc->unload("lubm", "POST");
echo $res. PHP_EOL;
// drop the database
$res = $gc->drop("lubm", false, "POST"); #delete the database directly
//$res = $gc->drop("lubm", true, "POST"); #leave a backup
echo $res. PHP_EOL;
?>

View File

@ -1,56 +0,0 @@
<?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.94", 9000);
// build database
$ret = $gc->build("lubm", "data/lubm/lubm.nt", $username, $password);
echo $ret . PHP_EOL;
// load rdf
$ret = $gc->load("lubm", $username, $password);
echo $ret . PHP_EOL;
// unload rdf
//$ret = $gc->unload("test", $username, $password);
//echo $ret . PHP_EOL;
// query
echo $gc->query($username, $password, "lubm", $sparql) . PHP_EOL;
// fquery--make a SPARQL query and save the result in the file
$gc->fquery($username, $password, "lubm", $sparql, $filename);
// show user information
$ret = $gc->showUser();
echo $ret. PHP_EOL;
// show
$ret = $gc->show($username, $password);
echo $ret. PHP_EOL;
// monitor the database
$ret = $gc->monitor("lubm", $username, $password);
echo $ret. PHP_EOL;
// save the database
$ret = $gc->checkpoint("lubm", $username, $password);
echo $ret. PHP_EOL;
?>

View File

@ -1,8 +1,8 @@
<?php
/*
# Filename: GstoreConnector.php
# Author: yangchaofan
# Last Modified: 2018-7-18 14:50
# Author: yangchaofan suxunbin
# Last Modified: 2019-5-16 11:00
# Description: http api for php
*/
@ -10,11 +10,18 @@ class GstoreConnector {
var $serverIP;
var $serverPort;
var $Url;
var $username;
var $password;
function __construct($ip, $port) {
$this->serverIP = $ip;
function __construct($ip, $port, $user, $passwd) {
if ($ip == "localhost")
$this->serverIP = "127.0.0.1";
else
$this->serverIP = $ip;
$this->serverPort = $port;
$this->Url = "http://" . $ip . ":" . strval($port);
$this->username = $user;
$this->password = $passwd;
}
function Encode($url) {
@ -51,6 +58,22 @@ class GstoreConnector {
return $res;
}
function Post($url, $strPost) {
$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);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $strPost);
$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);
@ -68,67 +91,195 @@ class GstoreConnector {
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);
function fPost($url, $strPost, $filename){
$curl = curl_init();
curl_setopt($curl, CURLOPT_BUFFERSIZE, 4096);
curl_setopt($curl, CURLOPT_URL, $this->Encode($url));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $strPost);
$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 show($username, $password) {
$cmd = $this->Url . "/?operation=show&username=" . $username . "&password=" . $password;
return $this->Get($cmd);
function build($db_name, $rdf_file_path, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=build&db_name=" . $db_name . "&ds_path=" . $rdf_file_path . "&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/build";
$strPost = "{\"db_name\": \"" . $db_name . "\", \"ds_path\": \"" . $rdf_file_path . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function user($type, $username1, $password1, $username2, $addition) {
$cmd = $this->Url . "/?operation=user&type=" . $type . "&username1=" . $username1 . "&password1=" . $password1 . "&username2" . $username2 . "&addition=" . $addition;
return $this->Get($cmd);
function load($db_name, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=load&db_name=" . $db_name . "&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/load";
$strPost = "{\"db_name\": \"" . $db_name . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function unload($db_name, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=unload&db_name=" . $db_name . "&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/unload";
$strPost = "{\"db_name\": \"" . $db_name . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function user($type, $username2, $addition, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=user&type=" . $type . "&username1=" . $this->username . "&password1=" . $this->password . "&username2=" . $username2 . "&addition=" . $addition;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/user";
$strPost = "{\"type\": \"" . $type . "\", \"username1\": \"" . $this->username . "\", \"password1\": \"" . $this->password . "\", \"username2\": \"" . $username2 . "\", \"addition\": \"" . $addition . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function showUser() {
$cmd = $this->Url . "/?operation=showUser";
return $this->Get($cmd);
function showUser($request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=showUser&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/showUser";
$strPost = "{\"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function monitor($db_name, $username, $password) {
$cmd = $this->Url . "/?operation=monitor&db_name=" . $db_name . "&username=" . $username . "&password=" . $password;
return $this->Get($cmd);
}
function query($db_name, $format, $sparql, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=query&username=" . $this->username . "&password=" . $this->password . "&db_name=" . $db_name . "&format=" . $format . "&sparql=" . $sparql;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/query";
$strPost = "{\"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\", \"db_name\": \"" . $db_name . "\", \"format\": \"" . $format . "\", \"sparql\": \"" . $sparql . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function fquery($db_name, $format, $sparql, $filename, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=query&username=" . $this->username . "&password=" . $this->password . "&db_name=" . $db_name . "&format=" . $format . "&sparql=" . $sparql;
$this->fGet($strUrl, $filename);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/query";
$strPost = "{\"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\", \"db_name\": \"" . $db_name . "\", \"format\": \"" . $format . "\", \"sparql\": \"" . $sparql . "\"}";
$this->fPost($strUrl, $strPost, $filename);
}
return;
}
function drop($db_name, $is_backup, $request_type="GET") {
if ($request_type == "GET")
{
if ($is_backup)
$strUrl = $this->Url . "/?operation=drop&db_name=" . $db_name . "&username=" . $this->username . "&password=" . $this->password . "&is_backup=true";
else
$strUrl = $this->Url . "/?operation=drop&db_name=" . $db_name . "&username=" . $this->username . "&password=" . $this->password . "&is_backup=false";
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/drop";
if ($is_backup)
$strPost = "{\"db_name\": \"" . $db_name . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\", \"is_backup\": \"true\"}";
else
$strPost = "{\"db_name\": \"" . $db_name . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\", \"is_backup\": \"false\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function monitor($db_name, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=monitor&db_name=" . $db_name . "&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/monitor";
$strPost = "{\"db_name\": \"" . $db_name . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function checkpoint($db_name, $request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=checkpoint&db_name=" . $db_name . "&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/checkpoint";
$strPost = "{\"db_name\": \"" . $db_name . "\", \"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function show($request_type="GET") {
if ($request_type == "GET")
{
$strUrl = $this->Url . "/?operation=show&username=" . $this->username . "&password=" . $this->password;
$res = $this->Get($strUrl);
}
elseif ($request_type == "POST")
{
$strUrl = $this->Url . "/show";
$strPost = "{\"username\": \"" . $this->username . "\", \"password\": \"" . $this->password . "\"}";
$res = $this->Post($strUrl, $strPost);
}
return $res;
}
function checkpoint($db_name, $username, $password) {
$cmd = $this->Url . "/?operation=checkpoint&db_name=" . $db_name . "&username=" . $username . "&password=" . $password;
return $this->Get($cmd);
}
}
?>

View File

@ -1,19 +1,24 @@
"""
# Filename: Benchmark.py
# Author: yangchaofan
# Last Modified: 2018-7-18 15:13
# Author: suxunbin
# Last Modified: 2019-5-15 20:11
# 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
sys.path.append('../src')
import GstoreConnector
# variables definition
tnum = 3000
# before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
# default db_name: lubm(must be built in advance)
IP = "127.0.0.1"
Port = 9000
username = "root"
password = "123456"
tnum = 1000
correctness = True
RequestType = "POST"
threads = []
result = [15, 0, 828, 27, 27, 5916]
@ -62,19 +67,18 @@ sparql5 = "select distinct ?x where\
}"
# thread function
def Mythread(rnum, sparql, filename):
def Mythread(rnum, sparql, filename, RequestType):
global correctness
# query
gc = GstoreConnector.GstoreConnector("172.31.222.94", 9000)
#gc.build("test", "data/lubm/lubm.nt", "root", "123456")
#gc.load("test", "root", "123456")
gc.fquery("root", "123456", "lubm", sparql, filename)
#res = gc.query("root", "123456", "test", sparql)
gc = GstoreConnector.GstoreConnector(IP, Port, username, password)
res = gc.query("lubm", "json", sparql, RequestType)
# read the file to a str
with open(filename, "r") as f:
res = f.read()
# fquery
#gc = GstoreConnector.GstoreConnector(IP, Port, username, password)
#gc.fquery("lubm", "json", sparql, filename, RequestType)
#with open(filename, "r") as f:
# res = f.read()
# count the nums
m = 0
@ -106,7 +110,7 @@ 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,))
t = threading.Thread(target=Mythread, args=(result[i%6],sparql[i%6],filename, RequestType,))
threads.append(t)
# start threads
@ -121,5 +125,3 @@ if (correctness == True):
print("The answers are correct!")
else:
print("The answers exist errors!")
print("Main thread exit")

View File

@ -0,0 +1,74 @@
"""
# Filename: GET-example.py
# Author: suxunbin
# Last Modified: 2019-5-15 18:26
# Description: a simple GET-example of python API
"""
import sys
sys.path.append('../src')
import GstoreConnector
# before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
# "GET" is a default parameter that can be omitted
IP = "127.0.0.1"
Port = 9000
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, Port, username and password
gc = GstoreConnector.GstoreConnector(IP, Port, username, password)
# build a database with a RDF graph
res = gc.build("lubm", "data/lubm/lubm.nt")
print(res)
# load the database
res = gc.load("lubm")
print(res);
# to add, delete a user or modify the privilege of a user, operation must be done by the root user
#res = gc.user("add_user", "user1", "111111")
#print(res);
# show all users
res = gc.showUser()
print(res)
# query
res = gc.query("lubm", "json", sparql)
print(res)
# query and save the result in a file
gc.fquery("lubm", "json", sparql, filename)
# save the database if you have changed the database
res = gc.checkpoint("lubm")
print(res)
# show information of the database
res = gc.monitor("lubm")
print(res)
# show all databases
res = gc.show()
print(res)
# unload the database
res = gc.unload("lubm")
print(res);
# drop the database
res = gc.drop("lubm", False) #delete the database directly
#res = gc.drop("lubm", True) #leave a backup
print(res);

View File

@ -0,0 +1,77 @@
"""
# Filename: POST-example.py
# Author: suxunbin
# Last Modified: 2019-5-15 18:26
# Description: a simple POST-example of python API
"""
import sys
sys.path.append('../src')
import GstoreConnector
# before you run this example, make sure that you have started up ghttp service (using bin/ghttp port)
IP = "127.0.0.1"
Port = 9000
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, Port, username and password
gc = GstoreConnector.GstoreConnector(IP, Port, username, password)
# build a database with a RDF graph
res = gc.build("lubm", "data/lubm/lubm.nt", "POST")
print(res)
# load the database
res = gc.load("lubm", "POST")
print(res);
# to add, delete a user or modify the privilege of a user, operation must be done by the root user
#res = gc.user("add_user", "user1", "111111", "POST")
#print(res);
# show all users
res = gc.showUser("POST")
print(res)
# query
res = gc.query("lubm", "json", sparql, "POST")
print(res)
# query and save the result in a file
gc.fquery("lubm", "json", sparql, filename, "POST")
# save the database if you have changed the database
res = gc.checkpoint("lubm", "POST")
print(res)
# show information of the database
res = gc.monitor("lubm", "POST")
print(res)
# show all databases
res = gc.show("POST")
print(res)
# unload the database
res = gc.unload("lubm", "POST")
print(res);
# drop the database
res = gc.drop("lubm", False, "POST") #delete the database directly
#res = gc.drop("lubm", True, "POST") #leave a backup
print(res);

View File

@ -1,56 +0,0 @@
"""
# 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 ghttp service (using bin/ghttp db_name port)
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.94", 9000)
# unload the database
#ret = gc.unload("test", username, password)
# build database with a RDF graph
ret = gc.build("lubm", "data/lubm/lubm.nt", username, password)
# load the database
ret = gc.load("lubm", username, password)
# show
print(gc.show(username, password))
# show information of all users
print(gc.showUser())
# monitor the database status
print(gc.monitor('lubm', username, password))
# save the database
print(gc.checkpoint('lubm', username, password))
# query
print(gc.query(username, password, "lubm", sparql))
# query and save the result in a file
gc.fquery(username, password, "lubm", sparql, filename)

View File

@ -1,23 +1,25 @@
"""
# Filename: GStoreConnector.py
# Author: yangchaofan
# Last Modified: 2018-7-18 14:44
# Author: yangchaofan suxunbin
# Last Modified: 2019-5-15 18:10
# Description: http api for python
"""
import requests
defaultServerIP = "127.0.0.1"
defaultServerPort = "3305"
defaultServerPort = "9000"
class GstoreConnector:
def __init__(self, ip, port):
def __init__(self, ip, port, username, password):
if (ip == "localhost"):
self.serverIP = defaultServerIP
else:
self.serverIP = ip
self.serverPort = port
self.Url = "http://" + self.serverIP + ":" + str(self.serverPort)
self.username = username
self.password = password
def UrlEncode(self, s):
ret = ""
@ -41,6 +43,10 @@ class GstoreConnector:
r = requests.get(self.UrlEncode(strUrl))
return r.text
def Post(self, strUrl, strPost):
r = requests.post(self.UrlEncode(strUrl), strPost)
return r.text
def fGet(self, strUrl, filename):
r = requests.get(self.UrlEncode(strUrl), stream=True)
with open(filename, 'wb') as fd:
@ -48,55 +54,125 @@ class GstoreConnector:
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)
def fPost(self, strUrl, strPost, filename):
r = requests.post(self.UrlEncode(strUrl), strPost, stream=True)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
return
def show(self, username, password):
cmd = self.Url + "/?operation=show&username=" + username + "&password=" + password
return self.Get(cmd)
def build(self, db_name, rdf_file_path, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/build"
strPost = '{\"db_name\": \"' + db_name + '\", \"ds_path\": \"' + rdf_file_path + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def user(self, type, username1, password1, username2, addition):
cmd = self.Url + "/?operation=user&type=" + type + "&username1=" + username1+ "&password1=" + password1 + "&username2=" + username2 + "&addition=" +addition
return self.Get(cmd)
def showUser(self):
cmd = self.Url + "/?operation=showUser"
return self.Get(cmd)
def monitor(self, db_name, username, password):
cmd = self.Url + "/?operation=monitor&db_name=" + db_name + "&username=" + username + "&password=" + password
return self.Get(cmd)
def load(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=load&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/load"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def checkpoint(self, db_name, username, password):
cmd = self.Url + "/?operation=checkpoint&db_name=" + db_name + "&username=" + username + "&password=" + password
return self.Get(cmd)
def unload(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=unload&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/unload"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def user(self, type, username2, addition, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=user&type=" + type + "&username1=" + self.username + "&password1=" + self.password + "&username2=" + username2 + "&addition=" +addition
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/user"
strPost = '{\"type\": \"' + type + '\", \"username1\": \"' + self.username + '\", \"password1\": \"' + self.password + '\", \"username2\": \"' + username2 + '\", \"addition\": \"' + addition + '\"}'
res = self.Post(strUrl, strPost)
return res
def showUser(self, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=showUser&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/showUser"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def query(self, db_name, format, sparql, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/query"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"db_name\": \"' + db_name + '\", \"format\": \"' + format + '\", \"sparql\": \"' + sparql + '\"}'
res = self.Post(strUrl, strPost)
return res
def fquery(self, db_name, format, sparql, filename, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=query&username=" + self.username + "&password=" + self.password + "&db_name=" + db_name + "&format=" + format + "&sparql=" + sparql
self.fGet(strUrl, filename)
elif request_type == 'POST':
strUrl = self.Url + "/query"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"db_name\": \"' + db_name + '\", \"format\": \"' + format + '\", \"sparql\": \"' + sparql + '\"}'
self.fPost(strUrl, strPost, filename)
return
def drop(self, db_name, is_backup, request_type='GET'):
if request_type == 'GET':
if is_backup:
strUrl = self.Url + "/?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=true"
else:
strUrl = self.Url + "/?operation=drop&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password + "&is_backup=false"
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/drop"
if is_backup:
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"is_backup\": \"true\"}'
else:
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\", \"is_backup\": \"false\"}'
res = self.Post(strUrl, strPost)
return res
def monitor(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=monitor&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/monitor"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def checkpoint(self, db_name, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=checkpoint&db_name=" + db_name + "&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/checkpoint"
strPost = '{\"db_name\": \"' + db_name + '\", \"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res
def show(self, request_type='GET'):
if request_type == 'GET':
strUrl = self.Url + "/?operation=show&username=" + self.username + "&password=" + self.password
res = self.Get(strUrl)
elif request_type == 'POST':
strUrl = self.Url + "/show"
strPost = '{\"username\": \"' + self.username + '\", \"password\": \"' + self.password + '\"}'
res = self.Post(strUrl, strPost)
return res

View File

@ -6,9 +6,9 @@ Compired with socket API, HTTP API is more stable and more standard, and can mai
## Easy Examples
We provide JAVA and C++ API for ghttp now. Please refer to example codes in `api/http/cpp` and `api/http/java`. To use these examples, please make sure that executables have already been generated.
We provide C++, java, python and php API for ghttp now. Please refer to example codes in `api/http/cpp`, `api/http/java`, `api/http/python` and `api/http/php`. To use these examples, please make sure that executables have already been generated.
Next, **start up ghttp service by using \texttt{./ghttp} command.** It is ok if you know a running usable ghttp server and try to connect to it. (you do not need to change anything if using examples, just by default). Then, for Java and C++ code, you need to compile the example codes in the directory gStore/api/http/.
Next, **start up ghttp service by using \texttt{bin/ghttp} command.** It is ok if you know a running usable ghttp server and try to connect to it. (you do not need to change anything if using examples, just by default). Then, for C++ and java code, you need to compile the example codes in the directory `api/http/cpp/example` and `api/http/java/example`.
Finally, go to the example directory and run the corresponding executables. All these four executables will connect to a specified ghttp server and do some load or query operations. Be sure that you see the query results in the terminal where you run the examples, otherwise please go to [Frequently Asked Questions](FAQ.md) for help or report it to us.(the report approach is described in [README](../README.md))
@ -29,13 +29,11 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
- client.h
- example/ (small example program to show the basic idea of using the C++ API)
- example.cpp
- GET-example.cpp
- Benchmark.cpp
- Benchmark1.cpp
- CppAPIExample.cpp
- POST-example.cpp
- Makefile
@ -59,9 +57,9 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
- Benckmark.java
- Benchmark1.java
- GETexample.java
- JavaAPIExample.java
- POSTexample.java
- Makefile
@ -71,7 +69,9 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
- Benchmark.py
- PyAPIExample.py
- GET-example.py
- POST-example.py
- src/
@ -95,7 +95,9 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
- Benchmark.php
- phpAPIExample.php
- POST-example.php
- GET-example.php
- src/
@ -106,35 +108,55 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
#### Interface
To use the C++ API, please place the phrase `#include "GstoreConnector.h"` in your cpp code. Functions in GstoreConnector.h should be called like below:
To use the C++ API, please place the phrase `#include "client.h"` in your cpp code. Functions in client.h should be called like below:
```
// initialize
GstoreConnector gc("172.31.222.93", 9016);
GstoreConnector gc("127.0.0.1", 9000, "root", "123456");
// build a new database by a RDF file.
// note that the relative path is related to gserver
gc.build("test", "data/lubm/LUBM_10.n3", "root", "123456");
// note that the relative path is related to the server
gc.build("lubm", "data/lubm/lubm.nt");
// load the database that you built.
gc.load("test", "root", "123456");
gc.load("lubm");
// then you can execute SPARQL query on this database.
std::string answer = gc.query("root", "123456", "test", sparql);
std::cout << answer << std::endl;
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
gc.user("add_user", "user1", "111111");
// make a SPARQL query and save the result in ans.txt
gc.query("root", "123456", "test", sparql, "ans.txt");
// show all users
gc.showUser();
// unload this database.
gc.unload("test", "root", "123456");
// query
std::string res = gc.query("lubm", "json", sparql);
std::cout << res << std::endl;
// query and save the result in a file called "ans.txt"
gc.fquery("lubm", "json", sparql, "ans.txt");
// save the database if you have changed the database
gc.checkpoint("lubm");
// show information of the database
gc.monitor("lubm");
// show all databases
gc.show();
// unload this database.
gc.unload("lubm");
// also, you can load some exist database directly and then query.
gc.load("lubm", "root", "123456");
answer = gc.query("root", "123456", "lubm", sparql);
gc.load("lubm");
res = gc.query("lubm", "json", sparql);
std::cout << res << std::endl;
gc.unload("lubm");
std::cout << answer << std::endl;
gc.unload("lubm", "root", "123456");
// drop the database directly
gc.drop("lubm", false);
// drop the database and leave a backup
gc.drop("lubm", true);
```
The original declaration of these functions are as below:
@ -142,15 +164,27 @@ The original declaration of these functions are as below:
```
GstoreConnector();
bool build(std::string _db_name, std::string _rdf_file_path, std::string username, std::string password);
std::string build(std::string db_name, std::string rdf_file_path, std::string request_type);
bool load(std::string _db_name, std::string username, std::string password);
std::string load(std::string db_name, std::string request_type);
bool unload(std::string _db_name, std::string username, std::string password);
std::string unload(std::string db_name, std::string request_type);
void query(std::string username, std::string password, std::string db_name, std::string sparql, std::string filename);
std::string user(std::string type, std::string username2, std::string addition, std::string request_type);
std::string query(std::string username, std::string password, std::string db_name, std::string sparql);
std::string showUser(std::string request_type);
std::string query(std::string db_name, std::string format, std::string sparql, std::string request_type);
std::string drop(std::string db_name, bool is_backup, std::string request_type);
std::string monitor(std::string db_name, std::string request_type);
std::string checkpoint(std::string db_name, std::string request_type);
std::string show(std::string request_type);
void fquery(std::string db_name, std::string format, std::string sparql, std::string filename, std::string request_type);
```
@ -163,28 +197,52 @@ std::string query(std::string username, std::string password, std::string db_nam
To use the Java API, please see gStore/api/http/java/src/jgsc/GstoreConnector.java. Functions should be called like below:
```
// initialize
GstoreConnector gc = new GstoreConnector("172.31.222.78", 3305);
GstoreConnector gc = new GstoreConnector("127.0.0.1", 9000, "root", "123456");
// build the database
gc.build("LUBM10", "data/lubm/lubm.nt", "root", "123456");
// build a new database by a RDF file.
// note that the relative path is related to the server
gc.build("lubm", "data/lubm/lubm.nt");
// load the database you built
gc.load("LUBM10", "root", "123456");
// load the database that you built.
gc.load("lubm");
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
gc.user("add_user", "user1", "111111");
// show all users
gc.showUser();
// query
String res = gc.query("lubm", "json", sparql);
System.out.println(res);
// query and save the result in a file called "ans.txt"
gc.fquery("lubm", "json", sparql, "ans.txt");
// save the database if you have changed the database
gc.checkpoint("lubm");
// show information of the database
gc.monitor("lubm");
// show all databases
gc.show();
// unload this database.
gc.unload("lubm");
// also, you can load some exist database directly and then query.
gc.load("LUBM10", "root", "123456");
gc.load("lubm");
res = gc.query("lubm", "json", sparql);
System.out.println(res);
gc.unload("lubm");
// make a SPARQL query
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");
// unload the database
gc.unload("LUBM10", "root", "123456");
// drop the database directly
gc.drop("lubm", false);
// drop the database and leave a backup
gc.drop("lubm", true);
```
@ -194,15 +252,28 @@ The original declaration of these functions are as below:
```
public class GstoreConnector();
public boolean build(String _db_name, String _rdf_file_path, String _username, String _password);
public String build(String db_name, String rdf_file_path, String request_type);
public boolean load(String _db_name, String _username, String _password);
public String load(String db_name, String request_type);
public boolean unload(String _db_name,String _username, String _password);
public String unload(String db_name, String request_type);
public String query(String _username, String _password, String _db_name, String _sparql);
public String user(String type, String username2, String addition, String request_type);
public String showUser(String request_type);
public String query(String db_name, String format, String sparql, String request_type);
public void fquery(String db_name, String format, String sparql, String filename, String request_type);
public String drop(String db_name, boolean is_backup, String request_type);
public String monitor(String db_name, String request_type);
public String checkpoint(String db_name, String request_type);
public String show(String request_type);
public void query(String _username, String _password, String _db_name, String _sparql, String _filename);
```
- - -
@ -214,38 +285,73 @@ public void query(String _username, String _password, String _db_name, String _s
To use Python API, please see gStore/api/http/python/src/GstoreConnector.py. Functions should be called like following:
```
# start a gc with given IP and Port
gc = GstoreConnector.GstoreConnector("172.31.222.78", 3305)
# start a gc with given IP, Port, username and password
gc = GstoreConnector.GstoreConnector("127.0.0.1", 9000, "root", "123456")
# build database with a RDF graph
ret = gc.build("test", "data/lubm/lubm.nt", username, password)
# build a database with a RDF graph
res = gc.build("lubm", "data/lubm/lubm.nt")
# load the database
ret = gc.load("test", username, password)
# load the database
res = gc.load("lubm")
# query
print (gc.query(username, password, "test", sparql))
# to add, delete a user or modify the privilege of a user, operation must be done by the root user
res = gc.user("add_user", "user1", "111111")
# query and save the result in a file
gc.fquery(username, password, "test", sparql, filename)
# show all users
res = gc.showUser()
# query
res = gc.query("lubm", "json", sparql)
print(res)
# query and save the result in a file called "ans.txt"
gc.fquery("lubm", "json", sparql, "ans.txt")
# save the database if you have changed the database
res = gc.checkpoint("lubm")
# show information of the database
res = gc.monitor("lubm")
# show all databases
res = gc.show()
# unload the database
res = gc.unload("lubm")
# drop the database directly
res = gc.drop("lubm", False)
# drop the database and leave a backup
res = gc.drop("lubm", True)
# unload the database
ret = gc.unload("test", username, password)
```
The original declaration of these functions are as below:
```
public class GstoreConnector()
def build(self, db_name, rdf_file_path, username, password):
def build(self, db_name, rdf_file_path, request_type):
def load(self, db_name, username, password):
def load(self, db_name, request_type):
def unload(self, db_name, username, password):
def unload(self, db_name, request_type):
def query(self, username, password, db_name, sparql):
def user(self, type, username2, addition, request_type):
def fquery(self, username, password, db_name, sparql, filename):
def showUser(self, request_type):
def query(self, db_name, format, sparql, request_type):
def fquery(self, db_name, format, sparql, filename, request_type):
def drop(self, db_name, is_backup, request_type):
def monitor(self, db_name, request_type):
def checkpoint(self, db_name, request_type):
def show(self, request_type):
```
@ -299,26 +405,55 @@ async query(db = '', sparql = '')
To use Php API, please see gStore/api/http/php/src/GstoreConnector.php. Functions should be called like following:
```
// start a gc
$gc = new GstoreConnector("172.31.222.78", 3305);
// start a gc with given IP, Port, username and password
$gc = new GstoreConnector("127.0.0.1", 9000, "root", "123456");
// build database
$ret = $gc->build("test", "data/lubm/lubm.nt", $username, $password);
echo $ret . PHP_EOL;
// build a database with a RDF graph
$res = $gc->build("lubm", "data/lubm/lubm.nt");
echo $res . PHP_EOL;
// load rdf
$ret = $gc->load("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);
// unload rdf
$ret = $gc->unload("test", $username, $password);
echo $ret . PHP_EOL;
// to add, delete a user or modify the privilege of a user, operation must be done by the root user
//$res = $gc->user("add_user", "user1", "111111");
//echo $res . PHP_EOL;
// show all users
$res = $gc->showUser();
echo $res. PHP_EOL;
// query
$res = $gc->query("lubm", "json", $sparql);
echo $res. PHP_EOL;
// query and save the result in a file called "ans.txt"
$gc->fquery("lubm", "json", $sparql, "ans.txt");
// save the database if you have changed the database
$res = $gc->checkpoint("lubm");
echo $res. PHP_EOL;
// show information of the database
$res = $gc->monitor("lubm");
echo $res. PHP_EOL;
// show all databases
$res = $gc->show();
echo $res. PHP_EOL;
// unload the database
$res = $gc->unload("lubm");
echo $res. PHP_EOL;
// drop the database directly
$res = $gc->drop("lubm", false);
echo $res. PHP_EOL;
// drop the database and leave a backup
$res = $gc->drop("lubm", true);
echo $res. PHP_EOL;
```
@ -327,15 +462,28 @@ The original declaration of these functions are as below:
```
class GstoreConnector
function build($db_name, $rdf_file_path, $username, $password)
function build($db_name, $rdf_file_path, $request_type)
function load($db_name, $username, $password)
function load($db_name, $request_type)
function unload($db_name, $username, $password)
function unload($db_name, $request_type)
function query($username, $password, $db_name, $sparql)
function user($type, $username2, $addition, $request_type)
function showUser($request_type)
function query($db_name, $format, $sparql, $request_type)
function fquery($db_name, $format, $sparql, $filename, $request_type)
function drop($db_name, $is_backup, $request_type)
function monitor($db_name, $request_type)
function checkpoint($db_name, $request_type)
function show($request_type)
function fquery($username, $password, $db_name, $sparql, $filename)
```
---

View File

@ -112,50 +112,55 @@ if you leave out the argument serverPort in the commond, then the corresponding
if you leave out the argument db_name in the commond, then the server will start with no database loaded.
operation: build, load, unload, query, monitor, show, checkpoint, user, drop
operation: build, load, unload, user, showUser, query, drop, monitor, show, checkpoint
```
// build a new database by a RDF file.
gc.build("test", "data/lubm/LUBM_10.n3", "root", "123456");
// initialize
GstoreConnector gc("127.0.0.1", 9000, "root", "123456");
// drop a database already built but leave a backup.
gc.drop("test", "root", "123456");
// build a new database by a RDF file.
gc.build("lubm", "data/lubm/lubm.nt");
// drop a database already built completely.
gc.drop_r("test", "root", "123456");
// load database
gc.load("test", "root", "123456");
// then you can execute SPARQL query on this database.
answer = gc.query("root", "123456", "test", sparql);
// output information of a database
cout << answer << std::endl;
// unload this database
gc.unload("lubm", "root", "123456");
// show all databases already built and if they are loaded
gc.show("root", "123456");
// show statistical information of a loaded database
gc.monitor("lubm", "root", "123456");
// save updates of a loaded database
gc.checkpoint("lubm", "root", "123456");
// load the database that you built.
gc.load("lubm");
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", "root", "123456", "Jack", "2");
gc.user("add_user", "Jack", "2");
//add privilege to user Jack(add_query, add_load, add_unload)
answer = gc.user("add_query", "root", "123456", "Jack", "lubm");
gc.user("add_query", "Jack", "lubm");
//delete privilege of a user Jack(delete_query, delete_load, delete_unload)
answer = gc.user("delete_query", "root", "123456", "Jack", "lubm");
gc.user("delete_query", "Jack", "lubm");
//delete user(with username: Jack, password: 2)
answer = gc.user("delete_user", "root", "123456", "Jack", "2");
gc.user("delete_user", "Jack", "2");
// show all users
gc.showUser();
// query
res = gc.query("lubm", "json", sparql);
std::cout << res << std::endl;
// save updates of a loaded database
gc.checkpoint("lubm");
// show statistical information of a loaded database
gc.monitor("lubm");
// show all databases already built and if they are loaded
gc.show();
// unload this database.
gc.unload("lubm");
// drop a database already built completely.
gc.drop("lubm", false);
// drop a database already built but leave a backup.
gc.drop("lubm", true);
```
```
@ -169,6 +174,7 @@ username: the username of the user that execute the operation
password: the password of the user that execute the operation
```
`ghttp` support both GET and POST request-type.
`ghttp` support concurrent read-only queries, but when queries containing updates come, the whole database will be locked.
The number of concurrent running queries is suggest to be lower than 300 on a machine with dozens of kernel threads, though we can run 13000 queries concurrently in our experiments.
To use the concurrency feature, you had better modify the system settings of 'open files' and 'maximum processes' to 65535 or larger.
@ -176,7 +182,7 @@ Three scripts are placed in [setup](../scripts/setup/) to help you modify the se
**If queries containing updates are sent via `ghttp`, you'd better often send a `checkpoint` command to the `ghttp` console. Otherwise, the updates may not be synchronize to disk and will be lost if the `ghttp` server is stopped abnormally.(For example, type "Ctrl+C")**
**Attention: you can not stop ghttp by simply type the command "Ctrl+C", because this will cause the changes of databases lost.
**Attention: you had better not stop ghttp by simply type the command "Ctrl+C", because this is not safe.
** In order to stop the ghttp server, you can type `bin/shutdown serverPort`
- - -