HTTP API

We provide HTTP api(suggested) and socket api, corresponding to ghttp and gserver respectively.

This chapter provides API for ghttp. Compared with socket API, HTTP API is more stable and more standard, and can maintain connection. Socket API can not guarantee correct transmission, so the network transmission is faster.

 

Easy Examples

We provide JAVA, C++, Python, PHP and Nodejs API for ghttp now. Please see api/http. To use these examples, please make sure that executables have already been generated.

Next, start up ghttp service by using  bin/ghttp  command. It is ok if you know a running usable ghttp server and try to connect to it. (you don't 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/.

Finally, go to the example directory and run the corresponding executables. All these 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 FAQ for help or report it to us.

You are advised to read the example code carefully, as well as the corresponding Makefile. This will help you to understand the API, especially if you want to write your own programs based on the API interface.

 

C++ API 

To use the C++ API, please place the phrase  #include "GstoreConnecotr.h"  in your cpp code. Functions in GstoreConnector.h should be called like below:

// initialize
GstoreConnector gc("172.31.222.93", 9016);
// 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.
std::string answer = gc.query("root", "123456", "test", sparql);
std::cout << answer << std::endl;
// make a SPARQL query and save the result in ans.txt
gc.query("root", "123456", "test", sparql, "ans.txt");
// unload this database.
gc.unload("test", "root", "123456");
// 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;
gc.unload("lubm", "root", "123456");

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);
bool load(std::string _db_name, std::string username,
std::string password);
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);

 

Java API

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);
// build the database
gc.build("LUBM10", "data/lubm/lubm.nt", "root", "123456");
// load the database you built
gc.load("LUBM10", "root", "123456");
// you can load some exist database directly and then query.
gc.load("LUBM10", "root", "123456");
// 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");

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 boolean load(String _db_name,
String _username, String _password);
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

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

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)

 

Nodejs API

Before using Nodejs API, type  npm install request  and  npm install request-promise  under the nodejs folder to add the required module.

To use Nodejs API, please see gStore/api/http/nodejs/index.js. Functions should be called like following:

# start a gc with given IP and Port
gc = new GStoreClient(username, password, "http://127.0.0.1:9000")
# build database with a RDF graph
ret = gc.build("test", "data/lubm/lubm.nt")
# load the database
ret = gc.load("test")
# query
print (gc.query("test", sparql))
# unload the database
ret = gc.unload("test")

The original declaration of these functions are as below:

class GStoreClient
async build(db = '', dataPath = '')
async load(db = '')
async unload(db = '')
async query(db = '', sparql = '')