feat: add php and python api for python;

bug exists in java api of http;

by zengli
This commit is contained in:
bookug 2018-08-08 13:33:17 +08:00
commit f223dd80b6
26 changed files with 1754 additions and 486 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
# Executables
*.pyc
*.exe
*.out
*.app

1
TODO Normal file
View File

@ -0,0 +1 @@
xxx

View File

@ -1,223 +1,261 @@
#include "client.h"
#include <curl/curl.h>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
CHttpClient::CHttpClient(void) :
m_bDebug(false)
{
}
CHttpClient::~CHttpClient(void)
{
}
static const std::string UrlEncode(const std::string& s)
{
std::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])==42) || (int(ptr[i])==45) || (int(ptr[i])==46) || (int(ptr[i])==47) || (int(ptr[i])==58) ||(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;
}
static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
if(itype == CURLINFO_TEXT)
{
//printf("[TEXT]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_IN)
{
printf("[HEADER_IN]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_OUT)
{
printf("[HEADER_OUT]%s\n", pData);
}
else if(itype == CURLINFO_DATA_IN)
{
printf("[DATA_IN]%s\n", pData);
}
else if(itype == CURLINFO_DATA_OUT)
{
printf("[DATA_OUT]%s\n", pData);
}
return 0;
}
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
if( NULL == str || NULL == buffer )
{
return -1;
}
char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
}
int CHttpClient::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 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);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
/**
* 线使线sleep或是wait等操作
* libcurl将会发信号打断这个wait从而导致程序退出
*/
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::Posts(const std::string & strUrl, const std::string & strPost, 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_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);
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
//缺省情况就是PEM所以无需设置另外支持DER
//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
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 CHttpClient::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;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void CHttpClient::SetDebug(bool bDebug)
{
m_bDebug = bDebug;
}
#include "client.h"
#include <curl/curl.h>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
CHttpClient::CHttpClient(void) :
m_bDebug(false)
{
}
CHttpClient::~CHttpClient(void)
{
}
static const std::string UrlEncode(const std::string& s)
{
std::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])==42) || (int(ptr[i])==45) || (int(ptr[i])==46) || (int(ptr[i])==47) || (int(ptr[i])==58) ||(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;
}
static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
if(itype == CURLINFO_TEXT)
{
//printf("[TEXT]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_IN)
{
printf("[HEADER_IN]%s\n", pData);
}
else if(itype == CURLINFO_HEADER_OUT)
{
printf("[HEADER_OUT]%s\n", pData);
}
else if(itype == CURLINFO_DATA_IN)
{
printf("[DATA_IN]%s\n", pData);
}
else if(itype == CURLINFO_DATA_OUT)
{
printf("[DATA_OUT]%s\n", pData);
}
return 0;
}
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
if( NULL == str || NULL == buffer )
{
return -1;
}
char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
}
int CHttpClient::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 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)
{
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 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);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
/**
* 线使线sleep或是wait等操作
* libcurl将会发信号打断这个wait从而导致程序退出
*/
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::Posts(const std::string & strUrl, const std::string & strPost, 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_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);
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
//缺省情况就是PEM所以无需设置另外支持DER
//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
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 CHttpClient::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;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void CHttpClient::SetDebug(bool bDebug)
{
m_bDebug = bDebug;
}

View File

@ -1,63 +1,67 @@
#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__
//REFERENCE: https://curl.haxx.se/
//libcurl is useful for developing http client, but not for server
//
//TODO: deal with cookie
//URL encode: http://www.ruanyifeng.com/blog/2010/02/url_encoding.html
#include <string>
class CHttpClient
{
public:
CHttpClient(void);
~CHttpClient(void);
public:
/**
* @brief HTTP POST请求
* @param strUrl ,Url地址,:http://www.baidu.com
* @param strPost ,使para1=val12=val2&
* @param strResponse ,
* @return Post成功
*/
int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
/**
* @brief HTTP GET请求
* @param strUrl ,Url地址,:http://www.baidu.com
* @param strResponse ,
* @return Post成功
*/
int Get(const std::string & strUrl, std::string & strResponse);
/**
* @brief HTTPS POST请求,
* @param strUrl ,Url地址,:https://www.alipay.com
* @param strPost ,使para1=val12=val2&
* @param strResponse ,
* @param pCaPath ,CA证书的路径.NULL,.
* @return Post成功
*/
int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
/**
* @brief HTTPS GET请求,
* @param strUrl ,Url地址,:https://www.alipay.com
* @param strResponse ,
* @param pCaPath ,CA证书的路径.NULL,.
* @return Post成功
*/
int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
public:
void SetDebug(bool bDebug);
private:
bool m_bDebug;
};
#endif
#ifndef __HTTP_CURL_H__
#define __HTTP_CURL_H__
//REFERENCE: https://curl.haxx.se/
//libcurl is useful for developing http client, but not for server
//
//TODO: deal with cookie
//URL encode: http://www.ruanyifeng.com/blog/2010/02/url_encoding.html
#include <string>
class CHttpClient
{
public:
CHttpClient(void);
~CHttpClient(void);
public:
/**
* @brief HTTP POST请求
* @param strUrl ,Url地址,:http://www.baidu.com
* @param strPost ,使para1=val1?2=val2&
* @param strResponse ,
* @return Post成功
*/
int Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse);
/**
* @brief HTTP GET请求
* @param strUrl ,Url地址,:http://www.baidu.com
* @param strResponse ,
* @return Post成功
*/
int Get(const std::string & strUrl, const std::string & filename, bool SavedOnFile);
int Get(const std::string & strUrl, std::string & strResponse);
/**
* @brief HTTPS POST请求,
* @param strUrl ,Url地址,:https://www.alipay.com
* @param strPost ,使para1=val1?2=val2&
* @param strResponse ,
* @param pCaPath ,CA证书的路径.NULL,.
* @return Post成功
*/
int Posts(const std::string & strUrl, const std::string & strPost, std::string & strResponse, const char * pCaPath = NULL);
/**
* @brief HTTPS GET请求,
* @param strUrl ,Url地址,:https://www.alipay.com
* @param strResponse ,
* @param pCaPath ,CA证书的路径.NULL,.
* @return Post成功
*/
int Gets(const std::string & strUrl, std::string & strResponse, const char * pCaPath = NULL);
public:
void SetDebug(bool bDebug);
private:
bool m_bDebug;
};
#endif

