Walkthrough: Installing Jena and Fuseki
PREREQUISITES
An apache based web server already pushing content to the web that you have administrative access to.
Simple background knowledge of Linux\Unix such as how to navigate around a directory tree.
Ruby installed on the web server.
SETUP
tar -zxvf apache-jena-2.11.2.tar.gz tar -xvzf jena-fuseki-1.0.2-distribution.tar.gz
[Note that you may need to change the file names to account for version changes and the like.] [Note that Fuseki will require Ruby to be installed or you won’t be able to use the special scripts for managing data with it, like “s-put” for loading although we are not going to use it here.] You’re going to need to set some environment variables and modify the PATH to help things work so you might as well do it as soon as they are unpacked:
export JENA_HOME=/your_path/apache-jena-2.11.2 export JENA=/your_path/apache-jena-2.11.2/ export FUSEKI=/your_path/jena-fuseki-1.0.2/ export JENAROOT=/your_path/apache-jena-2.11.2 PATH=$JENAROOT/bin:$PATH PATH=$FUSEKI:$PATH
EDIT THE FILES DIRECTLY [Note that you will need to change the directory locations to represent the locations on your system. Basically any time you see /your_path/ you are going to need to substitute the path to your home directory or whatever directory you are choosing to work from. You might not need all the exports. The one’s here were cobbled together from different sources and it should not hurt you to have them all.] The export commands allow certain features of Jena to know where the program files are no matter where you run them from on the system. Updating the path will give you access to the scripts in the apache-jena-2.11.2/bin/ directory without having to either be in that directory or type the full path to it. If you’re going to be using a triplestore then chances are you have a lot of data to load. Let’s assume that this is the case and use the OCLC 1 million most highly held books as an example. You can get this file at http://purl.oclc.org/dataset/WorldCat/datadumps/WorldCatMostHighlyHeld-2012-05-15.nt.gz . When you have it put it on the same computer as Jena/Fuseki and in some directory that you can find/navigate to. You’ll need to make a directory for the triplestore to be held in and this can be done as follows (again, make changes to represent the file structure that you want). This following set of commands will create the directory structure TStoreData/WorldCat/ off your home directory (If you’re not sure where that is then use the print working directory command “pwd” after you have entered “cd ~”).
cd ~mkdir TStoreDatamkdir TStoreData/WorldCat
Unfortunately there are some small problems that Jena will have with the WorldCat file. In short, there are unicode escape sequences that get read as angle brackets and as far as Jena is concerned these angle brackets should not be there and so it throws an error. I’m not sure why they are there so we’re just going to get rid of them at this point so that we can get some data loaded. To do this we run the following two commands from the directory where you put the original download:
sed 's_\\\u003E__g' < WorldCatMostHighlyHeld-2012-05-15.nt > WorldCatMostHighlyHeld-2012-05-15_angleFix1.nt
sed 's_\\\u003C__g' < WorldCatMostHighlyHeld-2012-05-15_angleFix1.nt > WorldCatMostHighlyHeld-2012-05-15_angleFix2.nt
sed is the “stream editor for filtering and transforming text”. It is going to run through the WorldCatMostHighlyHeld-2012-05-15.nt file looking to match and replace based on what is inside the single quotes and then
LOAD DATA
Now you’re ready to load some data. There are multiple ways to do this but we’re going the use the fastest. Execute the following command:
tdbloader2 --loc=/your_path/TStoreData/WorldCat/ /data2/home/jes6/CorporaAndData/WorldCat/WorldCatMostHighlyHeld-2012-05-15_angleFix2.nt
What you are doing here is telling the program tdbloader2 to go get the World Cat file we downloaded and then fixed, WorldCatMostHighlyHeld-2012-05-15_angleFix2.nt, from the directory it is in (on my machine it is /your_path/CorporaAndData/WorldCat/) and put the persistent triple store it will build in /your_path/TStoreData/WorldCat/. This method will create a directory for the triplestore if it doesn’t already exist so we didn’t need to make the directory earlier but it’s good to know how. It is also good to remember that if you enter the wrong directory then you’ll get data somewhere you didn’t expect. This method won’t load data into a directory that already has data in it so if you need to clean out a directory you could do something like:
rm -rf TStoreData/WorldCat/*
Executing this would delete all the contents of the TStoreData/WorldCat/ folder, including other folders. If you have a really big file it might take quite some time to load. This will be a pain if you have to sever your connection or have intermittent wifi access. To get around this issue the same tbdloader2 command but with “nohup” (short for “no hangups”) appended to the front (This is a great trick to use with any long process from the terminal).
nohup tdbloader2 --loc=/your_path/TStoreData/WorldCat/ /data2/home/jes6/CorporaAndData/WorldCat/WorldCatMostHighlyHeld-2012-05-15_angleFix2.nt
You might also find it useful to use the same terminal window for other things while the tbdloader2 process is working away. The easiest way to do this is to append an ‘&’ at the end of the command. So, our tdbloader2 command would become:
tdbloader2 --loc=/your_path/TStoreData/WorldCat/ /data2/home/jes6/CorporaAndData/WorldCat/WorldCatMostHighlyHeld-2012-05-15_angleFix2.nt &
Of course, we don’t always remember to background something up front so it is helpful to know how to do it after the fact too. If we had run one of the tbdloader2 commands and forgotten to add the ‘&’ we could add it after the fact by pressing “ctrl" + “z” together to suspend the process in the current window and then entering the command bg to send the last suspended process (in this case the one we just suspended) into the background and continue it. If you want to nohup and background then use this process, execute the nohup version, suspend it, then background it. We could then use the jobs command to check that it is running. This command will also give us the job numbers allowing us to do things like:
bg %3 to send job 3 into the background
fg %4 to bring job 4 into the foreground
kill %7 to kill job 7
kill -9 %7 to kill job 7 after asking in to shutdown nicely with the previous kill command and it didn’t listen (or is taking too long) and now we really want it to stop no matter what.
OPEN AND RUN THE SERVER From the fuseki directory issue the following command to start the server:
./fuseki-server --loc=/your_path/TStoreData/WorldCat/ --port=3030 /WorldCat
--loc=FILE is the location of the triplestore to load. In this case we are telling it to use the WorldCat data that we loaded above. There are more ways to load data an you can see them by looking at http://jena.apache.org/documentation/serving_data/index.html#running-a-fuseki-server . --port=NUM is the port that you’d like to provide access to the triplestore through. The default port that fuseki will use is 3030 so if you are ok using that port then you can just leave this option out. The last option in the line is extension to the base url that you would like it to use. Here we’re using “/WorldCat” since it is relatively short and a good description of what will be there. Note that the server is very likely something that you will want to run in the background, just like tdbloader. You can also issue the nohup command with it if you would like it to persist after you disconnect. See above for descriptions and examples for both of these. (In the long term you'll want to set it up as a service and have it start automatically with the server, but that is another process). While the server may start if you have a firewall it might not be possible to access it if the firewall does not allow access on the port you are requesting access through. We get through this by issuing this command:
sudo iptables --insert INPUT 8 -m state --state NEW -m tcp -p tcp --dport 3030 -j ACCEPT
and simply changing the port number as needed. You will need administrator (superuser) access to do this so make sure you know the password and/or that your account has these privileges. With the server started and the port open you can check your data and your server by visiting http://cwrc-apps-03.srv.ualberta.ca:3030/ (substituting your base URL for cwrc-app-03.srv.ualberta.ca and the appropriate port number). This will give you a simple dashboard that you can use to interact with fuseki and the data that it represents. Choose the “Control Panel” link and then the WorldCat dataset from the drop down menu. You will end up on a page that will allow you to enter SPARQL commands. Let’s ask the World Cat dataset to show us all the predicates that are used in it and to list each of these only once. Type: SELECT DISTINCT ?y WHERE {?x ?y ?z} into the to SPARQL Query box and press “Get Results” (note that the capitalization does not matter, it is just a convention). Depending on the speed of your server after a short while you should see the following as output, which is a list of all the predicates used in the WorldCat material that you loaded. ------------------------------------------------------------ | y | ============================================================ | <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> | | <http://purl.org/dc/terms/title> | | <http://purl.org/dc/terms/description> | | <http://purl.org/dc/terms/created> | | <http://rdfs.org/ns/void#dataDump> | | <http://purl.org/dc/terms/publisher> | | <http://vocab.org/waiver/terms/norms> | | <http://creativecommons.org/ns#morePermissions> | | <http://creativecommons.org/ns#attributionName> | | <http://purl.org/dc/terms/license> | | <http://creativecommons.org/ns#attributionURL> | | <http://rdfs.org/ns/void#exampleResource> | | <http://www.w3.org/2002/07/owl#sameAs> | | <http://rdfs.org/ns/void#uriSpace> | | <http://rdfs.org/ns/void#vocabulary> | | <http://rdfs.org/ns/void#subset> | | <http://rdfs.org/ns/void#openSearchDescription> | | <http://creativecommons.org/ns#useGuidelines> | | <http://www.w3.org/2000/01/rdf-schema#label> | | <http://xmlns.com/foaf/0.1/homepage> | | <http://xmlns.com/foaf/0.1/page> | | <http://schema.org/name> | | <http://www.loc.gov/mads/rdf/v1#isIdentifiedByAuthority> | | <http://schema.org/contributor> | | <http://schema.org/about> | | <http://schema.org/author> | | <http://purl.org/library/hasManifestation> | | <http://schema.org/inLanguage> | | <http://purl.org/library/holdingsCount> | | <http://schema.org/copyrightYear> | | <http://purl.org/library/oclcnum> | | <http://purl.org/library/placeOfPublication> | | <http://schema.org/description> | | <http://schema.org/publisher> | | <http://www.w3.org/1999/02/22-rdf-syntax-ns#value> | | <http://www.w3.org/2004/02/skos/core#inScheme> | | <http://schema.org/isbn> | | <http://purl.org/library/hasCarrier> | | <http://schema.org/bookFormat> | | <http://schema.org/datePublished> | | <http://schema.org/url> | | <http://schema.org/genre> | | <http://schema.org/numberOfPages> | | <http://schema.org/bookEdition> | | <http://schema.org/isFamilyFriendly> | | <http://schema.org/contentRating> | | <http://schema.org/illustrator> | | <http://schema.org/reviews> | | <http://schema.org/awards> | | <http://schema.org/itemReviewed> | | <http://schema.org/reviewBody> | | <http://schema.org/image> | | <http://purl.org/dc/terms/isPartOf> | ------------------------------------------------------------
Congratulations! You have a working triplestore! Interested in accessing the triplestore through a program? Here’s as example script showing a simple way to do it with Python. Of course, you will need to change the endpointURL to point at the location of your store.
#Simple script to send a SPARQL query to the CWRC SPARQL endpoint on our Jena triplestore and output the result #Modified from the example on p. 209 of Learning SPARQL, O'Reilly, Bob DuCharme import urllib2 endpointURL = "http://cwrc-apps-03.srv.ualberta.ca:3030/WorldCat/" query = """ SELECT DISTINCT ?y WHERE {?x ?y ?z} """ escapedQuery = urllib2.quote(query) print escapedQuery requestURL = endpointURL + "query?query=" + escapedQuery print requestURL print urllib2.quote(endpointURL + "query?query=" + query) request = urllib2.Request(requestURL) result = urllib2.urlopen(request) print result.read()












