-
Notifications
You must be signed in to change notification settings - Fork 211
API Explanation
We provide JAVA, C++, PHP and Python API for gStore now. Please refer to example codes in api/cpp/example, api/java/example, api/php and api/python/example. To use the four examples to have a try, please ensure that executables have already been generated. Otherwise, for Java and C++, just type make APIexample
in the root directory of gStore to compile the codes, as well as API.
Next, start up a gStore server by using ./gserver
command. It is ok if you know a running usable gStore server and try to connect to it, but notice that** the server ip and port of server and client must be matched.**(you don’t need to change any thing if using examples, just by default)
Then, for Java and C++ code, you need to compile the example codes in the directory gStore/api/. We provide a utility to do this, and you just need to type make APIexample
in the root directory of gStore. Or you can compile the codes by yourself, in this case please go to gStore/api/cpp/example/ and gStore/api/java/example/, respectively.
Finally, go to the example directory and run the corresponding executables. For C++, just use ./example
command to run it. And for Java, use make run
command or java -cp ../lib/GstoreJavaAPI.jar:. JavaAPIExample
to run it. For PHP, use php ./PHPAPIExample
. For python, use python ./PythonAPIExample
. All these four executables will connect to a specifed
gStore 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.(the report approach is described in README)
You are advised to read the example code carefully, as well as the corresponding Makefile. This will help you to understand the API, specially if you want to write your own programs based on the API 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:
// initialize the Gstore server's IP address and port.
GstoreConnector gc("127.0.0.1", 3305);
// build a new database by a RDF file.
// note that the relative path is related to gserver.
gc.build("LUBM10", "example/LUBM_10.n3");
// 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(sparql);
// unload this database.
gc.unload("LUBM10");
// also, you can load some exist database directly and then query.
gc.load("LUBM10");
// query a SPARQL in current database
answer = gc.query(sparql);
The original declaration of these functions are as below:
GstoreConnector();
GstoreConnector(string _ip, unsigned short _port);
GstoreConnector(unsigned short _port);
bool load(string _db_name);
bool unload(string _db_name);
bool build(string _db_name, string _rdf_file_path);
string query(string _sparql);
Notice:
- When using GstoreConnector(), the default value for ip and port is 127.0.0.1 and 3305, respectively.
- When using build(), the rdf_fle_path(the second parameter) should be related to the position where gserver lies in.
- Please remember to unload the database you have loaded, otherwise things may go wrong.(the errors may not be reported!)
You are advised to see gStore/api/cpp/example/Makefile for instructions on how to compile your code with the C++ API. Generally, what you must do is compile your own code to object with header in the C++ API, and link the object with static lib in the C++ API.
Let us assume that your source code is placed in test.cpp, whose position is ${GSTORE}/gStore/.(if using devGstore as name instead of gStore, then the path is ${GSTORE}/devGstore/ directory first:
Use g++ -c -I${GSTORE}/gStore/api/cpp/src/ test.cpp -o test.o
to compile your test.cpp into test.o, relative API header
is placed in api/cpp/src/.
Use g++ -o test test.o -L${GSTORE}/gStore/api/cpp/lib/ -lgstoreconnector
to link your test.o with the libgstoreconnector.a(a static lib) in api/cpp/lib/.
Then you can type ./test
to execute your own program, which uses our C++ API. It is also advised for you to place relative compile commands in a Makefile, as well as other commands if you like.
To use the Java API, please place the phrase import jgsc.GstoreConnector;
in your java code. Functions in GstoreConnector.java should be called like below:
// initialize the Gstore server's IP address and port.
GstoreConnector gc = new GstoreConnector("127.0.0.1", 3305);
// build a new database by a RDF file.
// note that the relative path is related to gserver.
gc.build("LUBM10", "example/LUBM_10.n3");
// 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(sparql);
//unload this database.
gc.unload("LUBM10");
//also, you can load some exist database directly and then query.
gc.load("LUBM10");// query a SPARQL in current database
answer = gc.query(sparql);
The original declaration of these functions are as below:
GstoreConnector();
GstoreConnector(string _ip, unsigned short _port);
GstoreConnector(unsigned short _port);
bool load(string _db_name);
bool unload(string _db_name);
bool build(string _db_name, string _rdf_file_path);
string query(string _sparql);
Notice:
- When using GstoreConnector(), the default value for ip and port is 127.0.0.1 and 3305, respectively.
- When using build(), the rdf_file_path(the second parameter) should be related to the position where gserver lies in.
- Please remember to unload the database you have loaded, otherwise things may go wrong.(the errors may not be reported!)
You are advised to see gStore/api/java/example/Makefile for instructions on how to compile your code with the Java API. Generally, what you must do is compile your own code to object with jar file in the Java API.
Let us assume that your source code is placed in test.java, whose position is ${GSTORE}/gStore/.(if using devGstore as name instead of gStore, then the path is ${GSTORE}/devGstore/ directory first:
Use javac -cp ${GSTORE}/gStore/api/java/lib/GstoreJavaAPI.jar test.java
to compile your test.java into test.class with the GstoreJavaAPI.jar(a jar package used in Java) in api/java/lib/.
Then you can type java -cp ${GSTORE}/gStore/api/java/lib/GstoreJavaAPI.jar:. test
to execute your own program(notice that the “:.” in command cannot be neglected), which uses our Java API. It is also advised for you to place relative compile commands in a Makefile, as well as other commands if you like.
To use the PHP API, please place the phrase include('GstoreConnector,php');
in your php code. Functions in GstoreConnector.php should be called like below:
// initialize the Gstore server's IP address and port.
$gc = new Connector("127.0.0.1", 3305);
// build a new database by a RDF file.
// note that the relative path is related to gserver.
$gc->build("LUBM10", "example/LUBM_10.n3");
// then you can execute SPARQL query on this database.
$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>. " +
"}";
$answer = gc->query($sparql);
//unload this database.
$gc->unload("LUBM10");
//also, you can load some exist database directly and then query.
$gc->load("LUBM10");// query a SPARQL in current database
$answer = gc->query(sparql);
The original declaration of these functions are as below: class Connector { public function __construct($host, $port); public function send($data); public function recv(); public function build($db_name, $rdf_file_path); public function load($db_name); public function unload($db_name); public function query($sparql); public function __destruct(); }
Notice:
- When using Connector(), the default value for ip and port is 127.0.0.1 and 3305, respectively.
- When using build(), the rdf_file_path(the second parameter) should be related to the position where gserver lies in.
- Please remember to unload the database you have loaded, otherwise things may go wrong.(the errors may not be reported!)
You can see gStore/api/php/PHPAPIExample for instructions on how to use PHP API. PHP script doesn’t need compiling. You can run PHP file directly or use it in your web project.
To use the Python API, please place the phrase from GstoreConnector import GstoreConnector
in your python code. Functions in GstoreConnector.py should be called like below:
// initialize the Gstore server's IP address and port.
gc = GstoreConnector('127.0.0.1', 3305)
// build a new database by a RDF file.
// note that the relative path is related to gserver.
gc.build('LUBM10', 'data/LUBM_10.n3')
// then you can execute SPARQL query on this database.
$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>. " +
"}";
answer = gc.query(sparql)
//unload this database.
gc.unload('LUBM10')
//also, you can load some exist database directly and then query.
gc.load('LUBM10')// query a SPARQL in current database
answer = gc.query(sparql)
The original declaration of these functions are as below: class GstoreConnector { def _connect(self) def _disconnect(self) def _send(self, msg): def _recv(self) def _pack(self, msg): def _communicate(f): def init(self, ip='127.0.0.1', port=3305): @_communicate def test(self) @_communicate def load(self, db_name) @_communicate def unload(self, db_name) @_communicate def build(self, db_name, rdf_file_path) @_communicate def drop(self, db_name) @_communicate def stop(self) @_communicate def query(self, sparql) @_communicate def show(self, _type=False) }
Notice:
- When using GstoreConnector(), the default value for ip and port is 127.0.0.1 and 3305, respectively.
- When using build(), the rdf_file_path(the second parameter) should be related to the position where gserver lies in.
- Please remember to unload the database you have loaded, otherwise things may go wrong.(the errors may not be reported!)
You are advised to see gStore/api/python/example/PythonAPIExample for examples on how to use python API. Python file doesn’t need compiling, and you can run it directly.