View File

@ -9,6 +9,11 @@ using namespace std;
//#define tnum 12000
#define tnum 3000
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)

View File

@ -0,0 +1,253 @@
#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,76 +1,81 @@
/*=============================================================================
# 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 run this example, you must start up the GStore server at first (use command ./gserver).
int main(int argc, char * argv[])
{
// 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", "root", "123456");
//load the database that you've build.
gc.load("lubm", "root", "123456");
// 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("root", "123456", "lubm", sparql);
std::cout << answer << std::endl;
// unload this database.
gc.unload("lubm", "root", "123456");
// also, you can load some exist database directly and then query.
gc.load("lubm", "root", "123456");
answer = gc.query("root", "123456", "lubm", sparql);
std::cout << answer << std::endl;
//you can monitor a database
answer = gc.monitor("lubm");
std::cout << answer << std::endl;
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", "root", "123456", "Jack", "2");
std::cout << answer << std::endl;
//add privilege to user Jack(add_query, add_load, add_unload)
answer = gc.user("add_query", "root", "123456", "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", "root", "123456", "Jack", "lubm");
std::cout << answer << std::endl;
//delete user(with username: Jack, password: 2)
answer = gc.user("delete_user", "root", "123456", "Jack", "2");
std::cout << answer << std::endl;
gc.unload("lubm", "root", "123456");
return 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[])
{
// 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", "root", "123456");
//load the database that you've build.
gc.load("lubm", "root", "123456");
// 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("root", "123456", "lubm", sparql);
std::cout << answer << std::endl;
// make a SPARQL query and save the result in ans.txt
gc.query("root", "123456", "lubm", sparql, "ans.txt");
// unload this database.
gc.unload("lubm", "root", "123456");
// also, you can load some exist database directly and then query.
gc.load("lubm", "root", "123456");
answer = gc.query("root", "123456", "lubm", sparql);
std::cout << answer << std::endl;
//you can monitor a database
answer = gc.monitor("lubm");
std::cout << answer << std::endl;
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", "root", "123456", "Jack", "2");
std::cout << answer << std::endl;
//add privilege to user Jack(add_query, add_load, add_unload)
answer = gc.user("add_query", "root", "123456", "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", "root", "123456", "Jack", "lubm");
std::cout << answer << std::endl;
//delete user(with username: Jack, password: 2)
answer = gc.user("delete_user", "root", "123456", "Jack", "2");
std::cout << answer << std::endl;
gc.unload("lubm", "root", "123456");
return 0;
}

View File

@ -7,7 +7,7 @@
#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()
{

View File

@ -166,6 +166,14 @@ GstoreConnector::query(string username, string password, string db_name, string
return recv_msg;
}
void GstoreConnector::query(string username, string password, string db_name, string sparql, string filename)
{
string url = "http://" + this->serverIP + ":" + std::to_string(this->serverPort);
string cmd = url + "/?operation=query&username=" + username + "&password=" + password + "&db_name=" + db_name + "&format=json&sparql=" + sparql;
int ret = hc.Get(cmd, filename, true);
return;
}
string
GstoreConnector::show()
{

View File

@ -31,7 +31,8 @@ public:
bool build(std::string _db_name, std::string _rdf_file_path, std::string username, std::string password);
bool drop(std::string _db_name);
std::string query(std::string username, std::string password, std::string db_name, std::string sparql);
std::string show(); //show all databases
void query(std::string username, std::string password, std::string db_name, std::string sparql, std::string filename);
std::string show(); //show all databases
std::string user(std::string type, std::string username1, std::string password1, std::string username2, std::string addtion);
std::string showUser();
std::string monitor(std::string db_name);

View File

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

View File

@ -0,0 +1,141 @@
/*=============================================================================
# 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", "test", sparql, filename);
FileInputStream in = null;
try {
in = new FileInputStream(filename);
int size = in.available();
byte[] buffer = new byte[size];
in.read(buffer);
in.close();
answer = new String(buffer);
} catch (IOException e) {
System.out.println("error occurs: fail to open " + filename);
}
// get result count
int m = 0;
for(int i = 0; i<sparql.length(); ++i)
{
if(sparql.charAt(i) == '?')
++m;
if(sparql.charAt(i) == '{')
break;
}
int n = 0;
for(int i = 0; i<answer.length(); ++i)
{
if(answer.charAt(i) == '{')
++n;
}
int Num = (n-3)/(m+1);
// compare the result
if (rnum != Num)
correctness = false;
}
}
public class Benchmark1
{
public static void main(String[] args)
{
GstoreConnector gc = new GstoreConnector("172.31.222.78", 3305);
gc.load("test", "root", "123456");
String[] spq = new String[6];
spq[0] = "select ?x where"
+"{"
+"?x <ub:name> <FullProfessor0> ."
+"}";
spq[1] = "select distinct ?x where"
+"{"
+"?x <rdf:type> <ub:GraduateStudent>."
+"?y <rdf:type> <ub:GraduateStudent>."
+"?z <rdf:type> <ub:GraduateStudent>."
+"?x <ub:memberOf> ?z."
+"?z <ub:subOrganizationOf> ?y."
+"?x <ub:undergaduateDegreeFrom> ?y."
+"}";
spq[2] = "select distinct ?x where"
+"{"
+"?x <rdf:type> <ub:Course>."
+"?x <ub:name> ?y."
+"}";
spq[3] = "select ?x where"
+"{"
+"?x <rdf:type> <ub:UndergraduateStudent>."
+"?y <ub:name> <Course1>."
+"?x <ub:takesCourse> ?y."
+"?z <ub:teacherOf> ?y."
+"?z <ub:name> <FullProfessor1>."
+"?z <ub:worksFor> ?w."
+"?w <ub:name> <Department0>."
+"}";
spq[4] = "select distinct ?x where"
+"{"
+"?x <rdf:type> <ub:UndergraduateStudent>."
+"?y <ub:name> <Course1>."
+"?x <ub:takesCourse> ?y."
+"?z <ub:teacherOf> ?y."
+"?z <ub:name> <FullProfessor1>."
+"?z <ub:worksFor> ?w."
+"?w <ub:name> <Department0>."
+"}";
spq[5] = "select distinct ?x where"
+"{"
+"?x <rdf:type> <ub:UndergraduateStudent>."
+"}";
int[] result = {15, 0, 828, 27, 27, 5916};
int tnum = 6;
tnum = 3000;
MyThread[] qt = new MyThread[tnum];
for(int i = 0; i < tnum; ++i)
{
qt[i] = new MyThread(i, gc, spq[i%6], result[i%6]);
qt[i].start();
}
for(int i = 0; i < tnum; ++i)
{
try {
qt[i].join();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
if (MyThread.correctness == true)
System.out.println("The results are correct!");
else
System.out.println("The results exist errors!");
}
}

View File

@ -7,13 +7,13 @@
import jgsc.GstoreConnector;
// before run this example, you must start up the GStore server at first (use command ./gserver).
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
public class JavaAPIExample
{
public static void main(String[] args)
{
// initialize the GStore server's IP address and port.
GstoreConnector gc = new GstoreConnector("172.31.222.93", 9016);
GstoreConnector gc = new GstoreConnector("172.31.222.94", 9000);
//below are for parallel test
//GstoreConnector gc = new GstoreConnector("172.31.222.94", 9000);
@ -86,8 +86,8 @@ public class JavaAPIExample
// build a new database by a RDF file.
// note that the relative path is related to gserver.
gc.build("LUBM10", "data/lubm/LUBM_10.n3", "root", "123456");
gc.load("LUBM10", "root", "123456");
gc.build("lubm", "data/lubm/lubm.nt", "root", "123456");
gc.load("lubm", "root", "123456");
// then you can execute SPARQL query on this database.
String sparql = "select ?x where "
@ -100,14 +100,14 @@ public class JavaAPIExample
+ "?z <ub:worksFor> ?w. "
+ "?w <ub:name> <Department0>. "
+ "}";
String answer = gc.query("root", "123456", "LUBM10", sparql);
String answer = gc.query("root", "123456", "lubm", sparql);
System.out.println(answer);
// unload this database.
gc.unload("LUBM10", "root", "123456");
gc.unload("lubm", "root", "123456");
// also, you can load some exist database directly and then query.
gc.load("LUBM10", "root", "123456");
gc.load("lubm", "root", "123456");
//sparql = "delete where "
//+ "{"
@ -117,29 +117,32 @@ public class JavaAPIExample
//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("root", "123456", "LUBM10", sparql);
answer = gc.query("root", "123456", "lubm", sparql);
System.out.println(answer);
// make a SPARQL query and save the result in ans.txt
gc.query("root", "123456", "lubm", sparql, "ans.txt");
//monitor a database
answer = gc.monitor("LUBM10");
answer = gc.monitor("lubm");
System.out.println(answer);
//add a user(with username: Jack, password: 2)
answer = gc.user("add_user", "root", "123456", "Jack", "2");
System.out.println(answer);
//add privilege to user Jack(add_query, add_load, add_unload)
answer = gc.user("add_query", "root", "123456", "Jack", "LUBM10");
answer = gc.user("add_query", "root", "123456", "Jack", "lubm");
System.out.println(answer);
//then Jack can query the database LUBM10
answer = gc.query("Jack", "2", "LUBM10", sparql);
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", "root", "123456", "Jack", "LUBM10");
answer = gc.user("delete_query", "root", "123456", "Jack", "lubm");
System.out.println(answer);
//delete user(with username: Jack, password: 2)
answer = gc.user("delete_user", "root", "123456", "Jack", "2");
System.out.println(answer);
gc.unload("LUBM10", "root", "123456");
gc.unload("lubm", "root", "123456");
}
}

View File

@ -13,8 +13,8 @@ run: JavaAPIExample.class
test: Benchmark.class
java -XX:-UseGCOverheadLimit -Xmx64G -cp ../lib/GstoreJavaAPI.jar:. Benchmark
test: Benchmark.class
java -XX:-UseGCOverheadLimit -Xmx64G -cp ../lib/GstoreJavaAPI.jar:. Benchmark >> time.txt
#test: Benchmark.class
#java -XX:-UseGCOverheadLimit -Xmx64G -cp ../lib/GstoreJavaAPI.jar:. Benchmark >> time.txt
clean:
rm -f JavaAPIExample.class

View File

@ -69,12 +69,12 @@ public class GstoreConnector {
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
//for (String key : map.keySet()) {
// System.out.println(key + "--->" + map.get(key));
//}
long t1 = System.currentTimeMillis(); //ms
System.out.println("Time to get header: "+(t1 - t0)+" ms");
//System.out.println("Time to get header: "+(t1 - t0)+" ms");
//System.out.println("============================================");
// 定义 BufferedReader输入流来读取URL的响应
@ -83,13 +83,13 @@ public class GstoreConnector {
while ((line = in.readLine()) != null) {
//PERFORMANCE: this can be very costly if result is very large, because many temporary Strings are produced
//In this case, just print the line directly will be much faster
result.append(line);
result.append(line+"\n");
//System.out.println("get data size: " + line.length());
//System.out.println(line);
}
long t2 = System.currentTimeMillis(); //ms
System.out.println("Time to get data: "+(t2 - t1)+" ms");
//System.out.println("Time to get data: "+(t2 - t1)+" ms");
} catch (Exception e) {
System.out.println("error in get request: " + e);
e.printStackTrace();
@ -107,6 +107,86 @@ public class GstoreConnector {
return result.toString();
}
public void sendGet(String param, String filename) {
String url = "http://" + this.serverIP + ":" + this.serverPort;
BufferedReader in = null;
System.out.println("parameter: "+param);
if (filename == null)
return;
FileWriter fw = null;
try {
fw = new FileWriter(filename);
} catch (IOException e) {
System.out.println("can not open " + filename + "!");
}
try {
param = URLEncoder.encode(param, "UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Broken VM does not support UTF-8");
}
try {
String urlNameString = url + "/" + param;
System.out.println("request: "+urlNameString);
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
//set agent to avoid: speed limited by server if server think the client not a browser
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
long t0 = System.currentTimeMillis(); //ms
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
//for (String key : map.keySet()) {
// System.out.println(key + "--->" + map.get(key));
//}
long t1 = System.currentTimeMillis(); // ms
//System.out.println("Time to get header: "+(t1 - t0)+" ms");
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
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];
}
long t2 = System.currentTimeMillis(); //ms
//System.out.println("Time to get data: "+(t2 - t1)+" ms");
} catch (Exception e) {
//System.out.println("error in get request: " + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
if (fw != null) {
fw.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return;
}
//NOTICE: no need to connect now, HTTP connection is kept by default
public boolean load(String _db_name, String _username, String _password) {
boolean connect_return = this.connect();
@ -139,7 +219,6 @@ public class GstoreConnector {
return false;
}
//String cmd = "unload/" + _db_name;
String cmd = "?operation=unload&db_name=" + _db_name + "&username=" + _username + "&password=" + _password;
String msg = this.sendGet(cmd);
@ -215,6 +294,21 @@ public class GstoreConnector {
return msg;
}
public void query(String _username, String _password, String _db_name, String _sparql, String _filename) {
boolean connect_return = this.connect();
if (!connect_return) {
System.err.println("connect to server error. @GstoreConnector.query");
}
String cmd = "?operation=query&username=" + _username + "&password=" + _password + "&db_name=" + _db_name + "&format=json&sparql=" + _sparql;
this.sendGet(cmd, _filename);
this.disconnect();
return;
}
// public String show() {
// return this.show(false);

View File

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

View File

@ -0,0 +1,40 @@
<?php
/*
# Filename: phpAPIExample.php
# Author: yangchaofan
# Last Modified: 2018-7-27 21:50
# Description: a simple example of php API
*/
// before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
require "../src/GstoreConnector.php";
// example
$username = "root";
$password = "123456";
$filename = "res.txt";
$sparql = "select ?x where { ?x <ub:name> <FullProfessor0> .}";
// start a gc
$gc = new GstoreConnector("172.31.222.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);
?>

View File

View File

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

View File

@ -0,0 +1,125 @@
"""
# Filename: Benchmark.py
# Author: yangchaofan
# Last Modified: 2018-7-18 15:13
# Description: a simple example of multi-thread query
"""
# before you run this example, make sure that you have started up ghttp service (using bin/ghttp db_name port)
import threading
import sys
sys.path.append('../src')
import GstoreConnector
# variables definition
tnum = 3000
correctness = True
threads = []
result = [15, 0, 828, 27, 27, 5916]
sparql = []
sparql0 = "select ?x where\
{\
?x <ub:name> <FullProfessor0> .\
}"
sparql1 = "select distinct ?x where\
{\
?x <rdf:type> <ub:GraduateStudent>.\
?y <rdf:type> <ub:GraduateStudent>.\
?z <rdf:type> <ub:GraduateStudent>.\
?x <ub:memberOf> ?z.\
?z <ub:subOrganizationOf> ?y.\
?x <ub:undergaduateDegreeFrom> ?y.\
}"
sparql2 = "select distinct ?x where\
{\
?x <rdf:type> <ub:Course>.\
?x <ub:name> ?y.\
}"
sparql3 = "select ?x where\
{\
?x <rdf:type> <ub:UndergraduateStudent>.\
?y <ub:name> <Course1>.\
?x <ub:takesCourse> ?y.\
?z <ub:teacherOf> ?y.\
?z <ub:name> <FullProfessor1>.\
?z <ub:worksFor> ?w.\
?w <ub:name> <Department0>.\
}"
sparql4 = "select distinct ?x where\
{\
?x <rdf:type> <ub:UndergraduateStudent>.\
?y <ub:name> <Course1>.\
?x <ub:takesCourse> ?y.\
?z <ub:teacherOf> ?y.\
?z <ub:name> <FullProfessor1>.\
?z <ub:worksFor> ?w.\
?w <ub:name> <Department0>.\
}"
sparql5 = "select distinct ?x where\
{\
?x <rdf:type> <ub:UndergraduateStudent>.\
}"
# thread function
def Mythread(rnum, sparql, filename):
global correctness
# query
gc = GstoreConnector.GstoreConnector("172.31.222.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)
# read the file to a str
with open(filename, "r") as f:
res = f.read()
# count the nums
m = 0
for i in range(len(sparql)):
if (sparql[i] == "?"):
m = m + 1
if (sparql[i] == "{"):
break
n = 0
for i in range(len(res)):
if (res[i] == "{"):
n = n + 1
Num = (n-3)/(m+1)
# compare the result
if (rnum != Num):
correctness = False
print("sparql: "+sparql)
print("Num: "+str(Num))
# create sparql
sparql.append(sparql0)
sparql.append(sparql1)
sparql.append(sparql2)
sparql.append(sparql3)
sparql.append(sparql4)
sparql.append(sparql5)
#create the threads
for i in range(tnum):
filename = "result/res" + str(i) + ".txt"
t = threading.Thread(target=Mythread, args=(result[i%6],sparql[i%6],filename,))
threads.append(t)
# start threads
for i in threads:
i.start()
# wait for the threads
for i in threads:
i.join()
if (correctness == True):
print("The answers are correct!")
else:
print("The answers exist errors!")
print("Main thread exit")

View File

@ -0,0 +1,44 @@
"""
# Filename: PyAPIExample.py
# Author: yangchaofan
# Last Modified: 2018-7-18 15:10
# Description: a simple example of python API
"""
import sys
sys.path.append('../src')
import GstoreConnector
# before you run this example, make sure that you have started up 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)
# 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

View File

@ -0,0 +1,100 @@
"""
# Filename: GStoreConnector.py
# Author: yangchaofan
# Last Modified: 2018-7-18 14:44
# Description: http api for python
"""
import requests
defaultServerIP = "127.0.0.1"
defaultServerPort = "3305"
class GstoreConnector:
def __init__(self, ip, port):
if (ip == "localhost"):
self.serverIP = defaultServerIP
else:
self.serverIP = ip
self.serverPort = port
self.Url = "http://" + self.serverIP + ":" + str(self.serverPort)
def UrlEncode(self, s):
ret = ""
for i in range(len(s)):
c = s[i]
if ((ord(c)==42) or (ord(c)==45) or (ord(c)==46) or (ord(c)==47) or (ord(c)==58) or (ord(c)==95)):
ret += c
elif ((ord(c)>=48) and (ord(c)<=57)):
ret += c
elif ((ord(c)>=65) and (ord(c)<=90)):
ret += c
elif ((ord(c)>=97) and (ord(c)<=122)):
ret += c
else:
ret += "{}{:X}".format("%", ord(c))
return ret
def Get(self, strUrl):
r = requests.get(self.UrlEncode(strUrl))
return r.text
def fGet(self, strUrl, filename):
r = requests.get(self.UrlEncode(strUrl), stream=True)
with open(filename, 'wb') as fd:
for chunk in r.iter_content(4096):
fd.write(chunk)
return
def load(self, db_name, username, password):
cmd = self.Url + "/?operation=load&db_name=" + db_name + "&username=" + username + "&password=" + password
res = self.Get(cmd)
print res
if res == "load database done.":
return True
return False
def unload(self, db_name, username, password):
cmd = self.Url + "/?operation=unload&db_name=" + db_name + "&username=" + username + "&password=" + password
res = self.Get(cmd)
print(res)
if res == "unload database done.":
return True
return False
def build(self, db_name, rdf_file_path, username, password):
cmd = self.Url + "/?operation=build&db_name=" + db_name + "&ds_path=" + rdf_file_path + "&username=" + username + "&password=" + password
res = self.Get(cmd)
print(res)
if res == "import RDF file to database done.":
return True
return False
def query(self, username, password, db_name, sparql):
cmd = self.Url + "/?operation=query&username=" + username + "&password=" + password + "&db_name=" + db_name + "&format=json&sparql=" + sparql
return self.Get(cmd)
def fquery(self, username, password, db_name, sparql, filename):
cmd = self.Url + "/?operation=query&username=" + username + "&password=" + password + "&db_name=" + db_name + "&format=json&sparql=" + sparql
self.fGet(cmd, filename)
return
def show(self):
cmd = self.Url + "/?operation=show"
return Get(cmd)
def user(self, type, username1, password1, username2, addtion):
cmd = self.Url + "/?operation=user&type=" + type + "&username1=" + username1+ "&password1=" + password1 + "&username2=" + username2 + "&addtion=" +addition
return self.Get(cmd)
def showUser(self):
cmd = self.Url + "/?operation=showUser"
return self.Get(cmd)
def monitor(self, db_name):
cmd = self.Url + "/?operation=monitor&db_name=" + db_name;
return self.Get(cmd)
def checkpoint(self, db_name):
cmd = self.Url + "/?operation=checkpoint&db_name=" + db_name
return self.Get(cmd)

View File

@ -70,7 +70,7 @@ The socket API of gStore is placed in api/socket directory in the root directory
- GstoreConnector.php (source code of PHP API)
-python/ (the python API)
- python/ (the python API)
- src/ (source code of Python API)
@ -395,6 +395,8 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
- Benchmark.cpp
- Benchmark1.cpp
- CppAPIExample.cpp
- Makefile
@ -405,81 +407,101 @@ The HTTP API of gStore is placed in api/http directory in the root directory of
- client.java
-lib/
- lib/
-src/
-Makefile
- src/
-jgsc/
-GstoreConnector.java
-example/
-Benckmark.java
- Makefile
-JavaAPIExample.java
- jgsc/
-Makefile
- GstoreConnector.java
- example/
- Benckmark.java
- Benchmark1.java
- JavaAPIExample.java
- Makefile
- python/ (the Python API)
- example/
- Benchmark.py
- PyAPIExample.py
- src/
- GstoreConnector.py
- php/ (the Php API)
- example/
- Benchmark.php
- phpAPIExample.php
- src/
- GstoreConnector.php
- - -
## C++ API
#### Interface
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:
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:
```
CHttpClient hc;
string res;
int ret;
// build a new database by a RDF file.
ret = hc.Get("http://127.0.0.1:9000/?operation=build&db_name=lubm&ds_path=data/lubm/lubm.nt&username=root&password=123456", res);
cout&lt;&lt;res&lt;&lt;endl;
// initialize
GstoreConnector gc("172.31.222.93", 9016);
// load databse
ret = hc.Get("http://127.0.0.1:9000/?operation=load&db_name=lumb&username=root&password=123456", res);
cout&lt;&lt;res&lt;&lt;endl;
// 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");
// load the database that you built.
gc.load("test", "root", "123456");
// then you can execute SPARQL query on this database.
ret = hc.Get("http://127.0.0.1:9000/?operation=query&username=root&password=123456&db_name=lubm&format=json&sparql="+sparql, res);
cout&lt;&lt;res&lt;&lt;endl;
std::string answer = gc.query("root", "123456", "test", sparql);
std::cout << answer << std::endl;
// output information of a database
ret = hc.Get("http://127.0.0.1:9000/?operation=monitor&db_name=lubm", res);
cout&lt;&lt;res&lt;&lt;endl;
// make a SPARQL query and save the result in ans.txt
gc.query("root", "123456", "test", sparql, "ans.txt");
// unload this databse
ret = hc.Get("http://127.0.0.1:9000/?operation=unload&db_name=lubm&username=root&password=123456", res);
cout&lt;&lt;res&lt;&lt;endl;
// unload this database.
gc.unload("test", "root", "123456");
//add a user(with username: Jack, password: 2)
ret = hc.Get("http://127.0.0.1:9000/?operation=user&type=add_user&username1=root&password1=123456&username2=Jack&addtion=2", res);
cout&lt;&lt;res&lt;&lt;endl;
// also, you can load some exist database directly and then query.
gc.load("lubm", "root", "123456");
answer = gc.query("root", "123456", "lubm", sparql);
//add privilege to user Jack(add_query, add_load, add_unload)
ret = hc.Get("http://127.0.0.1:9000/?operation=user&type=add_query&username1=root&password1=123456&username2=Jack&addtion=lubm", res);
cout&lt;&lt;res&lt;&lt;endl;
//delete privilege of a user Jack(delete_query, delete_load, delete_unload)
ret = hc.Get("http://127.0.0.1:9000/?operation=user&type=delete_query&username1=root&password1=123456&username2=Jack&addtion=lubm", res);
cout&lt;&lt;res&lt;&lt;endl;
//delete user(with username: Jack, password: 2)
ret = hc.Get("http://127.0.0.1:9000/?operation=user&type=delete_user&username1=root&password1=123456&username2=Jack&addtion=2", res);
cout&lt;&lt;res&lt;&lt;endl;
std::cout << answer << std::endl;
gc.unload("lubm", "root", "123456");
```
The original declaration of these functions are as below:
```
GstoreConnector();
CHttpClient();
int Post(const std::string &amp; strUrl, const std::string &amp; strPost, std::string &amp; strResponse);
bool build(std::string _db_name, std::string _rdf_file_path, std::string username, std::string password);
int Get(const std::string &amp; strUrl, std::string &amp; strResponse);
bool load(std::string _db_name, std::string username, std::string password);
int Posts(const std::string &amp; strUrl, const std::string &amp; strPost, std::string &amp; strResponse, const char * pCaPath = NULL);
bool unload(std::string _db_name, std::string username, std::string password);
void query(std::string username, std::string password, std::string db_name, std::string sparql, std::string filename);
std::string query(std::string username, std::string password, std::string db_name, std::string sparql);
int Gets(const std::string &amp; strUrl, std::string &amp; strResponse, const char * pCaPath = NULL);
```
- - -
@ -488,54 +510,139 @@ int Gets(const std::string &amp; strUrl, std::string &amp; strResponse, const ch
#### Interface
To use the Java API, please see gStore/api/http/java/HttpRequest.java. Functions should be called like below:
To use the Java API, please see gStore/api/http/java/src/jgsc/GstoreConnector.java. Functions should be called like below:
```
// build a new database by a RDF file.
String s=HttpRequest.sendGet("http://localhost:9000/?operation=build&db_name=lubm&ds_path=data/lubm/lubm.nt&username=root&password=123456", "");
System.out.println(s);
// load databse
String s=HttpRequest.sendGet("http://localhost:9000/?operation=load&db_name=lumb&username=root&password=123456", "");
System.out.println(s);
// initialize
GstoreConnector gc = new GstoreConnector("172.31.222.78", 3305);
// then you can execute SPARQL query on this database.
String s=HttpRequest.sendGet("http://localhost:9000/?operation=query&username=root&password=123456&db_name=lubm&format=json&sparql=" + sparql, "");
System.out.println(s);
// build the database
gc.build("LUBM10", "data/lubm/lubm.nt", "root", "123456");
// output information of a databse
String s=HttpRequest.sendGet("http://localhost:9000/?operation=monitor&db_name=lubm", "");
System.out.println(s);
// load the database you built
gc.load("LUBM10", "root", "123456");
// unload this database
String s=HttpRequest.sendGet("http://localhost:9000/?operation=unload&db_name=lubm&username=root&password=123456", "");
System.out.println(s);
// also, you can load some exist database directly and then query.
gc.load("LUBM10", "root", "123456");
//add a user(with username: Jack, password: 2)
ret = HttpRequest.sendGet("http://127.0.0.1:9000/?operation=user&type=add_user&username1=root&password1=123456&username2=Jack&addtion=2", "");
System.out.println(s);
// make a SPARQL query
answer = gc.query("root", "123456", "LUBM10", sparql);
System.out.println(answer);
//add privilege to user Jack(add_query, add_load, add_unload)
ret = HttpRequest.sendGet("http://127.0.0.1:9000/?operation=user&type=add_query&username1=root&password1=123456&username2=Jack&addtion=lubm", "");
System.out.println(s);
// make a SPARQL query and save the result in ans.txt
gc.query("root", "123456", "LUBM10", sparql, "ans.txt");
//delete privilege of a user Jack(delete_query, delete_load, delete_unload)
ret = HttpRequest.sendGet("http://127.0.0.1:9000/?operation=user&type=delete_query&username1=root&password1=123456&username2=Jack&addtion=lubm", "");
System.out.println(s);
// unload the database
gc.unload("LUBM10", "root", "123456");
//delete user(with username: Jack, password: 2)
ret = HttpRequest.sendGet("http://127.0.0.1:9000/?operation=user&type=delete_user&username1=root&password1=123456&username2=Jack&addtion=2", "");
System.out.println(s);
```
The original declaration of these functions are as below:
```
public class GstoreConnector();
public class HttpRequest();
public boolean build(String _db_name, String _rdf_file_path, String _username, String _password);
public static String sendGet(String url, String param);
public boolean load(String _db_name, String _username, String _password);
public static String sendPost(String url, String param);
public boolean unload(String _db_name,String _username, String _password);
public String query(String _username, String _password, String _db_name, String _sparql);
public void query(String _username, String _password, String _db_name, String _sparql, String _filename);
```
- - -
## Python API
#### Interface
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)
# build database with a RDF graph
ret = gc.build("test", "data/lubm/lubm.nt", username, password)
# load the database
ret = gc.load("test", username, password)
# query
print (gc.query(username, password, "test", sparql))
# query and save the result in a file
gc.fquery(username, password, "test", sparql, filename)
# 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 load(self, db_name, username, password):
def unload(self, db_name, username, password):
def query(self, username, password, db_name, sparql):
def fquery(self, username, password, db_name, sparql, filename):
```
- - -
## Php API
#### Interface
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);
// build database
$ret = $gc->build("test", "data/lubm/lubm.nt", $username, $password);
echo $ret . PHP_EOL;
// load rdf
$ret = $gc->load("test", $username, $password);
echo $ret . PHP_EOL;
// 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;
```
The original declaration of these functions are as below:
```
class GstoreConnector
function build($db_name, $rdf_file_path, $username, $password)
function load($db_name, $username, $password)
function unload($db_name, $username, $password)
function query($username, $password, $db_name, $sparql)
function fquery($username, $password, $db_name, $sparql, $filename)
```

View File

@ -15,6 +15,9 @@ readline | need to be installed
readline-devel | need to be installed
openjdk | needed if using Java api
openjdk-devel | needed if using Java api
requests | needed if using Python http api
pthreads | needed if using php http api
curl-devel | needed if using php http api
realpath | needed if using gconsole
ccache | optional, used to speed up the compilation
libcurl-devel | needed to be installed
@ -35,7 +38,60 @@ NOTICE:
7. To install ccache, you need to add epel repository if using CentOS, while in Ubuntu you can directly install it by `apt-get install ccache` command. If you can not install ccache(or maybe you do not want to), please go to modify the makefile(just change the CC variable to g++).
8. If you need to use the HTTP server in gStore, then Boost Library(like boost-devel, including boost headers for developing) must be installed and the version should not be less than 1.54. Remember to check the makefile for your installed path of Boost.
8. If you need to use the HTTP server in gStore, then Boost Library(like boost-devel, including boost headers for developing) must be installed and the version should not be less than 1.54. Remember to check the makefile for your installed path of Boost. To use Python api, you need to install requests by `pip install requests` in CentOS. To use php api, you need to install pthreads and curl in CentOS as follows:
```
1- get curl-devel
# yum install curl-devel
2- get php
# wget -c http://www.php.net/distributions/php-5.4.36.tar.gz
get pthreads
# wget -c http://pecl.php.net/get/pthreads-1.0.0.tgz
3- extract both
# tar zxvf php-5.4.36.tar.gz
# tar zxvf pthreads-1.0.0.tgz
4- move pthreads to php/ext folder
# mv pthreads-1.0.0 php-5.4.36/ext/pthreads
5- reconfigure sources
# ./buildconf --force
# ./configure --help | grep pthreads
You have to see --enable-pthreads listed. If do not, clear the buidls with this commands:
# rm -rf aclocal.m4
# rm -rf autom4te.cache/
# ./buildconf --force
6- Inside php folder run configure command to set what we need:
# ./configure --enable-debug --enable-maintainer-zts --enable-pthreads --prefix=/usr --with-config-file-path=/etc --with-curl
7- install php
We will run make clear just to be sure that no other crashed build will mess our new one.
# make clear
# make
# make install
8- Copy configuration file of PHP and add local lib to include path
# cp php.ini-development /etc/php.ini
Edit php.ini and set Include_path to be like this:
Include_path = "/usr/local/lib/php"
9- Check Modules
# php -m (check pthread loaded)
You have to see pthreads listed
10- If pthread is not listed, update php.ini
# echo "extension=pthreads.so" >> /etc/php.ini
```
9. Any other questions, please go to [FAQ](FAQ.md) page.

View File

@ -101,47 +101,42 @@ type:
`bin/ghttp db_name serverPort` or `bin/ghttp serverPort db_name` to start server with serverPort and load database named db_name initially.
(*Attention: the argument db_name and serverPort can be left out*)
Attention: the argument serverPort can be left out
if you leave out the argument serverPort in the commond, then the corresponding value will be set to default as 9000.
(*if you leave out the argument db_name in the commond, then the db_name will set to null which means not to load a database initially.*)
URL rules are listed blow:
parameters: operation, db_name, ds_path, format, sparql, type, username, password
NOTICE: do URL encoding before sending it to database server
operation: build, load, unload, query, monitor, show, checkpoint, user
```
// build a new database by a RDF file.
"http://127.0.0.1:9000/?operation=build&db_name=lubm&ds_path=data/lubm/lubm.nt&username=root&password=123456"
gc.build("test", "data/lubm/LUBM_10.n3", "root", "123456");
// load databse
"http://127.0.0.1:9000/?operation=load&db_name=lumb&username=root&password=123456"
gc.load("test", "root", "123456");
// then you can execute SPARQL query on this database.
"http://127.0.0.1:9000/?operation=query&username=root&password=123456&db_name=lubm&format=json&sparql="
answer = gc.query("root", "123456", "test", sparql);
// output information of a database
"http://127.0.0.1:9000/?operation=monitor&db_name=lubm"
cout << answer << std::endl;
// unload this databse
"http://127.0.0.1:9000/?operation=unload&db_name=lubm&username=root&password=123456"
gc.unload("lubm", "root", "123456");
//add a user(with username: Jack, passwor: 2)
"http://127.0.0.1:9000/?operation=user&type=add_user&username1=root&password1=123456&username2=Jack&addtion=2"
answer = gc.user("add_user", "root", "123456", "Jack", "2");
//add privilege to user Jack(add_query, add_load, add_unload)
"http://127.0.0.1:9000/?operation=user&type=add_query&username1=root&password1=123456&username2=Jack&addtion=lubm"
answer = gc.user("add_query", "root", "123456", "Jack", "lubm");
//delete privilege of a user Jack(delete_query, delete_load, delete_unload)
"http://127.0.0.1:9000/?operation=user&type=delete_query&username1=root&password1=123456&username2=Jack&addtion=lubm"
answer = gc.user("delete_query", "root", "123456", "Jack", "lubm");
//delete user(with username: Jack, password: 2)
"http://127.0.0.1:9000/?operation=user&type=delete_user&username1=root&password1=123456&username2=Jack&addtion=2"
answer = gc.user("delete_user", "root", "123456", "Jack", "2");
```
```
db_name: the name of database, like lubm
format: html, json, txt, csv
sparql: select ?s where { ?s ?p ?o . }
@ -150,7 +145,7 @@ operation: the type of operation: like load, unload, query ...
type: the type of operation that you execute on user, like: add_user, delete_user, add_query, add_load...
username: the username of the user that execute the operation
password: the password of the user that execute the operation
```
- - -